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

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

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