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

Side by Side Diff: ui/message_center/views/message_popup_collection_unittest.cc

Issue 369573004: Separate the logic of popup alignment and workarea handling as delegate. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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 "ui/message_center/views/message_popup_collection.h" 5 #include "ui/message_center/views/message_popup_collection.h"
6 6
7 #include <list> 7 #include <list>
8 8
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/events/event.h" 13 #include "ui/events/event.h"
14 #include "ui/events/event_constants.h" 14 #include "ui/events/event_constants.h"
15 #include "ui/gfx/display.h" 15 #include "ui/gfx/display.h"
16 #include "ui/gfx/rect.h" 16 #include "ui/gfx/rect.h"
17 #include "ui/message_center/fake_message_center.h" 17 #include "ui/message_center/fake_message_center.h"
18 #include "ui/message_center/views/desktop_popup_alignment_delegate.h"
18 #include "ui/message_center/views/toast_contents_view.h" 19 #include "ui/message_center/views/toast_contents_view.h"
19 #include "ui/views/test/views_test_base.h" 20 #include "ui/views/test/views_test_base.h"
20 #include "ui/views/widget/widget.h" 21 #include "ui/views/widget/widget.h"
21 #include "ui/views/widget/widget_delegate.h" 22 #include "ui/views/widget/widget_delegate.h"
22 23
23 namespace message_center { 24 namespace message_center {
24 namespace test { 25 namespace test {
25 26
26 class MessagePopupCollectionTest : public views::ViewsTestBase { 27 class MessagePopupCollectionTest : public views::ViewsTestBase {
27 public: 28 public:
28 virtual void SetUp() OVERRIDE { 29 virtual void SetUp() OVERRIDE {
29 views::ViewsTestBase::SetUp(); 30 views::ViewsTestBase::SetUp();
30 MessageCenter::Initialize(); 31 MessageCenter::Initialize();
31 MessageCenter::Get()->DisableTimersForTest(); 32 MessageCenter::Get()->DisableTimersForTest();
33 alignment_delegate_.reset(new DesktopPopupAlignmentDelegate);
32 collection_.reset(new MessagePopupCollection( 34 collection_.reset(new MessagePopupCollection(
33 GetContext(), MessageCenter::Get(), NULL, false)); 35 GetContext(), MessageCenter::Get(), NULL, alignment_delegate_.get()));
34 // This size fits test machines resolution and also can keep a few toasts 36 // This size fits test machines resolution and also can keep a few toasts
35 // w/o ill effects of hitting the screen overflow. This allows us to assume 37 // w/o ill effects of hitting the screen overflow. This allows us to assume
36 // and verify normal layout of the toast stack. 38 // and verify normal layout of the toast stack.
37 collection_->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), 39 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // taskbar at the bottom.
38 gfx::Rect(0, 0, 600, 400)); // Simulate a 40 gfx::Rect(0, 0, 600, 400));
39 // taskbar at the
40 // bottom.
41 id_ = 0; 41 id_ = 0;
42 PrepareForWait(); 42 PrepareForWait();
43 } 43 }
44 44
45 virtual void TearDown() OVERRIDE { 45 virtual void TearDown() OVERRIDE {
46 collection_.reset(); 46 collection_.reset();
47 MessageCenter::Shutdown(); 47 MessageCenter::Shutdown();
48 views::ViewsTestBase::TearDown(); 48 views::ViewsTestBase::TearDown();
49 } 49 }
50 50
(...skipping 10 matching lines...) Expand all
61 61
62 bool IsToastShown(const std::string& id) { 62 bool IsToastShown(const std::string& id) {
63 views::Widget* widget = collection_->GetWidgetForTest(id); 63 views::Widget* widget = collection_->GetWidgetForTest(id);
64 return widget && widget->IsVisible(); 64 return widget && widget->IsVisible();
65 } 65 }
66 66
67 views::Widget* GetWidget(const std::string& id) { 67 views::Widget* GetWidget(const std::string& id) {
68 return collection_->GetWidgetForTest(id); 68 return collection_->GetWidgetForTest(id);
69 } 69 }
70 70
71 void SetDisplayInfo(const gfx::Rect& work_area,
72 const gfx::Rect& display_bounds) {
73 gfx::Display dummy_display;
74 dummy_display.set_bounds(display_bounds);
75 dummy_display.set_work_area(work_area);
76 alignment_delegate_->RecomputeAlignment(dummy_display);
77 PrepareForWait();
78 }
79
71 gfx::Rect GetWorkArea() { 80 gfx::Rect GetWorkArea() {
72 return collection_->work_area_; 81 return alignment_delegate_->work_area_;
73 } 82 }
74 83
75 ToastContentsView* GetToast(const std::string& id) { 84 ToastContentsView* GetToast(const std::string& id) {
76 for (MessagePopupCollection::Toasts::iterator iter = 85 for (MessagePopupCollection::Toasts::iterator iter =
77 collection_->toasts_.begin(); 86 collection_->toasts_.begin();
78 iter != collection_->toasts_.end(); ++iter) { 87 iter != collection_->toasts_.end(); ++iter) {
79 if ((*iter)->id() == id) 88 if ((*iter)->id() == id)
80 return *iter; 89 return *iter;
81 } 90 }
82 return NULL; 91 return NULL;
(...skipping 28 matching lines...) Expand all
111 EXPECT_TRUE(GetToastCounts() > 0); 120 EXPECT_TRUE(GetToastCounts() > 0);
112 MessageCenter::Get()->RemoveAllNotifications(false); 121 MessageCenter::Get()->RemoveAllNotifications(false);
113 } 122 }
114 123
115 gfx::Rect GetToastRectAt(size_t index) { 124 gfx::Rect GetToastRectAt(size_t index) {
116 return collection_->GetToastRectAt(index); 125 return collection_->GetToastRectAt(index);
117 } 126 }
118 127
119 private: 128 private:
120 scoped_ptr<MessagePopupCollection> collection_; 129 scoped_ptr<MessagePopupCollection> collection_;
130 scoped_ptr<DesktopPopupAlignmentDelegate> alignment_delegate_;
121 int id_; 131 int id_;
122 }; 132 };
123 133
124 TEST_F(MessagePopupCollectionTest, DismissOnClick) { 134 TEST_F(MessagePopupCollectionTest, DismissOnClick) {
125 135
126 std::string id1 = AddNotification(); 136 std::string id1 = AddNotification();
127 std::string id2 = AddNotification(); 137 std::string id2 = AddNotification();
128 WaitForTransitionsDone(); 138 WaitForTransitionsDone();
129 139
130 EXPECT_EQ(2u, GetToastCounts()); 140 EXPECT_EQ(2u, GetToastCounts());
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 EXPECT_EQ(0, r3.height()); 204 EXPECT_EQ(0, r3.height());
195 205
196 CloseAllToasts(); 206 CloseAllToasts();
197 EXPECT_EQ(0u, GetToastCounts()); 207 EXPECT_EQ(0u, GetToastCounts());
198 } 208 }
199 209
200 TEST_F(MessagePopupCollectionTest, DefaultPositioningWithRightTaskbar) { 210 TEST_F(MessagePopupCollectionTest, DefaultPositioningWithRightTaskbar) {
201 // If taskbar is on the right we show the toasts bottom to top as usual. 211 // If taskbar is on the right we show the toasts bottom to top as usual.
202 212
203 // Simulate a taskbar at the right. 213 // Simulate a taskbar at the right.
204 collection()->SetDisplayInfo(gfx::Rect(0, 0, 590, 400), // Work-area. 214 SetDisplayInfo(gfx::Rect(0, 0, 590, 400), // Work-area.
205 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 215 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
206 std::string id0 = AddNotification(); 216 std::string id0 = AddNotification();
207 std::string id1 = AddNotification(); 217 std::string id1 = AddNotification();
208 WaitForTransitionsDone(); 218 WaitForTransitionsDone();
209 219
210 gfx::Rect r0 = GetToastRectAt(0); 220 gfx::Rect r0 = GetToastRectAt(0);
211 gfx::Rect r1 = GetToastRectAt(1); 221 gfx::Rect r1 = GetToastRectAt(1);
212 222
213 // 2 toasts are shown, equal size, vertical stack. 223 // 2 toasts are shown, equal size, vertical stack.
214 EXPECT_TRUE(IsToastShown(id0)); 224 EXPECT_TRUE(IsToastShown(id0));
215 EXPECT_TRUE(IsToastShown(id1)); 225 EXPECT_TRUE(IsToastShown(id1));
216 226
217 EXPECT_EQ(r0.width(), r1.width()); 227 EXPECT_EQ(r0.width(), r1.width());
218 EXPECT_EQ(r0.height(), r1.height()); 228 EXPECT_EQ(r0.height(), r1.height());
219 EXPECT_GT(r0.y(), r1.y()); 229 EXPECT_GT(r0.y(), r1.y());
220 EXPECT_EQ(r0.x(), r1.x()); 230 EXPECT_EQ(r0.x(), r1.x());
221 231
222 CloseAllToasts(); 232 CloseAllToasts();
223 EXPECT_EQ(0u, GetToastCounts()); 233 EXPECT_EQ(0u, GetToastCounts());
224 234
225 // Restore simulated taskbar position to bottom. 235 // Restore simulated taskbar position to bottom.
226 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area. 236 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
227 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 237 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
228 } 238 }
229 239
230 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithTopTaskbar) { 240 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithTopTaskbar) {
231 // Simulate a taskbar at the top. 241 // Simulate a taskbar at the top.
232 collection()->SetDisplayInfo(gfx::Rect(0, 10, 600, 390), // Work-area. 242 SetDisplayInfo(gfx::Rect(0, 10, 600, 390), // Work-area.
233 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 243 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
234 std::string id0 = AddNotification(); 244 std::string id0 = AddNotification();
235 std::string id1 = AddNotification(); 245 std::string id1 = AddNotification();
236 WaitForTransitionsDone(); 246 WaitForTransitionsDone();
237 247
238 gfx::Rect r0 = GetToastRectAt(0); 248 gfx::Rect r0 = GetToastRectAt(0);
239 gfx::Rect r1 = GetToastRectAt(1); 249 gfx::Rect r1 = GetToastRectAt(1);
240 250
241 // 2 toasts are shown, equal size, vertical stack. 251 // 2 toasts are shown, equal size, vertical stack.
242 EXPECT_TRUE(IsToastShown(id0)); 252 EXPECT_TRUE(IsToastShown(id0));
243 EXPECT_TRUE(IsToastShown(id1)); 253 EXPECT_TRUE(IsToastShown(id1));
244 254
245 EXPECT_EQ(r0.width(), r1.width()); 255 EXPECT_EQ(r0.width(), r1.width());
246 EXPECT_EQ(r0.height(), r1.height()); 256 EXPECT_EQ(r0.height(), r1.height());
247 EXPECT_LT(r0.y(), r1.y()); 257 EXPECT_LT(r0.y(), r1.y());
248 EXPECT_EQ(r0.x(), r1.x()); 258 EXPECT_EQ(r0.x(), r1.x());
249 259
250 CloseAllToasts(); 260 CloseAllToasts();
251 EXPECT_EQ(0u, GetToastCounts()); 261 EXPECT_EQ(0u, GetToastCounts());
252 262
253 // Restore simulated taskbar position to bottom. 263 // Restore simulated taskbar position to bottom.
254 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area. 264 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
255 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 265 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
256 } 266 }
257 267
258 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithLeftAndTopTaskbar) { 268 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithLeftAndTopTaskbar) {
259 // If there "seems" to be a taskbar on left and top (like in Unity), it is 269 // If there "seems" to be a taskbar on left and top (like in Unity), it is
260 // assumed that the actual taskbar is the top one. 270 // assumed that the actual taskbar is the top one.
261 271
262 // Simulate a taskbar at the top and left. 272 // Simulate a taskbar at the top and left.
263 collection()->SetDisplayInfo(gfx::Rect(10, 10, 590, 390), // Work-area. 273 SetDisplayInfo(gfx::Rect(10, 10, 590, 390), // Work-area.
264 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 274 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
265 std::string id0 = AddNotification(); 275 std::string id0 = AddNotification();
266 std::string id1 = AddNotification(); 276 std::string id1 = AddNotification();
267 WaitForTransitionsDone(); 277 WaitForTransitionsDone();
268 278
269 gfx::Rect r0 = GetToastRectAt(0); 279 gfx::Rect r0 = GetToastRectAt(0);
270 gfx::Rect r1 = GetToastRectAt(1); 280 gfx::Rect r1 = GetToastRectAt(1);
271 281
272 // 2 toasts are shown, equal size, vertical stack. 282 // 2 toasts are shown, equal size, vertical stack.
273 EXPECT_TRUE(IsToastShown(id0)); 283 EXPECT_TRUE(IsToastShown(id0));
274 EXPECT_TRUE(IsToastShown(id1)); 284 EXPECT_TRUE(IsToastShown(id1));
275 285
276 EXPECT_EQ(r0.width(), r1.width()); 286 EXPECT_EQ(r0.width(), r1.width());
277 EXPECT_EQ(r0.height(), r1.height()); 287 EXPECT_EQ(r0.height(), r1.height());
278 EXPECT_LT(r0.y(), r1.y()); 288 EXPECT_LT(r0.y(), r1.y());
279 EXPECT_EQ(r0.x(), r1.x()); 289 EXPECT_EQ(r0.x(), r1.x());
280 290
281 CloseAllToasts(); 291 CloseAllToasts();
282 EXPECT_EQ(0u, GetToastCounts()); 292 EXPECT_EQ(0u, GetToastCounts());
283 293
284 // Restore simulated taskbar position to bottom. 294 // Restore simulated taskbar position to bottom.
285 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area. 295 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
286 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 296 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
287 } 297 }
288 298
289 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithBottomAndTopTaskbar) { 299 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithBottomAndTopTaskbar) {
290 // If there "seems" to be a taskbar on bottom and top (like in Gnome), it is 300 // If there "seems" to be a taskbar on bottom and top (like in Gnome), it is
291 // assumed that the actual taskbar is the top one. 301 // assumed that the actual taskbar is the top one.
292 302
293 // Simulate a taskbar at the top and bottom. 303 // Simulate a taskbar at the top and bottom.
294 collection()->SetDisplayInfo(gfx::Rect(0, 10, 580, 400), // Work-area. 304 SetDisplayInfo(gfx::Rect(0, 10, 580, 400), // Work-area.
295 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 305 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
296 std::string id0 = AddNotification(); 306 std::string id0 = AddNotification();
297 std::string id1 = AddNotification(); 307 std::string id1 = AddNotification();
298 WaitForTransitionsDone(); 308 WaitForTransitionsDone();
299 309
300 gfx::Rect r0 = GetToastRectAt(0); 310 gfx::Rect r0 = GetToastRectAt(0);
301 gfx::Rect r1 = GetToastRectAt(1); 311 gfx::Rect r1 = GetToastRectAt(1);
302 312
303 // 2 toasts are shown, equal size, vertical stack. 313 // 2 toasts are shown, equal size, vertical stack.
304 EXPECT_TRUE(IsToastShown(id0)); 314 EXPECT_TRUE(IsToastShown(id0));
305 EXPECT_TRUE(IsToastShown(id1)); 315 EXPECT_TRUE(IsToastShown(id1));
306 316
307 EXPECT_EQ(r0.width(), r1.width()); 317 EXPECT_EQ(r0.width(), r1.width());
308 EXPECT_EQ(r0.height(), r1.height()); 318 EXPECT_EQ(r0.height(), r1.height());
309 EXPECT_LT(r0.y(), r1.y()); 319 EXPECT_LT(r0.y(), r1.y());
310 EXPECT_EQ(r0.x(), r1.x()); 320 EXPECT_EQ(r0.x(), r1.x());
311 321
312 CloseAllToasts(); 322 CloseAllToasts();
313 EXPECT_EQ(0u, GetToastCounts()); 323 EXPECT_EQ(0u, GetToastCounts());
314 324
315 // Restore simulated taskbar position to bottom. 325 // Restore simulated taskbar position to bottom.
316 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area. 326 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
317 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 327 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
318 } 328 }
319 329
320 TEST_F(MessagePopupCollectionTest, LeftPositioningWithLeftTaskbar) { 330 TEST_F(MessagePopupCollectionTest, LeftPositioningWithLeftTaskbar) {
321 // Simulate a taskbar at the left. 331 // Simulate a taskbar at the left.
322 collection()->SetDisplayInfo(gfx::Rect(10, 0, 590, 400), // Work-area. 332 SetDisplayInfo(gfx::Rect(10, 0, 590, 400), // Work-area.
323 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 333 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
324 std::string id0 = AddNotification(); 334 std::string id0 = AddNotification();
325 std::string id1 = AddNotification(); 335 std::string id1 = AddNotification();
326 WaitForTransitionsDone(); 336 WaitForTransitionsDone();
327 337
328 gfx::Rect r0 = GetToastRectAt(0); 338 gfx::Rect r0 = GetToastRectAt(0);
329 gfx::Rect r1 = GetToastRectAt(1); 339 gfx::Rect r1 = GetToastRectAt(1);
330 340
331 // 2 toasts are shown, equal size, vertical stack. 341 // 2 toasts are shown, equal size, vertical stack.
332 EXPECT_TRUE(IsToastShown(id0)); 342 EXPECT_TRUE(IsToastShown(id0));
333 EXPECT_TRUE(IsToastShown(id1)); 343 EXPECT_TRUE(IsToastShown(id1));
334 344
335 EXPECT_EQ(r0.width(), r1.width()); 345 EXPECT_EQ(r0.width(), r1.width());
336 EXPECT_EQ(r0.height(), r1.height()); 346 EXPECT_EQ(r0.height(), r1.height());
337 EXPECT_GT(r0.y(), r1.y()); 347 EXPECT_GT(r0.y(), r1.y());
338 EXPECT_EQ(r0.x(), r1.x()); 348 EXPECT_EQ(r0.x(), r1.x());
339 349
340 // Ensure that toasts are on the left. 350 // Ensure that toasts are on the left.
341 EXPECT_LT(r1.x(), GetWorkArea().CenterPoint().x()); 351 EXPECT_LT(r1.x(), GetWorkArea().CenterPoint().x());
342 352
343 CloseAllToasts(); 353 CloseAllToasts();
344 EXPECT_EQ(0u, GetToastCounts()); 354 EXPECT_EQ(0u, GetToastCounts());
345 355
346 // Restore simulated taskbar position to bottom. 356 // Restore simulated taskbar position to bottom.
347 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area. 357 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
348 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 358 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
349 } 359 }
350 360
351 TEST_F(MessagePopupCollectionTest, DetectMouseHover) { 361 TEST_F(MessagePopupCollectionTest, DetectMouseHover) {
352 std::string id0 = AddNotification(); 362 std::string id0 = AddNotification();
353 std::string id1 = AddNotification(); 363 std::string id1 = AddNotification();
354 WaitForTransitionsDone(); 364 WaitForTransitionsDone();
355 365
356 views::WidgetDelegateView* toast0 = GetToast(id0); 366 views::WidgetDelegateView* toast0 = GetToast(id0);
357 EXPECT_TRUE(toast0 != NULL); 367 EXPECT_TRUE(toast0 != NULL);
358 views::WidgetDelegateView* toast1 = GetToast(id1); 368 views::WidgetDelegateView* toast1 = GetToast(id1);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 std::string id2 = AddNotification(); 412 std::string id2 = AddNotification();
403 413
404 WaitForTransitionsDone(); 414 WaitForTransitionsDone();
405 views::WidgetDelegateView* toast2 = GetToast(id2); 415 views::WidgetDelegateView* toast2 = GetToast(id2);
406 EXPECT_TRUE(toast2 != NULL); 416 EXPECT_TRUE(toast2 != NULL);
407 } 417 }
408 418
409 419
410 } // namespace test 420 } // namespace test
411 } // namespace message_center 421 } // namespace message_center
OLDNEW
« no previous file with comments | « ui/message_center/views/message_popup_collection.cc ('k') | ui/message_center/views/popup_alignment_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698