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