Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Side by Side Diff: ash/system/toast/toast_manager_unittest.cc

Issue 1978313002: Support cancelling toast (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use std::vector back instead of std::list Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/display/display_manager.h" 5 #include "ash/display/display_manager.h"
6 #include "ash/screen_util.h" 6 #include "ash/screen_util.h"
7 #include "ash/shelf/shelf.h" 7 #include "ash/shelf/shelf.h"
8 #include "ash/shelf/shelf_layout_manager.h" 8 #include "ash/shelf/shelf_layout_manager.h"
9 #include "ash/shell.h" 9 #include "ash/shell.h"
10 #include "ash/system/toast/toast_manager.h" 10 #include "ash/system/toast/toast_manager.h"
11 #include "ash/test/ash_test_base.h" 11 #include "ash/test/ash_test_base.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h"
13 #include "ui/compositor/scoped_animation_duration_scale_mode.h" 14 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
14 15
15 namespace ash { 16 namespace ash {
16 17
17 // Long duration so the timeout doesn't occur. 18 // Long duration so the timeout doesn't occur.
18 const int64_t kLongLongDuration = INT64_MAX; 19 const int64_t kLongLongDuration = INT64_MAX;
19 20
20 class DummyEvent : public ui::Event { 21 class DummyEvent : public ui::Event {
21 public: 22 public:
22 DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeDelta(), 0) {} 23 DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeDelta(), 0) {}
23 ~DummyEvent() override {} 24 ~DummyEvent() override {}
24 }; 25 };
25 26
26 class ToastManagerTest : public test::AshTestBase { 27 class ToastManagerTest : public test::AshTestBase {
27 public: 28 public:
28 ToastManagerTest() {} 29 ToastManagerTest() {}
29 ~ToastManagerTest() override {} 30 ~ToastManagerTest() override {}
30 31
31 private: 32 private:
32 void SetUp() override { 33 void SetUp() override {
33 test::AshTestBase::SetUp(); 34 test::AshTestBase::SetUp();
34 35
35 manager_ = Shell::GetInstance()->toast_manager(); 36 manager_ = Shell::GetInstance()->toast_manager();
36 37
37 manager_->ResetToastIdForTesting(); 38 manager_->ResetSerialForTesting();
38 EXPECT_EQ(0, GetToastId()); 39 EXPECT_EQ(0, GetToastSerial());
39 } 40 }
40 41
41 protected: 42 protected:
42 ToastManager* manager() { return manager_; } 43 ToastManager* manager() { return manager_; }
43 44
44 int GetToastId() { return manager_->toast_id_for_testing(); } 45 int GetToastSerial() { return manager_->serial_for_testing(); }
45 46
46 ToastOverlay* GetCurrentOverlay() { 47 ToastOverlay* GetCurrentOverlay() {
47 return manager_->GetCurrentOverlayForTesting(); 48 return manager_->GetCurrentOverlayForTesting();
48 } 49 }
49 50
50 views::Widget* GetCurrentWidget() { 51 views::Widget* GetCurrentWidget() {
51 ToastOverlay* overlay = GetCurrentOverlay(); 52 ToastOverlay* overlay = GetCurrentOverlay();
52 return overlay ? overlay->widget_for_testing() : nullptr; 53 return overlay ? overlay->widget_for_testing() : nullptr;
53 } 54 }
54 55
(...skipping 14 matching lines...) Expand all
69 70
70 void SetShelfState(ShelfVisibilityState state) { 71 void SetShelfState(ShelfVisibilityState state) {
71 Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetState(state); 72 Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetState(state);
72 } 73 }
73 74
74 void SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior) { 75 void SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior) {
75 Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetAutoHideBehavior( 76 Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetAutoHideBehavior(
76 behavior); 77 behavior);
77 } 78 }
78 79
80 std::string ShowToast(const std::string& text, uint64_t duration) {
81 std::string id = "TOAST_ID_" + base::UintToString(serial_++);
82 manager()->Show(ToastData(id, text, duration));
83 return id;
84 }
85
86 void CancelToast(const std::string& id) { manager()->Cancel(id); }
87
79 private: 88 private:
80 ToastManager* manager_ = nullptr; 89 ToastManager* manager_ = nullptr;
90 unsigned int serial_ = 0;
81 91
82 DISALLOW_COPY_AND_ASSIGN(ToastManagerTest); 92 DISALLOW_COPY_AND_ASSIGN(ToastManagerTest);
83 }; 93 };
84 94
85 TEST_F(ToastManagerTest, ShowAndCloseAutomatically) { 95 TEST_F(ToastManagerTest, ShowAndCloseAutomatically) {
86 manager()->Show("DUMMY", 10); 96 ShowToast("DUMMY", 10);
87 97
88 EXPECT_EQ(1, GetToastId()); 98 EXPECT_EQ(1, GetToastSerial());
89 99
90 while (GetCurrentOverlay() != nullptr) 100 while (GetCurrentOverlay() != nullptr)
91 base::RunLoop().RunUntilIdle(); 101 base::RunLoop().RunUntilIdle();
92 } 102 }
93 103
94 TEST_F(ToastManagerTest, ShowAndCloseManually) { 104 TEST_F(ToastManagerTest, ShowAndCloseManually) {
95 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); 105 ShowToast("DUMMY", kLongLongDuration /* prevent timeout */);
96 106
97 EXPECT_EQ(1, GetToastId()); 107 EXPECT_EQ(1, GetToastSerial());
98 108
99 EXPECT_FALSE(GetCurrentWidget()->GetLayer()->GetAnimator()->is_animating()); 109 EXPECT_FALSE(GetCurrentWidget()->GetLayer()->GetAnimator()->is_animating());
100 110
101 ClickDismissButton(); 111 ClickDismissButton();
102 112
103 EXPECT_EQ(nullptr, GetCurrentOverlay()); 113 EXPECT_EQ(nullptr, GetCurrentOverlay());
104 } 114 }
105 115
106 TEST_F(ToastManagerTest, ShowAndCloseManuallyDuringAnimation) { 116 TEST_F(ToastManagerTest, ShowAndCloseManuallyDuringAnimation) {
107 ui::ScopedAnimationDurationScaleMode slow_animation_duration( 117 ui::ScopedAnimationDurationScaleMode slow_animation_duration(
108 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION); 118 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
109 119
110 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); 120 ShowToast("DUMMY", kLongLongDuration /* prevent timeout */);
111 EXPECT_TRUE(GetCurrentWidget()->GetLayer()->GetAnimator()->is_animating()); 121 EXPECT_TRUE(GetCurrentWidget()->GetLayer()->GetAnimator()->is_animating());
112 base::RunLoop().RunUntilIdle(); 122 base::RunLoop().RunUntilIdle();
113 123
114 EXPECT_EQ(1, GetToastId()); 124 EXPECT_EQ(1, GetToastSerial());
115 EXPECT_TRUE(GetCurrentWidget()->GetLayer()->GetAnimator()->is_animating()); 125 EXPECT_TRUE(GetCurrentWidget()->GetLayer()->GetAnimator()->is_animating());
116 126
117 // Close it during animation. 127 // Close it during animation.
118 ClickDismissButton(); 128 ClickDismissButton();
119 129
120 while (GetCurrentOverlay() != nullptr) 130 while (GetCurrentOverlay() != nullptr)
121 base::RunLoop().RunUntilIdle(); 131 base::RunLoop().RunUntilIdle();
122 } 132 }
123 133
124 TEST_F(ToastManagerTest, QueueMessage) { 134 TEST_F(ToastManagerTest, QueueMessage) {
125 manager()->Show("DUMMY1", 10); 135 ShowToast("DUMMY1", 10);
126 manager()->Show("DUMMY2", 10); 136 ShowToast("DUMMY2", 10);
127 manager()->Show("DUMMY3", 10); 137 ShowToast("DUMMY3", 10);
128 138
129 EXPECT_EQ(1, GetToastId()); 139 EXPECT_EQ(1, GetToastSerial());
130 EXPECT_EQ("DUMMY1", GetCurrentText()); 140 EXPECT_EQ("DUMMY1", GetCurrentText());
131 141
132 while (GetToastId() != 2) 142 while (GetToastSerial() != 2)
133 base::RunLoop().RunUntilIdle(); 143 base::RunLoop().RunUntilIdle();
134 144
135 EXPECT_EQ("DUMMY2", GetCurrentText()); 145 EXPECT_EQ("DUMMY2", GetCurrentText());
136 146
137 while (GetToastId() != 3) 147 while (GetToastSerial() != 3)
138 base::RunLoop().RunUntilIdle(); 148 base::RunLoop().RunUntilIdle();
139 149
140 EXPECT_EQ("DUMMY3", GetCurrentText()); 150 EXPECT_EQ("DUMMY3", GetCurrentText());
141 } 151 }
142 152
143 TEST_F(ToastManagerTest, PositionWithVisibleBottomShelf) { 153 TEST_F(ToastManagerTest, PositionWithVisibleBottomShelf) {
144 ShelfLayoutManager* shelf = 154 ShelfLayoutManager* shelf =
145 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); 155 Shelf::ForPrimaryDisplay()->shelf_layout_manager();
146 SetShelfState(ash::SHELF_VISIBLE); 156 SetShelfState(ash::SHELF_VISIBLE);
147 SetShelfAlignment(wm::SHELF_ALIGNMENT_BOTTOM); 157 SetShelfAlignment(wm::SHELF_ALIGNMENT_BOTTOM);
148 158
149 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); 159 ShowToast("DUMMY", kLongLongDuration /* prevent timeout */);
150 EXPECT_EQ(1, GetToastId()); 160 EXPECT_EQ(1, GetToastSerial());
151 161
152 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); 162 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen();
153 gfx::Rect root_bounds = 163 gfx::Rect root_bounds =
154 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); 164 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow());
155 165
156 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); 166 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds()));
157 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); 167 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1);
158 168
159 if (SupportsHostWindowResize()) { 169 if (SupportsHostWindowResize()) {
160 // If host resize is not supported, ShelfLayoutManager::GetIdealBounds() 170 // If host resize is not supported, ShelfLayoutManager::GetIdealBounds()
(...skipping 10 matching lines...) Expand all
171 std::unique_ptr<aura::Window> window( 181 std::unique_ptr<aura::Window> window(
172 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4))); 182 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4)));
173 183
174 ShelfLayoutManager* shelf = 184 ShelfLayoutManager* shelf =
175 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); 185 Shelf::ForPrimaryDisplay()->shelf_layout_manager();
176 SetShelfAlignment(wm::SHELF_ALIGNMENT_BOTTOM); 186 SetShelfAlignment(wm::SHELF_ALIGNMENT_BOTTOM);
177 SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); 187 SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
178 shelf->LayoutShelf(); 188 shelf->LayoutShelf();
179 EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state()); 189 EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state());
180 190
181 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); 191 ShowToast("DUMMY", kLongLongDuration /* prevent timeout */);
182 EXPECT_EQ(1, GetToastId()); 192 EXPECT_EQ(1, GetToastSerial());
183 193
184 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); 194 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen();
185 gfx::Rect root_bounds = 195 gfx::Rect root_bounds =
186 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); 196 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow());
187 197
188 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); 198 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds()));
189 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); 199 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1);
190 EXPECT_EQ(root_bounds.bottom() - ShelfLayoutManager::kAutoHideSize - 5, 200 EXPECT_EQ(root_bounds.bottom() - ShelfLayoutManager::kAutoHideSize - 5,
191 toast_bounds.bottom()); 201 toast_bounds.bottom());
192 } 202 }
193 203
194 TEST_F(ToastManagerTest, PositionWithHiddenBottomShelf) { 204 TEST_F(ToastManagerTest, PositionWithHiddenBottomShelf) {
195 ShelfLayoutManager* shelf = 205 ShelfLayoutManager* shelf =
196 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); 206 Shelf::ForPrimaryDisplay()->shelf_layout_manager();
197 SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_ALWAYS_HIDDEN); 207 SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_ALWAYS_HIDDEN);
198 SetShelfAlignment(wm::SHELF_ALIGNMENT_BOTTOM); 208 SetShelfAlignment(wm::SHELF_ALIGNMENT_BOTTOM);
199 SetShelfState(ash::SHELF_HIDDEN); 209 SetShelfState(ash::SHELF_HIDDEN);
200 210
201 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); 211 ShowToast("DUMMY", kLongLongDuration /* prevent timeout */);
202 EXPECT_EQ(1, GetToastId()); 212 EXPECT_EQ(1, GetToastSerial());
203 213
204 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); 214 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen();
205 gfx::Rect root_bounds = 215 gfx::Rect root_bounds =
206 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); 216 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow());
207 217
208 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); 218 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds()));
209 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); 219 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1);
210 EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom()); 220 EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom());
211 } 221 }
212 222
213 TEST_F(ToastManagerTest, PositionWithVisibleLeftShelf) { 223 TEST_F(ToastManagerTest, PositionWithVisibleLeftShelf) {
214 ShelfLayoutManager* shelf = 224 ShelfLayoutManager* shelf =
215 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); 225 Shelf::ForPrimaryDisplay()->shelf_layout_manager();
216 SetShelfState(ash::SHELF_VISIBLE); 226 SetShelfState(ash::SHELF_VISIBLE);
217 SetShelfAlignment(wm::SHELF_ALIGNMENT_LEFT); 227 SetShelfAlignment(wm::SHELF_ALIGNMENT_LEFT);
218 228
219 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); 229 ShowToast("DUMMY", kLongLongDuration /* prevent timeout */);
220 EXPECT_EQ(1, GetToastId()); 230 EXPECT_EQ(1, GetToastSerial());
221 231
222 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); 232 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen();
223 gfx::Rect root_bounds = 233 gfx::Rect root_bounds =
224 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); 234 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow());
225 235
226 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); 236 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds()));
227 EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom()); 237 EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom());
228 238
229 if (SupportsHostWindowResize()) { 239 if (SupportsHostWindowResize()) {
230 // If host resize is not supported, ShelfLayoutManager::GetIdealBounds() 240 // If host resize is not supported, ShelfLayoutManager::GetIdealBounds()
(...skipping 12 matching lines...) Expand all
243 253
244 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); 254 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
245 display_manager->SetUnifiedDesktopEnabled(true); 255 display_manager->SetUnifiedDesktopEnabled(true);
246 UpdateDisplay("1000x500,0+600-100x500"); 256 UpdateDisplay("1000x500,0+600-100x500");
247 257
248 ShelfLayoutManager* shelf = 258 ShelfLayoutManager* shelf =
249 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); 259 Shelf::ForPrimaryDisplay()->shelf_layout_manager();
250 SetShelfState(ash::SHELF_VISIBLE); 260 SetShelfState(ash::SHELF_VISIBLE);
251 SetShelfAlignment(wm::SHELF_ALIGNMENT_BOTTOM); 261 SetShelfAlignment(wm::SHELF_ALIGNMENT_BOTTOM);
252 262
253 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); 263 ShowToast("DUMMY", kLongLongDuration /* prevent timeout */);
254 EXPECT_EQ(1, GetToastId()); 264 EXPECT_EQ(1, GetToastSerial());
255 265
256 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); 266 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen();
257 gfx::Rect root_bounds = 267 gfx::Rect root_bounds =
258 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); 268 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow());
259 269
260 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); 270 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds()));
261 EXPECT_TRUE(root_bounds.Contains(toast_bounds)); 271 EXPECT_TRUE(root_bounds.Contains(toast_bounds));
262 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); 272 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1);
263 273
264 if (SupportsHostWindowResize()) { 274 if (SupportsHostWindowResize()) {
265 // If host resize is not supported, ShelfLayoutManager::GetIdealBounds() 275 // If host resize is not supported, ShelfLayoutManager::GetIdealBounds()
266 // doesn't return correct value. 276 // doesn't return correct value.
267 gfx::Rect shelf_bounds = shelf->GetIdealBounds(); 277 gfx::Rect shelf_bounds = shelf->GetIdealBounds();
268 EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); 278 EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds));
269 EXPECT_EQ(shelf_bounds.y() - 5, toast_bounds.bottom()); 279 EXPECT_EQ(shelf_bounds.y() - 5, toast_bounds.bottom());
270 EXPECT_EQ(root_bounds.bottom() - shelf_bounds.height() - 5, 280 EXPECT_EQ(root_bounds.bottom() - shelf_bounds.height() - 5,
271 toast_bounds.bottom()); 281 toast_bounds.bottom());
272 } 282 }
273 } 283 }
274 284
285 TEST_F(ToastManagerTest, CancelToast) {
286 std::string id1 = ShowToast("DUMMY", kLongLongDuration /* prevent timeout */);
287 std::string id2 = ShowToast("DUMMY", kLongLongDuration /* prevent timeout */);
288
hidehiko 2016/05/20 07:46:53 How about adding one more toast, so that cancellin
yoshiki 2016/05/20 08:14:55 Done.
289 // Confirm that the first toast is shown.
290 EXPECT_TRUE(GetCurrentOverlay());
291 // Cancel the queued toast.
292 CancelToast(id2);
293 // Confirm that the shown toast is still visible.
294 EXPECT_TRUE(GetCurrentOverlay());
295 // Cancel the shown toast.
296 CancelToast(id1);
297 // Confirm that the shown toast disappears.
298 EXPECT_FALSE(GetCurrentOverlay());
299 // Confirm that only 1 toast is shown.
300 EXPECT_EQ(1, GetToastSerial());
301 }
302
275 } // namespace ash 303 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698