- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / welcome_notification_unittest.cc
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.
4
5 #include "chrome/browser/notifications/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 "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"
21
22 const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh";
23
24 class MockMessageCenter : public message_center::FakeMessageCenter {
25  public:
26   MockMessageCenter()
27     : add_notification_calls_(0),
28       remove_notification_calls_(0),
29       notifications_with_shown_as_popup_(0) {};
30
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_;
35   }
36
37   // message_center::FakeMessageCenter Overrides
38   virtual bool HasNotification(const std::string& id) OVERRIDE {
39     return last_notification.get() &&
40         (last_notification->id() == id);
41   }
42
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_++;
50   }
51
52   virtual void RemoveNotification(const std::string& id, bool by_user)
53       OVERRIDE {
54     EXPECT_TRUE(last_notification.get());
55     last_notification.reset();
56     remove_notification_calls_++;
57   }
58
59   void CloseCurrentNotification() {
60     EXPECT_TRUE(last_notification.get());
61     last_notification->delegate()->Close(true);
62     RemoveNotification(last_notification->id(), true);
63   }
64
65  private:
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_;
70 };
71
72 class WelcomeNotificationTest : public testing::Test {
73  protected:
74   WelcomeNotificationTest() {
75     scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry(
76         new user_prefs::PrefRegistrySyncable());
77     WelcomeNotification::RegisterProfilePrefs(pref_registry.get());
78   }
79
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()));
86   }
87
88   virtual void TearDown() {
89     welcome_notification_.reset();
90     message_center_.reset();
91     profile_.reset();
92     message_loop_.reset();
93   }
94
95   void ShowChromeNowNotification() {
96     ShowNotification(
97         "ChromeNowNotification",
98         message_center::NotifierId(
99             message_center::NotifierId::APPLICATION,
100             kChromeNowExtensionID));
101   }
102
103   void ShowRegularNotification() {
104     ShowNotification(
105         "RegularNotification",
106         message_center::NotifierId(
107             message_center::NotifierId::APPLICATION,
108             "aaaabbbbccccddddeeeeffffggghhhhi"));
109   }
110
111   void FlushMessageLoop() {
112     message_loop_->RunUntilIdle();
113   }
114
115   TestingProfile* profile() { return profile_.get(); }
116   MockMessageCenter* message_center() { return message_center_.get(); }
117
118  private:
119   class TestNotificationDelegate : public NotificationDelegate {
120    public:
121     explicit TestNotificationDelegate(const std::string& id)
122         : id_(id) {}
123
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 {}
130
131     virtual std::string id() const OVERRIDE { return id_; }
132
133     virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
134       return NULL;
135     }
136
137    private:
138     virtual ~TestNotificationDelegate() {}
139
140     const std::string id_;
141
142     DISALLOW_COPY_AND_ASSIGN(TestNotificationDelegate);
143   };
144
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"),
155         gfx::Image(),
156         WebKit::WebTextDirectionDefault,
157         notifier_id,
158         base::UTF8ToUTF16("Source"),
159         base::UTF8ToUTF16(notification_id),
160         rich_notification_data,
161         new TestNotificationDelegate("TestNotification"));
162     welcome_notification_->ShowWelcomeNotificationIfNecessary(notification);
163   }
164
165   scoped_ptr<TestingProfile> profile_;
166   scoped_ptr<MockMessageCenter> message_center_;
167   scoped_ptr<WelcomeNotification> welcome_notification_;
168   scoped_ptr<base::MessageLoop> message_loop_;
169 };
170
171 // Show a regular notification. Expect that WelcomeNotification will
172 // not show a welcome notification.
173 TEST_F(WelcomeNotificationTest, FirstRunShowRegularNotification) {
174   EXPECT_FALSE(
175       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
176   EXPECT_FALSE(
177       profile()->GetPrefs()->GetBoolean(
178           prefs::kWelcomeNotificationPreviouslyPoppedUp));
179
180   ShowRegularNotification();
181
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);
185   EXPECT_FALSE(
186       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
187   EXPECT_FALSE(
188       profile()->GetPrefs()->GetBoolean(
189           prefs::kWelcomeNotificationPreviouslyPoppedUp));
190 }
191
192 // Show a Chrome Now notification. Expect that WelcomeNotification will
193 // show a welcome notification.
194 TEST_F(WelcomeNotificationTest, FirstRunChromeNowNotification) {
195   EXPECT_FALSE(
196       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
197   EXPECT_FALSE(
198       profile()->GetPrefs()->GetBoolean(
199           prefs::kWelcomeNotificationPreviouslyPoppedUp));
200
201   ShowChromeNowNotification();
202
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);
206   EXPECT_FALSE(
207       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
208   EXPECT_TRUE(
209       profile()->GetPrefs()->GetBoolean(
210           prefs::kWelcomeNotificationPreviouslyPoppedUp));
211 }
212
213 // Show a Chrome Now notification that was already shown before.
214 TEST_F(WelcomeNotificationTest, ShowWelcomeNotificationAgain) {
215   profile()->GetPrefs()->SetBoolean(
216       prefs::kWelcomeNotificationPreviouslyPoppedUp, true);
217   EXPECT_FALSE(
218       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
219   EXPECT_TRUE(
220       profile()->GetPrefs()->GetBoolean(
221           prefs::kWelcomeNotificationPreviouslyPoppedUp));
222
223   ShowChromeNowNotification();
224
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);
228   EXPECT_FALSE(
229       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
230   EXPECT_TRUE(
231       profile()->GetPrefs()->GetBoolean(
232           prefs::kWelcomeNotificationPreviouslyPoppedUp));
233 }
234
235 // Don't show a welcome notification if it was previously dismissed
236 TEST_F(WelcomeNotificationTest, WelcomeNotificationPreviouslyDismissed) {
237   profile()->GetPrefs()->SetBoolean(prefs::kWelcomeNotificationDismissed, true);
238   EXPECT_TRUE(
239       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
240   EXPECT_FALSE(
241       profile()->GetPrefs()->GetBoolean(
242           prefs::kWelcomeNotificationPreviouslyPoppedUp));
243
244   ShowChromeNowNotification();
245
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);
249   EXPECT_TRUE(
250       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
251   EXPECT_FALSE(
252       profile()->GetPrefs()->GetBoolean(
253           prefs::kWelcomeNotificationPreviouslyPoppedUp));
254 }
255
256 // Show a Chrome Now notification and dismiss it.
257 // Expect welcome toast dismissed to be true.
258 TEST_F(WelcomeNotificationTest, DismissWelcomeNotification) {
259   EXPECT_FALSE(
260       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
261   EXPECT_FALSE(
262       profile()->GetPrefs()->GetBoolean(
263           prefs::kWelcomeNotificationPreviouslyPoppedUp));
264
265   ShowChromeNowNotification();
266   message_center()->CloseCurrentNotification();
267   FlushMessageLoop();
268
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);
272   EXPECT_TRUE(
273       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
274   EXPECT_TRUE(
275       profile()->GetPrefs()->GetBoolean(
276           prefs::kWelcomeNotificationPreviouslyPoppedUp));
277 }
278
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) {
282   EXPECT_FALSE(
283       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
284   EXPECT_FALSE(
285       profile()->GetPrefs()->GetBoolean(
286           prefs::kWelcomeNotificationPreviouslyPoppedUp));
287
288   ShowChromeNowNotification();
289   profile()->GetPrefs()->SetBoolean(prefs::kWelcomeNotificationDismissed, true);
290
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);
294   EXPECT_TRUE(
295       profile()->GetPrefs()->GetBoolean(prefs::kWelcomeNotificationDismissed));
296   EXPECT_TRUE(
297       profile()->GetPrefs()->GetBoolean(
298           prefs::kWelcomeNotificationPreviouslyPoppedUp));
299 }