1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/notifications/extension_welcome_notification.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/test_simple_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/notifications/notification.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_pref_service_syncable.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/pref_registry/pref_registry_syncable.h"
20 #include "sync/api/fake_sync_change_processor.h"
21 #include "sync/api/sync_error_factory_mock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/message_center/fake_message_center.h"
24 #include "ui/message_center/notification.h"
26 class MockMessageCenter : public message_center::FakeMessageCenter {
29 : add_notification_calls_(0),
30 remove_notification_calls_(0),
31 notifications_with_shown_as_popup_(0) {
34 int add_notification_calls() { return add_notification_calls_; }
35 int remove_notification_calls() { return remove_notification_calls_; }
36 int notifications_with_shown_as_popup() {
37 return notifications_with_shown_as_popup_;
40 // message_center::FakeMessageCenter Overrides
41 virtual message_center::Notification* FindVisibleNotificationById(
42 const std::string& id) OVERRIDE {
43 if (last_notification.get() && last_notification->id() == id)
44 return last_notification.get();
48 virtual void AddNotification(
49 scoped_ptr<message_center::Notification> notification) OVERRIDE {
50 EXPECT_FALSE(last_notification.get());
51 last_notification.swap(notification);
52 add_notification_calls_++;
53 if (last_notification->shown_as_popup())
54 notifications_with_shown_as_popup_++;
57 virtual void RemoveNotification(const std::string& id,
58 bool by_user) OVERRIDE {
59 EXPECT_TRUE(last_notification.get());
60 last_notification.reset();
61 remove_notification_calls_++;
64 void CloseCurrentNotification() {
65 EXPECT_TRUE(last_notification.get());
66 last_notification->delegate()->Close(true);
67 RemoveNotification(last_notification->id(), true);
71 scoped_ptr<message_center::Notification> last_notification;
72 int add_notification_calls_;
73 int remove_notification_calls_;
74 int notifications_with_shown_as_popup_;
76 DISALLOW_COPY_AND_ASSIGN(MockMessageCenter);
79 class WelcomeNotificationDelegate
80 : public ExtensionWelcomeNotification::Delegate {
82 WelcomeNotificationDelegate()
83 : start_time_(base::Time::Now()),
84 message_center_(new MockMessageCenter()) {
87 // ExtensionWelcomeNotification::Delegate
88 virtual message_center::MessageCenter* GetMessageCenter() OVERRIDE {
89 return message_center_.get();
92 virtual base::Time GetCurrentTime() OVERRIDE {
93 return start_time_ + elapsed_time_;
96 virtual void PostTask(
97 const tracked_objects::Location& from_here,
98 const base::Closure& task) OVERRIDE {
99 EXPECT_TRUE(pending_task_.is_null());
100 pending_task_ = task;
103 // WelcomeNotificationDelegate
104 MockMessageCenter* message_center() const { return message_center_.get(); }
106 base::Time GetStartTime() const { return start_time_; }
108 void SetElapsedTime(base::TimeDelta elapsed_time) {
109 elapsed_time_ = elapsed_time;
112 void RunPendingTask() {
113 base::Closure task_to_run = pending_task_;
114 pending_task_.Reset();
119 const base::Time start_time_;
120 base::TimeDelta elapsed_time_;
121 scoped_ptr<MockMessageCenter> message_center_;
122 base::Closure pending_task_;
124 DISALLOW_COPY_AND_ASSIGN(WelcomeNotificationDelegate);
127 class ExtensionWelcomeNotificationTest : public testing::Test {
129 ExtensionWelcomeNotificationTest() {
130 scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry(
131 new user_prefs::PrefRegistrySyncable());
132 ExtensionWelcomeNotification::RegisterProfilePrefs(pref_registry.get());
135 virtual void SetUp() {
136 task_runner_ = new base::TestSimpleTaskRunner();
137 thread_task_runner_handle_.reset(
138 new base::ThreadTaskRunnerHandle(task_runner_));
139 profile_.reset(new TestingProfile());
140 delegate_ = new WelcomeNotificationDelegate();
141 welcome_notification_.reset(
142 ExtensionWelcomeNotification::Create(profile_.get(), delegate_));
145 virtual void TearDown() {
147 welcome_notification_.reset();
149 thread_task_runner_handle_.reset();
153 void StartPreferenceSyncing() const {
154 PrefServiceSyncable::FromProfile(profile_.get())
155 ->GetSyncableService(syncer::PREFERENCES)
156 ->MergeDataAndStartSyncing(syncer::PREFERENCES,
157 syncer::SyncDataList(),
158 scoped_ptr<syncer::SyncChangeProcessor>(
159 new syncer::FakeSyncChangeProcessor),
160 scoped_ptr<syncer::SyncErrorFactory>(
161 new syncer::SyncErrorFactoryMock()));
164 void ShowChromeNowNotification() const {
166 "ChromeNowNotification",
167 message_center::NotifierId(
168 message_center::NotifierId::APPLICATION,
169 ExtensionWelcomeNotification::kChromeNowExtensionID));
172 void ShowRegularNotification() const {
174 "RegularNotification",
175 message_center::NotifierId(message_center::NotifierId::APPLICATION,
176 "aaaabbbbccccddddeeeeffffggghhhhi"));
179 void FlushMessageLoop() { delegate_->RunPendingTask(); }
181 MockMessageCenter* message_center() const {
182 return delegate_->message_center();
184 base::TestSimpleTaskRunner* task_runner() const {
185 return task_runner_.get();
187 base::Time GetStartTime() const {
188 return delegate_->GetStartTime();
190 void SetElapsedTime(base::TimeDelta elapsed_time) const {
191 delegate_->SetElapsedTime(elapsed_time);
193 bool GetBooleanPref(const char* path) const {
194 return profile_->GetPrefs()->GetBoolean(path);
196 void SetBooleanPref(const char* path, bool value) const {
197 profile_->GetPrefs()->SetBoolean(path, value);
199 int64 GetInt64Pref(const char* path) const {
200 return profile_->GetPrefs()->GetInt64(path);
202 void SetInt64Pref(const char* path, int64 value) const {
203 profile_->GetPrefs()->SetInt64(path, value);
207 class TestNotificationDelegate : public NotificationDelegate {
209 explicit TestNotificationDelegate(const std::string& id) : id_(id) {}
211 // Overridden from NotificationDelegate:
212 virtual void Display() OVERRIDE {}
213 virtual void Error() OVERRIDE {}
214 virtual void Close(bool by_user) OVERRIDE {}
215 virtual void Click() OVERRIDE {}
216 virtual void ButtonClick(int index) OVERRIDE {}
218 virtual std::string id() const OVERRIDE { return id_; }
220 virtual content::WebContents* GetWebContents() const OVERRIDE {
225 virtual ~TestNotificationDelegate() {}
227 const std::string id_;
229 DISALLOW_COPY_AND_ASSIGN(TestNotificationDelegate);
232 void ShowNotification(std::string notification_id,
233 const message_center::NotifierId& notifier_id) const {
234 message_center::RichNotificationData rich_notification_data;
235 rich_notification_data.priority = 0;
236 Notification notification(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
237 GURL("http://tests.url"),
238 base::UTF8ToUTF16("Title"),
239 base::UTF8ToUTF16("Body"),
241 blink::WebTextDirectionDefault,
243 base::UTF8ToUTF16("Source"),
244 base::UTF8ToUTF16(notification_id),
245 rich_notification_data,
246 new TestNotificationDelegate("TestNotification"));
247 welcome_notification_->ShowWelcomeNotificationIfNecessary(notification);
250 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
251 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
252 scoped_ptr<TestingProfile> profile_;
253 // Weak Ref owned by welcome_notification_
254 WelcomeNotificationDelegate* delegate_;
255 scoped_ptr<ExtensionWelcomeNotification> welcome_notification_;
257 DISALLOW_COPY_AND_ASSIGN(ExtensionWelcomeNotificationTest);
260 // Show a regular notification. Expect that WelcomeNotification will
261 // not show a welcome notification.
262 TEST_F(ExtensionWelcomeNotificationTest, FirstRunShowRegularNotification) {
263 StartPreferenceSyncing();
264 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
265 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
266 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
268 ShowRegularNotification();
270 EXPECT_EQ(message_center()->add_notification_calls(), 0);
271 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
272 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
273 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
274 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
275 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
278 // Show a Chrome Now notification. Expect that WelcomeNotification will
279 // show a welcome notification.
280 TEST_F(ExtensionWelcomeNotificationTest, FirstRunChromeNowNotification) {
281 StartPreferenceSyncing();
282 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
283 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
284 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
286 ShowChromeNowNotification();
288 EXPECT_EQ(message_center()->add_notification_calls(), 1);
289 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
290 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
291 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
292 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
293 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
296 // Show a Chrome Now notification that was already shown before.
297 TEST_F(ExtensionWelcomeNotificationTest, ShowWelcomeNotificationAgain) {
298 StartPreferenceSyncing();
299 SetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp, true);
300 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
301 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
302 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
304 ShowChromeNowNotification();
306 EXPECT_EQ(message_center()->add_notification_calls(), 1);
307 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
308 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 1);
309 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
310 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
311 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
314 // Don't show a welcome notification if it was previously dismissed on another
315 // machine that wrote the synced flag.
316 TEST_F(ExtensionWelcomeNotificationTest,
317 WelcomeNotificationPreviouslyDismissed) {
318 StartPreferenceSyncing();
319 SetBooleanPref(prefs::kWelcomeNotificationDismissed, true);
320 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
321 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
322 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
324 ShowChromeNowNotification();
326 EXPECT_EQ(message_center()->add_notification_calls(), 0);
327 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
328 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
329 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
330 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
331 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
334 // Don't show a welcome notification if it was previously dismissed on this
336 TEST_F(ExtensionWelcomeNotificationTest,
337 WelcomeNotificationPreviouslyDismissedLocal) {
338 StartPreferenceSyncing();
339 SetBooleanPref(prefs::kWelcomeNotificationDismissedLocal, true);
340 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
341 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
342 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
344 ShowChromeNowNotification();
346 EXPECT_EQ(message_center()->add_notification_calls(), 0);
347 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
348 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
349 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
350 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
351 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
354 // Don't show a welcome notification if it was previously dismissed with the
355 // local flag and synced flag. This case is possible but rare.
356 TEST_F(ExtensionWelcomeNotificationTest,
357 WelcomeNotificationPreviouslyDismissedSyncedAndLocal) {
358 StartPreferenceSyncing();
359 SetBooleanPref(prefs::kWelcomeNotificationDismissed, true);
360 SetBooleanPref(prefs::kWelcomeNotificationDismissedLocal, true);
361 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
362 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
363 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
365 ShowChromeNowNotification();
367 EXPECT_EQ(message_center()->add_notification_calls(), 0);
368 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
369 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
370 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
371 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
372 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
375 // Show a Chrome Now notification and dismiss it.
376 // Expect welcome toast dismissed to be true.
377 TEST_F(ExtensionWelcomeNotificationTest, DismissWelcomeNotification) {
378 StartPreferenceSyncing();
379 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
380 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
381 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
383 ShowChromeNowNotification();
384 message_center()->CloseCurrentNotification();
387 EXPECT_EQ(message_center()->add_notification_calls(), 1);
388 EXPECT_EQ(message_center()->remove_notification_calls(), 1);
389 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
390 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
391 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
392 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
395 // Show a Chrome Now notification and dismiss it via a synced preference change.
396 // Expect welcome toast dismissed to be true.
397 TEST_F(ExtensionWelcomeNotificationTest, SyncedDismissalWelcomeNotification) {
398 StartPreferenceSyncing();
399 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
400 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
401 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
403 ShowChromeNowNotification();
404 SetBooleanPref(prefs::kWelcomeNotificationDismissed, true);
406 EXPECT_EQ(message_center()->add_notification_calls(), 1);
407 EXPECT_EQ(message_center()->remove_notification_calls(), 1);
408 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
409 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
410 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
411 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
414 // Simulate a delayed preference sync when the welcome notification was
415 // previously dismissed.
416 TEST_F(ExtensionWelcomeNotificationTest,
417 DelayedPreferenceSyncPreviouslyDismissed) {
418 // Show a notification while the preference system is not syncing.
419 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
420 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
421 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
423 ShowChromeNowNotification();
425 EXPECT_EQ(message_center()->add_notification_calls(), 0);
426 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
427 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
428 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
429 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
430 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
432 // Now start the preference syncing with a previously dismissed welcome.
433 SetBooleanPref(prefs::kWelcomeNotificationDismissed, true);
434 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
435 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
436 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
438 StartPreferenceSyncing();
440 EXPECT_EQ(message_center()->add_notification_calls(), 0);
441 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
442 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
443 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
444 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
445 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
448 // Simulate a delayed preference sync when the welcome notification was
450 TEST_F(ExtensionWelcomeNotificationTest, DelayedPreferenceSyncNeverShown) {
451 // Show a notification while the preference system is not syncing.
452 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
453 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
454 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
456 ShowChromeNowNotification();
458 EXPECT_EQ(message_center()->add_notification_calls(), 0);
459 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
460 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
461 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
462 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
463 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
465 // Now start the preference syncing with the default preference values.
466 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
467 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
468 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
470 StartPreferenceSyncing();
472 EXPECT_EQ(message_center()->add_notification_calls(), 1);
473 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
474 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
475 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
476 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
477 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
480 // Simulate the passage of time when the welcome notification
481 // automatically dismisses.
482 TEST_F(ExtensionWelcomeNotificationTest, TimeExpiredNotification) {
483 StartPreferenceSyncing();
484 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
485 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
486 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
487 EXPECT_EQ(GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp), 0);
488 EXPECT_TRUE(task_runner()->GetPendingTasks().empty());
490 ShowChromeNowNotification();
492 base::TimeDelta requested_show_time =
493 base::TimeDelta::FromDays(
494 ExtensionWelcomeNotification::kRequestedShowTimeDays);
496 EXPECT_EQ(task_runner()->GetPendingTasks().size(), 1U);
497 EXPECT_EQ(task_runner()->NextPendingTaskDelay(), requested_show_time);
499 EXPECT_EQ(message_center()->add_notification_calls(), 1);
500 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
501 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
502 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
503 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
504 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
506 GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp),
507 (GetStartTime() + requested_show_time).ToInternalValue());
509 SetElapsedTime(requested_show_time);
510 task_runner()->RunPendingTasks();
512 EXPECT_TRUE(task_runner()->GetPendingTasks().empty());
513 EXPECT_EQ(message_center()->add_notification_calls(), 1);
514 EXPECT_EQ(message_center()->remove_notification_calls(), 1);
515 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
516 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
517 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
518 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
520 GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp),
521 (GetStartTime() + requested_show_time).ToInternalValue());
524 // Simulate the passage of time after Chrome is closed and the welcome
525 // notification expiration elapses.
526 TEST_F(ExtensionWelcomeNotificationTest, NotificationPreviouslyExpired) {
527 StartPreferenceSyncing();
528 SetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp, true);
529 SetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp, 1);
530 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
531 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
532 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
533 EXPECT_EQ(GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp), 1);
534 EXPECT_TRUE(task_runner()->GetPendingTasks().empty());
536 const base::TimeDelta requested_show_time =
537 base::TimeDelta::FromDays(
538 ExtensionWelcomeNotification::kRequestedShowTimeDays);
539 SetElapsedTime(requested_show_time);
540 ShowChromeNowNotification();
542 EXPECT_TRUE(task_runner()->GetPendingTasks().empty());
543 EXPECT_EQ(message_center()->add_notification_calls(), 0);
544 EXPECT_EQ(message_center()->remove_notification_calls(), 0);
545 EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
546 EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
547 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
548 EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
549 EXPECT_EQ(GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp), 1);