OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/message_center_impl.h" | 5 #include "ui/message_center/message_center_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/test/test_mock_time_task_runner.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
21 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
22 #include "ui/message_center/message_center.h" | 23 #include "ui/message_center/message_center.h" |
| 24 #include "ui/message_center/message_center_style.h" |
23 #include "ui/message_center/message_center_types.h" | 25 #include "ui/message_center/message_center_types.h" |
24 #include "ui/message_center/notification_blocker.h" | 26 #include "ui/message_center/notification_blocker.h" |
25 #include "ui/message_center/notification_types.h" | 27 #include "ui/message_center/notification_types.h" |
26 #include "ui/message_center/notifier_settings.h" | 28 #include "ui/message_center/notifier_settings.h" |
27 | 29 |
28 using base::UTF8ToUTF16; | 30 using base::UTF8ToUTF16; |
29 | 31 |
30 namespace message_center { | 32 namespace message_center { |
31 | 33 |
32 class MessageCenterImplTest : public testing::Test, | 34 class MessageCenterImplTest : public testing::Test, |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 257 |
256 TEST_F(MessageCenterImplTest, PopupTimersEmptyController) { | 258 TEST_F(MessageCenterImplTest, PopupTimersEmptyController) { |
257 std::unique_ptr<PopupTimersController> popup_timers_controller = | 259 std::unique_ptr<PopupTimersController> popup_timers_controller = |
258 base::MakeUnique<PopupTimersController>(message_center()); | 260 base::MakeUnique<PopupTimersController>(message_center()); |
259 | 261 |
260 // Test that all functions succed without any timers created. | 262 // Test that all functions succed without any timers created. |
261 popup_timers_controller->PauseAll(); | 263 popup_timers_controller->PauseAll(); |
262 popup_timers_controller->StartAll(); | 264 popup_timers_controller->StartAll(); |
263 popup_timers_controller->CancelAll(); | 265 popup_timers_controller->CancelAll(); |
264 popup_timers_controller->TimerFinished("unknown"); | 266 popup_timers_controller->TimerFinished("unknown"); |
265 popup_timers_controller->PauseTimer("unknown"); | |
266 popup_timers_controller->CancelTimer("unknown"); | 267 popup_timers_controller->CancelTimer("unknown"); |
267 } | 268 } |
268 | 269 |
269 TEST_F(MessageCenterImplTest, PopupTimersControllerStartTimer) { | 270 TEST_F(MessageCenterImplTest, PopupTimersControllerStartTimer) { |
270 std::unique_ptr<MockPopupTimersController> popup_timers_controller = | 271 std::unique_ptr<MockPopupTimersController> popup_timers_controller = |
271 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); | 272 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); |
272 popup_timers_controller->StartTimer("test", | 273 popup_timers_controller->StartTimer("test", |
273 base::TimeDelta::FromMilliseconds(1)); | 274 base::TimeDelta::FromMilliseconds(1)); |
274 run_loop()->Run(); | 275 run_loop()->Run(); |
275 EXPECT_TRUE(popup_timers_controller->timer_finished()); | 276 EXPECT_TRUE(popup_timers_controller->timer_finished()); |
276 } | 277 } |
277 | 278 |
278 TEST_F(MessageCenterImplTest, PopupTimersControllerPauseTimer) { | |
279 std::unique_ptr<MockPopupTimersController> popup_timers_controller = | |
280 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); | |
281 popup_timers_controller->StartTimer("test", | |
282 base::TimeDelta::FromMilliseconds(1)); | |
283 popup_timers_controller->PauseTimer("test"); | |
284 run_loop()->RunUntilIdle(); | |
285 | |
286 EXPECT_FALSE(popup_timers_controller->timer_finished()); | |
287 } | |
288 | |
289 TEST_F(MessageCenterImplTest, PopupTimersControllerCancelTimer) { | 279 TEST_F(MessageCenterImplTest, PopupTimersControllerCancelTimer) { |
290 std::unique_ptr<MockPopupTimersController> popup_timers_controller = | 280 std::unique_ptr<MockPopupTimersController> popup_timers_controller = |
291 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); | 281 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); |
292 popup_timers_controller->StartTimer("test", | 282 popup_timers_controller->StartTimer("test", |
293 base::TimeDelta::FromMilliseconds(1)); | 283 base::TimeDelta::FromMilliseconds(1)); |
294 popup_timers_controller->CancelTimer("test"); | 284 popup_timers_controller->CancelTimer("test"); |
295 run_loop()->RunUntilIdle(); | 285 run_loop()->RunUntilIdle(); |
296 | 286 |
297 EXPECT_FALSE(popup_timers_controller->timer_finished()); | 287 EXPECT_FALSE(popup_timers_controller->timer_finished()); |
298 } | 288 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 popup_timers_controller->StartTimer("test3", | 320 popup_timers_controller->StartTimer("test3", |
331 base::TimeDelta::FromMilliseconds(3)); | 321 base::TimeDelta::FromMilliseconds(3)); |
332 popup_timers_controller->PauseAll(); | 322 popup_timers_controller->PauseAll(); |
333 popup_timers_controller->StartAll(); | 323 popup_timers_controller->StartAll(); |
334 run_loop()->Run(); | 324 run_loop()->Run(); |
335 | 325 |
336 EXPECT_EQ(popup_timers_controller->last_id(), "test2"); | 326 EXPECT_EQ(popup_timers_controller->last_id(), "test2"); |
337 EXPECT_TRUE(popup_timers_controller->timer_finished()); | 327 EXPECT_TRUE(popup_timers_controller->timer_finished()); |
338 } | 328 } |
339 | 329 |
340 TEST_F(MessageCenterImplTest, PopupTimersControllerStartMultipleTimersPause) { | 330 TEST_F(MessageCenterImplTest, PopupTimersControllerRestartOnUpdate) { |
| 331 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner = |
| 332 base::ThreadTaskRunnerHandle::Get(); |
| 333 |
| 334 scoped_refptr<base::TestMockTimeTaskRunner> task_runner( |
| 335 new base::TestMockTimeTaskRunner(base::Time::Now(), |
| 336 base::TimeTicks::Now())); |
| 337 base::MessageLoop::current()->SetTaskRunner(task_runner); |
| 338 |
| 339 NotifierId notifier_id(GURL("https://example.com")); |
| 340 |
| 341 message_center()->AddNotification(std::unique_ptr<Notification>( |
| 342 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"), |
| 343 UTF8ToUTF16("message"), gfx::Image() /* icon */, |
| 344 base::string16() /* display_source */, GURL(), |
| 345 notifier_id, RichNotificationData(), NULL))); |
| 346 |
341 std::unique_ptr<MockPopupTimersController> popup_timers_controller = | 347 std::unique_ptr<MockPopupTimersController> popup_timers_controller = |
342 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); | 348 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); |
343 popup_timers_controller->StartTimer("test", | |
344 base::TimeDelta::FromMilliseconds(5)); | |
345 popup_timers_controller->StartTimer("test2", | |
346 base::TimeDelta::FromMilliseconds(1)); | |
347 popup_timers_controller->StartTimer("test3", | |
348 base::TimeDelta::FromMilliseconds(3)); | |
349 popup_timers_controller->PauseTimer("test2"); | |
350 | 349 |
351 run_loop()->Run(); | 350 popup_timers_controller->OnNotificationDisplayed("id1", DISPLAY_SOURCE_POPUP); |
| 351 ASSERT_FALSE(popup_timers_controller->timer_finished()); |
352 | 352 |
353 EXPECT_EQ(popup_timers_controller->last_id(), "test3"); | 353 // Fast forward the |task_runner| by one second less than the auto-close timer |
354 EXPECT_TRUE(popup_timers_controller->timer_finished()); | 354 // frequency for Web Notifications. (As set by the |notifier_id|.) |
355 } | 355 task_runner->FastForwardBy( |
| 356 base::TimeDelta::FromSeconds(kAutocloseWebPageDelaySeconds - 1)); |
| 357 ASSERT_FALSE(popup_timers_controller->timer_finished()); |
356 | 358 |
357 TEST_F(MessageCenterImplTest, PopupTimersControllerResetTimer) { | 359 // Trigger a replacement of the notification in the timer controller. |
358 std::unique_ptr<MockPopupTimersController> popup_timers_controller = | 360 popup_timers_controller->OnNotificationUpdated("id1"); |
359 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); | |
360 popup_timers_controller->StartTimer("test", | |
361 base::TimeDelta::FromMilliseconds(5)); | |
362 popup_timers_controller->StartTimer("test2", | |
363 base::TimeDelta::FromMilliseconds(1)); | |
364 popup_timers_controller->StartTimer("test3", | |
365 base::TimeDelta::FromMilliseconds(3)); | |
366 popup_timers_controller->PauseTimer("test2"); | |
367 popup_timers_controller->ResetTimer("test", | |
368 base::TimeDelta::FromMilliseconds(2)); | |
369 | 361 |
370 run_loop()->Run(); | 362 // Fast forward the |task_runner| by one second less than the auto-close timer |
| 363 // frequency for Web Notifications again. It should have been reset. |
| 364 task_runner->FastForwardBy( |
| 365 base::TimeDelta::FromSeconds(kAutocloseWebPageDelaySeconds - 1)); |
| 366 ASSERT_FALSE(popup_timers_controller->timer_finished()); |
371 | 367 |
372 EXPECT_EQ(popup_timers_controller->last_id(), "test"); | 368 // Now fast forward the |task_runner| by two seconds (to avoid flakiness), |
373 EXPECT_TRUE(popup_timers_controller->timer_finished()); | 369 // after which the timer should have fired. |
| 370 task_runner->FastForwardBy(base::TimeDelta::FromSeconds(2)); |
| 371 ASSERT_TRUE(popup_timers_controller->timer_finished()); |
| 372 |
| 373 base::MessageLoop::current()->SetTaskRunner(old_task_runner); |
374 } | 374 } |
375 | 375 |
376 TEST_F(MessageCenterImplTest, NotificationBlocker) { | 376 TEST_F(MessageCenterImplTest, NotificationBlocker) { |
377 NotifierId notifier_id(NotifierId::APPLICATION, "app1"); | 377 NotifierId notifier_id(NotifierId::APPLICATION, "app1"); |
378 // Multiple blockers to verify the case that one blocker blocks but another | 378 // Multiple blockers to verify the case that one blocker blocks but another |
379 // doesn't. | 379 // doesn't. |
380 ToggledNotificationBlocker blocker1(message_center()); | 380 ToggledNotificationBlocker blocker1(message_center()); |
381 ToggledNotificationBlocker blocker2(message_center()); | 381 ToggledNotificationBlocker blocker2(message_center()); |
382 | 382 |
383 message_center()->AddNotification(std::unique_ptr<Notification>( | 383 message_center()->AddNotification(std::unique_ptr<Notification>( |
(...skipping 875 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1259 // Then open the message center. | 1259 // Then open the message center. |
1260 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER); | 1260 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER); |
1261 | 1261 |
1262 // Then update a notification; the update should have propagated. | 1262 // Then update a notification; the update should have propagated. |
1263 message_center()->RemoveNotification(id, false); | 1263 message_center()->RemoveNotification(id, false); |
1264 EXPECT_FALSE(message_center()->FindVisibleNotificationById(id)); | 1264 EXPECT_FALSE(message_center()->FindVisibleNotificationById(id)); |
1265 } | 1265 } |
1266 | 1266 |
1267 } // namespace internal | 1267 } // namespace internal |
1268 } // namespace message_center | 1268 } // namespace message_center |
OLD | NEW |