Index: ui/compositor/layer_unittest.cc |
diff --git a/ui/compositor/layer_unittest.cc b/ui/compositor/layer_unittest.cc |
index 1c8d97976f1f5d58c60f5d222fdac0b02ec5b842..5747f049aabc699df32ceeef108fbbac41d2fe68 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,71 @@ class ColoredLayer : public Layer, public LayerDelegate { |
SkColor color_; |
}; |
+class DrawStringLayerDelegate: public LayerDelegate { |
+ public: |
+ enum DrawFunction { |
+ STRING_WITH_HALO, |
+ STRING_FADED, |
+ STRING_WITH_SHADOWS |
+ }; |
+ |
+ DrawStringLayerDelegate( |
+ SkColor back_color, SkColor halo_color, DrawFunction func) |
+ : background_color_(back_color), |
+ halo_color_(halo_color), |
+ func_(func) { |
+ } |
+ |
+ ~DrawStringLayerDelegate() override {} |
+ |
+ void set_layer_size(const gfx::Size& layer_size) { |
+ layer_size_ = layer_size; |
+ } |
+ |
+ // Overridden from LayerDelegate: |
+ void OnPaintLayer(const ui::PaintContext& context) override { |
+ ui::PaintRecorder recorder(context, layer_size_); |
+ gfx::Rect bounds(layer_size_); |
+ recorder.canvas()->DrawColor(background_color_); |
+ const base::string16 text = base::ASCIIToUTF16("Tests!"); |
+ switch (func_) { |
+ case STRING_WITH_HALO: |
+ recorder.canvas()->DrawStringRectWithHalo( |
+ text, font_list_, SK_ColorRED, halo_color_, bounds, 0); |
+ break; |
+ case STRING_FADED: |
+ recorder.canvas()->DrawFadedString( |
+ text, font_list_, SK_ColorRED, bounds, 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, bounds, 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_; |
+ gfx::Size layer_size_; |
+}; |
+ |
class LayerWithRealCompositorTest : public testing::Test { |
public: |
LayerWithRealCompositorTest() { |
@@ -98,6 +166,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 +209,15 @@ class LayerWithRealCompositorTest : public testing::Test { |
return layer; |
} |
+ Layer* CreateDrawStringLayer(const gfx::Rect& bounds, |
+ DrawStringLayerDelegate* delegate) { |
+ Layer* layer = new Layer(LAYER_TEXTURED); |
+ layer->SetBounds(bounds); |
+ delegate->set_layer_size(layer->size()); |
+ layer->set_delegate(delegate); |
+ return layer; |
+ } |
+ |
void DrawTree(Layer* root) { |
GetCompositor()->SetRootLayer(root); |
GetCompositor()->ScheduleDraw(); |
@@ -1255,6 +1333,108 @@ TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) { |
EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); |
} |
+#if defined(OS_WIN) |
+TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithHalo) { |
+ gfx::Size size(50, 50); |
+ GetCompositor()->SetScaleAndSize(1.0f, size); |
+ DrawStringLayerDelegate delegate(SK_ColorBLUE, SK_ColorWHITE, |
+ DrawStringLayerDelegate::STRING_WITH_HALO); |
+ std::unique_ptr<Layer> layer( |
+ CreateDrawStringLayer(gfx::Rect(size), &delegate)); |
+ 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 = 1.0f; |
+ float percentage_pixels_small_error = 0.0f; |
+ float average_error_allowed_in_bad_pixels = 1.f; |
+ int large_error_allowed = 1; |
+ 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); |
+ DrawStringLayerDelegate delegate(SK_ColorBLUE, SK_ColorWHITE, |
+ DrawStringLayerDelegate::STRING_FADED); |
+ std::unique_ptr<Layer> layer( |
+ CreateDrawStringLayer(gfx::Rect(size), &delegate)); |
+ 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 = 8.0f; // 200px / (50*50) |
+ float percentage_pixels_small_error = 0.0f; |
+ float average_error_allowed_in_bad_pixels = 80.f; |
+ int large_error_allowed = 255; |
+ 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); |
+ DrawStringLayerDelegate delegate( |
+ SK_ColorBLUE, SK_ColorWHITE, |
+ DrawStringLayerDelegate::STRING_WITH_SHADOWS); |
+ std::unique_ptr<Layer> layer( |
+ CreateDrawStringLayer(gfx::Rect(size), &delegate)); |
+ 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 = 8.0f; // 200px / (50*50) |
danakj
2016/06/23 18:43:28
can be smaller even 7.4 or somethin
|
+ float percentage_pixels_small_error = 0.0f; |
+ float average_error_allowed_in_bad_pixels = 60.f; |
+ int large_error_allowed = 255; |
danakj
2016/06/23 18:43:28
Can be 246 then based on what you showed, it's not
|
+ 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) { |