diff options
Diffstat (limited to 'cc/test')
-rw-r--r-- | cc/test/DEPS | 5 | ||||
-rw-r--r-- | cc/test/data/green.png | 0 | ||||
-rw-r--r-- | cc/test/paths.cc | 44 | ||||
-rw-r--r-- | cc/test/paths.h | 26 | ||||
-rw-r--r-- | cc/test/pixel_test_output_surface.cc | 36 | ||||
-rw-r--r-- | cc/test/pixel_test_output_surface.h | 37 | ||||
-rw-r--r-- | cc/test/pixel_test_utils.cc | 81 | ||||
-rw-r--r-- | cc/test/pixel_test_utils.h | 25 | ||||
-rw-r--r-- | cc/test/run_all_unittests.cc | 2 |
9 files changed, 256 insertions, 0 deletions
diff --git a/cc/test/DEPS b/cc/test/DEPS new file mode 100644 index 0000000..76f1349 --- /dev/null +++ b/cc/test/DEPS @@ -0,0 +1,5 @@ +include_rules = [ + "+ui/gl/", +# TODO(jamesr): Remove once cc depends on GLES2Interface instead of WGC3D + "+webkit/gpu", +] diff --git a/cc/test/data/green.png b/cc/test/data/green.png new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/cc/test/data/green.png diff --git a/cc/test/paths.cc b/cc/test/paths.cc new file mode 100644 index 0000000..3551afb --- /dev/null +++ b/cc/test/paths.cc @@ -0,0 +1,44 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cc/test/paths.h" + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/path_service.h" + +namespace cc { +namespace test { + +bool PathProvider(int key, FilePath* result) { + FilePath cur; + switch (key) { + // The following are only valid in the development environment, and + // will fail if executed from an installed executable (because the + // generated path won't exist). + case DIR_TEST_DATA: + if (!PathService::Get(base::DIR_SOURCE_ROOT, &cur)) + return false; + cur = cur.Append(FILE_PATH_LITERAL("cc")); + cur = cur.Append(FILE_PATH_LITERAL("test")); + cur = cur.Append(FILE_PATH_LITERAL("data")); + if (!file_util::PathExists(cur)) // we don't want to create this + return false; + break; + default: + return false; + } + + *result = cur; + return true; +} + +// This cannot be done as a static initializer sadly since Visual Studio will +// eliminate this object file if there is no direct entry point into it. +void RegisterPathProvider() { + PathService::RegisterProvider(PathProvider, PATH_START, PATH_END); +} + +} // namespace test +} // namespace cc diff --git a/cc/test/paths.h b/cc/test/paths.h new file mode 100644 index 0000000..bc3f4e0 --- /dev/null +++ b/cc/test/paths.h @@ -0,0 +1,26 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CC_TEST_PATHS_H_ +#define CC_TEST_PATHS_H_ + +namespace cc { +namespace test { + +enum { + PATH_START = 5000, + + // Valid only in development and testing environments. + DIR_TEST_DATA, + + PATH_END +}; + +// Call once to register the provider for the path keys defined above. +void RegisterPathProvider(); + +} // namespace test +} // namespace cc + +#endif // CC_TEST_PATHS_H_ diff --git a/cc/test/pixel_test_output_surface.cc b/cc/test/pixel_test_output_surface.cc new file mode 100644 index 0000000..46985b7 --- /dev/null +++ b/cc/test/pixel_test_output_surface.cc @@ -0,0 +1,36 @@ +// Copyright 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cc/test/pixel_test_output_surface.h" + +#include "webkit/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h" + +namespace cc { + +PixelTestOutputSurface::PixelTestOutputSurface() { + scoped_ptr<webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl> + context(new webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl); + context->Initialize(WebKit::WebGraphicsContext3D::Attributes(), NULL); + context_ = context.PassAs<WebKit::WebGraphicsContext3D>(); +} + +PixelTestOutputSurface::~PixelTestOutputSurface() { +} + +bool PixelTestOutputSurface::bindToClient( + WebKit::WebCompositorOutputSurfaceClient*) { + return context_->makeContextCurrent(); +} + +const WebKit::WebCompositorOutputSurface::Capabilities& + PixelTestOutputSurface::capabilities() const { + static WebKit::WebCompositorOutputSurface::Capabilities capabilities; + return capabilities; +} + +WebKit::WebGraphicsContext3D* PixelTestOutputSurface::context3D() const { + return context_.get(); +} + +} // namespace cc diff --git a/cc/test/pixel_test_output_surface.h b/cc/test/pixel_test_output_surface.h new file mode 100644 index 0000000..f59e63c --- /dev/null +++ b/cc/test/pixel_test_output_surface.h @@ -0,0 +1,37 @@ +// Copyright 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CC_TEST_PIXEL_TEST_GRAPHICS_CONTEXT_H_ +#define CC_TEST_PIXEL_TEST_GRAPHICS_CONTEXT_H_ + +#include "base/memory/scoped_ptr.h" +#include "cc/output_surface.h" + +namespace cc { + +class PixelTestOutputSurface : public OutputSurface { + public: + static scoped_ptr<PixelTestOutputSurface> create() { + return make_scoped_ptr(new PixelTestOutputSurface); + } + + virtual ~PixelTestOutputSurface(); + + // OutputSurface overrides. + virtual bool bindToClient(WebKit::WebCompositorOutputSurfaceClient*) OVERRIDE; + virtual const WebKit::WebCompositorOutputSurface::Capabilities& capabilities() const OVERRIDE; + virtual WebKit::WebGraphicsContext3D* context3D() const OVERRIDE; + virtual void sendFrameToParentCompositor( + const WebKit::WebCompositorFrame&) OVERRIDE { } + + private: + PixelTestOutputSurface(); + + scoped_ptr<WebKit::WebGraphicsContext3D> context_; +}; + +} // namespace cc + + +#endif // CC_TEST_PIXEL_TEST_GRAPHICS_CONTEXT_H_ diff --git a/cc/test/pixel_test_utils.cc b/cc/test/pixel_test_utils.cc new file mode 100644 index 0000000..ad455c0 --- /dev/null +++ b/cc/test/pixel_test_utils.cc @@ -0,0 +1,81 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cc/test/pixel_test_utils.h" + +#include "base/file_util.h" +#include "base/logging.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "ui/gfx/codec/png_codec.h" + +namespace cc { +namespace test { + +bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path) { + std::vector<unsigned char> png_data; + const bool discard_transparency = true; + if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, + discard_transparency, + &png_data) && + file_util::CreateDirectory(file_path.DirName())) { + char* data = reinterpret_cast<char*>(&png_data[0]); + int size = static_cast<int>(png_data.size()); + return file_util::WriteFile(file_path, data, size) == size; + } + return false; +} + +bool ReadPNGFile(const FilePath& file_path, SkBitmap* bitmap) { + DCHECK(bitmap); + std::string png_data; + return file_util::ReadFileToString(file_path, &png_data) && + gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), + png_data.length(), + bitmap); +} + +bool IsSameAsPNGFile(const SkBitmap& gen_bmp, FilePath ref_img_path) { + SkBitmap ref_bmp; + if (!ReadPNGFile(ref_img_path, &ref_bmp)) { + LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value(); + return false; + } + + if (ref_bmp.width() != gen_bmp.width() || + ref_bmp.height() != gen_bmp.height()) { + LOG(ERROR) + << "Dimensions do not match (Expected) vs (Actual):" + << "(" << ref_bmp.width() << "x" << ref_bmp.height() + << ") vs. " + << "(" << gen_bmp.width() << "x" << gen_bmp.height() << ")"; + return false; + } + + // Compare pixels and create a simple diff image. + int diff_pixels_count = 0; + SkAutoLockPixels lock_bmp(gen_bmp); + SkAutoLockPixels lock_ref_bmp(ref_bmp); + // The reference images were saved with no alpha channel. Use the mask to + // set alpha to 0. + uint32_t kAlphaMask = 0x00FFFFFF; + for (int x = 0; x < gen_bmp.width(); ++x) { + for (int y = 0; y < gen_bmp.height(); ++y) { + if ((*gen_bmp.getAddr32(x, y) & kAlphaMask) != + (*ref_bmp.getAddr32(x, y) & kAlphaMask)) { + ++diff_pixels_count; + } + } + } + + if (diff_pixels_count != 0) { + LOG(ERROR) << "Images differ by pixel count: " << diff_pixels_count; + return false; + } + + return true; +} + +} // namespace test +} // namespace cc + diff --git a/cc/test/pixel_test_utils.h b/cc/test/pixel_test_utils.h new file mode 100644 index 0000000..93367d8 --- /dev/null +++ b/cc/test/pixel_test_utils.h @@ -0,0 +1,25 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/file_path.h" + +class SkBitmap; + +namespace cc { +namespace test { + +// Encodes a bitmap into a PNG and write to disk. Returns true on success. The +// parent directory does not have to exist. +bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path); + +// Reads and decodes a PNG image to a bitmap. Returns true on success. The PNG +// should have been encoded using |gfx::PNGCodec::Encode|. +bool ReadPNGFile(const FilePath& file_path, SkBitmap* bitmap); + +// Compares with a PNG file on disk, and returns true if it is the same as +// the given image. |ref_img_path| is absolute. +bool IsSameAsPNGFile(const SkBitmap& gen_bmp, FilePath ref_img_path); + +} // namespace test +} // namespace cc diff --git a/cc/test/run_all_unittests.cc b/cc/test/run_all_unittests.cc index 4ea8697..e1092c9 100644 --- a/cc/test/run_all_unittests.cc +++ b/cc/test/run_all_unittests.cc @@ -4,9 +4,11 @@ #include "base/message_loop.h" #include "base/test/test_suite.h" +#include "cc/test/paths.h" #include "testing/gmock/include/gmock/gmock.h" int main(int argc, char** argv) { + cc::test::RegisterPathProvider(); ::testing::InitGoogleMock(&argc, argv); TestSuite test_suite(argc, argv); MessageLoop message_loop; |