forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot_aura_unittest.cc
361 lines (303 loc) · 11.2 KB
/
snapshot_aura_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/snapshot/snapshot.h"
#include <stddef.h>
#include <stdint.h>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "base/test/test_simple_task_runner.h"
#include "base/test/test_timeouts.h"
#include "base/win/windows_version.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkPixelRef.h"
#include "ui/aura/test/aura_test_helper.h"
#include "ui/aura/test/test_screen.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/compositor/test/draw_waiter_for_test.h"
#include "ui/compositor/test/test_context_factories.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/image/image.h"
#include "ui/gl/gl_implementation.h"
namespace ui {
namespace {
SkColor GetExpectedColorForPoint(int x, int y) {
return SkColorSetRGB(std::min(x, 255), std::min(y, 255), 0);
}
// Paint simple rectangle on the specified aura window.
class TestPaintingWindowDelegate : public aura::test::TestWindowDelegate {
public:
explicit TestPaintingWindowDelegate(const gfx::Size& window_size)
: window_size_(window_size) {
}
TestPaintingWindowDelegate(const TestPaintingWindowDelegate&) = delete;
TestPaintingWindowDelegate& operator=(const TestPaintingWindowDelegate&) =
delete;
~TestPaintingWindowDelegate() override {}
void OnPaint(const ui::PaintContext& context) override {
ui::PaintRecorder recorder(context, window_size_);
for (int y = 0; y < window_size_.height(); ++y) {
for (int x = 0; x < window_size_.width(); ++x) {
recorder.canvas()->FillRect(gfx::Rect(x, y, 1, 1),
GetExpectedColorForPoint(x, y));
}
}
}
private:
gfx::Size window_size_;
};
size_t GetFailedPixelsCountWithScaleFactor(const gfx::Image& image,
int scale_factor) {
const SkBitmap* bitmap = image.ToSkBitmap();
uint32_t* bitmap_data =
reinterpret_cast<uint32_t*>(bitmap->pixelRef()->pixels());
size_t result = 0;
for (int y = 0; y < bitmap->height(); y += scale_factor) {
for (int x = 0; x < bitmap->width(); x += scale_factor) {
if (static_cast<SkColor>(bitmap_data[x + y * bitmap->width()]) !=
GetExpectedColorForPoint(x / scale_factor, y / scale_factor)) {
++result;
}
}
}
return result;
}
size_t GetFailedPixelsCount(const gfx::Image& image) {
return GetFailedPixelsCountWithScaleFactor(image, 1);
}
} // namespace
class SnapshotAuraTest : public testing::Test {
public:
SnapshotAuraTest() = default;
SnapshotAuraTest(const SnapshotAuraTest&) = delete;
SnapshotAuraTest& operator=(const SnapshotAuraTest&) = delete;
~SnapshotAuraTest() override = default;
void SetUp() override {
testing::Test::SetUp();
task_environment_ = std::make_unique<base::test::TaskEnvironment>(
base::test::TaskEnvironment::MainThreadType::UI);
// The ContextFactory must exist before any Compositors are created.
// Snapshot test tests real drawing and readback, so needs pixel output.
const bool enable_pixel_output = true;
context_factories_ =
std::make_unique<ui::TestContextFactories>(enable_pixel_output);
helper_ = std::make_unique<aura::test::AuraTestHelper>(
context_factories_->GetContextFactory());
helper_->SetUp();
}
void TearDown() override {
test_window_.reset();
delegate_.reset();
helper_->RunAllPendingInMessageLoop();
helper_.reset();
context_factories_.reset();
task_environment_.reset();
testing::Test::TearDown();
}
protected:
aura::Window* test_window() { return test_window_.get(); }
aura::Window* root_window() { return helper_->GetContext(); }
aura::TestScreen* test_screen() { return helper_->GetTestScreen(); }
void WaitForDraw() {
helper_->GetHost()->compositor()->ScheduleDraw();
ui::DrawWaiterForTest::WaitForCompositingEnded(
helper_->GetHost()->compositor());
}
void SetupTestWindow(const gfx::Rect& window_bounds) {
delegate_ =
std::make_unique<TestPaintingWindowDelegate>(window_bounds.size());
test_window_.reset(aura::test::CreateTestWindowWithDelegate(
delegate_.get(), 0, window_bounds, root_window()));
}
gfx::Image GrabSnapshotForTestWindow() {
gfx::Rect source_rect(test_window_->bounds().size());
aura::Window::ConvertRectToTarget(
test_window(), root_window(), &source_rect);
scoped_refptr<SnapshotHolder> holder(new SnapshotHolder);
ui::GrabWindowSnapshotAsync(
root_window(), source_rect,
base::BindOnce(&SnapshotHolder::SnapshotCallback, holder));
holder->WaitForSnapshot();
DCHECK(holder->completed());
return holder->image();
}
private:
class SnapshotHolder : public base::RefCountedThreadSafe<SnapshotHolder> {
public:
SnapshotHolder() : completed_(false) {}
void SnapshotCallback(gfx::Image image) {
DCHECK(!completed_);
image_ = image;
completed_ = true;
run_loop_.Quit();
}
void WaitForSnapshot() { run_loop_.Run(); }
bool completed() const { return completed_; }
const gfx::Image& image() const { return image_; }
private:
friend class base::RefCountedThreadSafe<SnapshotHolder>;
virtual ~SnapshotHolder() {}
base::RunLoop run_loop_;
gfx::Image image_;
bool completed_;
};
std::unique_ptr<base::test::TaskEnvironment> task_environment_;
std::unique_ptr<ui::TestContextFactories> context_factories_;
std::unique_ptr<aura::test::AuraTestHelper> helper_;
std::unique_ptr<aura::Window> test_window_;
std::unique_ptr<TestPaintingWindowDelegate> delegate_;
std::vector<unsigned char> png_representation_;
};
#if BUILDFLAG(IS_WIN) && !defined(NDEBUG)
// https://crbug.com/852512
#define MAYBE_FullScreenWindow DISABLED_FullScreenWindow
#elif BUILDFLAG(IS_LINUX)
// https://crbug.com/1143031
#define MAYBE_FullScreenWindow DISABLED_FullScreenWindow
#else
#define MAYBE_FullScreenWindow FullScreenWindow
#endif
TEST_F(SnapshotAuraTest, MAYBE_FullScreenWindow) {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
// TODO(https://crbug.com/1143031): Fix this test to run in < action_timeout()
// on the Linux Debug & TSAN bots.
const base::test::ScopedRunLoopTimeout increased_run_timeout(
FROM_HERE, TestTimeouts::action_max_timeout());
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) ||
// BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_WIN)
// TODO(https://crbug.com/850556): Make work on Windows.
if (::testing::internal::AlwaysTrue()) {
GTEST_SKIP();
}
#endif
SetupTestWindow(root_window()->bounds());
WaitForDraw();
gfx::Image snapshot = GrabSnapshotForTestWindow();
EXPECT_EQ(test_window()->bounds().size().ToString(),
snapshot.Size().ToString());
EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
}
TEST_F(SnapshotAuraTest, PartialBounds) {
#if BUILDFLAG(IS_WIN)
// TODO(https://crbug.com/850556): Make work on Windows.
if (::testing::internal::AlwaysTrue()) {
GTEST_SKIP();
}
#endif
gfx::Rect test_bounds(100, 100, 300, 200);
SetupTestWindow(test_bounds);
WaitForDraw();
gfx::Image snapshot = GrabSnapshotForTestWindow();
EXPECT_EQ(test_bounds.size().ToString(), snapshot.Size().ToString());
EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
}
TEST_F(SnapshotAuraTest, Rotated) {
#if BUILDFLAG(IS_WIN)
// TODO(https://crbug.com/850556): Make work on Windows.
if (::testing::internal::AlwaysTrue()) {
GTEST_SKIP();
}
#endif
test_screen()->SetDisplayRotation(display::Display::ROTATE_90);
gfx::Rect test_bounds(100, 100, 300, 200);
SetupTestWindow(test_bounds);
WaitForDraw();
gfx::Image snapshot = GrabSnapshotForTestWindow();
EXPECT_EQ(test_bounds.size().ToString(), snapshot.Size().ToString());
EXPECT_EQ(0u, GetFailedPixelsCount(snapshot));
}
TEST_F(SnapshotAuraTest, UIScale) {
#if BUILDFLAG(IS_WIN)
// TODO(https://crbug.com/850556): Make work on Windows.
if (::testing::internal::AlwaysTrue()) {
GTEST_SKIP();
}
#endif
const float kUIScale = 0.5f;
test_screen()->SetUIScale(kUIScale);
gfx::Rect test_bounds(100, 100, 300, 200);
SetupTestWindow(test_bounds);
WaitForDraw();
// Snapshot always captures the physical pixels.
gfx::SizeF snapshot_size(test_bounds.size());
snapshot_size.InvScale(kUIScale);
gfx::Image snapshot = GrabSnapshotForTestWindow();
EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
snapshot.Size().ToString());
EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 1 / kUIScale));
}
TEST_F(SnapshotAuraTest, DeviceScaleFactor) {
#if BUILDFLAG(IS_WIN)
// TODO(https://crbug.com/850556): Make work on Windows.
if (::testing::internal::AlwaysTrue()) {
GTEST_SKIP();
}
#endif
test_screen()->SetDeviceScaleFactor(2.0f);
gfx::Rect test_bounds(100, 100, 150, 100);
SetupTestWindow(test_bounds);
WaitForDraw();
// Snapshot always captures the physical pixels.
gfx::SizeF snapshot_size(test_bounds.size());
snapshot_size.Scale(2.0f);
gfx::Image snapshot = GrabSnapshotForTestWindow();
EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
snapshot.Size().ToString());
EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 2));
}
TEST_F(SnapshotAuraTest, RotateAndUIScale) {
#if BUILDFLAG(IS_WIN)
// TODO(https://crbug.com/850556): Make work on Windows.
if (::testing::internal::AlwaysTrue()) {
GTEST_SKIP();
}
#endif
const float kUIScale = 0.5f;
test_screen()->SetUIScale(kUIScale);
test_screen()->SetDisplayRotation(display::Display::ROTATE_90);
gfx::Rect test_bounds(100, 100, 200, 300);
SetupTestWindow(test_bounds);
WaitForDraw();
// Snapshot always captures the physical pixels.
gfx::SizeF snapshot_size(test_bounds.size());
snapshot_size.InvScale(kUIScale);
gfx::Image snapshot = GrabSnapshotForTestWindow();
EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
snapshot.Size().ToString());
EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 1 / kUIScale));
}
TEST_F(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) {
#if BUILDFLAG(IS_WIN)
// TODO(https://crbug.com/850556): Make work on Windows.
if (::testing::internal::AlwaysTrue()) {
GTEST_SKIP();
}
#endif
test_screen()->SetDeviceScaleFactor(2.0f);
const float kUIScale = 0.5f;
test_screen()->SetUIScale(kUIScale);
test_screen()->SetDisplayRotation(display::Display::ROTATE_90);
gfx::Rect test_bounds(20, 30, 100, 150);
SetupTestWindow(test_bounds);
WaitForDraw();
// Snapshot always captures the physical pixels.
gfx::SizeF snapshot_size(test_bounds.size());
snapshot_size.Scale(2.0f / kUIScale);
gfx::Image snapshot = GrabSnapshotForTestWindow();
EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(),
snapshot.Size().ToString());
EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 2 / kUIScale));
}
} // namespace ui