Chromium Code Reviews| 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 &(png_representation[0]), png_representation.size()); | 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(); | |
|
enne (OOO)
2014/01/07 19:49:29
Also, fixed the multithreaded case (chromeos=1 vs
| |
| 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 { return completed_; }; | |
| 148 const gfx::Image& image() const { return image_; } | |
| 149 | |
| 150 private: | |
| 151 friend class base::RefCountedThreadSafe<SnapshotHolder>; | |
| 152 | |
| 153 virtual ~SnapshotHolder() {} | |
| 154 | |
| 155 gfx::Image image_; | |
| 156 bool completed_; | |
| 157 }; | |
| 158 | |
| 107 scoped_ptr<aura::test::AuraTestHelper> helper_; | 159 scoped_ptr<aura::test::AuraTestHelper> helper_; |
| 108 scoped_ptr<aura::Window> test_window_; | 160 scoped_ptr<aura::Window> test_window_; |
| 109 scoped_ptr<TestPaintingWindowDelegate> delegate_; | 161 scoped_ptr<TestPaintingWindowDelegate> delegate_; |
| 110 std::vector<unsigned char> png_representation_; | 162 std::vector<unsigned char> png_representation_; |
| 111 | 163 |
| 112 DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest); | 164 DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest); |
| 113 }; | 165 }; |
| 114 | 166 |
| 115 TEST_F(SnapshotAuraTest, FullScreenWindow) { | 167 INSTANTIATE_TEST_CASE_P(SnapshotAuraTest, SnapshotAuraTest, ::testing::Bool()); |
| 168 | |
| 169 TEST_P(SnapshotAuraTest, FullScreenWindow) { | |
| 116 SetupTestWindow(root_window()->bounds()); | 170 SetupTestWindow(root_window()->bounds()); |
| 117 WaitForDraw(); | 171 WaitForDraw(); |
| 118 | 172 |
| 119 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 173 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 120 EXPECT_EQ(test_window()->bounds().size().ToString(), | 174 EXPECT_EQ(test_window()->bounds().size().ToString(), |
| 121 snapshot.Size().ToString()); | 175 snapshot.Size().ToString()); |
| 122 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 176 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 123 } | 177 } |
| 124 | 178 |
| 125 TEST_F(SnapshotAuraTest, PartialBounds) { | 179 TEST_P(SnapshotAuraTest, PartialBounds) { |
| 126 gfx::Rect test_bounds(100, 100, 300, 200); | 180 gfx::Rect test_bounds(100, 100, 300, 200); |
| 127 SetupTestWindow(test_bounds); | 181 SetupTestWindow(test_bounds); |
| 128 WaitForDraw(); | 182 WaitForDraw(); |
| 129 | 183 |
| 130 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 184 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 131 EXPECT_EQ(test_bounds.size().ToString(), | 185 EXPECT_EQ(test_bounds.size().ToString(), |
| 132 snapshot.Size().ToString()); | 186 snapshot.Size().ToString()); |
| 133 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 187 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 134 } | 188 } |
| 135 | 189 |
| 136 TEST_F(SnapshotAuraTest, Rotated) { | 190 TEST_P(SnapshotAuraTest, Rotated) { |
| 137 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); | 191 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); |
| 138 | 192 |
| 139 gfx::Rect test_bounds(100, 100, 300, 200); | 193 gfx::Rect test_bounds(100, 100, 300, 200); |
| 140 SetupTestWindow(test_bounds); | 194 SetupTestWindow(test_bounds); |
| 141 WaitForDraw(); | 195 WaitForDraw(); |
| 142 | 196 |
| 143 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 197 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 144 EXPECT_EQ(test_bounds.size().ToString(), | 198 EXPECT_EQ(test_bounds.size().ToString(), |
| 145 snapshot.Size().ToString()); | 199 snapshot.Size().ToString()); |
| 146 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 200 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 147 } | 201 } |
| 148 | 202 |
| 149 TEST_F(SnapshotAuraTest, UIScale) { | 203 TEST_P(SnapshotAuraTest, UIScale) { |
| 150 const float kUIScale = 1.25f; | 204 const float kUIScale = 1.25f; |
| 151 test_screen()->SetUIScale(kUIScale); | 205 test_screen()->SetUIScale(kUIScale); |
| 152 | 206 |
| 153 gfx::Rect test_bounds(100, 100, 300, 200); | 207 gfx::Rect test_bounds(100, 100, 300, 200); |
| 154 SetupTestWindow(test_bounds); | 208 SetupTestWindow(test_bounds); |
| 155 WaitForDraw(); | 209 WaitForDraw(); |
| 156 | 210 |
| 157 // Snapshot always captures the physical pixels. | 211 // Snapshot always captures the physical pixels. |
| 158 gfx::SizeF snapshot_size(test_bounds.size()); | 212 gfx::SizeF snapshot_size(test_bounds.size()); |
| 159 snapshot_size.Scale(1.0f / kUIScale); | 213 snapshot_size.Scale(1.0f / kUIScale); |
| 160 | 214 |
| 161 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 215 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 162 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 216 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
| 163 snapshot.Size().ToString()); | 217 snapshot.Size().ToString()); |
| 164 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 218 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 165 } | 219 } |
| 166 | 220 |
| 167 TEST_F(SnapshotAuraTest, DeviceScaleFactor) { | 221 TEST_P(SnapshotAuraTest, DeviceScaleFactor) { |
| 168 test_screen()->SetDeviceScaleFactor(2.0f); | 222 test_screen()->SetDeviceScaleFactor(2.0f); |
| 169 | 223 |
| 170 gfx::Rect test_bounds(100, 100, 150, 100); | 224 gfx::Rect test_bounds(100, 100, 150, 100); |
| 171 SetupTestWindow(test_bounds); | 225 SetupTestWindow(test_bounds); |
| 172 WaitForDraw(); | 226 WaitForDraw(); |
| 173 | 227 |
| 174 // Snapshot always captures the physical pixels. | 228 // Snapshot always captures the physical pixels. |
| 175 gfx::SizeF snapshot_size(test_bounds.size()); | 229 gfx::SizeF snapshot_size(test_bounds.size()); |
| 176 snapshot_size.Scale(2.0f); | 230 snapshot_size.Scale(2.0f); |
| 177 | 231 |
| 178 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 232 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 179 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 233 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
| 180 snapshot.Size().ToString()); | 234 snapshot.Size().ToString()); |
| 181 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 235 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 182 } | 236 } |
| 183 | 237 |
| 184 TEST_F(SnapshotAuraTest, RotateAndUIScale) { | 238 TEST_P(SnapshotAuraTest, RotateAndUIScale) { |
| 185 const float kUIScale = 1.25f; | 239 const float kUIScale = 1.25f; |
| 186 test_screen()->SetUIScale(kUIScale); | 240 test_screen()->SetUIScale(kUIScale); |
| 187 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); | 241 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); |
| 188 | 242 |
| 189 gfx::Rect test_bounds(100, 100, 300, 200); | 243 gfx::Rect test_bounds(100, 100, 300, 200); |
| 190 SetupTestWindow(test_bounds); | 244 SetupTestWindow(test_bounds); |
| 191 WaitForDraw(); | 245 WaitForDraw(); |
| 192 | 246 |
| 193 // Snapshot always captures the physical pixels. | 247 // Snapshot always captures the physical pixels. |
| 194 gfx::SizeF snapshot_size(test_bounds.size()); | 248 gfx::SizeF snapshot_size(test_bounds.size()); |
| 195 snapshot_size.Scale(1.0f / kUIScale); | 249 snapshot_size.Scale(1.0f / kUIScale); |
| 196 | 250 |
| 197 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 251 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 198 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 252 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
| 199 snapshot.Size().ToString()); | 253 snapshot.Size().ToString()); |
| 200 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 254 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 201 } | 255 } |
| 202 | 256 |
| 203 TEST_F(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) { | 257 TEST_P(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) { |
| 204 test_screen()->SetDeviceScaleFactor(2.0f); | 258 test_screen()->SetDeviceScaleFactor(2.0f); |
| 205 const float kUIScale = 1.25f; | 259 const float kUIScale = 1.25f; |
| 206 test_screen()->SetUIScale(kUIScale); | 260 test_screen()->SetUIScale(kUIScale); |
| 207 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); | 261 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90); |
| 208 | 262 |
| 209 gfx::Rect test_bounds(20, 30, 150, 100); | 263 gfx::Rect test_bounds(20, 30, 150, 100); |
| 210 SetupTestWindow(test_bounds); | 264 SetupTestWindow(test_bounds); |
| 211 WaitForDraw(); | 265 WaitForDraw(); |
| 212 | 266 |
| 213 // Snapshot always captures the physical pixels. | 267 // Snapshot always captures the physical pixels. |
| 214 gfx::SizeF snapshot_size(test_bounds.size()); | 268 gfx::SizeF snapshot_size(test_bounds.size()); |
| 215 snapshot_size.Scale(2.0f / kUIScale); | 269 snapshot_size.Scale(2.0f / kUIScale); |
| 216 | 270 |
| 217 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 271 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
| 218 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 272 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
| 219 snapshot.Size().ToString()); | 273 snapshot.Size().ToString()); |
| 220 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 274 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
| 221 } | 275 } |
| 222 | 276 |
| 223 } // namespace ui | 277 } // namespace ui |
| OLD | NEW |