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

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: rewrite test Created 4 years, 10 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 <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/json/json_reader.h" 15 #include "base/json/json_reader.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
19 #include "base/path_service.h" 19 #include "base/path_service.h"
20 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
22 #include "base/trace_event/trace_event.h" 23 #include "base/trace_event/trace_event.h"
23 #include "cc/layers/delegated_frame_provider.h" 24 #include "cc/layers/delegated_frame_provider.h"
24 #include "cc/layers/delegated_frame_resource_collection.h" 25 #include "cc/layers/delegated_frame_resource_collection.h"
25 #include "cc/layers/layer.h" 26 #include "cc/layers/layer.h"
26 #include "cc/output/copy_output_request.h" 27 #include "cc/output/copy_output_request.h"
27 #include "cc/output/copy_output_result.h" 28 #include "cc/output/copy_output_result.h"
28 #include "cc/output/delegated_frame_data.h" 29 #include "cc/output/delegated_frame_data.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_sequence.h" 34 #include "ui/compositor/layer_animation_sequence.h"
34 #include "ui/compositor/layer_animator.h" 35 #include "ui/compositor/layer_animator.h"
35 #include "ui/compositor/paint_context.h" 36 #include "ui/compositor/paint_context.h"
36 #include "ui/compositor/paint_recorder.h" 37 #include "ui/compositor/paint_recorder.h"
37 #include "ui/compositor/scoped_animation_duration_scale_mode.h" 38 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
38 #include "ui/compositor/scoped_layer_animation_settings.h" 39 #include "ui/compositor/scoped_layer_animation_settings.h"
39 #include "ui/compositor/test/context_factories_for_test.h" 40 #include "ui/compositor/test/context_factories_for_test.h"
40 #include "ui/compositor/test/draw_waiter_for_test.h" 41 #include "ui/compositor/test/draw_waiter_for_test.h"
41 #include "ui/compositor/test/test_compositor_host.h" 42 #include "ui/compositor/test/test_compositor_host.h"
42 #include "ui/compositor/test/test_layers.h" 43 #include "ui/compositor/test/test_layers.h"
43 #include "ui/gfx/canvas.h" 44 #include "ui/gfx/canvas.h"
44 #include "ui/gfx/codec/png_codec.h" 45 #include "ui/gfx/codec/png_codec.h"
46 #include "ui/gfx/font_list.h"
45 #include "ui/gfx/gfx_paths.h" 47 #include "ui/gfx/gfx_paths.h"
46 #include "ui/gfx/skia_util.h" 48 #include "ui/gfx/skia_util.h"
47 49
48 using cc::MatchesPNGFile; 50 using cc::MatchesPNGFile;
51 using cc::WritePNGFile;
52
53 using TestFunction = void(*)(gfx::Canvas*, const base::string16&,
danakj 2016/01/28 19:30:39 Can you give this a better name? What is this func
54 const gfx::FontList&, const gfx::Rect&);
49 55
50 namespace ui { 56 namespace ui {
51 57
52 namespace { 58 namespace {
53 59
54 // There are three test classes in here that configure the Compositor and 60 // There are three test classes in here that configure the Compositor and
55 // Layer's slightly differently: 61 // Layer's slightly differently:
56 // - LayerWithNullDelegateTest uses NullLayerDelegate as the LayerDelegate. This 62 // - LayerWithNullDelegateTest uses NullLayerDelegate as the LayerDelegate. This
57 // is typically the base class you want to use. 63 // is typically the base class you want to use.
58 // - LayerWithDelegateTest uses LayerDelegate on the delegates. 64 // - LayerWithDelegateTest uses LayerDelegate on the delegates.
(...skipping 22 matching lines...) Expand all
81 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} 87 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
82 88
83 base::Closure PrepareForLayerBoundsChange() override { 89 base::Closure PrepareForLayerBoundsChange() override {
84 return base::Closure(); 90 return base::Closure();
85 } 91 }
86 92
87 private: 93 private:
88 SkColor color_; 94 SkColor color_;
89 }; 95 };
90 96
97 class DrawStringLayer : public Layer, public LayerDelegate {
98 public:
99 explicit DrawStringLayer(TestFunction func)
100 : Layer(LAYER_TEXTURED),
101 func_(func) {
102 set_delegate(this);
103 }
104
105 ~DrawStringLayer() override {}
106
107 // Overridden from LayerDelegate:
108 void OnPaintLayer(const ui::PaintContext& context) override {
109 ui::PaintRecorder recorder(context, size());
110 recorder.canvas()->DrawColor(SK_ColorBLUE);
111 const base::string16 text = base::ASCIIToUTF16("Plz, don't crash!");
112 func_(recorder.canvas(), text, font_list_, gfx::Rect(size()));
danakj 2016/01/28 19:30:39 how about just making an enum for the various ways
113 }
114
115 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {}
116
117 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
118
119 base::Closure PrepareForLayerBoundsChange() override {
120 return base::Closure();
121 }
122
123 private:
124 TestFunction func_;
125 gfx::FontList font_list_;
126 };
127
91 class LayerWithRealCompositorTest : public testing::Test { 128 class LayerWithRealCompositorTest : public testing::Test {
92 public: 129 public:
93 LayerWithRealCompositorTest() { 130 LayerWithRealCompositorTest() {
94 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) { 131 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) {
95 test_data_directory_ = test_data_directory_.AppendASCII("compositor"); 132 test_data_directory_ = test_data_directory_.AppendASCII("compositor");
96 } else { 133 } else {
97 LOG(ERROR) << "Could not open test data directory."; 134 LOG(ERROR) << "Could not open test data directory.";
98 } 135 }
136 gfx::FontList::SetDefaultFontDescription("Arial,50px");
99 } 137 }
100 ~LayerWithRealCompositorTest() override {} 138 ~LayerWithRealCompositorTest() override {}
101 139
102 // Overridden from testing::Test: 140 // Overridden from testing::Test:
103 void SetUp() override { 141 void SetUp() override {
104 bool enable_pixel_output = true; 142 bool enable_pixel_output = true;
105 ui::ContextFactory* context_factory = 143 ui::ContextFactory* context_factory =
106 InitializeContextFactoryForTests(enable_pixel_output); 144 InitializeContextFactoryForTests(enable_pixel_output);
107 145
108 const gfx::Rect host_bounds(10, 10, 500, 500); 146 const gfx::Rect host_bounds(10, 10, 500, 500);
(...skipping 22 matching lines...) Expand all
131 layer->SetBounds(bounds); 169 layer->SetBounds(bounds);
132 return layer; 170 return layer;
133 } 171 }
134 172
135 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) { 173 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) {
136 Layer* layer = CreateLayer(LAYER_NOT_DRAWN); 174 Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
137 layer->SetBounds(bounds); 175 layer->SetBounds(bounds);
138 return layer; 176 return layer;
139 } 177 }
140 178
179 Layer* CreateDrawStringLayer(const gfx::Rect& bounds, TestFunction func) {
180 Layer* layer = new DrawStringLayer(func);
181 layer->SetBounds(bounds);
182 return layer;
183 }
184
141 void DrawTree(Layer* root) { 185 void DrawTree(Layer* root) {
142 GetCompositor()->SetRootLayer(root); 186 GetCompositor()->SetRootLayer(root);
143 GetCompositor()->ScheduleDraw(); 187 GetCompositor()->ScheduleDraw();
144 WaitForSwap(); 188 WaitForSwap();
145 } 189 }
146 190
147 void ReadPixels(SkBitmap* bitmap) { 191 void ReadPixels(SkBitmap* bitmap) {
148 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); 192 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size()));
149 } 193 }
150 194
(...skipping 1061 matching lines...) Expand 10 before | Expand all | Expand 10 after
1212 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); 1256 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1213 1257
1214 // l11 back to front 1258 // l11 back to front
1215 l0->StackAbove(l11.get(), l12.get()); 1259 l0->StackAbove(l11.get(), l12.get());
1216 DrawTree(l0.get()); 1260 DrawTree(l0.get());
1217 ReadPixels(&bitmap); 1261 ReadPixels(&bitmap);
1218 ASSERT_FALSE(bitmap.empty()); 1262 ASSERT_FALSE(bitmap.empty());
1219 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); 1263 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1220 } 1264 }
1221 1265
1266 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithHalo) {
1267 TestFunction draw_func =
1268 [](gfx::Canvas* canvas,
1269 const base::string16& text,
1270 const gfx::FontList& font_list,
1271 const gfx::Rect& rect) {
1272 canvas->DrawStringRectWithHalo(
1273 text, font_list, SK_ColorRED,
1274 SK_ColorWHITE, rect, 0);
1275 };
1276 gfx::Size size(512, 512);
1277 GetCompositor()->SetScaleAndSize(1.0f, size);
1278 scoped_ptr<Layer> layer(CreateDrawStringLayer(gfx::Rect(size), draw_func));
1279 DrawTree(layer.get());
1280
1281 SkBitmap bitmap;
1282 ReadPixels(&bitmap);
1283 ASSERT_FALSE(bitmap.empty());
1284
1285 base::FilePath ref_img =
1286 test_data_directory().AppendASCII("string_with_halo.png");
1287 WritePNGFile(bitmap, ref_img, true);
danakj 2016/01/28 19:30:39 If you write the PNG every time, then it will alwa
1288 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, cc::ExactPixelComparator(true)));
1289 }
1290
1291 TEST_F(LayerWithRealCompositorTest, CanvasDrawFadedString) {
1292 TestFunction draw_func =
1293 [](gfx::Canvas* canvas,
1294 const base::string16& text,
1295 const gfx::FontList& font_list,
1296 const gfx::Rect& rect) {
1297 canvas->DrawFadedString(
1298 text, font_list, SK_ColorRED, rect, 0);
1299 };
1300 gfx::Size size(512, 512);
1301 GetCompositor()->SetScaleAndSize(1.0f, size);
1302 scoped_ptr<Layer> layer(CreateDrawStringLayer(gfx::Rect(size), draw_func));
1303 DrawTree(layer.get());
1304
1305 SkBitmap bitmap;
1306 ReadPixels(&bitmap);
1307 ASSERT_FALSE(bitmap.empty());
1308
1309 base::FilePath ref_img =
1310 test_data_directory().AppendASCII("string.png");
danakj 2016/01/28 19:30:39 string_faded.png?
1311 WritePNGFile(bitmap, ref_img, true);
1312 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, cc::ExactPixelComparator(true)));
1313 }
1314
1315 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithShadows) {
1316 TestFunction draw_func =
1317 [](gfx::Canvas* canvas,
1318 const base::string16& text,
1319 const gfx::FontList& font_list,
1320 const gfx::Rect& rect) {
1321 gfx::ShadowValues shadows;
1322 shadows.push_back(
1323 gfx::ShadowValue(gfx::Vector2d(3, 3),
1324 2, SK_ColorRED));
1325 canvas->DrawStringRectWithShadows(
1326 text, font_list, SK_ColorRED,
1327 rect, 0, 0, shadows);
1328 };
1329 gfx::Size size(512, 512);
1330 GetCompositor()->SetScaleAndSize(1.0f, size);
1331 scoped_ptr<Layer> layer(CreateDrawStringLayer(gfx::Rect(size), draw_func));
1332 DrawTree(layer.get());
1333
1334 SkBitmap bitmap;
1335 ReadPixels(&bitmap);
1336 ASSERT_FALSE(bitmap.empty());
1337
1338 base::FilePath ref_img =
1339 test_data_directory().AppendASCII("string_with_shadow.png");
1340 WritePNGFile(bitmap, ref_img, true);
1341 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, cc::ExactPixelComparator(true)));
1342 }
1343
1222 // Opacity is rendered correctly. 1344 // Opacity is rendered correctly.
1223 // Checks that modifying the hierarchy correctly affects final composite. 1345 // Checks that modifying the hierarchy correctly affects final composite.
1224 TEST_F(LayerWithRealCompositorTest, Opacity) { 1346 TEST_F(LayerWithRealCompositorTest, Opacity) {
1225 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); 1347 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50));
1226 1348
1227 // l0 1349 // l0
1228 // +-l11 1350 // +-l11
1229 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED, 1351 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED,
1230 gfx::Rect(0, 0, 50, 50))); 1352 gfx::Rect(0, 0, 50, 50)));
1231 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN, 1353 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN,
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after
1878 root->SetOpacity(0.5f); 2000 root->SetOpacity(0.5f);
1879 WaitForSwap(); 2001 WaitForSwap();
1880 EXPECT_EQ(1u, animation_observer.animation_step_count()); 2002 EXPECT_EQ(1u, animation_observer.animation_step_count());
1881 2003
1882 EXPECT_FALSE(animation_observer.shutdown()); 2004 EXPECT_FALSE(animation_observer.shutdown());
1883 ResetCompositor(); 2005 ResetCompositor();
1884 EXPECT_TRUE(animation_observer.shutdown()); 2006 EXPECT_TRUE(animation_observer.shutdown());
1885 } 2007 }
1886 2008
1887 } // namespace ui 2009 } // 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