| 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" | 7 #include "base/bind.h" |
| 8 #include "base/test/test_simple_task_runner.h" | 8 #include "base/test/test_simple_task_runner.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/aura/root_window.h" | 10 #include "ui/aura/root_window.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 size_t result = 0; | 53 size_t result = 0; |
| 54 for (int i = 0; i < bitmap->width() * bitmap->height(); ++i) { | 54 for (int i = 0; i < bitmap->width() * bitmap->height(); ++i) { |
| 55 if (static_cast<SkColor>(bitmap_data[i]) != kPaintColor) | 55 if (static_cast<SkColor>(bitmap_data[i]) != kPaintColor) |
| 56 ++result; | 56 ++result; |
| 57 } | 57 } |
| 58 return result; | 58 return result; |
| 59 } | 59 } |
| 60 | 60 |
| 61 } // namespace | 61 } // namespace |
| 62 | 62 |
| 63 class SnapshotAuraTest : public testing::TestWithParam<bool> { | 63 class SnapshotAuraTest : public testing::Test { |
| 64 public: | 64 public: |
| 65 SnapshotAuraTest() {} | 65 SnapshotAuraTest() {} |
| 66 virtual ~SnapshotAuraTest() {} | 66 virtual ~SnapshotAuraTest() {} |
| 67 | 67 |
| 68 virtual void SetUp() OVERRIDE { | 68 virtual void SetUp() OVERRIDE { |
| 69 testing::Test::SetUp(); | 69 testing::Test::SetUp(); |
| 70 helper_.reset( | 70 helper_.reset( |
| 71 new aura::test::AuraTestHelper(base::MessageLoopForUI::current())); | 71 new aura::test::AuraTestHelper(base::MessageLoopForUI::current())); |
| 72 // Snapshot test tests real drawing and readback, so needs a real context. | 72 // Snapshot test tests real drawing and readback, so needs a real context. |
| 73 bool allow_test_contexts = false; | 73 bool allow_test_contexts = false; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 92 dispatcher()->host()->compositor()->ScheduleDraw(); | 92 dispatcher()->host()->compositor()->ScheduleDraw(); |
| 93 ui::DrawWaiterForTest::Wait(dispatcher()->host()->compositor()); | 93 ui::DrawWaiterForTest::Wait(dispatcher()->host()->compositor()); |
| 94 } | 94 } |
| 95 | 95 |
| 96 void SetupTestWindow(const gfx::Rect& window_bounds) { | 96 void SetupTestWindow(const gfx::Rect& window_bounds) { |
| 97 delegate_.reset(new TestPaintingWindowDelegate(window_bounds.size())); | 97 delegate_.reset(new TestPaintingWindowDelegate(window_bounds.size())); |
| 98 test_window_.reset(aura::test::CreateTestWindowWithDelegate( | 98 test_window_.reset(aura::test::CreateTestWindowWithDelegate( |
| 99 delegate_.get(), 0, window_bounds, root_window())); | 99 delegate_.get(), 0, window_bounds, root_window())); |
| 100 } | 100 } |
| 101 | 101 |
| 102 bool is_async_test() const { return GetParam(); } | |
| 103 | |
| 104 gfx::Image GrabSnapshotForTestWindow() { | 102 gfx::Image GrabSnapshotForTestWindow() { |
| 105 gfx::Rect source_rect(test_window_->bounds().size()); | 103 gfx::Rect source_rect(test_window_->bounds().size()); |
| 106 if (!is_async_test()) { | |
| 107 std::vector<unsigned char> png_representation; | |
| 108 ui::GrabWindowSnapshot(test_window(), &png_representation, source_rect); | |
| 109 return gfx::Image::CreateFrom1xPNGBytes(&(png_representation[0]), | |
| 110 png_representation.size()); | |
| 111 } | |
| 112 | 104 |
| 113 scoped_refptr<base::TestSimpleTaskRunner> task_runner( | 105 scoped_refptr<base::TestSimpleTaskRunner> task_runner( |
| 114 new base::TestSimpleTaskRunner()); | 106 new base::TestSimpleTaskRunner()); |
| 115 scoped_refptr<SnapshotHolder> holder(new SnapshotHolder); | 107 scoped_refptr<SnapshotHolder> holder(new SnapshotHolder); |
| 116 ui::GrabWindowSnapshotAsync( | 108 ui::GrabWindowSnapshotAsync( |
| 117 test_window(), | 109 test_window(), |
| 118 source_rect, | 110 source_rect, |
| 119 task_runner, | 111 task_runner, |
| 120 base::Bind(&SnapshotHolder::SnapshotCallback, holder)); | 112 base::Bind(&SnapshotHolder::SnapshotCallback, holder)); |
| 121 | 113 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 }; | 152 }; |
| 161 | 153 |
| 162 scoped_ptr<aura::test::AuraTestHelper> helper_; | 154 scoped_ptr<aura::test::AuraTestHelper> helper_; |
| 163 scoped_ptr<aura::Window> test_window_; | 155 scoped_ptr<aura::Window> test_window_; |
| 164 scoped_ptr<TestPaintingWindowDelegate> delegate_; | 156 scoped_ptr<TestPaintingWindowDelegate> delegate_; |
| 165 std::vector<unsigned char> png_representation_; | 157 std::vector<unsigned char> png_representation_; |
| 166 | 158 |
| 167 DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest); | 159 DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest); |
| 168 }; | 160 }; |
| 169 | 161 |
| 170 INSTANTIATE_TEST_CASE_P(SnapshotAuraTest, SnapshotAuraTest, ::testing::Bool()); | 162 TEST_F(SnapshotAuraTest, FullScreenWindow) { |
| 171 | |
| 172 TEST_P(SnapshotAuraTest, FullScreenWindow) { | |
| 173 SetupTestWindow(root_window()->bounds()); | 163 SetupTestWindow(root_window()->bounds()); |
| 174 WaitForDraw(); | 164 WaitForDraw(); |
| 175 | 165 |
| 176 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 166 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 177 EXPECT_EQ(test_window()->bounds().size().ToString(), | 167 EXPECT_EQ(test_window()->bounds().size().ToString(), |
| 178 snapshot.Size().ToString()); | 168 snapshot.Size().ToString()); |
| 179 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 169 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 180 } | 170 } |
| 181 | 171 |
| 182 TEST_P(SnapshotAuraTest, PartialBounds) { | 172 TEST_F(SnapshotAuraTest, PartialBounds) { |
| 183 gfx::Rect test_bounds(100, 100, 300, 200); | 173 gfx::Rect test_bounds(100, 100, 300, 200); |
| 184 SetupTestWindow(test_bounds); | 174 SetupTestWindow(test_bounds); |
| 185 WaitForDraw(); | 175 WaitForDraw(); |
| 186 | 176 |
| 187 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 177 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 188 EXPECT_EQ(test_bounds.size().ToString(), | 178 EXPECT_EQ(test_bounds.size().ToString(), |
| 189 snapshot.Size().ToString()); | 179 snapshot.Size().ToString()); |
| 190 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 180 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 191 } | 181 } |
| 192 | 182 |
| 193 TEST_P(SnapshotAuraTest, Rotated) { | 183 TEST_F(SnapshotAuraTest, Rotated) { |
| 194 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); | 184 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); |
| 195 | 185 |
| 196 gfx::Rect test_bounds(100, 100, 300, 200); | 186 gfx::Rect test_bounds(100, 100, 300, 200); |
| 197 SetupTestWindow(test_bounds); | 187 SetupTestWindow(test_bounds); |
| 198 WaitForDraw(); | 188 WaitForDraw(); |
| 199 | 189 |
| 200 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 190 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 201 EXPECT_EQ(test_bounds.size().ToString(), | 191 EXPECT_EQ(test_bounds.size().ToString(), |
| 202 snapshot.Size().ToString()); | 192 snapshot.Size().ToString()); |
| 203 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 193 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 204 } | 194 } |
| 205 | 195 |
| 206 TEST_P(SnapshotAuraTest, UIScale) { | 196 TEST_F(SnapshotAuraTest, UIScale) { |
| 207 const float kUIScale = 1.25f; | 197 const float kUIScale = 1.25f; |
| 208 test_screen()->SetUIScale(kUIScale); | 198 test_screen()->SetUIScale(kUIScale); |
| 209 | 199 |
| 210 gfx::Rect test_bounds(100, 100, 300, 200); | 200 gfx::Rect test_bounds(100, 100, 300, 200); |
| 211 SetupTestWindow(test_bounds); | 201 SetupTestWindow(test_bounds); |
| 212 WaitForDraw(); | 202 WaitForDraw(); |
| 213 | 203 |
| 214 // Snapshot always captures the physical pixels. | 204 // Snapshot always captures the physical pixels. |
| 215 gfx::SizeF snapshot_size(test_bounds.size()); | 205 gfx::SizeF snapshot_size(test_bounds.size()); |
| 216 snapshot_size.Scale(1.0f / kUIScale); | 206 snapshot_size.Scale(1.0f / kUIScale); |
| 217 | 207 |
| 218 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 208 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 219 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 209 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
| 220 snapshot.Size().ToString()); | 210 snapshot.Size().ToString()); |
| 221 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 211 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 222 } | 212 } |
| 223 | 213 |
| 224 TEST_P(SnapshotAuraTest, DeviceScaleFactor) { | 214 TEST_F(SnapshotAuraTest, DeviceScaleFactor) { |
| 225 test_screen()->SetDeviceScaleFactor(2.0f); | 215 test_screen()->SetDeviceScaleFactor(2.0f); |
| 226 | 216 |
| 227 gfx::Rect test_bounds(100, 100, 150, 100); | 217 gfx::Rect test_bounds(100, 100, 150, 100); |
| 228 SetupTestWindow(test_bounds); | 218 SetupTestWindow(test_bounds); |
| 229 WaitForDraw(); | 219 WaitForDraw(); |
| 230 | 220 |
| 231 // Snapshot always captures the physical pixels. | 221 // Snapshot always captures the physical pixels. |
| 232 gfx::SizeF snapshot_size(test_bounds.size()); | 222 gfx::SizeF snapshot_size(test_bounds.size()); |
| 233 snapshot_size.Scale(2.0f); | 223 snapshot_size.Scale(2.0f); |
| 234 | 224 |
| 235 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 225 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 236 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 226 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
| 237 snapshot.Size().ToString()); | 227 snapshot.Size().ToString()); |
| 238 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 228 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 239 } | 229 } |
| 240 | 230 |
| 241 TEST_P(SnapshotAuraTest, RotateAndUIScale) { | 231 TEST_F(SnapshotAuraTest, RotateAndUIScale) { |
| 242 const float kUIScale = 1.25f; | 232 const float kUIScale = 1.25f; |
| 243 test_screen()->SetUIScale(kUIScale); | 233 test_screen()->SetUIScale(kUIScale); |
| 244 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); | 234 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); |
| 245 | 235 |
| 246 gfx::Rect test_bounds(100, 100, 300, 200); | 236 gfx::Rect test_bounds(100, 100, 300, 200); |
| 247 SetupTestWindow(test_bounds); | 237 SetupTestWindow(test_bounds); |
| 248 WaitForDraw(); | 238 WaitForDraw(); |
| 249 | 239 |
| 250 // Snapshot always captures the physical pixels. | 240 // Snapshot always captures the physical pixels. |
| 251 gfx::SizeF snapshot_size(test_bounds.size()); | 241 gfx::SizeF snapshot_size(test_bounds.size()); |
| 252 snapshot_size.Scale(1.0f / kUIScale); | 242 snapshot_size.Scale(1.0f / kUIScale); |
| 253 | 243 |
| 254 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 244 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 255 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 245 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
| 256 snapshot.Size().ToString()); | 246 snapshot.Size().ToString()); |
| 257 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 247 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 258 } | 248 } |
| 259 | 249 |
| 260 TEST_P(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) { | 250 TEST_F(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) { |
| 261 test_screen()->SetDeviceScaleFactor(2.0f); | 251 test_screen()->SetDeviceScaleFactor(2.0f); |
| 262 const float kUIScale = 1.25f; | 252 const float kUIScale = 1.25f; |
| 263 test_screen()->SetUIScale(kUIScale); | 253 test_screen()->SetUIScale(kUIScale); |
| 264 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); | 254 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); |
| 265 | 255 |
| 266 gfx::Rect test_bounds(20, 30, 150, 100); | 256 gfx::Rect test_bounds(20, 30, 150, 100); |
| 267 SetupTestWindow(test_bounds); | 257 SetupTestWindow(test_bounds); |
| 268 WaitForDraw(); | 258 WaitForDraw(); |
| 269 | 259 |
| 270 // Snapshot always captures the physical pixels. | 260 // Snapshot always captures the physical pixels. |
| 271 gfx::SizeF snapshot_size(test_bounds.size()); | 261 gfx::SizeF snapshot_size(test_bounds.size()); |
| 272 snapshot_size.Scale(2.0f / kUIScale); | 262 snapshot_size.Scale(2.0f / kUIScale); |
| 273 | 263 |
| 274 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 264 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 275 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 265 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
| 276 snapshot.Size().ToString()); | 266 snapshot.Size().ToString()); |
| 277 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 267 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 278 } | 268 } |
| 279 | 269 |
| 280 } // namespace ui | 270 } // namespace ui |
| OLD | NEW |