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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
ash / shelf / shelf_background_animator_unittest.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/shelf/shelf_background_animator.h"
#include <memory>
#include "ash/animation/animation_change_type.h"
#include "ash/public/cpp/shelf_config.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_background_animator_observer.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/test_mock_time_task_runner.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/animation/slide_animation.h"
namespace ash {
namespace {
// A valid color value that is distinct from any final animation state values.
// Used to check if color values are changed during animations.
const SkColor kDummyColor = SK_ColorBLUE;
// Observer that caches color values for the last observation. This observer
// will also call a set callback when ShelfBackgroundAnimator completes an
// animation.
class TestShelfBackgroundObserver : public ShelfBackgroundAnimatorObserver {
public:
TestShelfBackgroundObserver() = default;
TestShelfBackgroundObserver(const TestShelfBackgroundObserver&) = delete;
TestShelfBackgroundObserver& operator=(const TestShelfBackgroundObserver&) =
delete;
~TestShelfBackgroundObserver() override = default;
SkColor background_color() const { return background_color_; }
// Convenience function to get the alpha value from |background_color_|.
int GetBackgroundAlpha() const;
// Sets |animation_complete_callback_| to be called when animation ends.
void SetAnimationCompleteCallback(base::OnceClosure callback);
// ShelfBackgroundObserver:
void UpdateShelfBackground(SkColor color) override;
void OnShelfBackgroundAnimationEnded() override;
private:
int background_color_ = SK_ColorTRANSPARENT;
base::OnceClosure animation_complete_callback_;
};
int TestShelfBackgroundObserver::GetBackgroundAlpha() const {
return SkColorGetA(background_color_);
}
void TestShelfBackgroundObserver::UpdateShelfBackground(SkColor color) {
background_color_ = color;
}
void TestShelfBackgroundObserver::OnShelfBackgroundAnimationEnded() {
if (!animation_complete_callback_.is_null())
std::move(animation_complete_callback_).Run();
}
void TestShelfBackgroundObserver::SetAnimationCompleteCallback(
base::OnceClosure callback) {
animation_complete_callback_ = std::move(callback);
}
} // namespace
// Provides internal access to a ShelfBackgroundAnimator instance.
class ShelfBackgroundAnimatorTestApi {
public:
explicit ShelfBackgroundAnimatorTestApi(ShelfBackgroundAnimator* animator)
: animator_(animator) {}
ShelfBackgroundAnimatorTestApi(const ShelfBackgroundAnimatorTestApi&) =
delete;
ShelfBackgroundAnimatorTestApi& operator=(
const ShelfBackgroundAnimatorTestApi&) = delete;
~ShelfBackgroundAnimatorTestApi() = default;
ShelfBackgroundType previous_background_type() const {
return animator_->previous_background_type_;
}
gfx::SlideAnimation* animator() const { return animator_->animator_.get(); }
SkColor shelf_background_target_color() const {
return animator_->shelf_background_values_.target_color();
}
private:
// The instance to provide internal access to.
raw_ptr<ShelfBackgroundAnimator, DanglingUntriaged> animator_;
};
class ShelfBackgroundAnimatorTest : public AshTestBase {
public:
ShelfBackgroundAnimatorTest() = default;
ShelfBackgroundAnimatorTest(const ShelfBackgroundAnimatorTest&) = delete;
ShelfBackgroundAnimatorTest& operator=(const ShelfBackgroundAnimatorTest&) =
delete;
~ShelfBackgroundAnimatorTest() override = default;
// testing::Test:
void SetUp() override;
protected:
// Convenience wrapper for |animator_|'s PaintBackground().
void PaintBackground(
ShelfBackgroundType background_type,
AnimationChangeType change_type = AnimationChangeType::IMMEDIATE);
// Set all of the color values for the |observer_|.
void SetColorValuesOnObserver(SkColor color);
// Waits for animation to complete.
void WaitForAnimationCompletion();
TestShelfBackgroundObserver observer_;
// Test target.
raw_ptr<ShelfBackgroundAnimator, DanglingUntriaged> animator_;
// Provides internal access to |animator_|.
std::unique_ptr<ShelfBackgroundAnimatorTestApi> test_api_;
};
void ShelfBackgroundAnimatorTest::SetUp() {
AshTestBase::SetUp();
animator_ =
GetPrimaryShelf()->shelf_widget()->background_animator_for_testing();
animator_->AddObserver(&observer_);
test_api_ = std::make_unique<ShelfBackgroundAnimatorTestApi>(animator_);
}
void ShelfBackgroundAnimatorTest::PaintBackground(
ShelfBackgroundType background_type,
AnimationChangeType change_type) {
animator_->PaintBackground(background_type, change_type);
}
void ShelfBackgroundAnimatorTest::SetColorValuesOnObserver(SkColor color) {
observer_.UpdateShelfBackground(color);
}
void ShelfBackgroundAnimatorTest::WaitForAnimationCompletion() {
base::RunLoop run_loop;
observer_.SetAnimationCompleteCallback(run_loop.QuitWhenIdleClosure());
run_loop.Run();
}
// Verify the |previous_background_type_| and |target_background_type_| values
// when animating to the same target type multiple times.
TEST_F(ShelfBackgroundAnimatorTest, BackgroundTypesWhenAnimatingToSameTarget) {
PaintBackground(ShelfBackgroundType::kMaximized);
EXPECT_EQ(ShelfBackgroundType::kMaximized,
animator_->target_background_type());
PaintBackground(ShelfBackgroundType::kDefaultBg);
EXPECT_EQ(ShelfBackgroundType::kDefaultBg,
animator_->target_background_type());
EXPECT_EQ(ShelfBackgroundType::kMaximized,
test_api_->previous_background_type());
PaintBackground(ShelfBackgroundType::kDefaultBg);
EXPECT_EQ(ShelfBackgroundType::kDefaultBg,
animator_->target_background_type());
EXPECT_EQ(ShelfBackgroundType::kMaximized,
test_api_->previous_background_type());
}
// Verify subsequent calls to PaintBackground() using the
// AnimationChangeType::ANIMATE change type are ignored.
TEST_F(ShelfBackgroundAnimatorTest,
MultipleAnimateCallsToSameTargetAreIgnored) {
PaintBackground(ShelfBackgroundType::kMaximized);
SetColorValuesOnObserver(kDummyColor);
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
animator_->PaintBackground(ShelfBackgroundType::kDefaultBg,
AnimationChangeType::ANIMATE);
WaitForAnimationCompletion();
EXPECT_NE(observer_.background_color(), kDummyColor);
SetColorValuesOnObserver(kDummyColor);
animator_->PaintBackground(ShelfBackgroundType::kDefaultBg,
AnimationChangeType::ANIMATE);
EXPECT_EQ(observer_.background_color(), kDummyColor);
}
// Verify observers are updated with the current values when they are added.
TEST_F(ShelfBackgroundAnimatorTest, ObserversUpdatedWhenAdded) {
animator_->RemoveObserver(&observer_);
SetColorValuesOnObserver(kDummyColor);
animator_->AddObserver(&observer_);
EXPECT_NE(observer_.background_color(), kDummyColor);
}
// Verify the alpha values for the ShelfBackgroundType::kDefaultBg state.
TEST_F(ShelfBackgroundAnimatorTest, DefaultBackground) {
PaintBackground(ShelfBackgroundType::kDefaultBg);
EXPECT_EQ(ShelfBackgroundType::kDefaultBg,
animator_->target_background_type());
EXPECT_EQ((int)SkColorGetA(ShelfConfig::Get()->GetDefaultShelfColor(
GetPrimaryShelf()->shelf_widget())),
observer_.GetBackgroundAlpha());
}
// Verify the alpha values for the ShelfBackgroundType::kMaximized state.
TEST_F(ShelfBackgroundAnimatorTest, MaximizedBackground) {
PaintBackground(ShelfBackgroundType::kMaximized);
EXPECT_EQ(ShelfBackgroundType::kMaximized,
animator_->target_background_type());
EXPECT_EQ((int)SkColorGetA(ShelfConfig::Get()->GetMaximizedShelfColor(
GetPrimaryShelf()->shelf_widget())),
observer_.GetBackgroundAlpha());
}
TEST_F(ShelfBackgroundAnimatorTest,
AnimatorIsDetroyedWhenCompletingSuccessfully) {
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
PaintBackground(ShelfBackgroundType::kMaximized,
AnimationChangeType::ANIMATE);
EXPECT_TRUE(test_api_->animator());
WaitForAnimationCompletion();
EXPECT_FALSE(test_api_->animator());
}
TEST_F(ShelfBackgroundAnimatorTest,
AnimatorDestroyedWhenChangingBackgroundImmediately) {
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
PaintBackground(ShelfBackgroundType::kMaximized,
AnimationChangeType::ANIMATE);
EXPECT_TRUE(test_api_->animator());
PaintBackground(ShelfBackgroundType::kDefaultBg,
AnimationChangeType::IMMEDIATE);
EXPECT_FALSE(test_api_->animator());
}
// Verify that existing animator is used when animating to the previous state.
TEST_F(ShelfBackgroundAnimatorTest,
ExistingAnimatorIsReusedWhenAnimatingToPreviousState) {
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
// First PaintBackground() must be immediate so that the
// ShelfBackgroundAnimator has its color set correctly.
PaintBackground(ShelfBackgroundType::kDefaultBg,
AnimationChangeType::IMMEDIATE);
PaintBackground(ShelfBackgroundType::kMaximized,
AnimationChangeType::ANIMATE);
const gfx::SlideAnimation* animator = test_api_->animator();
EXPECT_TRUE(animator);
PaintBackground(ShelfBackgroundType::kDefaultBg,
AnimationChangeType::ANIMATE);
EXPECT_EQ(animator, test_api_->animator());
}
// Verify that existing animator is not re-used when the target background isn't
// the same as the previous background.
TEST_F(ShelfBackgroundAnimatorTest,
ExistingAnimatorNotReusedWhenTargetBackgroundNotPreviousBackground) {
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
PaintBackground(ShelfBackgroundType::kHomeLauncher,
AnimationChangeType::ANIMATE);
const gfx::SlideAnimation* animator = test_api_->animator();
EXPECT_TRUE(animator);
EXPECT_NE(ShelfBackgroundType::kMaximized,
test_api_->previous_background_type());
PaintBackground(ShelfBackgroundType::kMaximized,
AnimationChangeType::ANIMATE);
EXPECT_NE(animator, test_api_->animator());
}
// Verify observers are always notified, even when alpha values don't change.
TEST_F(ShelfBackgroundAnimatorTest,
ObserversAreNotifiedWhenSnappingToSameTargetBackground) {
PaintBackground(ShelfBackgroundType::kDefaultBg);
SetColorValuesOnObserver(kDummyColor);
PaintBackground(ShelfBackgroundType::kDefaultBg);
EXPECT_NE(observer_.background_color(), kDummyColor);
}
class ShelfBackgroundTargetColorTest : public NoSessionAshTestBase {
public:
ShelfBackgroundTargetColorTest() = default;
ShelfBackgroundTargetColorTest(const ShelfBackgroundTargetColorTest&) =
delete;
ShelfBackgroundTargetColorTest& operator=(
const ShelfBackgroundTargetColorTest&) = delete;
~ShelfBackgroundTargetColorTest() override = default;
protected:
// Helper function to notify session state changes.
void NotifySessionStateChanged(session_manager::SessionState state) {
GetSessionControllerClient()->SetSessionState(state);
}
};
// Verify the target color of the shelf background is updated based on session
// state, starting from LOGIN_PRIMARY.
TEST_F(ShelfBackgroundTargetColorTest, ShelfBackgroundColorUpdatedFromLogin) {
ShelfBackgroundAnimatorTestApi test_api(
Shelf::ForWindow(Shell::Get()->GetPrimaryRootWindow())
->shelf_widget()
->background_animator_for_testing());
NotifySessionStateChanged(session_manager::SessionState::LOGIN_PRIMARY);
EXPECT_EQ(test_api.shelf_background_target_color(), SK_ColorTRANSPARENT);
SimulateUserLogin("user1@test.com");
NotifySessionStateChanged(session_manager::SessionState::ACTIVE);
EXPECT_EQ(test_api.shelf_background_target_color(),
ShelfConfig::Get()->GetDefaultShelfColor(
GetPrimaryShelf()->shelf_widget()));
}
// Verify the target color of the shelf background is updated based on session
// state, starting from OOBE.
TEST_F(ShelfBackgroundTargetColorTest, ShelfBackgroundColorUpdatedFromOOBE) {
ShelfBackgroundAnimatorTestApi test_api(
Shelf::ForWindow(Shell::Get()->GetPrimaryRootWindow())
->shelf_widget()
->background_animator_for_testing());
NotifySessionStateChanged(session_manager::SessionState::OOBE);
EXPECT_EQ(test_api.shelf_background_target_color(), SK_ColorTRANSPARENT);
SimulateUserLogin("user1@test.com");
NotifySessionStateChanged(
session_manager::SessionState::LOGGED_IN_NOT_ACTIVE);
EXPECT_EQ(test_api.shelf_background_target_color(), SK_ColorTRANSPARENT);
NotifySessionStateChanged(session_manager::SessionState::ACTIVE);
EXPECT_EQ(test_api.shelf_background_target_color(),
ShelfConfig::Get()->GetDefaultShelfColor(
GetPrimaryShelf()->shelf_widget()));
}
} // namespace ash