OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "ash/common/shelf/shelf_background_animator.h" | |
6 | |
7 #include "ash/common/material_design/test/material_design_controller_test_api.h" | |
8 #include "ash/common/shelf/shelf_background_animator_observer.h" | |
9 #include "ash/common/shelf/shelf_constants.h" | |
10 #include "base/bind.h" | |
11 #include "base/macros.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace ash { | |
15 namespace test { | |
16 | |
17 using test::MaterialDesignControllerTestAPI; | |
18 | |
19 namespace { | |
20 | |
21 const int kMaxAlpha = 255; | |
22 | |
23 // Observer that caches alpha values for the last observation. | |
24 class TestShelfBackgroundObserver : public ShelfBackgroundAnimatorObserver { | |
25 public: | |
26 TestShelfBackgroundObserver(); | |
27 ~TestShelfBackgroundObserver() override; | |
28 | |
29 int opaque_background_alpha() const { return opaque_background_alpha_; } | |
30 | |
31 int item_background_alpha() const { return item_background_alpha_; } | |
32 | |
33 int asset_background_alpha() const { return asset_background_alpha_; } | |
34 | |
35 // ShelfBackgroundObserver: | |
36 void UpdateShelfOpaqueBackground(int alpha) override; | |
37 void UpdateShelfAssetBackground(int alpha) override; | |
38 void UpdateShelfItemBackground(int alpha) override; | |
39 | |
40 private: | |
41 int opaque_background_alpha_; | |
42 | |
43 int item_background_alpha_; | |
44 | |
James Cook
2016/06/14 17:50:02
nit: no blank lines needed
bruthig
2016/07/26 19:50:02
Done.
| |
45 int asset_background_alpha_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(TestShelfBackgroundObserver); | |
48 }; | |
49 | |
50 TestShelfBackgroundObserver::TestShelfBackgroundObserver() | |
51 : opaque_background_alpha_(0), | |
James Cook
2016/06/14 17:50:01
optional: initialize in class declaration
bruthig
2016/07/26 19:50:02
Done.
| |
52 item_background_alpha_(0), | |
53 asset_background_alpha_(0) {} | |
54 | |
55 TestShelfBackgroundObserver::~TestShelfBackgroundObserver() {} | |
56 | |
57 void TestShelfBackgroundObserver::UpdateShelfOpaqueBackground(int alpha) { | |
58 opaque_background_alpha_ = alpha; | |
59 } | |
60 | |
61 void TestShelfBackgroundObserver::UpdateShelfAssetBackground(int alpha) { | |
62 asset_background_alpha_ = alpha; | |
63 } | |
64 | |
65 void TestShelfBackgroundObserver::UpdateShelfItemBackground(int alpha) { | |
66 item_background_alpha_ = alpha; | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 // Provides internal access to a ShelfBackgroundAnimator instance. | |
72 class ShelfBackgroundAnimatorTestApi { | |
73 public: | |
74 explicit ShelfBackgroundAnimatorTestApi(ShelfBackgroundAnimator* animator); | |
75 | |
76 virtual ~ShelfBackgroundAnimatorTestApi(); | |
James Cook
2016/06/14 17:50:01
doesn't need virtual
bruthig
2016/07/26 19:50:02
Done.
| |
77 | |
78 ShelfBackgroundType previous_background_type() const { | |
79 return animator_->previous_background_type_; | |
80 } | |
81 | |
82 BackgroundAnimator* opaque_background_animator() const { | |
83 return animator_->opaque_background_animator_.get(); | |
84 } | |
85 | |
86 BackgroundAnimator* asset_background_animator() const { | |
87 return animator_->asset_background_animator_.get(); | |
88 } | |
89 | |
90 BackgroundAnimator* item_background_animator() const { | |
91 return animator_->item_background_animator_.get(); | |
92 } | |
93 | |
94 private: | |
95 // The instance to provide internal access to. | |
96 ShelfBackgroundAnimator* animator_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTestApi); | |
99 }; | |
100 | |
101 ShelfBackgroundAnimatorTestApi::ShelfBackgroundAnimatorTestApi( | |
102 ShelfBackgroundAnimator* animator) | |
103 : animator_(animator) {} | |
104 | |
105 ShelfBackgroundAnimatorTestApi::~ShelfBackgroundAnimatorTestApi() {} | |
James Cook
2016/06/14 17:50:01
optional: feel free to inline little methods like
bruthig
2016/07/26 19:50:02
Done.
| |
106 | |
107 class ShelfBackgroundAnimatorTestBase : public testing::Test { | |
108 public: | |
109 ShelfBackgroundAnimatorTestBase(); | |
110 ~ShelfBackgroundAnimatorTestBase() override; | |
111 | |
112 virtual MaterialDesignController::Mode GetMaterialDesignMode() = 0; | |
113 | |
114 int expected_translucent_alpha() const { return expected_translucent_alpha_; } | |
115 | |
116 // testing::Test: | |
117 void SetUp() override; | |
118 | |
119 protected: | |
120 // Convenience wrapper for |animator_|'s PaintBackground() that always uses | |
121 // BACKGROUND_CHANGE_IMMEDIATE. | |
122 void PaintBackground(ShelfBackgroundType background_type); | |
123 | |
124 TestShelfBackgroundObserver observer_; | |
125 | |
126 // Test target. | |
127 std::unique_ptr<ShelfBackgroundAnimator> animator_; | |
128 | |
129 // Provides internal access to |animator_|. | |
130 std::unique_ptr<ShelfBackgroundAnimatorTestApi> test_api_; | |
131 | |
132 private: | |
133 std::unique_ptr<MaterialDesignControllerTestAPI> material_mode_test_api_; | |
134 | |
135 // The expected alpha value for translucent items. Cannot be a constant | |
136 // because it is different for material design and non-material. | |
137 int expected_translucent_alpha_; | |
James Cook
2016/06/14 17:50:01
other reviewers may disagree, but I'm generally OK
bruthig
2016/07/26 19:50:02
Normally I would use protected members as well but
| |
138 | |
139 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorTestBase); | |
140 }; | |
141 | |
142 ShelfBackgroundAnimatorTestBase::ShelfBackgroundAnimatorTestBase() | |
143 : expected_translucent_alpha_(0) {} | |
James Cook
2016/06/14 17:50:01
optional: init in class decl above
bruthig
2016/07/26 19:50:02
Done.
| |
144 | |
145 ShelfBackgroundAnimatorTestBase::~ShelfBackgroundAnimatorTestBase() {} | |
146 | |
147 void ShelfBackgroundAnimatorTestBase::SetUp() { | |
148 material_mode_test_api_.reset( | |
149 new MaterialDesignControllerTestAPI(GetMaterialDesignMode())); | |
150 animator_.reset(new ShelfBackgroundAnimator()); | |
151 animator_->AddObserver(&observer_); | |
152 | |
153 test_api_.reset(new ShelfBackgroundAnimatorTestApi(animator_.get())); | |
154 | |
155 // Initialized after the Material Design mode because GetShelfConstant() | |
156 // depends on the mode. | |
157 expected_translucent_alpha_ = GetShelfConstant(SHELF_BACKGROUND_ALPHA); | |
158 } | |
159 | |
160 void ShelfBackgroundAnimatorTestBase::PaintBackground( | |
161 ShelfBackgroundType background_type) { | |
162 animator_->PaintBackground(background_type, BACKGROUND_CHANGE_IMMEDIATE); | |
163 } | |
164 | |
165 class ShelfBackgroundAnimatorNonMDTest | |
166 : public ShelfBackgroundAnimatorTestBase { | |
167 public: | |
168 ShelfBackgroundAnimatorNonMDTest() {} | |
169 ~ShelfBackgroundAnimatorNonMDTest() override {} | |
170 | |
171 MaterialDesignController::Mode GetMaterialDesignMode() override { | |
172 return MaterialDesignController::NON_MATERIAL; | |
173 } | |
174 | |
175 private: | |
176 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorNonMDTest); | |
177 }; | |
178 | |
179 // Verify the alpha values for the SHELF_BACKGROUND_DEFAULT state. | |
180 TEST_F(ShelfBackgroundAnimatorNonMDTest, DefaultBackground) { | |
181 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
182 | |
183 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
184 EXPECT_EQ(0, observer_.opaque_background_alpha()); | |
185 EXPECT_EQ(0, observer_.asset_background_alpha()); | |
186 EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha()); | |
187 } | |
188 | |
189 // Verify the alpha values for the SHELF_BACKGROUND_OVERLAP state. | |
190 TEST_F(ShelfBackgroundAnimatorNonMDTest, OverlapBackground) { | |
191 PaintBackground(SHELF_BACKGROUND_OVERLAP); | |
192 | |
193 EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, animator_->target_background_type()); | |
194 EXPECT_EQ(0, observer_.opaque_background_alpha()); | |
195 EXPECT_EQ(expected_translucent_alpha(), observer_.asset_background_alpha()); | |
196 EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha()); | |
197 } | |
198 | |
199 // Verify the alpha values for the SHELF_BACKGROUND_MAXIMIZED state. | |
200 TEST_F(ShelfBackgroundAnimatorNonMDTest, MaximizedBackground) { | |
201 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
202 | |
203 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); | |
204 EXPECT_EQ(kMaxAlpha, observer_.opaque_background_alpha()); | |
205 EXPECT_EQ(expected_translucent_alpha(), observer_.asset_background_alpha()); | |
206 EXPECT_EQ(kMaxAlpha, observer_.item_background_alpha()); | |
207 } | |
208 | |
209 class ShelfBackgroundAnimatorMDTest : public ShelfBackgroundAnimatorTestBase { | |
210 public: | |
211 ShelfBackgroundAnimatorMDTest() {} | |
212 ~ShelfBackgroundAnimatorMDTest() override {} | |
213 | |
214 MaterialDesignController::Mode GetMaterialDesignMode() override { | |
215 return MaterialDesignController::MATERIAL_EXPERIMENTAL; | |
James Cook
2016/06/14 17:50:02
So the changes in this CL are tied to MATERIAL_EXP
bruthig
2016/07/26 19:50:02
Correct, but more accurately this CL is tied to th
| |
216 } | |
217 | |
218 private: | |
219 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimatorMDTest); | |
220 }; | |
221 | |
222 // Verify the |previous_background_type_| and |target_background_type_| values | |
223 // when animating to the same target type multiple times. | |
224 TEST_F(ShelfBackgroundAnimatorMDTest, | |
225 BackgroundTypesWhenAnimatingToSameTarget) { | |
226 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
227 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); | |
228 | |
229 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
230 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
231 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, test_api_->previous_background_type()); | |
232 | |
233 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
234 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
235 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, test_api_->previous_background_type()); | |
236 } | |
237 | |
238 // Verify the alpha values for the SHELF_BACKGROUND_DEFAULT state. | |
239 TEST_F(ShelfBackgroundAnimatorMDTest, DefaultBackground) { | |
240 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
241 | |
242 EXPECT_EQ(SHELF_BACKGROUND_DEFAULT, animator_->target_background_type()); | |
243 EXPECT_EQ(0, observer_.opaque_background_alpha()); | |
244 EXPECT_EQ(0, observer_.asset_background_alpha()); | |
245 EXPECT_EQ(expected_translucent_alpha(), observer_.item_background_alpha()); | |
246 } | |
247 | |
248 // Verify the alpha values for the SHELF_BACKGROUND_OVERLAP state. | |
249 TEST_F(ShelfBackgroundAnimatorMDTest, OverlapBackground) { | |
250 PaintBackground(SHELF_BACKGROUND_OVERLAP); | |
251 | |
252 EXPECT_EQ(SHELF_BACKGROUND_OVERLAP, animator_->target_background_type()); | |
253 EXPECT_EQ(expected_translucent_alpha(), observer_.opaque_background_alpha()); | |
254 EXPECT_EQ(0, observer_.asset_background_alpha()); | |
255 EXPECT_EQ(0, observer_.item_background_alpha()); | |
256 } | |
257 | |
258 // Verify the alpha values for the SHELF_BACKGROUND_MAXIMIZED state. | |
259 TEST_F(ShelfBackgroundAnimatorMDTest, MaximizedBackground) { | |
260 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
261 | |
262 EXPECT_EQ(SHELF_BACKGROUND_MAXIMIZED, animator_->target_background_type()); | |
263 EXPECT_EQ(kMaxAlpha, observer_.opaque_background_alpha()); | |
264 EXPECT_EQ(0, observer_.asset_background_alpha()); | |
265 EXPECT_EQ(0, observer_.item_background_alpha()); | |
266 } | |
267 | |
268 // Verify that existing animators are used when animating to the previous state. | |
269 TEST_F(ShelfBackgroundAnimatorMDTest, VerifyExistingAnimatorsAreReused) { | |
270 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
271 PaintBackground(SHELF_BACKGROUND_MAXIMIZED); | |
272 const BackgroundAnimator* opaque_animator = | |
273 test_api_->opaque_background_animator(); | |
274 const BackgroundAnimator* asset_animator = | |
275 test_api_->asset_background_animator(); | |
276 const BackgroundAnimator* item_animator = | |
277 test_api_->item_background_animator(); | |
278 | |
279 PaintBackground(SHELF_BACKGROUND_DEFAULT); | |
280 | |
281 EXPECT_EQ(opaque_animator, test_api_->opaque_background_animator()); | |
282 EXPECT_EQ(asset_animator, test_api_->asset_background_animator()); | |
283 EXPECT_EQ(item_animator, test_api_->item_background_animator()); | |
284 } | |
285 | |
286 } // namespace test | |
287 } // namespace ash | |
James Cook
2016/06/14 17:50:01
Nice test suite.
bruthig
2016/07/26 19:50:02
:D
| |
OLD | NEW |