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

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

Issue 2654853002: Remove gfx::Canvas::DrawStringRectWithHalo. (Closed)
Patch Set: msw review Created 3 years, 11 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>
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 } 85 }
86 86
87 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} 87 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {}
88 88
89 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} 89 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
90 90
91 private: 91 private:
92 SkColor color_; 92 SkColor color_;
93 }; 93 };
94 94
95 // Layer delegate for painting text with effects on canvas. 95 // Layer delegate for painting text with fade effect on canvas.
96 class DrawStringLayerDelegate : public LayerDelegate { 96 class DrawFadedStringLayerDelegateDelegate : public LayerDelegate {
msw 2017/01/26 00:52:10 nit: DelegateDelegate?
Evan Stade 2017/03/22 22:59:56 Done.
97 public: 97 public:
98 enum DrawFunction { 98 DrawFadedStringLayerDelegateDelegate(SkColor back_color,
99 STRING_WITH_HALO, 99 const gfx::Size& layer_size)
100 STRING_FADED, 100 : background_color_(back_color), layer_size_(layer_size) {}
101 };
102 101
103 DrawStringLayerDelegate( 102 ~DrawFadedStringLayerDelegateDelegate() override {}
104 SkColor back_color, SkColor halo_color,
105 DrawFunction func, const gfx::Size& layer_size)
106 : background_color_(back_color),
107 halo_color_(halo_color),
108 func_(func),
109 layer_size_(layer_size) {
110 }
111
112 ~DrawStringLayerDelegate() override {}
113 103
114 // Overridden from LayerDelegate: 104 // Overridden from LayerDelegate:
115 void OnPaintLayer(const ui::PaintContext& context) override { 105 void OnPaintLayer(const ui::PaintContext& context) override {
116 ui::PaintRecorder recorder(context, layer_size_); 106 ui::PaintRecorder recorder(context, layer_size_);
117 gfx::Rect bounds(layer_size_); 107 gfx::Rect bounds(layer_size_);
118 recorder.canvas()->DrawColor(background_color_); 108 recorder.canvas()->DrawColor(background_color_);
119 const base::string16 text = base::ASCIIToUTF16("Tests!"); 109 const base::string16 text = base::ASCIIToUTF16("Tests!");
120 switch (func_) { 110 recorder.canvas()->DrawFadedString(text, font_list_, SK_ColorRED, bounds,
121 case STRING_WITH_HALO: 111 0);
122 recorder.canvas()->DrawStringRectWithHalo(
123 text, font_list_, SK_ColorRED, halo_color_, bounds, 0);
124 break;
125 case STRING_FADED:
126 recorder.canvas()->DrawFadedString(
127 text, font_list_, SK_ColorRED, bounds, 0);
128 break;
129 default:
130 NOTREACHED();
131 }
132 } 112 }
133 113
134 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} 114 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {}
135 115
136 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} 116 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
137 117
138 private: 118 private:
139 const SkColor background_color_; 119 const SkColor background_color_;
140 const SkColor halo_color_;
141 const DrawFunction func_;
142 const gfx::FontList font_list_; 120 const gfx::FontList font_list_;
143 const gfx::Size layer_size_; 121 const gfx::Size layer_size_;
144 122
145 DISALLOW_COPY_AND_ASSIGN(DrawStringLayerDelegate); 123 DISALLOW_COPY_AND_ASSIGN(DrawFadedStringLayerDelegateDelegate);
146 }; 124 };
147 125
148 class LayerWithRealCompositorTest : public testing::Test { 126 class LayerWithRealCompositorTest : public testing::Test {
149 public: 127 public:
150 LayerWithRealCompositorTest() { 128 LayerWithRealCompositorTest() {
151 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) { 129 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) {
152 test_data_directory_ = test_data_directory_.AppendASCII("compositor"); 130 test_data_directory_ = test_data_directory_.AppendASCII("compositor");
153 } else { 131 } else {
154 LOG(ERROR) << "Could not open test data directory."; 132 LOG(ERROR) << "Could not open test data directory.";
155 } 133 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 layer->SetBounds(bounds); 169 layer->SetBounds(bounds);
192 return layer; 170 return layer;
193 } 171 }
194 172
195 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) { 173 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) {
196 Layer* layer = CreateLayer(LAYER_NOT_DRAWN); 174 Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
197 layer->SetBounds(bounds); 175 layer->SetBounds(bounds);
198 return layer; 176 return layer;
199 } 177 }
200 178
201 std::unique_ptr<Layer> CreateDrawStringLayer( 179 std::unique_ptr<Layer> CreateDrawFadedStringLayerDelegate(
202 const gfx::Rect& bounds, DrawStringLayerDelegate* delegate) { 180 const gfx::Rect& bounds,
181 DrawFadedStringLayerDelegateDelegate* delegate) {
203 std::unique_ptr<Layer> layer(new Layer(LAYER_TEXTURED)); 182 std::unique_ptr<Layer> layer(new Layer(LAYER_TEXTURED));
204 layer->SetBounds(bounds); 183 layer->SetBounds(bounds);
205 layer->set_delegate(delegate); 184 layer->set_delegate(delegate);
206 return layer; 185 return layer;
207 } 186 }
208 187
209 void DrawTree(Layer* root) { 188 void DrawTree(Layer* root) {
210 GetCompositor()->SetRootLayer(root); 189 GetCompositor()->SetRootLayer(root);
211 GetCompositor()->ScheduleDraw(); 190 GetCompositor()->ScheduleDraw();
212 WaitForSwap(); 191 WaitForSwap();
(...skipping 1230 matching lines...) Expand 10 before | Expand all | Expand 10 after
1443 ReadPixels(&bitmap); 1422 ReadPixels(&bitmap);
1444 ASSERT_FALSE(bitmap.empty()); 1423 ASSERT_FALSE(bitmap.empty());
1445 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); 1424 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1446 } 1425 }
1447 1426
1448 // It is really hard to write pixel test on text rendering, 1427 // It is really hard to write pixel test on text rendering,
1449 // due to different font appearance. 1428 // due to different font appearance.
1450 // So we choose to check result only on Windows. 1429 // So we choose to check result only on Windows.
1451 // See https://codereview.chromium.org/1634103003/#msg41 1430 // See https://codereview.chromium.org/1634103003/#msg41
1452 #if defined(OS_WIN) 1431 #if defined(OS_WIN)
1453 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithHalo) { 1432 TEST_F(LayerWithRealCompositorTest, CanvasDrawFadedString) {
1454 gfx::Size size(50, 50); 1433 gfx::Size size(50, 50);
1455 GetCompositor()->SetScaleAndSize(1.0f, size); 1434 GetCompositor()->SetScaleAndSize(1.0f, size);
1456 DrawStringLayerDelegate delegate(SK_ColorBLUE, SK_ColorWHITE, 1435 DrawFadedStringLayerDelegateDelegate delegate(SK_ColorBLUE, SK_ColorWHITE,
1457 DrawStringLayerDelegate::STRING_WITH_HALO, 1436 size);
1458 size);
1459 std::unique_ptr<Layer> layer( 1437 std::unique_ptr<Layer> layer(
1460 CreateDrawStringLayer(gfx::Rect(size), &delegate)); 1438 CreateDrawFadedStringLayerDelegate(gfx::Rect(size), &delegate));
1461 DrawTree(layer.get()); 1439 DrawTree(layer.get());
1462 1440
1463 SkBitmap bitmap; 1441 SkBitmap bitmap;
1464 ReadPixels(&bitmap);
1465 ASSERT_FALSE(bitmap.empty());
1466
1467 base::FilePath ref_img =
1468 test_data_directory().AppendASCII("string_with_halo.png");
1469 // WritePNGFile(bitmap, ref_img, true);
1470
1471 float percentage_pixels_large_error = 1.0f;
1472 float percentage_pixels_small_error = 0.0f;
1473 float average_error_allowed_in_bad_pixels = 1.f;
1474 int large_error_allowed = 1;
1475 int small_error_allowed = 0;
1476
1477 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1478 cc::FuzzyPixelComparator(
1479 true,
1480 percentage_pixels_large_error,
1481 percentage_pixels_small_error,
1482 average_error_allowed_in_bad_pixels,
1483 large_error_allowed,
1484 small_error_allowed)));
1485 }
1486
1487 TEST_F(LayerWithRealCompositorTest, CanvasDrawFadedString) {
1488 gfx::Size size(50, 50);
1489 GetCompositor()->SetScaleAndSize(1.0f, size);
1490 DrawStringLayerDelegate delegate(SK_ColorBLUE, SK_ColorWHITE,
1491 DrawStringLayerDelegate::STRING_FADED,
1492 size);
1493 std::unique_ptr<Layer> layer(
1494 CreateDrawStringLayer(gfx::Rect(size), &delegate));
1495 DrawTree(layer.get());
1496
1497 SkBitmap bitmap;
1498 ReadPixels(&bitmap); 1442 ReadPixels(&bitmap);
1499 ASSERT_FALSE(bitmap.empty()); 1443 ASSERT_FALSE(bitmap.empty());
1500 1444
1501 base::FilePath ref_img = 1445 base::FilePath ref_img =
1502 test_data_directory().AppendASCII("string_faded.png"); 1446 test_data_directory().AppendASCII("string_faded.png");
1503 // WritePNGFile(bitmap, ref_img, true); 1447 // WritePNGFile(bitmap, ref_img, true);
1504 1448
1505 float percentage_pixels_large_error = 8.0f; // 200px / (50*50) 1449 float percentage_pixels_large_error = 8.0f; // 200px / (50*50)
1506 float percentage_pixels_small_error = 0.0f; 1450 float percentage_pixels_small_error = 0.0f;
1507 float average_error_allowed_in_bad_pixels = 80.f; 1451 float average_error_allowed_in_bad_pixels = 80.f;
(...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after
2279 layer.set_name("foo"); 2223 layer.set_name("foo");
2280 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> debug_info = 2224 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> debug_info =
2281 layer.TakeDebugInfo(nullptr); 2225 layer.TakeDebugInfo(nullptr);
2282 std::string trace_format("bar,"); 2226 std::string trace_format("bar,");
2283 debug_info->AppendAsTraceFormat(&trace_format); 2227 debug_info->AppendAsTraceFormat(&trace_format);
2284 std::string expected("bar,{\"layer_name\":\"foo\"}"); 2228 std::string expected("bar,{\"layer_name\":\"foo\"}");
2285 EXPECT_EQ(expected, trace_format); 2229 EXPECT_EQ(expected, trace_format);
2286 } 2230 }
2287 2231
2288 } // namespace ui 2232 } // 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