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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/message_center/message_center_impl.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/message_center_types.h"
#include "ui/message_center/notification_blocker.h"
#include "ui/message_center/notification_types.h"
namespace message_center {
namespace {
class MessageCenterImplTest : public testing::Test,
public MessageCenterObserver {
public:
MessageCenterImplTest() {}
virtual void SetUp() OVERRIDE {
MessageCenter::Initialize();
message_center_ = MessageCenter::Get();
loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_DEFAULT));
run_loop_.reset(new base::RunLoop());
closure_ = run_loop_->QuitClosure();
}
virtual void TearDown() OVERRIDE {
run_loop_.reset();
loop_.reset();
message_center_ = NULL;
MessageCenter::Shutdown();
}
MessageCenter* message_center() const { return message_center_; }
base::RunLoop* run_loop() const { return run_loop_.get(); }
base::Closure closure() const { return closure_; }
private:
MessageCenter* message_center_;
scoped_ptr<base::MessageLoop> loop_;
scoped_ptr<base::RunLoop> run_loop_;
base::Closure closure_;
DISALLOW_COPY_AND_ASSIGN(MessageCenterImplTest);
};
class ToggledNotificationBlocker : public NotificationBlocker {
public:
explicit ToggledNotificationBlocker(MessageCenter* message_center)
: NotificationBlocker(message_center),
notifications_enabled_(true) {}
virtual ~ToggledNotificationBlocker() {}
void SetNotificationsEnabled(bool enabled) {
if (notifications_enabled_ != enabled) {
notifications_enabled_ = enabled;
FOR_EACH_OBSERVER(
NotificationBlocker::Observer, observers(), OnBlockingStateChanged());
}
}
// NotificationBlocker overrides:
virtual bool ShouldShowNotificationAsPopup(
const message_center::NotifierId& notifier_id) const OVERRIDE {
return notifications_enabled_;
}
private:
bool notifications_enabled_;
DISALLOW_COPY_AND_ASSIGN(ToggledNotificationBlocker);
};
class PopupNotificationBlocker : public ToggledNotificationBlocker {
public:
PopupNotificationBlocker(MessageCenter* message_center,
const NotifierId& allowed_notifier)
: ToggledNotificationBlocker(message_center),
allowed_notifier_(allowed_notifier) {}
virtual ~PopupNotificationBlocker() {}
// NotificationBlocker overrides:
virtual bool ShouldShowNotificationAsPopup(
const NotifierId& notifier_id) const OVERRIDE {
return (notifier_id == allowed_notifier_) ||
ToggledNotificationBlocker::ShouldShowNotificationAsPopup(notifier_id);
}
private:
NotifierId allowed_notifier_;
DISALLOW_COPY_AND_ASSIGN(PopupNotificationBlocker);
};
bool PopupNotificationsContain(
const NotificationList::PopupNotifications& popups,
const std::string& id) {
for (NotificationList::PopupNotifications::const_iterator iter =
popups.begin(); iter != popups.end(); ++iter) {
if ((*iter)->id() == id)
return true;
}
return false;
}
} // namespace
namespace internal {
class MockPopupTimersController : public PopupTimersController {
public:
MockPopupTimersController(MessageCenter* message_center,
base::Closure quit_closure)
: PopupTimersController(message_center),
timer_finished_(false),
quit_closure_(quit_closure) {}
virtual ~MockPopupTimersController() {}
virtual void TimerFinished(const std::string& id) OVERRIDE {
LOG(INFO) << "In timer finished for id " << id;
base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
timer_finished_ = true;
last_id_ = id;
}
bool timer_finished() const { return timer_finished_; }
const std::string& last_id() const { return last_id_; }
private:
bool timer_finished_;
std::string last_id_;
base::Closure quit_closure_;
};
TEST_F(MessageCenterImplTest, PopupTimersEmptyController) {
scoped_ptr<PopupTimersController> popup_timers_controller =
make_scoped_ptr(new PopupTimersController(message_center()));
// Test that all functions succed without any timers created.
popup_timers_controller->PauseAll();
popup_timers_controller->StartAll();
popup_timers_controller->CancelAll();
popup_timers_controller->TimerFinished("unknown");
popup_timers_controller->PauseTimer("unknown");
popup_timers_controller->CancelTimer("unknown");
}
TEST_F(MessageCenterImplTest, PopupTimersControllerStartTimer) {
scoped_ptr<MockPopupTimersController> popup_timers_controller =
make_scoped_ptr(
new MockPopupTimersController(message_center(), closure()));
popup_timers_controller->StartTimer("test",
base::TimeDelta::FromMilliseconds(1));
run_loop()->Run();
EXPECT_TRUE(popup_timers_controller->timer_finished());
}
TEST_F(MessageCenterImplTest, PopupTimersControllerPauseTimer) {
scoped_ptr<MockPopupTimersController> popup_timers_controller =
make_scoped_ptr(
new MockPopupTimersController(message_center(), closure()));
popup_timers_controller->StartTimer("test",
base::TimeDelta::FromMilliseconds(1));
popup_timers_controller->PauseTimer("test");
run_loop()->RunUntilIdle();
EXPECT_FALSE(popup_timers_controller->timer_finished());
}
TEST_F(MessageCenterImplTest, PopupTimersControllerCancelTimer) {
scoped_ptr<MockPopupTimersController> popup_timers_controller =
make_scoped_ptr(
new MockPopupTimersController(message_center(), closure()));
popup_timers_controller->StartTimer("test",
base::TimeDelta::FromMilliseconds(1));
popup_timers_controller->CancelTimer("test");
run_loop()->RunUntilIdle();
EXPECT_FALSE(popup_timers_controller->timer_finished());
}
TEST_F(MessageCenterImplTest, PopupTimersControllerPauseAllTimers) {
scoped_ptr<MockPopupTimersController> popup_timers_controller =
make_scoped_ptr(
new MockPopupTimersController(message_center(), closure()));
popup_timers_controller->StartTimer("test",
base::TimeDelta::FromMilliseconds(1));
popup_timers_controller->PauseAll();
run_loop()->RunUntilIdle();
EXPECT_FALSE(popup_timers_controller->timer_finished());
}
TEST_F(MessageCenterImplTest, PopupTimersControllerStartAllTimers) {
scoped_ptr<MockPopupTimersController> popup_timers_controller =
make_scoped_ptr(
new MockPopupTimersController(message_center(), closure()));
popup_timers_controller->StartTimer("test",
base::TimeDelta::FromMilliseconds(1));
popup_timers_controller->PauseAll();
popup_timers_controller->StartAll();
run_loop()->Run();
EXPECT_TRUE(popup_timers_controller->timer_finished());
}
TEST_F(MessageCenterImplTest, PopupTimersControllerStartMultipleTimers) {
scoped_ptr<MockPopupTimersController> popup_timers_controller =
make_scoped_ptr(
new MockPopupTimersController(message_center(), closure()));
popup_timers_controller->StartTimer("test",
base::TimeDelta::FromMilliseconds(5));
popup_timers_controller->StartTimer("test2",
base::TimeDelta::FromMilliseconds(1));
popup_timers_controller->StartTimer("test3",
base::TimeDelta::FromMilliseconds(3));
popup_timers_controller->PauseAll();
popup_timers_controller->StartAll();
run_loop()->Run();
EXPECT_EQ(popup_timers_controller->last_id(), "test2");
EXPECT_TRUE(popup_timers_controller->timer_finished());
}
TEST_F(MessageCenterImplTest, PopupTimersControllerStartMultipleTimersPause) {
scoped_ptr<MockPopupTimersController> popup_timers_controller =
make_scoped_ptr(
new MockPopupTimersController(message_center(), closure()));
popup_timers_controller->StartTimer("test",
base::TimeDelta::FromMilliseconds(5));
popup_timers_controller->StartTimer("test2",
base::TimeDelta::FromMilliseconds(1));
popup_timers_controller->StartTimer("test3",
base::TimeDelta::FromMilliseconds(3));
popup_timers_controller->PauseTimer("test2");
run_loop()->Run();
EXPECT_EQ(popup_timers_controller->last_id(), "test3");
EXPECT_TRUE(popup_timers_controller->timer_finished());
}
TEST_F(MessageCenterImplTest, PopupTimersControllerResetTimer) {
scoped_ptr<MockPopupTimersController> popup_timers_controller =
make_scoped_ptr(
new MockPopupTimersController(message_center(), closure()));
popup_timers_controller->StartTimer("test",
base::TimeDelta::FromMilliseconds(5));
popup_timers_controller->StartTimer("test2",
base::TimeDelta::FromMilliseconds(1));
popup_timers_controller->StartTimer("test3",
base::TimeDelta::FromMilliseconds(3));
popup_timers_controller->PauseTimer("test2");
popup_timers_controller->ResetTimer("test",
base::TimeDelta::FromMilliseconds(2));
run_loop()->Run();
EXPECT_EQ(popup_timers_controller->last_id(), "test");
EXPECT_TRUE(popup_timers_controller->timer_finished());
}
TEST_F(MessageCenterImplTest, NotificationBlocker) {
NotifierId notifier_id(NotifierId::APPLICATION, "app1");
// Multiple blockers to verify the case that one blocker blocks but another
// doesn't.
ToggledNotificationBlocker blocker1(message_center());
ToggledNotificationBlocker blocker2(message_center());
message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
NOTIFICATION_TYPE_SIMPLE,
"id1",
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id,
RichNotificationData(),
NULL)));
message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
NOTIFICATION_TYPE_SIMPLE,
"id2",
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id,
RichNotificationData(),
NULL)));
EXPECT_EQ(2u, message_center()->GetPopupNotifications().size());
EXPECT_EQ(2u, message_center()->GetNotifications().size());
// Block all notifications. All popups are gone and message center should be
// hidden.
blocker1.SetNotificationsEnabled(false);
EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
EXPECT_EQ(2u, message_center()->GetNotifications().size());
// Updates |blocker2| state, which doesn't affect the global state.
blocker2.SetNotificationsEnabled(false);
EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
EXPECT_EQ(2u, message_center()->GetNotifications().size());
blocker2.SetNotificationsEnabled(true);
EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
EXPECT_EQ(2u, message_center()->GetNotifications().size());
// If |blocker2| blocks, then unblocking blocker1 doesn't change the global
// state.
blocker2.SetNotificationsEnabled(false);
blocker1.SetNotificationsEnabled(true);
EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
EXPECT_EQ(2u, message_center()->GetNotifications().size());
// Unblock both blockers, which recovers the global state, but the popups
// aren't shown.
blocker2.SetNotificationsEnabled(true);
EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
EXPECT_EQ(2u, message_center()->GetNotifications().size());
}
TEST_F(MessageCenterImplTest, NotificationsDuringBlocked) {
NotifierId notifier_id(NotifierId::APPLICATION, "app1");
ToggledNotificationBlocker blocker(message_center());
message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
NOTIFICATION_TYPE_SIMPLE,
"id1",
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id,
RichNotificationData(),
NULL)));
EXPECT_EQ(1u, message_center()->GetPopupNotifications().size());
EXPECT_EQ(1u, message_center()->GetNotifications().size());
// Create a notification during blocked. Still no popups.
blocker.SetNotificationsEnabled(false);
message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
NOTIFICATION_TYPE_SIMPLE,
"id2",
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id,
RichNotificationData(),
NULL)));
EXPECT_TRUE(message_center()->GetPopupNotifications().empty());
EXPECT_EQ(2u, message_center()->GetNotifications().size());
// Unblock notifications, the id1 should appear as a popup.
blocker.SetNotificationsEnabled(true);
NotificationList::PopupNotifications popups =
message_center()->GetPopupNotifications();
EXPECT_EQ(1u, popups.size());
EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
EXPECT_EQ(2u, message_center()->GetNotifications().size());
}
// Similar to other blocker cases but this test case allows |notifier_id2| even
// in blocked.
TEST_F(MessageCenterImplTest, NotificationBlockerAllowsPopups) {
NotifierId notifier_id1(NotifierId::APPLICATION, "app1");
NotifierId notifier_id2(NotifierId::APPLICATION, "app2");
PopupNotificationBlocker blocker(message_center(), notifier_id2);
message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
NOTIFICATION_TYPE_SIMPLE,
"id1",
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id1,
RichNotificationData(),
NULL)));
message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
NOTIFICATION_TYPE_SIMPLE,
"id2",
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id2,
RichNotificationData(),
NULL)));
// "id1" is closed but "id2" is still visible as a popup.
blocker.SetNotificationsEnabled(false);
NotificationList::PopupNotifications popups =
message_center()->GetPopupNotifications();
EXPECT_EQ(1u, popups.size());
EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
EXPECT_EQ(2u, message_center()->GetNotifications().size());
message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
NOTIFICATION_TYPE_SIMPLE,
"id3",
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id1,
RichNotificationData(),
NULL)));
message_center()->AddNotification(scoped_ptr<Notification>(new Notification(
NOTIFICATION_TYPE_SIMPLE,
"id4",
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id2,
RichNotificationData(),
NULL)));
popups = message_center()->GetPopupNotifications();
EXPECT_EQ(2u, popups.size());
EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
EXPECT_TRUE(PopupNotificationsContain(popups, "id4"));
EXPECT_EQ(4u, message_center()->GetNotifications().size());
blocker.SetNotificationsEnabled(true);
popups = message_center()->GetPopupNotifications();
EXPECT_EQ(3u, popups.size());
EXPECT_TRUE(PopupNotificationsContain(popups, "id2"));
EXPECT_TRUE(PopupNotificationsContain(popups, "id3"));
EXPECT_TRUE(PopupNotificationsContain(popups, "id4"));
EXPECT_EQ(4u, message_center()->GetNotifications().size());
}
TEST_F(MessageCenterImplTest, QueueUpdatesWithCenterVisible) {
std::string id("id1");
std::string id2("id2");
NotifierId notifier_id1(NotifierId::APPLICATION, "app1");
scoped_ptr<Notification> notification(new Notification(
NOTIFICATION_TYPE_SIMPLE,
id,
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id1,
RichNotificationData(),
NULL));
message_center()->AddNotification(notification.Pass());
notification.reset(new Notification(
NOTIFICATION_TYPE_MULTIPLE,
id2,
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id1,
RichNotificationData(),
NULL));
message_center()->UpdateNotification(id, notification.Pass());
EXPECT_TRUE(message_center()->HasNotification(id2));
EXPECT_FALSE(message_center()->HasNotification(id));
message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
notification.reset(new Notification(
NOTIFICATION_TYPE_MULTIPLE,
id,
UTF8ToUTF16("title"),
UTF8ToUTF16("message"),
gfx::Image() /* icon */,
base::string16() /* display_source */,
notifier_id1,
RichNotificationData(),
NULL));
message_center()->UpdateNotification(id2, notification.Pass());
EXPECT_TRUE(message_center()->HasNotification(id2));
EXPECT_FALSE(message_center()->HasNotification(id));
message_center()->SetVisibility(VISIBILITY_TRANSIENT);
EXPECT_FALSE(message_center()->HasNotification(id2));
EXPECT_TRUE(message_center()->HasNotification(id));
}
} // namespace internal
} // namespace message_center
|