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
3 // found in the LICENSE file.
5 #include "ui/snapshot/snapshot.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/aura/root_window.h"
9 #include "ui/aura/test/aura_test_helper.h"
10 #include "ui/aura/test/test_screen.h"
11 #include "ui/aura/test/test_window_delegate.h"
12 #include "ui/aura/test/test_windows.h"
13 #include "ui/aura/window.h"
14 #include "ui/compositor/layer.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/gfx_paths.h"
17 #include "ui/gfx/image/image.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/gfx/size_conversions.h"
20 #include "ui/gfx/transform.h"
21 #include "ui/gl/gl_implementation.h"
25 const SkColor kPaintColor = SK_ColorRED;
27 // Paint simple rectangle on the specified aura window.
28 class TestPaintingWindowDelegate : public aura::test::TestWindowDelegate {
30 explicit TestPaintingWindowDelegate(const gfx::Size& window_size)
31 : window_size_(window_size) {
34 virtual ~TestPaintingWindowDelegate() {
37 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
38 canvas->FillRect(gfx::Rect(window_size_), kPaintColor);
42 gfx::Size window_size_;
44 DISALLOW_COPY_AND_ASSIGN(TestPaintingWindowDelegate);
47 size_t GetFailedPixelsCount(const gfx::Image& image) {
48 const SkBitmap* bitmap = image.ToSkBitmap();
49 uint32* bitmap_data = reinterpret_cast<uint32*>(
50 bitmap->pixelRef()->pixels());
52 for (int i = 0; i < bitmap->width() * bitmap->height(); ++i) {
53 if (static_cast<SkColor>(bitmap_data[i]) != kPaintColor)
61 class SnapshotAuraTest : public testing::Test {
64 virtual ~SnapshotAuraTest() {}
66 virtual void SetUp() OVERRIDE {
67 testing::Test::SetUp();
69 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
73 virtual void TearDown() OVERRIDE {
76 helper_->RunAllPendingInMessageLoop();
78 testing::Test::TearDown();
82 aura::Window* test_window() { return test_window_.get(); }
83 aura::RootWindow* root_window() { return helper_->root_window(); }
84 aura::TestScreen* test_screen() { return helper_->test_screen(); }
87 root_window()->compositor()->ScheduleDraw();
88 ui::DrawWaiterForTest::Wait(root_window()->compositor());
91 void SetupTestWindow(const gfx::Rect& window_bounds) {
92 delegate_.reset(new TestPaintingWindowDelegate(window_bounds.size()));
93 test_window_.reset(aura::test::CreateTestWindowWithDelegate(
94 delegate_.get(), 0, window_bounds, root_window()));
97 gfx::Image GrabSnapshotForTestWindow() {
98 std::vector<unsigned char> png_representation;
99 gfx::Rect local_bounds(test_window_->bounds().size());
100 ui::GrabWindowSnapshot(test_window(), &png_representation, local_bounds);
101 return gfx::Image::CreateFrom1xPNGBytes(
102 &(png_representation[0]), png_representation.size());
106 scoped_ptr<aura::test::AuraTestHelper> helper_;
107 scoped_ptr<aura::Window> test_window_;
108 scoped_ptr<TestPaintingWindowDelegate> delegate_;
109 std::vector<unsigned char> png_representation_;
111 DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest);
114 TEST_F(SnapshotAuraTest, FullScreenWindow) {
115 SetupTestWindow(root_window()->bounds());
118 gfx::Image snapshot = GrabSnapshotForTestWindow();
119 EXPECT_EQ(test_window()->bounds().size().ToString(),
120 snapshot.Size().ToString());
121 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
124 TEST_F(SnapshotAuraTest, PartialBounds) {
125 gfx::Rect test_bounds(100, 100, 300, 200);
126 SetupTestWindow(test_bounds);
129 gfx::Image snapshot = GrabSnapshotForTestWindow();
130 EXPECT_EQ(test_bounds.size().ToString(),
131 snapshot.Size().ToString());
132 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
135 TEST_F(SnapshotAuraTest, Rotated) {
136 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90);
138 gfx::Rect test_bounds(100, 100, 300, 200);
139 SetupTestWindow(test_bounds);
142 gfx::Image snapshot = GrabSnapshotForTestWindow();
143 EXPECT_EQ(test_bounds.size().ToString(),
144 snapshot.Size().ToString());
145 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
148 TEST_F(SnapshotAuraTest, UIScale) {
149 const float kUIScale = 1.25f;
150 test_screen()->SetUIScale(kUIScale);
152 gfx::Rect test_bounds(100, 100, 300, 200);
153 SetupTestWindow(test_bounds);
156 // Snapshot always captures the physical pixels.
157 gfx::SizeF snapshot_size(test_bounds.size());
158 snapshot_size.Scale(1.0f / kUIScale);
160 gfx::Image snapshot = GrabSnapshotForTestWindow();
161 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
162 snapshot.Size().ToString());
163 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
166 TEST_F(SnapshotAuraTest, DeviceScaleFactor) {
167 test_screen()->SetDeviceScaleFactor(2.0f);
169 gfx::Rect test_bounds(100, 100, 150, 100);
170 SetupTestWindow(test_bounds);
173 // Snapshot always captures the physical pixels.
174 gfx::SizeF snapshot_size(test_bounds.size());
175 snapshot_size.Scale(2.0f);
177 gfx::Image snapshot = GrabSnapshotForTestWindow();
178 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
179 snapshot.Size().ToString());
180 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
183 TEST_F(SnapshotAuraTest, RotateAndUIScale) {
184 const float kUIScale = 1.25f;
185 test_screen()->SetUIScale(kUIScale);
186 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90);
188 gfx::Rect test_bounds(100, 100, 300, 200);
189 SetupTestWindow(test_bounds);
192 // Snapshot always captures the physical pixels.
193 gfx::SizeF snapshot_size(test_bounds.size());
194 snapshot_size.Scale(1.0f / kUIScale);
196 gfx::Image snapshot = GrabSnapshotForTestWindow();
197 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
198 snapshot.Size().ToString());
199 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
202 TEST_F(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) {
203 test_screen()->SetDeviceScaleFactor(2.0f);
204 const float kUIScale = 1.25f;
205 test_screen()->SetUIScale(kUIScale);
206 test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90);
208 gfx::Rect test_bounds(20, 30, 150, 100);
209 SetupTestWindow(test_bounds);
212 // Snapshot always captures the physical pixels.
213 gfx::SizeF snapshot_size(test_bounds.size());
214 snapshot_size.Scale(2.0f / kUIScale);
216 gfx::Image snapshot = GrabSnapshotForTestWindow();
217 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
218 snapshot.Size().ToString());
219 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));