Index: ui/compositor/layer_unittest.cc |
diff --git a/ui/compositor/layer_unittest.cc b/ui/compositor/layer_unittest.cc |
index d9f5a7a08dbf68dc6e5dcf3d1528975b9df6ab49..27f7011906499cf386f18f692a2a8bc56701d31b 100644 |
--- a/ui/compositor/layer_unittest.cc |
+++ b/ui/compositor/layer_unittest.cc |
@@ -19,6 +19,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/delegated_frame_provider.h" |
#include "cc/layers/delegated_frame_resource_collection.h" |
@@ -42,10 +43,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 { |
@@ -88,6 +91,66 @@ class ColoredLayer : public Layer, public LayerDelegate { |
SkColor color_; |
}; |
+class DrawStringLayer : public Layer, public LayerDelegate { |
+ public: |
+ enum DrawFunction { |
+ STRING_WITH_HALO, |
+ STRING_FADED, |
+ STRING_WITH_SHADOWS |
+ }; |
+ |
+ DrawStringLayer(SkColor back_color, SkColor halo_color, DrawFunction func) |
+ : Layer(LAYER_TEXTURED), |
+ background_color_(back_color), |
+ halo_color_(halo_color), |
+ func_(func) { |
+ set_delegate(this); |
+ } |
+ |
+ ~DrawStringLayer() override {} |
+ |
+ // Overridden from LayerDelegate: |
+ void OnPaintLayer(const ui::PaintContext& context) override { |
+ ui::PaintRecorder recorder(context, size()); |
+ recorder.canvas()->DrawColor(background_color_); |
+ const base::string16 text = base::ASCIIToUTF16("Danakj"); |
+ switch (func_) { |
+ case STRING_WITH_HALO: |
+ recorder.canvas()->DrawStringRectWithHalo( |
+ text, font_list_, SK_ColorRED, halo_color_, gfx::Rect(size()), 0); |
+ break; |
+ case STRING_FADED: |
+ recorder.canvas()->DrawFadedString( |
+ text, font_list_, SK_ColorRED, gfx::Rect(size()), 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, gfx::Rect(size()), 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_; |
+}; |
+ |
class LayerWithRealCompositorTest : public testing::Test { |
public: |
LayerWithRealCompositorTest() { |
@@ -96,6 +159,7 @@ class LayerWithRealCompositorTest : public testing::Test { |
} else { |
LOG(ERROR) << "Could not open test data directory."; |
} |
+ gfx::FontList::SetDefaultFontDescription("Times New Roman,15px"); |
} |
~LayerWithRealCompositorTest() override {} |
@@ -138,6 +202,15 @@ class LayerWithRealCompositorTest : public testing::Test { |
return layer; |
} |
+ Layer* CreateDrawStringLayer(const gfx::Rect& bounds, |
+ SkColor background_color, |
+ SkColor halo_color, |
+ DrawStringLayer::DrawFunction func) { |
+ Layer* layer = new DrawStringLayer(background_color, halo_color, func); |
+ layer->SetBounds(bounds); |
+ return layer; |
+ } |
+ |
void DrawTree(Layer* root) { |
GetCompositor()->SetRootLayer(root); |
GetCompositor()->ScheduleDraw(); |
@@ -193,6 +266,30 @@ class LayerWithRealCompositorTest : public testing::Test { |
return test_data_directory_; |
} |
+ bool CompareColor(const SkBitmap& bmp, const gfx::Size& size, SkColor color) { |
+ SkBitmap ref_bmp; |
+ ref_bmp.allocPixels( |
+ SkImageInfo::MakeN32Premul(size.width(), size.height())); |
+ SkCanvas canvas(ref_bmp); |
+ canvas.clear(color); |
+ |
+ cc::ExactPixelComparator comparator(true); |
+ return comparator.Compare(bmp, ref_bmp); |
+ } |
+ |
+ bool HasColor(const SkBitmap& bitmap, const gfx::Size& size, SkColor color) { |
+ SkAutoLockPixels lock(bitmap); |
+ for (int x = 0; x < bitmap.width(); x++) { |
+ for (int y = 0; y < bitmap.height(); y++) { |
+ SkColor actual_color = bitmap.getColor(x, y); |
+ if (actual_color == color) { |
+ return true; |
+ } |
+ } |
+ } |
+ return false; |
+ } |
+ |
private: |
class ReadbackHolder : public base::RefCountedThreadSafe<ReadbackHolder> { |
public: |
@@ -1219,6 +1316,61 @@ TEST_F(LayerWithRealCompositorTest, ModifyHierarchy) { |
EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); |
} |
+TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithHalo) { |
+ gfx::Size size(50, 50); |
+ SkColor check_color = SK_ColorBLUE; |
+ SkColor halo_color = SK_ColorWHITE; |
+ GetCompositor()->SetScaleAndSize(1.0f, size); |
+ scoped_ptr<Layer> layer(CreateDrawStringLayer( |
+ gfx::Rect(size), check_color, halo_color, |
+ DrawStringLayer::STRING_WITH_HALO)); |
+ DrawTree(layer.get()); |
+ |
+ SkBitmap bitmap; |
+ ReadPixels(&bitmap); |
+ ASSERT_FALSE(bitmap.empty()); |
+ ASSERT_EQ(50, bitmap.width()); |
+ ASSERT_EQ(50, bitmap.height()); |
+ |
+ EXPECT_FALSE(CompareColor(bitmap, size, check_color)); |
+ EXPECT_TRUE(HasColor(bitmap, size, halo_color)); |
+} |
+ |
+TEST_F(LayerWithRealCompositorTest, CanvasDrawFadedString) { |
+ gfx::Size size(50, 50); |
+ SkColor check_color = SK_ColorBLUE; |
+ SkColor halo_color = SK_ColorWHITE; |
+ GetCompositor()->SetScaleAndSize(1.0f, size); |
+ scoped_ptr<Layer> layer(CreateDrawStringLayer( |
+ gfx::Rect(size), check_color, halo_color, DrawStringLayer::STRING_FADED)); |
+ DrawTree(layer.get()); |
+ |
+ SkBitmap bitmap; |
+ ReadPixels(&bitmap); |
+ ASSERT_FALSE(bitmap.empty()); |
+ |
+ EXPECT_FALSE(CompareColor(bitmap, size, check_color)); |
+ EXPECT_FALSE(HasColor(bitmap, size, halo_color)); |
+} |
+ |
+TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithShadows) { |
+ gfx::Size size(50, 50); |
+ SkColor check_color = SK_ColorBLUE; |
+ SkColor halo_color = SK_ColorWHITE; |
+ GetCompositor()->SetScaleAndSize(1.0f, size); |
+ scoped_ptr<Layer> layer(CreateDrawStringLayer( |
+ gfx::Rect(size), check_color, halo_color, |
+ DrawStringLayer::STRING_WITH_SHADOWS)); |
+ DrawTree(layer.get()); |
+ |
+ SkBitmap bitmap; |
+ ReadPixels(&bitmap); |
+ ASSERT_FALSE(bitmap.empty()); |
+ |
+ EXPECT_FALSE(CompareColor(bitmap, size, check_color)); |
+ EXPECT_FALSE(HasColor(bitmap, size, halo_color)); |
+} |
+ |
// Opacity is rendered correctly. |
// Checks that modifying the hierarchy correctly affects final composite. |
TEST_F(LayerWithRealCompositorTest, Opacity) { |