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

Side by Side Diff: ui/message_center/message_center_impl_unittest.cc

Issue 2874673002: Fix flakiness of message_center_unittests in mac (Closed)
Patch Set: . Created 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 229
230 } // namespace 230 } // namespace
231 231
232 namespace internal { 232 namespace internal {
233 233
234 class MockPopupTimersController : public PopupTimersController { 234 class MockPopupTimersController : public PopupTimersController {
235 public: 235 public:
236 MockPopupTimersController(MessageCenter* message_center, 236 MockPopupTimersController(MessageCenter* message_center,
237 base::Closure quit_closure) 237 base::Closure quit_closure)
238 : PopupTimersController(message_center), 238 : PopupTimersController(message_center),
239 timer_finished_(false), 239 timer_finished_(0),
240 quit_closure_(quit_closure) {} 240 quit_closure_(quit_closure) {}
241 ~MockPopupTimersController() override {} 241 ~MockPopupTimersController() override {}
242 242
243 void TimerFinished(const std::string& id) override { 243 void TimerFinished(const std::string& id) override {
244 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); 244 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_);
245 timer_finished_ = true; 245 timer_finished_++;
246 last_id_ = id; 246 last_id_ = id;
247 } 247 }
248 248
249 bool timer_finished() const { return timer_finished_; } 249 int timer_finished() const { return timer_finished_; }
250 const std::string& last_id() const { return last_id_; } 250 const std::string& last_id() const { return last_id_; }
251 251
252 private: 252 private:
253 bool timer_finished_; 253 int timer_finished_;
254 std::string last_id_; 254 std::string last_id_;
255 base::Closure quit_closure_; 255 base::Closure quit_closure_;
256 }; 256 };
257 257
258 TEST_F(MessageCenterImplTest, PopupTimersEmptyController) { 258 TEST_F(MessageCenterImplTest, PopupTimersEmptyController) {
259 std::unique_ptr<PopupTimersController> popup_timers_controller = 259 std::unique_ptr<PopupTimersController> popup_timers_controller =
260 base::MakeUnique<PopupTimersController>(message_center()); 260 base::MakeUnique<PopupTimersController>(message_center());
261 261
262 // Test that all functions succed without any timers created. 262 // Test that all functions succed without any timers created.
263 popup_timers_controller->PauseAll(); 263 popup_timers_controller->PauseAll();
264 popup_timers_controller->StartAll(); 264 popup_timers_controller->StartAll();
265 popup_timers_controller->CancelAll(); 265 popup_timers_controller->CancelAll();
266 popup_timers_controller->TimerFinished("unknown"); 266 popup_timers_controller->TimerFinished("unknown");
267 popup_timers_controller->CancelTimer("unknown"); 267 popup_timers_controller->CancelTimer("unknown");
268 } 268 }
269 269
270 TEST_F(MessageCenterImplTest, PopupTimersControllerStartTimer) { 270 TEST_F(MessageCenterImplTest, PopupTimersControllerStartTimer) {
271 std::unique_ptr<MockPopupTimersController> popup_timers_controller = 271 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
272 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); 272 base::MakeUnique<MockPopupTimersController>(message_center(), closure());
273 popup_timers_controller->StartTimer("test", 273 popup_timers_controller->StartTimer("test",
274 base::TimeDelta::FromMilliseconds(1)); 274 base::TimeDelta::FromMilliseconds(1));
275 run_loop()->Run(); 275 run_loop()->Run();
276 EXPECT_TRUE(popup_timers_controller->timer_finished()); 276 EXPECT_EQ(popup_timers_controller->timer_finished(), 1);
277 } 277 }
278 278
279 TEST_F(MessageCenterImplTest, PopupTimersControllerCancelTimer) { 279 TEST_F(MessageCenterImplTest, PopupTimersControllerCancelTimer) {
280 std::unique_ptr<MockPopupTimersController> popup_timers_controller = 280 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
281 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); 281 base::MakeUnique<MockPopupTimersController>(message_center(), closure());
282 popup_timers_controller->StartTimer("test", 282 popup_timers_controller->StartTimer("test",
283 base::TimeDelta::FromMilliseconds(1)); 283 base::TimeDelta::FromMilliseconds(1));
284 popup_timers_controller->CancelTimer("test"); 284 popup_timers_controller->CancelTimer("test");
285 run_loop()->RunUntilIdle(); 285 run_loop()->RunUntilIdle();
286 286
287 EXPECT_FALSE(popup_timers_controller->timer_finished()); 287 EXPECT_EQ(popup_timers_controller->timer_finished(), 0);
288 } 288 }
289 289
290 TEST_F(MessageCenterImplTest, PopupTimersControllerPauseAllTimers) { 290 TEST_F(MessageCenterImplTest, PopupTimersControllerPauseAllTimers) {
291 std::unique_ptr<MockPopupTimersController> popup_timers_controller = 291 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
292 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); 292 base::MakeUnique<MockPopupTimersController>(message_center(), closure());
293 popup_timers_controller->StartTimer("test", 293 popup_timers_controller->StartTimer("test",
294 base::TimeDelta::FromMilliseconds(1)); 294 base::TimeDelta::FromMilliseconds(1));
295 popup_timers_controller->PauseAll(); 295 popup_timers_controller->PauseAll();
296 run_loop()->RunUntilIdle(); 296 run_loop()->RunUntilIdle();
297 297
298 EXPECT_FALSE(popup_timers_controller->timer_finished()); 298 EXPECT_EQ(popup_timers_controller->timer_finished(), 0);
299 } 299 }
300 300
301 TEST_F(MessageCenterImplTest, PopupTimersControllerStartAllTimers) { 301 TEST_F(MessageCenterImplTest, PopupTimersControllerStartAllTimers) {
302 std::unique_ptr<MockPopupTimersController> popup_timers_controller = 302 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
303 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); 303 base::MakeUnique<MockPopupTimersController>(message_center(), closure());
304 popup_timers_controller->StartTimer("test", 304 popup_timers_controller->StartTimer("test",
305 base::TimeDelta::FromMilliseconds(1)); 305 base::TimeDelta::FromMilliseconds(1));
306 popup_timers_controller->PauseAll(); 306 popup_timers_controller->PauseAll();
307 popup_timers_controller->StartAll(); 307 popup_timers_controller->StartAll();
308 run_loop()->Run(); 308 run_loop()->Run();
309 309
310 EXPECT_TRUE(popup_timers_controller->timer_finished()); 310 EXPECT_EQ(popup_timers_controller->timer_finished(), 1);
311 } 311 }
312 312
313 TEST_F(MessageCenterImplTest, PopupTimersControllerStartMultipleTimers) { 313 TEST_F(MessageCenterImplTest, PopupTimersControllerStartMultipleTimers) {
314 std::unique_ptr<MockPopupTimersController> popup_timers_controller = 314 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
315 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); 315 base::MakeUnique<MockPopupTimersController>(message_center(), closure());
316 popup_timers_controller->StartTimer("test", 316 popup_timers_controller->StartTimer("test", base::TimeDelta::Max());
317 base::TimeDelta::FromMilliseconds(5));
318 popup_timers_controller->StartTimer("test2", 317 popup_timers_controller->StartTimer("test2",
319 base::TimeDelta::FromMilliseconds(1)); 318 base::TimeDelta::FromMilliseconds(1));
yhanada 2017/05/10 09:32:10 Should this be changed to TimeDelta::FromMilliseco
320 popup_timers_controller->StartTimer("test3", 319 popup_timers_controller->StartTimer("test3", base::TimeDelta::Max());
321 base::TimeDelta::FromMilliseconds(3));
322 popup_timers_controller->PauseAll(); 320 popup_timers_controller->PauseAll();
323 popup_timers_controller->StartAll(); 321 popup_timers_controller->StartAll();
324 run_loop()->Run(); 322 run_loop()->Run();
325 323
326 EXPECT_EQ(popup_timers_controller->last_id(), "test2"); 324 EXPECT_EQ(popup_timers_controller->last_id(), "test2");
327 EXPECT_TRUE(popup_timers_controller->timer_finished()); 325 EXPECT_EQ(popup_timers_controller->timer_finished(), 1);
328 } 326 }
329 327
330 TEST_F(MessageCenterImplTest, PopupTimersControllerRestartOnUpdate) { 328 TEST_F(MessageCenterImplTest, PopupTimersControllerRestartOnUpdate) {
331 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner = 329 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner =
332 base::ThreadTaskRunnerHandle::Get(); 330 base::ThreadTaskRunnerHandle::Get();
333 331
334 scoped_refptr<base::TestMockTimeTaskRunner> task_runner( 332 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(
335 new base::TestMockTimeTaskRunner(base::Time::Now(), 333 new base::TestMockTimeTaskRunner(base::Time::Now(),
336 base::TimeTicks::Now())); 334 base::TimeTicks::Now()));
337 base::MessageLoop::current()->SetTaskRunner(task_runner); 335 base::MessageLoop::current()->SetTaskRunner(task_runner);
338 336
339 NotifierId notifier_id(GURL("https://example.com")); 337 NotifierId notifier_id(GURL("https://example.com"));
340 338
341 message_center()->AddNotification(std::unique_ptr<Notification>( 339 message_center()->AddNotification(std::unique_ptr<Notification>(
342 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"), 340 new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"),
343 UTF8ToUTF16("message"), gfx::Image() /* icon */, 341 UTF8ToUTF16("message"), gfx::Image() /* icon */,
344 base::string16() /* display_source */, GURL(), 342 base::string16() /* display_source */, GURL(),
345 notifier_id, RichNotificationData(), NULL))); 343 notifier_id, RichNotificationData(), NULL)));
346 344
347 std::unique_ptr<MockPopupTimersController> popup_timers_controller = 345 std::unique_ptr<MockPopupTimersController> popup_timers_controller =
348 base::MakeUnique<MockPopupTimersController>(message_center(), closure()); 346 base::MakeUnique<MockPopupTimersController>(message_center(), closure());
349 347
350 popup_timers_controller->OnNotificationDisplayed("id1", DISPLAY_SOURCE_POPUP); 348 popup_timers_controller->OnNotificationDisplayed("id1", DISPLAY_SOURCE_POPUP);
351 ASSERT_FALSE(popup_timers_controller->timer_finished()); 349 ASSERT_EQ(popup_timers_controller->timer_finished(), 0);
352 350
353 // Fast forward the |task_runner| by one second less than the auto-close timer 351 // Fast forward the |task_runner| by one second less than the auto-close timer
354 // frequency for Web Notifications. (As set by the |notifier_id|.) 352 // frequency for Web Notifications. (As set by the |notifier_id|.)
355 task_runner->FastForwardBy( 353 task_runner->FastForwardBy(
356 base::TimeDelta::FromSeconds(kAutocloseWebPageDelaySeconds - 1)); 354 base::TimeDelta::FromSeconds(kAutocloseWebPageDelaySeconds - 1));
357 ASSERT_FALSE(popup_timers_controller->timer_finished()); 355 ASSERT_EQ(popup_timers_controller->timer_finished(), 0);
358 356
359 // Trigger a replacement of the notification in the timer controller. 357 // Trigger a replacement of the notification in the timer controller.
360 popup_timers_controller->OnNotificationUpdated("id1"); 358 popup_timers_controller->OnNotificationUpdated("id1");
361 359
362 // Fast forward the |task_runner| by one second less than the auto-close timer 360 // 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. 361 // frequency for Web Notifications again. It should have been reset.
364 task_runner->FastForwardBy( 362 task_runner->FastForwardBy(
365 base::TimeDelta::FromSeconds(kAutocloseWebPageDelaySeconds - 1)); 363 base::TimeDelta::FromSeconds(kAutocloseWebPageDelaySeconds - 1));
366 ASSERT_FALSE(popup_timers_controller->timer_finished()); 364 ASSERT_EQ(popup_timers_controller->timer_finished(), 0);
367 365
368 // Now fast forward the |task_runner| by two seconds (to avoid flakiness), 366 // Now fast forward the |task_runner| by two seconds (to avoid flakiness),
369 // after which the timer should have fired. 367 // after which the timer should have fired.
370 task_runner->FastForwardBy(base::TimeDelta::FromSeconds(2)); 368 task_runner->FastForwardBy(base::TimeDelta::FromSeconds(2));
371 ASSERT_TRUE(popup_timers_controller->timer_finished()); 369 ASSERT_EQ(popup_timers_controller->timer_finished(), 1);
372 370
373 base::MessageLoop::current()->SetTaskRunner(old_task_runner); 371 base::MessageLoop::current()->SetTaskRunner(old_task_runner);
374 } 372 }
375 373
376 TEST_F(MessageCenterImplTest, NotificationBlocker) { 374 TEST_F(MessageCenterImplTest, NotificationBlocker) {
377 NotifierId notifier_id(NotifierId::APPLICATION, "app1"); 375 NotifierId notifier_id(NotifierId::APPLICATION, "app1");
378 // Multiple blockers to verify the case that one blocker blocks but another 376 // Multiple blockers to verify the case that one blocker blocks but another
379 // doesn't. 377 // doesn't.
380 ToggledNotificationBlocker blocker1(message_center()); 378 ToggledNotificationBlocker blocker1(message_center());
381 ToggledNotificationBlocker blocker2(message_center()); 379 ToggledNotificationBlocker blocker2(message_center());
(...skipping 880 matching lines...) Expand 10 before | Expand all | Expand 10 after
1262 // Then open the message center. 1260 // Then open the message center.
1263 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER); 1261 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
1264 1262
1265 // Then update a notification; the update should have propagated. 1263 // Then update a notification; the update should have propagated.
1266 message_center()->RemoveNotification(id, false); 1264 message_center()->RemoveNotification(id, false);
1267 EXPECT_FALSE(message_center()->FindVisibleNotificationById(id)); 1265 EXPECT_FALSE(message_center()->FindVisibleNotificationById(id));
1268 } 1266 }
1269 1267
1270 } // namespace internal 1268 } // namespace internal
1271 } // namespace message_center 1269 } // namespace message_center
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698