1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/notifications/welcome_notification.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/notifications/notification.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_pref_service_syncable.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/user_prefs/pref_registry_syncable.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/message_center/fake_message_center.h"
20 #include "ui/message_center/notification.h"
22 const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh";
24 class MockMessageCenter : public message_center::FakeMessageCenter {
27 : add_notification_calls_(0),
28 remove_notification_calls_(0),
29 notifications_with_shown_as_popup_(0) {};
31 int add_notification_calls() { return add_notification_calls_; }
32 int remove_notification_calls() { return remove_notification_calls_; }
33 int notifications_with_shown_as_popup() {
34 return notifications_with_shown_as_popup_;
37 // message_center::FakeMessageCenter Overrides
38 virtual bool HasNotification(const std::string& id) OVERRIDE {
39 return last_notification.get() &&
40 (last_notification->id() == id);
43 virtual void AddNotification(
44 scoped_ptr<message_center::Notification> notification) OVERRIDE {
45 EXPECT_FALSE(last_notification.get());
46 last_notification.swap(notification);
47 add_notification_calls_++;
48 if (last_notification->shown_as_popup())
49 notifications_with_shown_as_popup_++;
52 virtual void RemoveNotification(const std::string& id, bool by_user)
54 EXPECT_TRUE(last_notification.get());
55 last_notification.reset();
56 remove_notification_calls_++;
59 void CloseCurrentNotification() {
60 EXPECT_TRUE(last_notification.get());
61 last_notification->delegate()->Close(true);
62 RemoveNotification(last_notification->id(), true);
66 scoped_ptr<message_center::Notification> last_notification;
67 int add_notification_calls_;
68 int remove_notification_calls_;
69 int notifications_with_shown_as_popup_;
72 class WelcomeNotificationTest : public testing::Test {
74 WelcomeNotificationTest() {
75 scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry(
76 new user_prefs::PrefRegistrySyncable());
77 WelcomeNotification::RegisterProfilePrefs(pref_registry.get());
80 virtual void SetUp() {
81 message_loop_.reset(new base::MessageLoop());
82 profile_.reset(new TestingProfile());
83 message_center_.reset(new MockMessageCenter());
84 welcome_notification_.reset(
85 new WelcomeNotification(profile_.get(), message_center_.get()));
88 virtual void TearDown() {
89 welcome_notification_.reset();
90 message_center_.reset();
92 message_loop_.reset();
95 void ShowChromeNowNotification() {
97 "ChromeNowNotification",
98 message_center::NotifierId(
99 message_center::NotifierId::APPLICATION,
100 kChromeNowExtensionID));
103 void ShowRegularNotification() {
105 "RegularNotification",
106 message_center::NotifierId(
107 message_center::NotifierId::APPLICATION,
108 "aaaabbbbccccddddeeeeffffggghhhhi"));
111 void FlushMessageLoop() {
112 message_loop_->RunUntilIdle();
115 TestingProfile* profile() { return profile_.get(); }
116 MockMessageCenter* message_center() { return message_center_.get(); }
119 class TestNotificationDelegate : public NotificationDelegate {
121 explicit TestNotificationDelegate(const std::string& id)
124 // Overridden from NotificationDelegate:
125 virtual void Display() OVERRIDE {}
126 virtual void Error() OVERRIDE {}
127 virtual void Close(bool by_user) OVERRIDE {}
128 virtual void Click() OVERRIDE {}
129 virtual void ButtonClick(int index) OVERRIDE {}
131 virtual std::string id() const OVERRIDE { return id_; }
133 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
138 virtual ~TestNotificationDelegate() {}
140 const std::string id_;
142 DISALLOW_COPY_AND_ASSIGN(TestNotificationDelegate);
145 void ShowNotification(
146 std::string notification_id,
147 const message_center::NotifierId& notifier_id) {
148 message_center::RichNotificationData rich_notification_data;
149 rich_notification_data.priority = 0;
150 Notification notification(
151 message_center::NOTIFICATION_TYPE_BASE_FORMAT,
152 GURL("http://tests.url"),
153 base::UTF8ToUTF16("Title"),
154 base::UTF8ToUTF16("Body"),
156 WebKit::WebTextDirectionDefault,
158 base::UTF8ToUTF16("Source"),
159 base::UTF8ToUTF16(notification_id),
160 rich_notification_data,
161 new TestNotificationDelegate("TestNotification"));
162 welcome_notification_->ShowWelcomeNotificationIfNecessary(notification);
165 scoped_ptr<TestingProfile> profile_;
166 scoped_ptr<MockMessageCenter> message_center_;
167 scoped_ptr<WelcomeNotification> welcome_notification_;
168 scoped_ptr<base::MessageLoop> message_loop_;
171 // Show a regular notification. Expect that WelcomeNotification will
172 // not show a welcome notification.
173 TEST_F(WelcomeNotificationTest, FirstRunShowRegularNotification) {
175 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
177 profile()->GetPrefs()->GetBoolean(
178 prefs::kWelcomeNotificationPreviouslyPoppedUp));
180 ShowRegularNotification();
182 EXPECT_TRUE(message_center()->add_notification_calls() == 0);
183 EXPECT_TRUE(message_center()->remove_notification_calls() == 0);
184 EXPECT_TRUE(message_center()->notifications_with_shown_as_popup() == 0);
186 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
188 profile()->GetPrefs()->GetBoolean(
189 prefs::kWelcomeNotificationPreviouslyPoppedUp));
192 // Show a Chrome Now notification. Expect that WelcomeNotification will
193 // show a welcome notification.
194 TEST_F(WelcomeNotificationTest, FirstRunChromeNowNotification) {
196 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
198 profile()->GetPrefs()->GetBoolean(
199 prefs::kWelcomeNotificationPreviouslyPoppedUp));
201 ShowChromeNowNotification();
203 EXPECT_TRUE(message_center()->add_notification_calls() == 1);
204 EXPECT_TRUE(message_center()->remove_notification_calls() == 0);
205 EXPECT_TRUE(message_center()->notifications_with_shown_as_popup() == 0);
207 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
209 profile()->GetPrefs()->GetBoolean(
210 prefs::kWelcomeNotificationPreviouslyPoppedUp));
213 // Show a Chrome Now notification that was already shown before.
214 TEST_F(WelcomeNotificationTest, ShowWelcomeNotificationAgain) {
215 profile()->GetPrefs()->SetBoolean(
216 prefs::kWelcomeNotificationPreviouslyPoppedUp, true);
218 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
220 profile()->GetPrefs()->GetBoolean(
221 prefs::kWelcomeNotificationPreviouslyPoppedUp));
223 ShowChromeNowNotification();
225 EXPECT_TRUE(message_center()->add_notification_calls() == 1);
226 EXPECT_TRUE(message_center()->remove_notification_calls() == 0);
227 EXPECT_TRUE(message_center()->notifications_with_shown_as_popup() == 1);
229 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
231 profile()->GetPrefs()->GetBoolean(
232 prefs::kWelcomeNotificationPreviouslyPoppedUp));
235 // Don't show a welcome notification if it was previously dismissed
236 TEST_F(WelcomeNotificationTest, WelcomeNotificationPreviouslyDismissed) {
237 profile()->GetPrefs()->SetBoolean(prefs::kWelcomeNotificationDismissed, true);
239 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
241 profile()->GetPrefs()->GetBoolean(
242 prefs::kWelcomeNotificationPreviouslyPoppedUp));
244 ShowChromeNowNotification();
246 EXPECT_TRUE(message_center()->add_notification_calls() == 0);
247 EXPECT_TRUE(message_center()->remove_notification_calls() == 0);
248 EXPECT_TRUE(message_center()->notifications_with_shown_as_popup() == 0);
250 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
252 profile()->GetPrefs()->GetBoolean(
253 prefs::kWelcomeNotificationPreviouslyPoppedUp));
256 // Show a Chrome Now notification and dismiss it.
257 // Expect welcome toast dismissed to be true.
258 TEST_F(WelcomeNotificationTest, DismissWelcomeNotification) {
260 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
262 profile()->GetPrefs()->GetBoolean(
263 prefs::kWelcomeNotificationPreviouslyPoppedUp));
265 ShowChromeNowNotification();
266 message_center()->CloseCurrentNotification();
269 EXPECT_TRUE(message_center()->add_notification_calls() == 1);
270 EXPECT_TRUE(message_center()->remove_notification_calls() == 1);
271 EXPECT_TRUE(message_center()->notifications_with_shown_as_popup() == 0);
273 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
275 profile()->GetPrefs()->GetBoolean(
276 prefs::kWelcomeNotificationPreviouslyPoppedUp));
279 // Show a Chrome Now notification and dismiss it via a synced preference change.
280 // Expect welcome toast dismissed to be true.
281 TEST_F(WelcomeNotificationTest, SyncedDismissalWelcomeNotification) {
283 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
285 profile()->GetPrefs()->GetBoolean(
286 prefs::kWelcomeNotificationPreviouslyPoppedUp));
288 ShowChromeNowNotification();
289 profile()->GetPrefs()->SetBoolean(prefs::kWelcomeNotificationDismissed, true);
291 EXPECT_TRUE(message_center()->add_notification_calls() == 1);
292 EXPECT_TRUE(message_center()->remove_notification_calls() == 1);
293 EXPECT_TRUE(message_center()->notifications_with_shown_as_popup() == 0);
295 profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
297 profile()->GetPrefs()->GetBoolean(
298 prefs::kWelcomeNotificationPreviouslyPoppedUp));