OLD | NEW |
---|---|
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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" | |
13 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
14 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
15 #include "ui/gfx/compositor/compositor_observer.h" | 16 #include "ui/gfx/compositor/compositor_observer.h" |
17 #include "ui/gfx/compositor/compositor_setup.h" | |
18 #include "ui/gfx/compositor/dip_util.h" | |
16 #include "ui/gfx/compositor/layer.h" | 19 #include "ui/gfx/compositor/layer.h" |
17 #include "ui/gfx/compositor/layer_animation_sequence.h" | 20 #include "ui/gfx/compositor/layer_animation_sequence.h" |
18 #include "ui/gfx/compositor/test/test_compositor_host.h" | 21 #include "ui/gfx/compositor/test/test_compositor_host.h" |
19 #include "ui/gfx/gfx_paths.h" | 22 #include "ui/gfx/gfx_paths.h" |
20 #include "ui/gfx/skia_util.h" | 23 #include "ui/gfx/skia_util.h" |
21 | 24 |
22 #include "ui/gfx/compositor/compositor_setup.h" | |
23 | |
24 namespace ui { | 25 namespace ui { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 // Encodes a bitmap into a PNG and write to disk. Returns true on success. The | 29 // Encodes a bitmap into a PNG and write to disk. Returns true on success. The |
29 // parent directory does not have to exist. | 30 // parent directory does not have to exist. |
30 bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path) { | 31 bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path) { |
31 std::vector<unsigned char> png_data; | 32 std::vector<unsigned char> png_data; |
32 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data) && | 33 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data) && |
33 file_util::CreateDirectory(file_path.DirName())) { | 34 file_util::CreateDirectory(file_path.DirName())) { |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
359 | 360 |
360 class LayerWithDelegateTest : public testing::Test, public CompositorDelegate { | 361 class LayerWithDelegateTest : public testing::Test, public CompositorDelegate { |
361 public: | 362 public: |
362 LayerWithDelegateTest() : schedule_draw_invoked_(false) {} | 363 LayerWithDelegateTest() : schedule_draw_invoked_(false) {} |
363 virtual ~LayerWithDelegateTest() {} | 364 virtual ~LayerWithDelegateTest() {} |
364 | 365 |
365 // Overridden from testing::Test: | 366 // Overridden from testing::Test: |
366 virtual void SetUp() OVERRIDE { | 367 virtual void SetUp() OVERRIDE { |
367 ui::SetupTestCompositor(); | 368 ui::SetupTestCompositor(); |
368 compositor_.reset(new Compositor( | 369 compositor_.reset(new Compositor( |
369 this, gfx::kNullAcceleratedWidget, gfx::Size(1000, 1000))); | 370 this, gfx::kNullAcceleratedWidget)); |
371 compositor_->WidgetScaleOrSizeChanged(1.0f, gfx::Size(1000, 1000)); | |
370 } | 372 } |
371 | 373 |
372 virtual void TearDown() OVERRIDE { | 374 virtual void TearDown() OVERRIDE { |
373 } | 375 } |
374 | 376 |
375 Compositor* compositor() { return compositor_.get(); } | 377 Compositor* compositor() { return compositor_.get(); } |
376 | 378 |
377 virtual Layer* CreateLayer(LayerType type) { | 379 virtual Layer* CreateLayer(LayerType type) { |
378 return new Layer(type); | 380 return new Layer(type); |
379 } | 381 } |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
850 | 852 |
851 // Opacity changes should no longer alert the removed observer. | 853 // Opacity changes should no longer alert the removed observer. |
852 observer.Reset(); | 854 observer.Reset(); |
853 l2->SetOpacity(0.5f); | 855 l2->SetOpacity(0.5f); |
854 RunPendingMessages(); | 856 RunPendingMessages(); |
855 EXPECT_FALSE(observer.notified()); | 857 EXPECT_FALSE(observer.notified()); |
856 } | 858 } |
857 | 859 |
858 // Checks that modifying the hierarchy correctly affects final composite. | 860 // Checks that modifying the hierarchy correctly affects final composite. |
859 TEST_F(LayerWithRealCompositorTest, MAYBE_ModifyHierarchy) { | 861 TEST_F(LayerWithRealCompositorTest, MAYBE_ModifyHierarchy) { |
860 GetCompositor()->WidgetSizeChanged(gfx::Size(50, 50)); | 862 GetCompositor()->WidgetScaleOrSizeChanged(1.0f, gfx::Size(50, 50)); |
861 | 863 |
862 // l0 | 864 // l0 |
863 // +-l11 | 865 // +-l11 |
864 // | +-l21 | 866 // | +-l21 |
865 // +-l12 | 867 // +-l12 |
866 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED, | 868 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED, |
867 gfx::Rect(0, 0, 50, 50))); | 869 gfx::Rect(0, 0, 50, 50))); |
868 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN, | 870 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN, |
869 gfx::Rect(0, 0, 25, 25))); | 871 gfx::Rect(0, 0, 25, 25))); |
870 scoped_ptr<Layer> l21(CreateColorLayer(SK_ColorMAGENTA, | 872 scoped_ptr<Layer> l21(CreateColorLayer(SK_ColorMAGENTA, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
910 l0->StackAbove(l12.get(), l11.get()); | 912 l0->StackAbove(l12.get(), l11.get()); |
911 DrawTree(l0.get()); | 913 DrawTree(l0.get()); |
912 ASSERT_TRUE(ReadPixels(&bitmap)); | 914 ASSERT_TRUE(ReadPixels(&bitmap)); |
913 ASSERT_FALSE(bitmap.empty()); | 915 ASSERT_FALSE(bitmap.empty()); |
914 EXPECT_TRUE(IsSameAsPNGFile(bitmap, ref_img1)); | 916 EXPECT_TRUE(IsSameAsPNGFile(bitmap, ref_img1)); |
915 } | 917 } |
916 | 918 |
917 // Opacity is rendered correctly. | 919 // Opacity is rendered correctly. |
918 // Checks that modifying the hierarchy correctly affects final composite. | 920 // Checks that modifying the hierarchy correctly affects final composite. |
919 TEST_F(LayerWithRealCompositorTest, MAYBE_Opacity) { | 921 TEST_F(LayerWithRealCompositorTest, MAYBE_Opacity) { |
920 GetCompositor()->WidgetSizeChanged(gfx::Size(50, 50)); | 922 GetCompositor()->WidgetScaleOrSizeChanged(1.0f, gfx::Size(50, 50)); |
921 | 923 |
922 // l0 | 924 // l0 |
923 // +-l11 | 925 // +-l11 |
924 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED, | 926 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED, |
925 gfx::Rect(0, 0, 50, 50))); | 927 gfx::Rect(0, 0, 50, 50))); |
926 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN, | 928 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN, |
927 gfx::Rect(0, 0, 25, 25))); | 929 gfx::Rect(0, 0, 25, 25))); |
928 | 930 |
929 FilePath ref_img = test_data_directory().AppendASCII("Opacity.png"); | 931 FilePath ref_img = test_data_directory().AppendASCII("Opacity.png"); |
930 | 932 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1014 // ScheduleDraw() should have been invoked. | 1016 // ScheduleDraw() should have been invoked. |
1015 EXPECT_TRUE(schedule_draw_invoked_); | 1017 EXPECT_TRUE(schedule_draw_invoked_); |
1016 // Because SchedulePaint() was invoked from OnPaintLayer() |child| should | 1018 // Because SchedulePaint() was invoked from OnPaintLayer() |child| should |
1017 // still need to be painted. | 1019 // still need to be painted. |
1018 DrawTree(root.get()); | 1020 DrawTree(root.get()); |
1019 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear()); | 1021 EXPECT_EQ(1, child_delegate.GetPaintCountAndClear()); |
1020 EXPECT_TRUE(child_delegate.last_clip_rect().Contains( | 1022 EXPECT_TRUE(child_delegate.last_clip_rect().Contains( |
1021 gfx::Rect(10, 10, 30, 30))); | 1023 gfx::Rect(10, 10, 30, 30))); |
1022 } | 1024 } |
1023 | 1025 |
1026 TEST_F(LayerWithRealCompositorTest, ScaleUp) { | |
sky
2012/05/08 00:41:59
Can you also add coverage that a scale is set appr
oshima
2012/05/08 01:24:16
Done.
| |
1027 test::ScopedDIPEnablerForTest enable; | |
1028 | |
1029 scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE, | |
1030 gfx::Rect(10, 20, 200, 220))); | |
1031 TestLayerDelegate root_delegate; | |
1032 root_delegate.AddColor(SK_ColorWHITE); | |
1033 root->set_delegate(&root_delegate); | |
1034 | |
1035 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorWHITE, | |
1036 gfx::Rect(10, 20, 140, 180))); | |
1037 TestLayerDelegate l1_delegate; | |
1038 l1_delegate.AddColor(SK_ColorWHITE); | |
1039 l1->set_delegate(&l1_delegate); | |
1040 | |
1041 GetCompositor()->WidgetScaleOrSizeChanged(1.0f, gfx::Size(500, 500)); | |
1042 GetCompositor()->SetRootLayer(root.get()); | |
1043 root->Add(l1.get()); | |
1044 RunPendingMessages(); | |
1045 | |
1046 EXPECT_EQ("10,20 200x220", root->bounds().ToString()); | |
1047 EXPECT_EQ("10,20 140x180", l1->bounds().ToString()); | |
1048 gfx::Size size_in_pixel = root->web_layer().bounds(); | |
1049 EXPECT_EQ("200x220", size_in_pixel.ToString()); | |
1050 size_in_pixel = l1->web_layer().bounds(); | |
1051 EXPECT_EQ("140x180", size_in_pixel.ToString()); | |
1052 | |
1053 SchedulePaintForLayer(root.get()); | |
1054 SchedulePaintForLayer(l1.get()); | |
1055 RunPendingMessages(); | |
1056 | |
1057 EXPECT_EQ("200x220", root_delegate.paint_size().ToString()); | |
1058 EXPECT_EQ("140x180", l1_delegate.paint_size().ToString()); | |
1059 | |
1060 // Scale up to 2.0. Changing scale doesn't change the bounds in DIP. | |
1061 GetCompositor()->WidgetScaleOrSizeChanged(2.0f, gfx::Size(500, 500)); | |
1062 EXPECT_EQ("10,20 200x220", root->bounds().ToString()); | |
1063 EXPECT_EQ("10,20 140x180", l1->bounds().ToString()); | |
1064 // Pixel size must have been scaled up. | |
1065 size_in_pixel = root->web_layer().bounds(); | |
1066 EXPECT_EQ("400x440", size_in_pixel.ToString()); | |
1067 size_in_pixel = l1->web_layer().bounds(); | |
1068 EXPECT_EQ("280x360", size_in_pixel.ToString()); | |
1069 | |
1070 // Canvas size must have been scaled down too. | |
1071 SchedulePaintForLayer(root.get()); | |
1072 SchedulePaintForLayer(l1.get()); | |
1073 RunPendingMessages(); | |
1074 EXPECT_EQ("400x440", root_delegate.paint_size().ToString()); | |
1075 EXPECT_EQ("280x360", l1_delegate.paint_size().ToString()); | |
1076 | |
1077 // Scale down back to 1.0f. | |
1078 GetCompositor()->WidgetScaleOrSizeChanged(1.0f, gfx::Size(500, 500)); | |
1079 EXPECT_EQ("10,20 200x220", root->bounds().ToString()); | |
1080 EXPECT_EQ("10,20 140x180", l1->bounds().ToString()); | |
1081 // Pixel size must have been scaled down. | |
1082 size_in_pixel = root->web_layer().bounds(); | |
1083 EXPECT_EQ("200x220", size_in_pixel.ToString()); | |
1084 size_in_pixel = l1->web_layer().bounds(); | |
1085 EXPECT_EQ("140x180", size_in_pixel.ToString()); | |
1086 | |
1087 // Canvas size must have been scaled down too. | |
1088 SchedulePaintForLayer(root.get()); | |
1089 SchedulePaintForLayer(l1.get()); | |
1090 RunPendingMessages(); | |
1091 EXPECT_EQ("200x220", root_delegate.paint_size().ToString()); | |
1092 EXPECT_EQ("140x180", l1_delegate.paint_size().ToString()); | |
1093 } | |
1094 | |
1095 TEST_F(LayerWithRealCompositorTest, ScaleReparent) { | |
1096 test::ScopedDIPEnablerForTest enable; | |
1097 scoped_ptr<Layer> root(CreateColorLayer(SK_ColorWHITE, | |
1098 gfx::Rect(10, 20, 200, 220))); | |
1099 scoped_ptr<Layer> l1(CreateColorLayer(SK_ColorWHITE, | |
1100 gfx::Rect(10, 20, 140, 180))); | |
1101 GetCompositor()->WidgetScaleOrSizeChanged(1.0f, gfx::Size(500, 500)); | |
1102 GetCompositor()->SetRootLayer(root.get()); | |
1103 RunPendingMessages(); | |
1104 | |
1105 root->Add(l1.get()); | |
1106 EXPECT_EQ("10,20 140x180", l1->bounds().ToString()); | |
1107 gfx::Size size_in_pixel = l1->web_layer().bounds(); | |
1108 EXPECT_EQ("140x180", size_in_pixel.ToString()); | |
1109 | |
1110 // Remove l1 from root and change the scale. | |
1111 root->Remove(l1.get()); | |
1112 EXPECT_EQ(NULL, l1->parent()); | |
1113 EXPECT_EQ(NULL, l1->GetCompositor()); | |
1114 GetCompositor()->WidgetScaleOrSizeChanged(2.0f, gfx::Size(500, 500)); | |
1115 // Sanity check on root and l1. | |
1116 EXPECT_EQ("10,20 200x220", root->bounds().ToString()); | |
1117 size_in_pixel = l1->web_layer().bounds(); | |
1118 EXPECT_EQ("140x180", size_in_pixel.ToString()); | |
1119 | |
1120 root->Add(l1.get()); | |
1121 EXPECT_EQ("10,20 140x180", l1->bounds().ToString()); | |
1122 size_in_pixel = l1->web_layer().bounds(); | |
1123 EXPECT_EQ("280x360", size_in_pixel.ToString()); | |
1124 } | |
1125 | |
1024 } // namespace ui | 1126 } // namespace ui |
OLD | NEW |