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