Chromium Code Reviews| Index: ui/compositor/layer_unittest.cc |
| diff --git a/ui/compositor/layer_unittest.cc b/ui/compositor/layer_unittest.cc |
| index f24ad4659b42b8c3078d943566fed31b3cbc8fc9..153067dba8435e9b1ef7e3db47b66f51e769c5da 100644 |
| --- a/ui/compositor/layer_unittest.cc |
| +++ b/ui/compositor/layer_unittest.cc |
| @@ -20,6 +20,7 @@ |
| #include "base/path_service.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/stringprintf.h" |
| +#include "base/strings/utf_string_conversions.h" |
| #include "base/trace_event/trace_event.h" |
| #include "cc/layers/layer.h" |
| #include "cc/output/copy_output_request.h" |
| @@ -44,10 +45,12 @@ |
| #include "ui/compositor/test/test_layers.h" |
| #include "ui/gfx/canvas.h" |
| #include "ui/gfx/codec/png_codec.h" |
| +#include "ui/gfx/font_list.h" |
| #include "ui/gfx/gfx_paths.h" |
| #include "ui/gfx/skia_util.h" |
| using cc::MatchesPNGFile; |
| +using cc::WritePNGFile; |
| namespace ui { |
| @@ -90,6 +93,66 @@ class ColoredLayer : public Layer, public LayerDelegate { |
| SkColor color_; |
| }; |
| +class DrawStringLayer : public Layer, public LayerDelegate { |
|
danakj
2016/05/23 19:44:01
Why subclass Layer? This is just a LayerDelegate r
Lof
2016/05/24 13:52:28
Done.
|
| + public: |
| + enum DrawFunction { |
| + STRING_WITH_HALO, |
| + STRING_FADED, |
| + STRING_WITH_SHADOWS |
| + }; |
| + |
| + DrawStringLayer(SkColor back_color, SkColor halo_color, DrawFunction func) |
| + : Layer(LAYER_TEXTURED), |
| + background_color_(back_color), |
| + halo_color_(halo_color), |
| + func_(func) { |
| + set_delegate(this); |
| + } |
| + |
| + ~DrawStringLayer() override {} |
| + |
| + // Overridden from LayerDelegate: |
| + void OnPaintLayer(const ui::PaintContext& context) override { |
| + ui::PaintRecorder recorder(context, size()); |
| + recorder.canvas()->DrawColor(background_color_); |
| + const base::string16 text = base::ASCIIToUTF16("Danakj"); |
|
danakj
2016/05/23 19:42:55
lol how about "TEST" or something?
Lof
2016/05/24 13:52:28
Done.
|
| + switch (func_) { |
| + case STRING_WITH_HALO: |
| + recorder.canvas()->DrawStringRectWithHalo( |
| + text, font_list_, SK_ColorRED, halo_color_, gfx::Rect(size()), 0); |
| + break; |
| + case STRING_FADED: |
| + recorder.canvas()->DrawFadedString( |
| + text, font_list_, SK_ColorRED, gfx::Rect(size()), 0); |
| + break; |
| + case STRING_WITH_SHADOWS: { |
| + gfx::ShadowValues shadows; |
| + shadows.push_back( |
| + gfx::ShadowValue(gfx::Vector2d(2, 2), 2, SK_ColorRED)); |
| + recorder.canvas()->DrawStringRectWithShadows( |
| + text, font_list_, SK_ColorRED, gfx::Rect(size()), 0, 0, shadows); |
| + break; |
| + } |
| + default: |
| + NOTREACHED(); |
| + } |
| + } |
| + |
| + void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} |
| + |
| + void OnDeviceScaleFactorChanged(float device_scale_factor) override {} |
| + |
| + base::Closure PrepareForLayerBoundsChange() override { |
| + return base::Closure(); |
| + } |
| + |
| + private: |
| + SkColor background_color_; |
| + SkColor halo_color_; |
| + DrawFunction func_; |
| + gfx::FontList font_list_; |
| +}; |
| + |
| class LayerWithRealCompositorTest : public testing::Test { |
| public: |
| LayerWithRealCompositorTest() { |
| @@ -98,6 +161,7 @@ class LayerWithRealCompositorTest : public testing::Test { |
| } else { |
| LOG(ERROR) << "Could not open test data directory."; |
| } |
| + gfx::FontList::SetDefaultFontDescription("Arial, Times New Roman, 15px"); |
| } |
| ~LayerWithRealCompositorTest() override {} |
| @@ -140,6 +204,15 @@ class LayerWithRealCompositorTest : public testing::Test { |
| return layer; |
| } |
| + Layer* CreateDrawStringLayer(const gfx::Rect& bounds, |
| + SkColor background_color, |
| + SkColor halo_color, |
| + DrawStringLayer::DrawFunction func) { |
| + Layer* layer = new DrawStringLayer(background_color, halo_color, func); |
| + layer->SetBounds(bounds); |
| + return layer; |
| + } |
| + |
| void DrawTree(Layer* root) { |
| GetCompositor()->SetRootLayer(root); |
| GetCompositor()->ScheduleDraw(); |
| @@ -1228,6 +1301,104 @@ TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) { |
| EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); |
| } |
| +#if defined(OS_WIN) |
|
danakj
2016/05/23 19:42:55
Why only OS_WIN?
tomhudson
2016/05/23 19:49:05
We should probably run this on every platform, but
Lof
2016/05/24 13:52:28
Done.
Lof
2016/05/24 13:52:28
Done.
|
| +TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithHalo) { |
| + gfx::Size size(50, 50); |
| + GetCompositor()->SetScaleAndSize(1.0f, size); |
| + std::unique_ptr<Layer> layer(CreateDrawStringLayer( |
| + gfx::Rect(size), SK_ColorBLUE, SK_ColorWHITE, |
| + DrawStringLayer::STRING_WITH_HALO)); |
| + DrawTree(layer.get()); |
| + |
| + SkBitmap bitmap; |
| + ReadPixels(&bitmap); |
| + ASSERT_FALSE(bitmap.empty()); |
| + |
| + base::FilePath ref_img = |
| + test_data_directory().AppendASCII("string_with_halo.png"); |
| + // WritePNGFile(bitmap, ref_img, true); |
| + |
| + float percentage_pixels_large_error = 10.0f; // 250px / (50*50) |
| + float percentage_pixels_small_error = 0.0f; |
| + float average_error_allowed_in_bad_pixels = 256.f; |
| + int large_error_allowed = 256; |
| + int small_error_allowed = 0; |
| + |
| + EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, |
| + cc::FuzzyPixelComparator( |
| + true, |
| + percentage_pixels_large_error, |
| + percentage_pixels_small_error, |
| + average_error_allowed_in_bad_pixels, |
| + large_error_allowed, |
| + small_error_allowed))); |
| +} |
| + |
| +TEST_F(LayerWithRealCompositorTest, CanvasDrawFadedString) { |
| + gfx::Size size(50, 50); |
| + GetCompositor()->SetScaleAndSize(1.0f, size); |
| + std::unique_ptr<Layer> layer(CreateDrawStringLayer( |
| + gfx::Rect(size), SK_ColorBLUE, SK_ColorWHITE, |
| + DrawStringLayer::STRING_FADED)); |
| + DrawTree(layer.get()); |
| + |
| + SkBitmap bitmap; |
| + ReadPixels(&bitmap); |
| + ASSERT_FALSE(bitmap.empty()); |
| + |
| + base::FilePath ref_img = |
| + test_data_directory().AppendASCII("string_faded.png"); |
| + // WritePNGFile(bitmap, ref_img, true); |
| + |
| + float percentage_pixels_large_error = 15.0f; // 375px / (50*50) |
| + float percentage_pixels_small_error = 0.0f; |
| + float average_error_allowed_in_bad_pixels = 256.f; |
| + int large_error_allowed = 256; |
| + int small_error_allowed = 0; |
| + |
| + EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, |
| + cc::FuzzyPixelComparator( |
| + true, |
| + percentage_pixels_large_error, |
| + percentage_pixels_small_error, |
| + average_error_allowed_in_bad_pixels, |
| + large_error_allowed, |
| + small_error_allowed))); |
| +} |
| + |
| +TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithShadows) { |
| + gfx::Size size(50, 50); |
| + GetCompositor()->SetScaleAndSize(1.0f, size); |
| + std::unique_ptr<Layer> layer(CreateDrawStringLayer( |
| + gfx::Rect(size), SK_ColorBLUE, SK_ColorWHITE, |
| + DrawStringLayer::STRING_WITH_SHADOWS)); |
| + DrawTree(layer.get()); |
| + |
| + SkBitmap bitmap; |
| + ReadPixels(&bitmap); |
| + ASSERT_FALSE(bitmap.empty()); |
| + |
| + base::FilePath ref_img = |
| + test_data_directory().AppendASCII("string_with_shadows.png"); |
| + // WritePNGFile(bitmap, ref_img, true); |
| + |
| + float percentage_pixels_large_error = 20.0f; // 500px / (50*50) |
| + float percentage_pixels_small_error = 0.0f; |
| + float average_error_allowed_in_bad_pixels = 256.f; |
| + int large_error_allowed = 256; |
| + int small_error_allowed = 0; |
| + |
| + EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, |
| + cc::FuzzyPixelComparator( |
| + true, |
| + percentage_pixels_large_error, |
| + percentage_pixels_small_error, |
| + average_error_allowed_in_bad_pixels, |
| + large_error_allowed, |
| + small_error_allowed))); |
| +} |
| +#endif // defined(OS_WIN) |
| + |
| // Opacity is rendered correctly. |
| // Checks that modifying the hierarchy correctly affects final composite. |
| TEST_F(LayerWithRealCompositorTest, Opacity) { |