OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/snapshot/snapshot.h" | 5 #include "ui/snapshot/snapshot.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/test/test_simple_task_runner.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "ui/aura/root_window.h" | 10 #include "ui/aura/root_window.h" |
9 #include "ui/aura/test/aura_test_helper.h" | 11 #include "ui/aura/test/aura_test_helper.h" |
10 #include "ui/aura/test/test_screen.h" | 12 #include "ui/aura/test/test_screen.h" |
11 #include "ui/aura/test/test_window_delegate.h" | 13 #include "ui/aura/test/test_window_delegate.h" |
12 #include "ui/aura/test/test_windows.h" | 14 #include "ui/aura/test/test_windows.h" |
13 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
14 #include "ui/compositor/layer.h" | 16 #include "ui/compositor/layer.h" |
15 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
16 #include "ui/gfx/gfx_paths.h" | 18 #include "ui/gfx/gfx_paths.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 size_t result = 0; | 53 size_t result = 0; |
52 for (int i = 0; i < bitmap->width() * bitmap->height(); ++i) { | 54 for (int i = 0; i < bitmap->width() * bitmap->height(); ++i) { |
53 if (static_cast<SkColor>(bitmap_data[i]) != kPaintColor) | 55 if (static_cast<SkColor>(bitmap_data[i]) != kPaintColor) |
54 ++result; | 56 ++result; |
55 } | 57 } |
56 return result; | 58 return result; |
57 } | 59 } |
58 | 60 |
59 } // namespace | 61 } // namespace |
60 | 62 |
61 class SnapshotAuraTest : public testing::Test { | 63 class SnapshotAuraTest : public testing::TestWithParam<bool> { |
62 public: | 64 public: |
63 SnapshotAuraTest() {} | 65 SnapshotAuraTest() {} |
64 virtual ~SnapshotAuraTest() {} | 66 virtual ~SnapshotAuraTest() {} |
65 | 67 |
66 virtual void SetUp() OVERRIDE { | 68 virtual void SetUp() OVERRIDE { |
67 testing::Test::SetUp(); | 69 testing::Test::SetUp(); |
68 helper_.reset( | 70 helper_.reset( |
69 new aura::test::AuraTestHelper(base::MessageLoopForUI::current())); | 71 new aura::test::AuraTestHelper(base::MessageLoopForUI::current())); |
70 helper_->SetUp(); | 72 // Snapshot test tests real drawing and readback, so needs a real context. |
| 73 bool allow_test_contexts = false; |
| 74 helper_->SetUp(allow_test_contexts); |
71 } | 75 } |
72 | 76 |
73 virtual void TearDown() OVERRIDE { | 77 virtual void TearDown() OVERRIDE { |
74 test_window_.reset(); | 78 test_window_.reset(); |
75 delegate_.reset(); | 79 delegate_.reset(); |
76 helper_->RunAllPendingInMessageLoop(); | 80 helper_->RunAllPendingInMessageLoop(); |
77 helper_->TearDown(); | 81 helper_->TearDown(); |
78 testing::Test::TearDown(); | 82 testing::Test::TearDown(); |
79 } | 83 } |
80 | 84 |
81 protected: | 85 protected: |
82 aura::Window* test_window() { return test_window_.get(); } | 86 aura::Window* test_window() { return test_window_.get(); } |
83 aura::Window* root_window() { return helper_->root_window(); } | 87 aura::Window* root_window() { return helper_->root_window(); } |
84 aura::WindowEventDispatcher* dispatcher() { return helper_->dispatcher(); } | 88 aura::WindowEventDispatcher* dispatcher() { return helper_->dispatcher(); } |
85 aura::TestScreen* test_screen() { return helper_->test_screen(); } | 89 aura::TestScreen* test_screen() { return helper_->test_screen(); } |
86 | 90 |
87 void WaitForDraw() { | 91 void WaitForDraw() { |
88 dispatcher()->host()->compositor()->ScheduleDraw(); | 92 dispatcher()->host()->compositor()->ScheduleDraw(); |
89 ui::DrawWaiterForTest::Wait(dispatcher()->host()->compositor()); | 93 ui::DrawWaiterForTest::Wait(dispatcher()->host()->compositor()); |
90 } | 94 } |
91 | 95 |
92 void SetupTestWindow(const gfx::Rect& window_bounds) { | 96 void SetupTestWindow(const gfx::Rect& window_bounds) { |
93 delegate_.reset(new TestPaintingWindowDelegate(window_bounds.size())); | 97 delegate_.reset(new TestPaintingWindowDelegate(window_bounds.size())); |
94 test_window_.reset(aura::test::CreateTestWindowWithDelegate( | 98 test_window_.reset(aura::test::CreateTestWindowWithDelegate( |
95 delegate_.get(), 0, window_bounds, root_window())); | 99 delegate_.get(), 0, window_bounds, root_window())); |
96 } | 100 } |
97 | 101 |
| 102 bool is_async_test() const { return GetParam(); } |
| 103 |
98 gfx::Image GrabSnapshotForTestWindow() { | 104 gfx::Image GrabSnapshotForTestWindow() { |
99 std::vector<unsigned char> png_representation; | 105 gfx::Rect source_rect(test_window_->bounds().size()); |
100 gfx::Rect local_bounds(test_window_->bounds().size()); | 106 if (!is_async_test()) { |
101 ui::GrabWindowSnapshot(test_window(), &png_representation, local_bounds); | 107 std::vector<unsigned char> png_representation; |
102 return gfx::Image::CreateFrom1xPNGBytes( | 108 ui::GrabWindowSnapshot(test_window(), &png_representation, source_rect); |
103 base::RefCountedBytes::TakeVector(&png_representation)); | 109 return gfx::Image::CreateFrom1xPNGBytes(&(png_representation[0]), |
| 110 png_representation.size()); |
| 111 } |
| 112 |
| 113 scoped_refptr<base::TestSimpleTaskRunner> task_runner( |
| 114 new base::TestSimpleTaskRunner()); |
| 115 scoped_refptr<SnapshotHolder> holder(new SnapshotHolder); |
| 116 ui::GrabWindowSnapshotAsync( |
| 117 test_window(), |
| 118 source_rect, |
| 119 task_runner, |
| 120 base::Bind(&SnapshotHolder::SnapshotCallback, holder)); |
| 121 |
| 122 // Wait for copy response. |
| 123 WaitForDraw(); |
| 124 // Run internal snapshot callback to scale/rotate response image. |
| 125 task_runner->RunUntilIdle(); |
| 126 // Run SnapshotHolder callback. |
| 127 helper_->RunAllPendingInMessageLoop(); |
| 128 |
| 129 if (holder->completed()) |
| 130 return holder->image(); |
| 131 |
| 132 // Callback never called. |
| 133 NOTREACHED(); |
| 134 return gfx::Image(); |
104 } | 135 } |
105 | 136 |
106 private: | 137 private: |
| 138 class SnapshotHolder : public base::RefCountedThreadSafe<SnapshotHolder> { |
| 139 public: |
| 140 SnapshotHolder() : completed_(false) {} |
| 141 |
| 142 void SnapshotCallback(const gfx::Image& image) { |
| 143 DCHECK(!completed_); |
| 144 image_ = image; |
| 145 completed_ = true; |
| 146 } |
| 147 bool completed() const { |
| 148 return completed_; |
| 149 }; |
| 150 const gfx::Image& image() const { return image_; } |
| 151 |
| 152 private: |
| 153 friend class base::RefCountedThreadSafe<SnapshotHolder>; |
| 154 |
| 155 virtual ~SnapshotHolder() {} |
| 156 |
| 157 gfx::Image image_; |
| 158 bool completed_; |
| 159 }; |
| 160 |
107 scoped_ptr<aura::test::AuraTestHelper> helper_; | 161 scoped_ptr<aura::test::AuraTestHelper> helper_; |
108 scoped_ptr<aura::Window> test_window_; | 162 scoped_ptr<aura::Window> test_window_; |
109 scoped_ptr<TestPaintingWindowDelegate> delegate_; | 163 scoped_ptr<TestPaintingWindowDelegate> delegate_; |
110 std::vector<unsigned char> png_representation_; | 164 std::vector<unsigned char> png_representation_; |
111 | 165 |
112 DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest); | 166 DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest); |
113 }; | 167 }; |
114 | 168 |
115 TEST_F(SnapshotAuraTest, FullScreenWindow) { | 169 INSTANTIATE_TEST_CASE_P(SnapshotAuraTest, SnapshotAuraTest, ::testing::Bool()); |
| 170 |
| 171 TEST_P(SnapshotAuraTest, FullScreenWindow) { |
116 SetupTestWindow(root_window()->bounds()); | 172 SetupTestWindow(root_window()->bounds()); |
117 WaitForDraw(); | 173 WaitForDraw(); |
118 | 174 |
119 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 175 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
120 EXPECT_EQ(test_window()->bounds().size().ToString(), | 176 EXPECT_EQ(test_window()->bounds().size().ToString(), |
121 snapshot.Size().ToString()); | 177 snapshot.Size().ToString()); |
122 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 178 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
123 } | 179 } |
124 | 180 |
125 TEST_F(SnapshotAuraTest, PartialBounds) { | 181 TEST_P(SnapshotAuraTest, PartialBounds) { |
126 gfx::Rect test_bounds(100, 100, 300, 200); | 182 gfx::Rect test_bounds(100, 100, 300, 200); |
127 SetupTestWindow(test_bounds); | 183 SetupTestWindow(test_bounds); |
128 WaitForDraw(); | 184 WaitForDraw(); |
129 | 185 |
130 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 186 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
131 EXPECT_EQ(test_bounds.size().ToString(), | 187 EXPECT_EQ(test_bounds.size().ToString(), |
132 snapshot.Size().ToString()); | 188 snapshot.Size().ToString()); |
133 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 189 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
134 } | 190 } |
135 | 191 |
136 TEST_F(SnapshotAuraTest, Rotated) { | 192 TEST_P(SnapshotAuraTest, Rotated) { |
137 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); | 193 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); |
138 | 194 |
139 gfx::Rect test_bounds(100, 100, 300, 200); | 195 gfx::Rect test_bounds(100, 100, 300, 200); |
140 SetupTestWindow(test_bounds); | 196 SetupTestWindow(test_bounds); |
141 WaitForDraw(); | 197 WaitForDraw(); |
142 | 198 |
143 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 199 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
144 EXPECT_EQ(test_bounds.size().ToString(), | 200 EXPECT_EQ(test_bounds.size().ToString(), |
145 snapshot.Size().ToString()); | 201 snapshot.Size().ToString()); |
146 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 202 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
147 } | 203 } |
148 | 204 |
149 TEST_F(SnapshotAuraTest, UIScale) { | 205 TEST_P(SnapshotAuraTest, UIScale) { |
150 const float kUIScale = 1.25f; | 206 const float kUIScale = 1.25f; |
151 test_screen()->SetUIScale(kUIScale); | 207 test_screen()->SetUIScale(kUIScale); |
152 | 208 |
153 gfx::Rect test_bounds(100, 100, 300, 200); | 209 gfx::Rect test_bounds(100, 100, 300, 200); |
154 SetupTestWindow(test_bounds); | 210 SetupTestWindow(test_bounds); |
155 WaitForDraw(); | 211 WaitForDraw(); |
156 | 212 |
157 // Snapshot always captures the physical pixels. | 213 // Snapshot always captures the physical pixels. |
158 gfx::SizeF snapshot_size(test_bounds.size()); | 214 gfx::SizeF snapshot_size(test_bounds.size()); |
159 snapshot_size.Scale(1.0f / kUIScale); | 215 snapshot_size.Scale(1.0f / kUIScale); |
160 | 216 |
161 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 217 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
162 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 218 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
163 snapshot.Size().ToString()); | 219 snapshot.Size().ToString()); |
164 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 220 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
165 } | 221 } |
166 | 222 |
167 TEST_F(SnapshotAuraTest, DeviceScaleFactor) { | 223 TEST_P(SnapshotAuraTest, DeviceScaleFactor) { |
168 test_screen()->SetDeviceScaleFactor(2.0f); | 224 test_screen()->SetDeviceScaleFactor(2.0f); |
169 | 225 |
170 gfx::Rect test_bounds(100, 100, 150, 100); | 226 gfx::Rect test_bounds(100, 100, 150, 100); |
171 SetupTestWindow(test_bounds); | 227 SetupTestWindow(test_bounds); |
172 WaitForDraw(); | 228 WaitForDraw(); |
173 | 229 |
174 // Snapshot always captures the physical pixels. | 230 // Snapshot always captures the physical pixels. |
175 gfx::SizeF snapshot_size(test_bounds.size()); | 231 gfx::SizeF snapshot_size(test_bounds.size()); |
176 snapshot_size.Scale(2.0f); | 232 snapshot_size.Scale(2.0f); |
177 | 233 |
178 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 234 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
179 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 235 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
180 snapshot.Size().ToString()); | 236 snapshot.Size().ToString()); |
181 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 237 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
182 } | 238 } |
183 | 239 |
184 TEST_F(SnapshotAuraTest, RotateAndUIScale) { | 240 TEST_P(SnapshotAuraTest, RotateAndUIScale) { |
185 const float kUIScale = 1.25f; | 241 const float kUIScale = 1.25f; |
186 test_screen()->SetUIScale(kUIScale); | 242 test_screen()->SetUIScale(kUIScale); |
187 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); | 243 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); |
188 | 244 |
189 gfx::Rect test_bounds(100, 100, 300, 200); | 245 gfx::Rect test_bounds(100, 100, 300, 200); |
190 SetupTestWindow(test_bounds); | 246 SetupTestWindow(test_bounds); |
191 WaitForDraw(); | 247 WaitForDraw(); |
192 | 248 |
193 // Snapshot always captures the physical pixels. | 249 // Snapshot always captures the physical pixels. |
194 gfx::SizeF snapshot_size(test_bounds.size()); | 250 gfx::SizeF snapshot_size(test_bounds.size()); |
195 snapshot_size.Scale(1.0f / kUIScale); | 251 snapshot_size.Scale(1.0f / kUIScale); |
196 | 252 |
197 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 253 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
198 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 254 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
199 snapshot.Size().ToString()); | 255 snapshot.Size().ToString()); |
200 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 256 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
201 } | 257 } |
202 | 258 |
203 TEST_F(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) { | 259 TEST_P(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) { |
204 test_screen()->SetDeviceScaleFactor(2.0f); | 260 test_screen()->SetDeviceScaleFactor(2.0f); |
205 const float kUIScale = 1.25f; | 261 const float kUIScale = 1.25f; |
206 test_screen()->SetUIScale(kUIScale); | 262 test_screen()->SetUIScale(kUIScale); |
207 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); | 263 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); |
208 | 264 |
209 gfx::Rect test_bounds(20, 30, 150, 100); | 265 gfx::Rect test_bounds(20, 30, 150, 100); |
210 SetupTestWindow(test_bounds); | 266 SetupTestWindow(test_bounds); |
211 WaitForDraw(); | 267 WaitForDraw(); |
212 | 268 |
213 // Snapshot always captures the physical pixels. | 269 // Snapshot always captures the physical pixels. |
214 gfx::SizeF snapshot_size(test_bounds.size()); | 270 gfx::SizeF snapshot_size(test_bounds.size()); |
215 snapshot_size.Scale(2.0f / kUIScale); | 271 snapshot_size.Scale(2.0f / kUIScale); |
216 | 272 |
217 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 273 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
218 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 274 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
219 snapshot.Size().ToString()); | 275 snapshot.Size().ToString()); |
220 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 276 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
221 } | 277 } |
222 | 278 |
223 } // namespace ui | 279 } // namespace ui |
OLD | NEW |