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:
MockMessageCenter()28 MockMessageCenter()
29 : add_notification_calls_(0),
30 remove_notification_calls_(0),
31 notifications_with_shown_as_popup_(0) {
32 }
33
add_notification_calls()34 int add_notification_calls() { return add_notification_calls_; }
remove_notification_calls()35 int remove_notification_calls() { return remove_notification_calls_; }
notifications_with_shown_as_popup()36 int notifications_with_shown_as_popup() {
37 return notifications_with_shown_as_popup_;
38 }
39
40 // message_center::FakeMessageCenter Overrides
FindVisibleNotificationById(const std::string & id)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();
45 return NULL;
46 }
47
AddNotification(scoped_ptr<message_center::Notification> notification)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_++;
55 }
56
RemoveNotification(const std::string & id,bool by_user)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_++;
62 }
63
CloseCurrentNotification()64 void CloseCurrentNotification() {
65 EXPECT_TRUE(last_notification.get());
66 last_notification->delegate()->Close(true);
67 RemoveNotification(last_notification->id(), true);
68 }
69
70 private:
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_;
75
76 DISALLOW_COPY_AND_ASSIGN(MockMessageCenter);
77 };
78
79 class WelcomeNotificationDelegate
80 : public ExtensionWelcomeNotification::Delegate {
81 public:
WelcomeNotificationDelegate()82 WelcomeNotificationDelegate()
83 : start_time_(base::Time::Now()),
84 message_center_(new MockMessageCenter()) {
85 }
86
87 // ExtensionWelcomeNotification::Delegate
GetMessageCenter()88 virtual message_center::MessageCenter* GetMessageCenter() OVERRIDE {
89 return message_center_.get();
90 }
91
GetCurrentTime()92 virtual base::Time GetCurrentTime() OVERRIDE {
93 return start_time_ + elapsed_time_;
94 }
95
PostTask(const tracked_objects::Location & from_here,const base::Closure & task)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;
101 }
102
103 // WelcomeNotificationDelegate
message_center() const104 MockMessageCenter* message_center() const { return message_center_.get(); }
105
GetStartTime() const106 base::Time GetStartTime() const { return start_time_; }
107
SetElapsedTime(base::TimeDelta elapsed_time)108 void SetElapsedTime(base::TimeDelta elapsed_time) {
109 elapsed_time_ = elapsed_time;
110 }
111
RunPendingTask()112 void RunPendingTask() {
113 base::Closure task_to_run = pending_task_;
114 pending_task_.Reset();
115 task_to_run.Run();
116 }
117
118 private:
119 const base::Time start_time_;
120 base::TimeDelta elapsed_time_;
121 scoped_ptr<MockMessageCenter> message_center_;
122 base::Closure pending_task_;
123
124 DISALLOW_COPY_AND_ASSIGN(WelcomeNotificationDelegate);
125 };
126
127 class ExtensionWelcomeNotificationTest : public testing::Test {
128 protected:
ExtensionWelcomeNotificationTest()129 ExtensionWelcomeNotificationTest() {
130 scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry(
131 new user_prefs::PrefRegistrySyncable());
132 ExtensionWelcomeNotification::RegisterProfilePrefs(pref_registry.get());
133 }
134
SetUp()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_));
143 }
144
TearDown()145 virtual void TearDown() {
146 delegate_ = NULL;
147 welcome_notification_.reset();
148 profile_.reset();
149 thread_task_runner_handle_.reset();
150 task_runner_ = NULL;
151 }
152
StartPreferenceSyncing() const153 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()));
162 }
163
ShowChromeNowNotification() const164 void ShowChromeNowNotification() const {
165 ShowNotification(
166 "ChromeNowNotification",
167 message_center::NotifierId(
168 message_center::NotifierId::APPLICATION,
169 ExtensionWelcomeNotification::kChromeNowExtensionID));
170 }
171
ShowRegularNotification() const172 void ShowRegularNotification() const {
173 ShowNotification(
174 "RegularNotification",
175 message_center::NotifierId(message_center::NotifierId::APPLICATION,
176 "aaaabbbbccccddddeeeeffffggghhhhi"));
177 }
178
FlushMessageLoop()179 void FlushMessageLoop() { delegate_->RunPendingTask(); }
180
message_center() const181 MockMessageCenter* message_center() const {
182 return delegate_->message_center();
183 }
task_runner() const184 base::TestSimpleTaskRunner* task_runner() const {
185 return task_runner_.get();
186 }
GetStartTime() const187 base::Time GetStartTime() const {
188 return delegate_->GetStartTime();
189 }
SetElapsedTime(base::TimeDelta elapsed_time) const190 void SetElapsedTime(base::TimeDelta elapsed_time) const {
191 delegate_->SetElapsedTime(elapsed_time);
192 }
GetBooleanPref(const char * path) const193 bool GetBooleanPref(const char* path) const {
194 return profile_->GetPrefs()->GetBoolean(path);
195 }
SetBooleanPref(const char * path,bool value) const196 void SetBooleanPref(const char* path, bool value) const {
197 profile_->GetPrefs()->SetBoolean(path, value);
198 }
GetInt64Pref(const char * path) const199 int64 GetInt64Pref(const char* path) const {
200 return profile_->GetPrefs()->GetInt64(path);
201 }
SetInt64Pref(const char * path,int64 value) const202 void SetInt64Pref(const char* path, int64 value) const {
203 profile_->GetPrefs()->SetInt64(path, value);
204 }
205
206 private:
207 class TestNotificationDelegate : public NotificationDelegate {
208 public:
TestNotificationDelegate(const std::string & id)209 explicit TestNotificationDelegate(const std::string& id) : id_(id) {}
210
211 // Overridden from NotificationDelegate:
Display()212 virtual void Display() OVERRIDE {}
Error()213 virtual void Error() OVERRIDE {}
Close(bool by_user)214 virtual void Close(bool by_user) OVERRIDE {}
Click()215 virtual void Click() OVERRIDE {}
ButtonClick(int index)216 virtual void ButtonClick(int index) OVERRIDE {}
217
id() const218 virtual std::string id() const OVERRIDE { return id_; }
219
GetWebContents() const220 virtual content::WebContents* GetWebContents() const OVERRIDE {
221 return NULL;
222 }
223
224 private:
~TestNotificationDelegate()225 virtual ~TestNotificationDelegate() {}
226
227 const std::string id_;
228
229 DISALLOW_COPY_AND_ASSIGN(TestNotificationDelegate);
230 };
231
ShowNotification(std::string notification_id,const message_center::NotifierId & notifier_id) const232 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"),
240 gfx::Image(),
241 blink::WebTextDirectionDefault,
242 notifier_id,
243 base::UTF8ToUTF16("Source"),
244 base::UTF8ToUTF16(notification_id),
245 rich_notification_data,
246 new TestNotificationDelegate("TestNotification"));
247 welcome_notification_->ShowWelcomeNotificationIfNecessary(notification);
248 }
249
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_;
256
257 DISALLOW_COPY_AND_ASSIGN(ExtensionWelcomeNotificationTest);
258 };
259
260 // Show a regular notification. Expect that WelcomeNotification will
261 // not show a welcome notification.
TEST_F(ExtensionWelcomeNotificationTest,FirstRunShowRegularNotification)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));
267
268 ShowRegularNotification();
269
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));
276 }
277
278 // Show a Chrome Now notification. Expect that WelcomeNotification will
279 // show a welcome notification.
TEST_F(ExtensionWelcomeNotificationTest,FirstRunChromeNowNotification)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));
285
286 ShowChromeNowNotification();
287
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));
294 }
295
296 // Show a Chrome Now notification that was already shown before.
TEST_F(ExtensionWelcomeNotificationTest,ShowWelcomeNotificationAgain)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));
303
304 ShowChromeNowNotification();
305
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));
312 }
313
314 // Don't show a welcome notification if it was previously dismissed on another
315 // machine that wrote the synced flag.
TEST_F(ExtensionWelcomeNotificationTest,WelcomeNotificationPreviouslyDismissed)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));
323
324 ShowChromeNowNotification();
325
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));
332 }
333
334 // Don't show a welcome notification if it was previously dismissed on this
335 // machine.
TEST_F(ExtensionWelcomeNotificationTest,WelcomeNotificationPreviouslyDismissedLocal)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));
343
344 ShowChromeNowNotification();
345
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));
352 }
353
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.
TEST_F(ExtensionWelcomeNotificationTest,WelcomeNotificationPreviouslyDismissedSyncedAndLocal)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));
364
365 ShowChromeNowNotification();
366
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));
373 }
374
375 // Show a Chrome Now notification and dismiss it.
376 // Expect welcome toast dismissed to be true.
TEST_F(ExtensionWelcomeNotificationTest,DismissWelcomeNotification)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));
382
383 ShowChromeNowNotification();
384 message_center()->CloseCurrentNotification();
385 FlushMessageLoop();
386
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));
393 }
394
395 // Show a Chrome Now notification and dismiss it via a synced preference change.
396 // Expect welcome toast dismissed to be true.
TEST_F(ExtensionWelcomeNotificationTest,SyncedDismissalWelcomeNotification)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));
402
403 ShowChromeNowNotification();
404 SetBooleanPref(prefs::kWelcomeNotificationDismissed, true);
405
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));
412 }
413
414 // Simulate a delayed preference sync when the welcome notification was
415 // previously dismissed.
TEST_F(ExtensionWelcomeNotificationTest,DelayedPreferenceSyncPreviouslyDismissed)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));
422
423 ShowChromeNowNotification();
424
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));
431
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));
437
438 StartPreferenceSyncing();
439
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));
446 }
447
448 // Simulate a delayed preference sync when the welcome notification was
449 // never shown.
TEST_F(ExtensionWelcomeNotificationTest,DelayedPreferenceSyncNeverShown)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));
455
456 ShowChromeNowNotification();
457
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));
464
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));
469
470 StartPreferenceSyncing();
471
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));
478 }
479
480 // Simulate the passage of time when the welcome notification
481 // automatically dismisses.
TEST_F(ExtensionWelcomeNotificationTest,TimeExpiredNotification)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());
489
490 ShowChromeNowNotification();
491
492 base::TimeDelta requested_show_time =
493 base::TimeDelta::FromDays(
494 ExtensionWelcomeNotification::kRequestedShowTimeDays);
495
496 EXPECT_EQ(task_runner()->GetPendingTasks().size(), 1U);
497 EXPECT_EQ(task_runner()->NextPendingTaskDelay(), requested_show_time);
498
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));
505 EXPECT_EQ(
506 GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp),
507 (GetStartTime() + requested_show_time).ToInternalValue());
508
509 SetElapsedTime(requested_show_time);
510 task_runner()->RunPendingTasks();
511
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));
519 EXPECT_EQ(
520 GetInt64Pref(prefs::kWelcomeNotificationExpirationTimestamp),
521 (GetStartTime() + requested_show_time).ToInternalValue());
522 }
523
524 // Simulate the passage of time after Chrome is closed and the welcome
525 // notification expiration elapses.
TEST_F(ExtensionWelcomeNotificationTest,NotificationPreviouslyExpired)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());
535
536 const base::TimeDelta requested_show_time =
537 base::TimeDelta::FromDays(
538 ExtensionWelcomeNotification::kRequestedShowTimeDays);
539 SetElapsedTime(requested_show_time);
540 ShowChromeNowNotification();
541
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);
550 }
551