- add sources.
[platform/framework/web/crosswalk.git] / src / ui / snapshot / snapshot_aura_unittest.cc
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.
4
5 #include "ui/snapshot/snapshot.h"
6
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"
22
23 namespace ui {
24 namespace {
25 const SkColor kPaintColor = SK_ColorRED;
26
27 // Paint simple rectangle on the specified aura window.
28 class TestPaintingWindowDelegate : public aura::test::TestWindowDelegate {
29  public:
30   explicit TestPaintingWindowDelegate(const gfx::Size& window_size)
31       : window_size_(window_size) {
32   }
33
34   virtual ~TestPaintingWindowDelegate() {
35   }
36
37   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
38     canvas->FillRect(gfx::Rect(window_size_), kPaintColor);
39   }
40
41  private:
42   gfx::Size window_size_;
43
44   DISALLOW_COPY_AND_ASSIGN(TestPaintingWindowDelegate);
45 };
46
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());
51   size_t result = 0;
52   for (int i = 0; i < bitmap->width() * bitmap->height(); ++i) {
53     if (static_cast<SkColor>(bitmap_data[i]) != kPaintColor)
54       ++result;
55   }
56   return result;
57 }
58
59 }  // namespace
60
61 class SnapshotAuraTest : public testing::Test {
62  public:
63   SnapshotAuraTest() {}
64   virtual ~SnapshotAuraTest() {}
65
66   virtual void SetUp() OVERRIDE {
67     testing::Test::SetUp();
68     helper_.reset(
69         new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
70     helper_->SetUp();
71   }
72
73   virtual void TearDown() OVERRIDE {
74     test_window_.reset();
75     delegate_.reset();
76     helper_->RunAllPendingInMessageLoop();
77     helper_->TearDown();
78     testing::Test::TearDown();
79   }
80
81  protected:
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(); }
85
86   void WaitForDraw() {
87     root_window()->compositor()->ScheduleDraw();
88     ui::DrawWaiterForTest::Wait(root_window()->compositor());
89   }
90
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()));
95   }
96
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());
103   }
104
105  private:
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_;
110
111   DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest);
112 };
113
114 TEST_F(SnapshotAuraTest, FullScreenWindow) {
115   SetupTestWindow(root_window()->bounds());
116   WaitForDraw();
117
118   gfx::Image snapshot = GrabSnapshotForTestWindow();
119   EXPECT_EQ(test_window()->bounds().size().ToString(),
120             snapshot.Size().ToString());
121   EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
122 }
123
124 TEST_F(SnapshotAuraTest, PartialBounds) {
125   gfx::Rect test_bounds(100, 100, 300, 200);
126   SetupTestWindow(test_bounds);
127   WaitForDraw();
128
129   gfx::Image snapshot = GrabSnapshotForTestWindow();
130   EXPECT_EQ(test_bounds.size().ToString(),
131             snapshot.Size().ToString());
132   EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
133 }
134
135 TEST_F(SnapshotAuraTest, Rotated) {
136   test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90);
137
138   gfx::Rect test_bounds(100, 100, 300, 200);
139   SetupTestWindow(test_bounds);
140   WaitForDraw();
141
142   gfx::Image snapshot = GrabSnapshotForTestWindow();
143   EXPECT_EQ(test_bounds.size().ToString(),
144             snapshot.Size().ToString());
145   EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
146 }
147
148 TEST_F(SnapshotAuraTest, UIScale) {
149   const float kUIScale = 1.25f;
150   test_screen()->SetUIScale(kUIScale);
151
152   gfx::Rect test_bounds(100, 100, 300, 200);
153   SetupTestWindow(test_bounds);
154   WaitForDraw();
155
156   // Snapshot always captures the physical pixels.
157   gfx::SizeF snapshot_size(test_bounds.size());
158   snapshot_size.Scale(1.0f / kUIScale);
159
160   gfx::Image snapshot = GrabSnapshotForTestWindow();
161   EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
162             snapshot.Size().ToString());
163   EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
164 }
165
166 TEST_F(SnapshotAuraTest, DeviceScaleFactor) {
167   test_screen()->SetDeviceScaleFactor(2.0f);
168
169   gfx::Rect test_bounds(100, 100, 150, 100);
170   SetupTestWindow(test_bounds);
171   WaitForDraw();
172
173   // Snapshot always captures the physical pixels.
174   gfx::SizeF snapshot_size(test_bounds.size());
175   snapshot_size.Scale(2.0f);
176
177   gfx::Image snapshot = GrabSnapshotForTestWindow();
178   EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
179             snapshot.Size().ToString());
180   EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
181 }
182
183 TEST_F(SnapshotAuraTest, RotateAndUIScale) {
184   const float kUIScale = 1.25f;
185   test_screen()->SetUIScale(kUIScale);
186   test_screen()->SetDisplayRotation(gfx::Display::ROTATE_90);
187
188   gfx::Rect test_bounds(100, 100, 300, 200);
189   SetupTestWindow(test_bounds);
190   WaitForDraw();
191
192   // Snapshot always captures the physical pixels.
193   gfx::SizeF snapshot_size(test_bounds.size());
194   snapshot_size.Scale(1.0f / kUIScale);
195
196   gfx::Image snapshot = GrabSnapshotForTestWindow();
197   EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
198             snapshot.Size().ToString());
199   EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
200 }
201
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);
207
208   gfx::Rect test_bounds(20, 30, 150, 100);
209   SetupTestWindow(test_bounds);
210   WaitForDraw();
211
212   // Snapshot always captures the physical pixels.
213   gfx::SizeF snapshot_size(test_bounds.size());
214   snapshot_size.Scale(2.0f / kUIScale);
215
216   gfx::Image snapshot = GrabSnapshotForTestWindow();
217   EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
218             snapshot.Size().ToString());
219   EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
220 }
221
222 }  // namespace ui