Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(636)

Side by Side Diff: ui/compositor/layer_unittest.cc

Issue 1634103003: Fix gfx::Canvas::DrawStringRectWithHalo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix fuzzy comparator's params Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ui/gfx/canvas.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/compositor/layer.h" 5 #include "ui/compositor/layer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
16 #include "base/json/json_reader.h" 16 #include "base/json/json_reader.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
19 #include "base/message_loop/message_loop.h" 19 #include "base/message_loop/message_loop.h"
20 #include "base/path_service.h" 20 #include "base/path_service.h"
21 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h" 22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
23 #include "base/trace_event/trace_event.h" 24 #include "base/trace_event/trace_event.h"
24 #include "cc/layers/layer.h" 25 #include "cc/layers/layer.h"
25 #include "cc/output/copy_output_request.h" 26 #include "cc/output/copy_output_request.h"
26 #include "cc/output/copy_output_result.h" 27 #include "cc/output/copy_output_result.h"
27 #include "cc/surfaces/surface_id.h" 28 #include "cc/surfaces/surface_id.h"
28 #include "cc/surfaces/surface_sequence.h" 29 #include "cc/surfaces/surface_sequence.h"
29 #include "cc/test/pixel_test_utils.h" 30 #include "cc/test/pixel_test_utils.h"
30 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
31 #include "ui/compositor/compositor_observer.h" 32 #include "ui/compositor/compositor_observer.h"
32 #include "ui/compositor/dip_util.h" 33 #include "ui/compositor/dip_util.h"
33 #include "ui/compositor/layer_animation_element.h" 34 #include "ui/compositor/layer_animation_element.h"
34 #include "ui/compositor/layer_animation_observer.h" 35 #include "ui/compositor/layer_animation_observer.h"
35 #include "ui/compositor/layer_animation_sequence.h" 36 #include "ui/compositor/layer_animation_sequence.h"
36 #include "ui/compositor/layer_animator.h" 37 #include "ui/compositor/layer_animator.h"
37 #include "ui/compositor/paint_context.h" 38 #include "ui/compositor/paint_context.h"
38 #include "ui/compositor/paint_recorder.h" 39 #include "ui/compositor/paint_recorder.h"
39 #include "ui/compositor/scoped_animation_duration_scale_mode.h" 40 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
40 #include "ui/compositor/scoped_layer_animation_settings.h" 41 #include "ui/compositor/scoped_layer_animation_settings.h"
41 #include "ui/compositor/test/context_factories_for_test.h" 42 #include "ui/compositor/test/context_factories_for_test.h"
42 #include "ui/compositor/test/draw_waiter_for_test.h" 43 #include "ui/compositor/test/draw_waiter_for_test.h"
43 #include "ui/compositor/test/test_compositor_host.h" 44 #include "ui/compositor/test/test_compositor_host.h"
44 #include "ui/compositor/test/test_layers.h" 45 #include "ui/compositor/test/test_layers.h"
45 #include "ui/gfx/canvas.h" 46 #include "ui/gfx/canvas.h"
46 #include "ui/gfx/codec/png_codec.h" 47 #include "ui/gfx/codec/png_codec.h"
48 #include "ui/gfx/font_list.h"
47 #include "ui/gfx/gfx_paths.h" 49 #include "ui/gfx/gfx_paths.h"
48 #include "ui/gfx/skia_util.h" 50 #include "ui/gfx/skia_util.h"
49 51
50 using cc::MatchesPNGFile; 52 using cc::MatchesPNGFile;
53 using cc::WritePNGFile;
51 54
52 namespace ui { 55 namespace ui {
53 56
54 namespace { 57 namespace {
55 58
56 // There are three test classes in here that configure the Compositor and 59 // There are three test classes in here that configure the Compositor and
57 // Layer's slightly differently: 60 // Layer's slightly differently:
58 // - LayerWithNullDelegateTest uses NullLayerDelegate as the LayerDelegate. This 61 // - LayerWithNullDelegateTest uses NullLayerDelegate as the LayerDelegate. This
59 // is typically the base class you want to use. 62 // is typically the base class you want to use.
60 // - LayerWithDelegateTest uses LayerDelegate on the delegates. 63 // - LayerWithDelegateTest uses LayerDelegate on the delegates.
(...skipping 22 matching lines...) Expand all
83 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} 86 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
84 87
85 base::Closure PrepareForLayerBoundsChange() override { 88 base::Closure PrepareForLayerBoundsChange() override {
86 return base::Closure(); 89 return base::Closure();
87 } 90 }
88 91
89 private: 92 private:
90 SkColor color_; 93 SkColor color_;
91 }; 94 };
92 95
96 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.
97 public:
98 enum DrawFunction {
99 STRING_WITH_HALO,
100 STRING_FADED,
101 STRING_WITH_SHADOWS
102 };
103
104 DrawStringLayer(SkColor back_color, SkColor halo_color, DrawFunction func)
105 : Layer(LAYER_TEXTURED),
106 background_color_(back_color),
107 halo_color_(halo_color),
108 func_(func) {
109 set_delegate(this);
110 }
111
112 ~DrawStringLayer() override {}
113
114 // Overridden from LayerDelegate:
115 void OnPaintLayer(const ui::PaintContext& context) override {
116 ui::PaintRecorder recorder(context, size());
117 recorder.canvas()->DrawColor(background_color_);
118 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.
119 switch (func_) {
120 case STRING_WITH_HALO:
121 recorder.canvas()->DrawStringRectWithHalo(
122 text, font_list_, SK_ColorRED, halo_color_, gfx::Rect(size()), 0);
123 break;
124 case STRING_FADED:
125 recorder.canvas()->DrawFadedString(
126 text, font_list_, SK_ColorRED, gfx::Rect(size()), 0);
127 break;
128 case STRING_WITH_SHADOWS: {
129 gfx::ShadowValues shadows;
130 shadows.push_back(
131 gfx::ShadowValue(gfx::Vector2d(2, 2), 2, SK_ColorRED));
132 recorder.canvas()->DrawStringRectWithShadows(
133 text, font_list_, SK_ColorRED, gfx::Rect(size()), 0, 0, shadows);
134 break;
135 }
136 default:
137 NOTREACHED();
138 }
139 }
140
141 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {}
142
143 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
144
145 base::Closure PrepareForLayerBoundsChange() override {
146 return base::Closure();
147 }
148
149 private:
150 SkColor background_color_;
151 SkColor halo_color_;
152 DrawFunction func_;
153 gfx::FontList font_list_;
154 };
155
93 class LayerWithRealCompositorTest : public testing::Test { 156 class LayerWithRealCompositorTest : public testing::Test {
94 public: 157 public:
95 LayerWithRealCompositorTest() { 158 LayerWithRealCompositorTest() {
96 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) { 159 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) {
97 test_data_directory_ = test_data_directory_.AppendASCII("compositor"); 160 test_data_directory_ = test_data_directory_.AppendASCII("compositor");
98 } else { 161 } else {
99 LOG(ERROR) << "Could not open test data directory."; 162 LOG(ERROR) << "Could not open test data directory.";
100 } 163 }
164 gfx::FontList::SetDefaultFontDescription("Arial, Times New Roman, 15px");
101 } 165 }
102 ~LayerWithRealCompositorTest() override {} 166 ~LayerWithRealCompositorTest() override {}
103 167
104 // Overridden from testing::Test: 168 // Overridden from testing::Test:
105 void SetUp() override { 169 void SetUp() override {
106 bool enable_pixel_output = true; 170 bool enable_pixel_output = true;
107 ui::ContextFactory* context_factory = 171 ui::ContextFactory* context_factory =
108 InitializeContextFactoryForTests(enable_pixel_output); 172 InitializeContextFactoryForTests(enable_pixel_output);
109 173
110 const gfx::Rect host_bounds(10, 10, 500, 500); 174 const gfx::Rect host_bounds(10, 10, 500, 500);
(...skipping 22 matching lines...) Expand all
133 layer->SetBounds(bounds); 197 layer->SetBounds(bounds);
134 return layer; 198 return layer;
135 } 199 }
136 200
137 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) { 201 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) {
138 Layer* layer = CreateLayer(LAYER_NOT_DRAWN); 202 Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
139 layer->SetBounds(bounds); 203 layer->SetBounds(bounds);
140 return layer; 204 return layer;
141 } 205 }
142 206
207 Layer* CreateDrawStringLayer(const gfx::Rect& bounds,
208 SkColor background_color,
209 SkColor halo_color,
210 DrawStringLayer::DrawFunction func) {
211 Layer* layer = new DrawStringLayer(background_color, halo_color, func);
212 layer->SetBounds(bounds);
213 return layer;
214 }
215
143 void DrawTree(Layer* root) { 216 void DrawTree(Layer* root) {
144 GetCompositor()->SetRootLayer(root); 217 GetCompositor()->SetRootLayer(root);
145 GetCompositor()->ScheduleDraw(); 218 GetCompositor()->ScheduleDraw();
146 WaitForSwap(); 219 WaitForSwap();
147 } 220 }
148 221
149 void ReadPixels(SkBitmap* bitmap) { 222 void ReadPixels(SkBitmap* bitmap) {
150 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); 223 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size()));
151 } 224 }
152 225
(...skipping 1068 matching lines...) Expand 10 before | Expand all | Expand 10 after
1221 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); 1294 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1222 1295
1223 // l11 back to front 1296 // l11 back to front
1224 l0->StackAbove(l11.get(), l12.get()); 1297 l0->StackAbove(l11.get(), l12.get());
1225 DrawTree(l0.get()); 1298 DrawTree(l0.get());
1226 ReadPixels(&bitmap); 1299 ReadPixels(&bitmap);
1227 ASSERT_FALSE(bitmap.empty()); 1300 ASSERT_FALSE(bitmap.empty());
1228 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); 1301 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1229 } 1302 }
1230 1303
1304 #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.
1305 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithHalo) {
1306 gfx::Size size(50, 50);
1307 GetCompositor()->SetScaleAndSize(1.0f, size);
1308 std::unique_ptr<Layer> layer(CreateDrawStringLayer(
1309 gfx::Rect(size), SK_ColorBLUE, SK_ColorWHITE,
1310 DrawStringLayer::STRING_WITH_HALO));
1311 DrawTree(layer.get());
1312
1313 SkBitmap bitmap;
1314 ReadPixels(&bitmap);
1315 ASSERT_FALSE(bitmap.empty());
1316
1317 base::FilePath ref_img =
1318 test_data_directory().AppendASCII("string_with_halo.png");
1319 // WritePNGFile(bitmap, ref_img, true);
1320
1321 float percentage_pixels_large_error = 10.0f; // 250px / (50*50)
1322 float percentage_pixels_small_error = 0.0f;
1323 float average_error_allowed_in_bad_pixels = 256.f;
1324 int large_error_allowed = 256;
1325 int small_error_allowed = 0;
1326
1327 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1328 cc::FuzzyPixelComparator(
1329 true,
1330 percentage_pixels_large_error,
1331 percentage_pixels_small_error,
1332 average_error_allowed_in_bad_pixels,
1333 large_error_allowed,
1334 small_error_allowed)));
1335 }
1336
1337 TEST_F(LayerWithRealCompositorTest, CanvasDrawFadedString) {
1338 gfx::Size size(50, 50);
1339 GetCompositor()->SetScaleAndSize(1.0f, size);
1340 std::unique_ptr<Layer> layer(CreateDrawStringLayer(
1341 gfx::Rect(size), SK_ColorBLUE, SK_ColorWHITE,
1342 DrawStringLayer::STRING_FADED));
1343 DrawTree(layer.get());
1344
1345 SkBitmap bitmap;
1346 ReadPixels(&bitmap);
1347 ASSERT_FALSE(bitmap.empty());
1348
1349 base::FilePath ref_img =
1350 test_data_directory().AppendASCII("string_faded.png");
1351 // WritePNGFile(bitmap, ref_img, true);
1352
1353 float percentage_pixels_large_error = 15.0f; // 375px / (50*50)
1354 float percentage_pixels_small_error = 0.0f;
1355 float average_error_allowed_in_bad_pixels = 256.f;
1356 int large_error_allowed = 256;
1357 int small_error_allowed = 0;
1358
1359 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1360 cc::FuzzyPixelComparator(
1361 true,
1362 percentage_pixels_large_error,
1363 percentage_pixels_small_error,
1364 average_error_allowed_in_bad_pixels,
1365 large_error_allowed,
1366 small_error_allowed)));
1367 }
1368
1369 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithShadows) {
1370 gfx::Size size(50, 50);
1371 GetCompositor()->SetScaleAndSize(1.0f, size);
1372 std::unique_ptr<Layer> layer(CreateDrawStringLayer(
1373 gfx::Rect(size), SK_ColorBLUE, SK_ColorWHITE,
1374 DrawStringLayer::STRING_WITH_SHADOWS));
1375 DrawTree(layer.get());
1376
1377 SkBitmap bitmap;
1378 ReadPixels(&bitmap);
1379 ASSERT_FALSE(bitmap.empty());
1380
1381 base::FilePath ref_img =
1382 test_data_directory().AppendASCII("string_with_shadows.png");
1383 // WritePNGFile(bitmap, ref_img, true);
1384
1385 float percentage_pixels_large_error = 20.0f; // 500px / (50*50)
1386 float percentage_pixels_small_error = 0.0f;
1387 float average_error_allowed_in_bad_pixels = 256.f;
1388 int large_error_allowed = 256;
1389 int small_error_allowed = 0;
1390
1391 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1392 cc::FuzzyPixelComparator(
1393 true,
1394 percentage_pixels_large_error,
1395 percentage_pixels_small_error,
1396 average_error_allowed_in_bad_pixels,
1397 large_error_allowed,
1398 small_error_allowed)));
1399 }
1400 #endif // defined(OS_WIN)
1401
1231 // Opacity is rendered correctly. 1402 // Opacity is rendered correctly.
1232 // Checks that modifying the hierarchy correctly affects final composite. 1403 // Checks that modifying the hierarchy correctly affects final composite.
1233 TEST_F(LayerWithRealCompositorTest, Opacity) { 1404 TEST_F(LayerWithRealCompositorTest, Opacity) {
1234 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); 1405 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50));
1235 1406
1236 // l0 1407 // l0
1237 // +-l11 1408 // +-l11
1238 std::unique_ptr<Layer> l0( 1409 std::unique_ptr<Layer> l0(
1239 CreateColorLayer(SK_ColorRED, gfx::Rect(0, 0, 50, 50))); 1410 CreateColorLayer(SK_ColorRED, gfx::Rect(0, 0, 50, 50)));
1240 std::unique_ptr<Layer> l11( 1411 std::unique_ptr<Layer> l11(
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after
1877 root->SetOpacity(0.5f); 2048 root->SetOpacity(0.5f);
1878 WaitForSwap(); 2049 WaitForSwap();
1879 EXPECT_EQ(1u, animation_observer.animation_step_count()); 2050 EXPECT_EQ(1u, animation_observer.animation_step_count());
1880 2051
1881 EXPECT_FALSE(animation_observer.shutdown()); 2052 EXPECT_FALSE(animation_observer.shutdown());
1882 ResetCompositor(); 2053 ResetCompositor();
1883 EXPECT_TRUE(animation_observer.shutdown()); 2054 EXPECT_TRUE(animation_observer.shutdown());
1884 } 2055 }
1885 2056
1886 } // namespace ui 2057 } // namespace ui
OLDNEW
« no previous file with comments | « no previous file | ui/gfx/canvas.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698