Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / extension_welcome_notification_unittest.cc
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.
4
5 #include "chrome/browser/notifications/extension_welcome_notification.h"
6
7 #include <string>
8
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/user_prefs/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"
25
26 const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh";
27
28 class MockMessageCenter : public message_center::FakeMessageCenter {
29  public:
30   MockMessageCenter()
31       : add_notification_calls_(0),
32         remove_notification_calls_(0),
33         notifications_with_shown_as_popup_(0) {
34   }
35
36   int add_notification_calls() { return add_notification_calls_; }
37   int remove_notification_calls() { return remove_notification_calls_; }
38   int notifications_with_shown_as_popup() {
39     return notifications_with_shown_as_popup_;
40   }
41
42   // message_center::FakeMessageCenter Overrides
43   virtual bool HasNotification(const std::string& id) OVERRIDE {
44     return last_notification.get() && (last_notification->id() == id);
45   }
46
47   virtual void AddNotification(
48       scoped_ptr<message_center::Notification> notification) OVERRIDE {
49     EXPECT_FALSE(last_notification.get());
50     last_notification.swap(notification);
51     add_notification_calls_++;
52     if (last_notification->shown_as_popup())
53       notifications_with_shown_as_popup_++;
54   }
55
56   virtual void RemoveNotification(const std::string& id,
57                                   bool by_user) OVERRIDE {
58     EXPECT_TRUE(last_notification.get());
59     last_notification.reset();
60     remove_notification_calls_++;
61   }
62
63   void CloseCurrentNotification() {
64     EXPECT_TRUE(last_notification.get());
65     last_notification->delegate()->Close(true);
66     RemoveNotification(last_notification->id(), true);
67   }
68
69  private:
70   scoped_ptr<message_center::Notification> last_notification;
71   int add_notification_calls_;
72   int remove_notification_calls_;
73   int notifications_with_shown_as_popup_;
74
75   DISALLOW_COPY_AND_ASSIGN(MockMessageCenter);
76 };
77
78 class WelcomeNotificationDelegate
79     : public ExtensionWelcomeNotification::Delegate {
80 public:
81   WelcomeNotificationDelegate()
82       : start_time_(base::Time::Now()),
83         message_center_(new MockMessageCenter()) {
84   }
85
86   // ExtensionWelcomeNotification::Delegate
87   virtual message_center::MessageCenter* GetMessageCenter() OVERRIDE {
88     return message_center_.get();
89   }
90
91   virtual base::Time GetCurrentTime() OVERRIDE {
92     return start_time_ + elapsed_time_;
93   }
94
95   virtual void PostTask(
96       const tracked_objects::Location& from_here,
97       const base::Closure& task) OVERRIDE {
98     EXPECT_TRUE(pending_task_.is_null());
99     pending_task_ = task;
100   }
101
102   // WelcomeNotificationDelegate
103   MockMessageCenter* message_center() const { return message_center_.get(); }
104
105   base::Time GetStartTime() const { return start_time_; }
106
107   void SetElapsedTime(base::TimeDelta elapsed_time) {
108     elapsed_time_ = elapsed_time;
109   }
110
111   void RunPendingTask() {
112     base::Closure task_to_run = pending_task_;
113     pending_task_.Reset();
114     task_to_run.Run();
115   }
116
117  private:
118   const base::Time start_time_;
119   base::TimeDelta elapsed_time_;
120   scoped_ptr<MockMessageCenter> message_center_;
121   base::Closure pending_task_;
122
123   DISALLOW_COPY_AND_ASSIGN(WelcomeNotificationDelegate);
124 };
125
126 class ExtensionWelcomeNotificationTest : public testing::Test {
127  protected:
128   ExtensionWelcomeNotificationTest() {
129     scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry(
130         new user_prefs::PrefRegistrySyncable());
131     ExtensionWelcomeNotification::RegisterProfilePrefs(pref_registry.get());
132   }
133
134   virtual void SetUp() {
135     task_runner_ = new base::TestSimpleTaskRunner();
136     thread_task_runner_handle_.reset(
137         new base::ThreadTaskRunnerHandle(task_runner_));
138     profile_.reset(new TestingProfile());
139     delegate_ = new WelcomeNotificationDelegate();
140     welcome_notification_ = ExtensionWelcomeNotification::Create(
141         kChromeNowExtensionID, profile_.get(), delegate_);
142   }
143
144   virtual void TearDown() {
145     delegate_ = NULL;
146     welcome_notification_.reset();
147     profile_.reset();
148     thread_task_runner_handle_.reset();
149     task_runner_ = NULL;
150   }
151
152   void StartPreferenceSyncing() const {
153     PrefServiceSyncable::FromProfile(profile_.get())
154         ->GetSyncableService(syncer::PREFERENCES)
155         ->MergeDataAndStartSyncing(syncer::PREFERENCES,
156                                    syncer::SyncDataList(),
157                                    scoped_ptr<syncer::SyncChangeProcessor>(
158                                        new syncer::FakeSyncChangeProcessor),
159                                    scoped_ptr<syncer::SyncErrorFactory>(
160                                        new syncer::SyncErrorFactoryMock()));
161   }
162
163   void ShowChromeNowNotification() const {
164     ShowNotification(
165         "ChromeNowNotification",
166         message_center::NotifierId(message_center::NotifierId::APPLICATION,
167                                    kChromeNowExtensionID));
168   }
169
170   void ShowRegularNotification() const {
171     ShowNotification(
172         "RegularNotification",
173         message_center::NotifierId(message_center::NotifierId::APPLICATION,
174                                    "aaaabbbbccccddddeeeeffffggghhhhi"));
175   }
176
177   void FlushMessageLoop() { delegate_->RunPendingTask(); }
178
179   MockMessageCenter* message_center() const {
180     return delegate_->message_center();
181   }
182   base::TestSimpleTaskRunner* task_runner() const {
183     return task_runner_.get();
184   }
185   base::Time GetStartTime() const {
186     return delegate_->GetStartTime();
187   }
188   void SetElapsedTime(base::TimeDelta elapsed_time) const {
189     delegate_->SetElapsedTime(elapsed_time);
190   }
191   bool GetBooleanPref(const char* path) const {
192     return profile_->GetPrefs()->GetBoolean(path);
193   }
194   void SetBooleanPref(const char* path, bool value) const {
195     profile_->GetPrefs()->SetBoolean(path, value);
196   }
197   int64 GetInt64Pref(const char* path) const {
198     return profile_->GetPrefs()->GetInt64(path);
199   }
200   void SetInt64Pref(const char* path, int64 value) const {
201     profile_->GetPrefs()->SetInt64(path, value);
202   }
203
204  private:
205   class TestNotificationDelegate : public NotificationDelegate {
206    public:
207     explicit TestNotificationDelegate(const std::string& id) : id_(id) {}
208
209     // Overridden from NotificationDelegate:
210     virtual void Display() OVERRIDE {}
211     virtual void Error() OVERRIDE {}
212     virtual void Close(bool by_user) OVERRIDE {}
213     virtual void Click() OVERRIDE {}
214     virtual void ButtonClick(int index) OVERRIDE {}
215
216     virtual std::string id() const OVERRIDE { return id_; }
217
218     virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
219       return NULL;
220     }
221
222    private:
223     virtual ~TestNotificationDelegate() {}
224
225     const std::string id_;
226
227     DISALLOW_COPY_AND_ASSIGN(TestNotificationDelegate);
228   };
229
230   void ShowNotification(std::string notification_id,
231                         const message_center::NotifierId& notifier_id) const {
232     message_center::RichNotificationData rich_notification_data;
233     rich_notification_data.priority = 0;
234     Notification notification(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
235                               GURL("http://tests.url"),
236                               base::UTF8ToUTF16("Title"),
237                               base::UTF8ToUTF16("Body"),
238                               gfx::Image(),
239                               blink::WebTextDirectionDefault,
240                               notifier_id,
241                               base::UTF8ToUTF16("Source"),
242                               base::UTF8ToUTF16(notification_id),
243                               rich_notification_data,
244                               new TestNotificationDelegate("TestNotification"));
245     welcome_notification_->ShowWelcomeNotificationIfNecessary(notification);
246   }
247
248   scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
249   scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
250   scoped_ptr<TestingProfile> profile_;
251   // Weak Ref owned by welcome_notification_
252   WelcomeNotificationDelegate* delegate_;
253   scoped_ptr<ExtensionWelcomeNotification> welcome_notification_;
254
255   DISALLOW_COPY_AND_ASSIGN(ExtensionWelcomeNotificationTest);
256 };
257
258 // Show a regular notification. Expect that WelcomeNotification will
259 // not show a welcome notification.
260 TEST_F(ExtensionWelcomeNotificationTest, FirstRunShowRegularNotification) {
261   StartPreferenceSyncing();
262   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
263   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
264   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
265
266   ShowRegularNotification();
267
268   EXPECT_EQ(message_center()->add_notification_calls(), 0);
269   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
270   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
271   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
272   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
273   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
274 }
275
276 // Show a Chrome Now notification. Expect that WelcomeNotification will
277 // show a welcome notification.
278 TEST_F(ExtensionWelcomeNotificationTest, FirstRunChromeNowNotification) {
279   StartPreferenceSyncing();
280   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
281   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
282   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
283
284   ShowChromeNowNotification();
285
286   EXPECT_EQ(message_center()->add_notification_calls(), 1);
287   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
288   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
289   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
290   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
291   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
292 }
293
294 // Show a Chrome Now notification that was already shown before.
295 TEST_F(ExtensionWelcomeNotificationTest, ShowWelcomeNotificationAgain) {
296   StartPreferenceSyncing();
297   SetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp, true);
298   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
299   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
300   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
301
302   ShowChromeNowNotification();
303
304   EXPECT_EQ(message_center()->add_notification_calls(), 1);
305   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
306   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 1);
307   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
308   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
309   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
310 }
311
312 // Don't show a welcome notification if it was previously dismissed on another
313 // machine that wrote the synced flag.
314 TEST_F(ExtensionWelcomeNotificationTest,
315        WelcomeNotificationPreviouslyDismissed) {
316   StartPreferenceSyncing();
317   SetBooleanPref(prefs::kWelcomeNotificationDismissed, true);
318   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
319   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
320   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
321
322   ShowChromeNowNotification();
323
324   EXPECT_EQ(message_center()->add_notification_calls(), 0);
325   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
326   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
327   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
328   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
329   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
330 }
331
332 // Don't show a welcome notification if it was previously dismissed on this
333 // machine.
334 TEST_F(ExtensionWelcomeNotificationTest,
335        WelcomeNotificationPreviouslyDismissedLocal) {
336   StartPreferenceSyncing();
337   SetBooleanPref(prefs::kWelcomeNotificationDismissedLocal, true);
338   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
339   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
340   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
341
342   ShowChromeNowNotification();
343
344   EXPECT_EQ(message_center()->add_notification_calls(), 0);
345   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
346   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
347   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
348   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
349   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
350 }
351
352 // Don't show a welcome notification if it was previously dismissed with the
353 // local flag and synced flag. This case is possible but rare.
354 TEST_F(ExtensionWelcomeNotificationTest,
355        WelcomeNotificationPreviouslyDismissedSyncedAndLocal) {
356   StartPreferenceSyncing();
357   SetBooleanPref(prefs::kWelcomeNotificationDismissed, true);
358   SetBooleanPref(prefs::kWelcomeNotificationDismissedLocal, true);
359   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
360   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
361   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
362
363   ShowChromeNowNotification();
364
365   EXPECT_EQ(message_center()->add_notification_calls(), 0);
366   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
367   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
368   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
369   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
370   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
371 }
372
373 // Show a Chrome Now notification and dismiss it.
374 // Expect welcome toast dismissed to be true.
375 TEST_F(ExtensionWelcomeNotificationTest, DismissWelcomeNotification) {
376   StartPreferenceSyncing();
377   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
378   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
379   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
380
381   ShowChromeNowNotification();
382   message_center()->CloseCurrentNotification();
383   FlushMessageLoop();
384
385   EXPECT_EQ(message_center()->add_notification_calls(), 1);
386   EXPECT_EQ(message_center()->remove_notification_calls(), 1);
387   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
388   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
389   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
390   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
391 }
392
393 // Show a Chrome Now notification and dismiss it via a synced preference change.
394 // Expect welcome toast dismissed to be true.
395 TEST_F(ExtensionWelcomeNotificationTest, SyncedDismissalWelcomeNotification) {
396   StartPreferenceSyncing();
397   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
398   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
399   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
400
401   ShowChromeNowNotification();
402   SetBooleanPref(prefs::kWelcomeNotificationDismissed, true);
403
404   EXPECT_EQ(message_center()->add_notification_calls(), 1);
405   EXPECT_EQ(message_center()->remove_notification_calls(), 1);
406   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
407   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
408   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
409   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
410 }
411
412 // Simulate a delayed preference sync when the welcome notification was
413 // previously dismissed.
414 TEST_F(ExtensionWelcomeNotificationTest,
415        DelayedPreferenceSyncPreviouslyDismissed) {
416   // Show a notification while the preference system is not syncing.
417   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
418   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
419   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
420
421   ShowChromeNowNotification();
422
423   EXPECT_EQ(message_center()->add_notification_calls(), 0);
424   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
425   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
426   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
427   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
428   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
429
430   // Now start the preference syncing with a previously dismissed welcome.
431   SetBooleanPref(prefs::kWelcomeNotificationDismissed, true);
432   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
433   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
434   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
435
436   StartPreferenceSyncing();
437
438   EXPECT_EQ(message_center()->add_notification_calls(), 0);
439   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
440   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
441   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
442   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
443   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
444 }
445
446 // Simulate a delayed preference sync when the welcome notification was
447 // never shown.
448 TEST_F(ExtensionWelcomeNotificationTest, DelayedPreferenceSyncNeverShown) {
449   // Show a notification while the preference system is not syncing.
450   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
451   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
452   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
453
454   ShowChromeNowNotification();
455
456   EXPECT_EQ(message_center()->add_notification_calls(), 0);
457   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
458   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
459   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
460   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
461   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
462
463   // Now start the preference syncing with the default preference values.
464   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
465   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
466   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
467
468   StartPreferenceSyncing();
469
470   EXPECT_EQ(message_center()->add_notification_calls(), 1);
471   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
472   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
473   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
474   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
475   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
476 }
477
478 // Simulate the passage of time when the welcome notification
479 // automatically dismisses.
480 TEST_F(ExtensionWelcomeNotificationTest, TimeExpiredNotification) {
481   StartPreferenceSyncing();
482   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
483   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
484   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
485   EXPECT_EQ(GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp), 0);
486   EXPECT_TRUE(task_runner()->GetPendingTasks().empty());
487
488   ShowChromeNowNotification();
489
490   base::TimeDelta requested_show_time =
491       base::TimeDelta::FromDays(
492           ExtensionWelcomeNotification::kRequestedShowTimeDays);
493
494   EXPECT_EQ(task_runner()->GetPendingTasks().size(), 1U);
495   EXPECT_EQ(task_runner()->NextPendingTaskDelay(), requested_show_time);
496
497   EXPECT_EQ(message_center()->add_notification_calls(), 1);
498   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
499   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
500   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
501   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
502   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
503   EXPECT_EQ(
504       GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp),
505       (GetStartTime() + requested_show_time).ToInternalValue());
506
507   SetElapsedTime(requested_show_time);
508   task_runner()->RunPendingTasks();
509
510   EXPECT_TRUE(task_runner()->GetPendingTasks().empty());
511   EXPECT_EQ(message_center()->add_notification_calls(), 1);
512   EXPECT_EQ(message_center()->remove_notification_calls(), 1);
513   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
514   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
515   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
516   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
517   EXPECT_EQ(
518       GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp),
519       (GetStartTime() + requested_show_time).ToInternalValue());
520 }
521
522 // Simulate the passage of time after Chrome is closed and the welcome
523 // notification expiration elapses.
524 TEST_F(ExtensionWelcomeNotificationTest, NotificationPreviouslyExpired) {
525   StartPreferenceSyncing();
526   SetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp, true);
527   SetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp, 1);
528   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
529   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
530   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
531   EXPECT_EQ(GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp), 1);
532   EXPECT_TRUE(task_runner()->GetPendingTasks().empty());
533
534   const base::TimeDelta requested_show_time =
535       base::TimeDelta::FromDays(
536           ExtensionWelcomeNotification::kRequestedShowTimeDays);
537   SetElapsedTime(requested_show_time);
538   ShowChromeNowNotification();
539
540   EXPECT_TRUE(task_runner()->GetPendingTasks().empty());
541   EXPECT_EQ(message_center()->add_notification_calls(), 0);
542   EXPECT_EQ(message_center()->remove_notification_calls(), 0);
543   EXPECT_EQ(message_center()->notifications_with_shown_as_popup(), 0);
544   EXPECT_FALSE(GetBooleanPref(prefs::kWelcomeNotificationDismissed));
545   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationDismissedLocal));
546   EXPECT_TRUE(GetBooleanPref(prefs::kWelcomeNotificationPreviouslyPoppedUp));
547   EXPECT_EQ(GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp), 1);
548 }
549
550 // C++ Readability Review Change Trigger