Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / background / background_contents_service_unittest.cc
blob655379b44cd168b7fc3e9d55f37dfa17a6d106eb
1 // Copyright (c) 2012 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 <string>
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/scoped_user_pref_update.h"
12 #include "base/run_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/background/background_contents.h"
15 #include "chrome/browser/background/background_contents_service.h"
16 #include "chrome/browser/background/background_contents_service_factory.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/common/extensions/extension_test_util.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/browser_with_test_window_test.h"
22 #include "chrome/test/base/testing_browser_process.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "chrome/test/base/testing_profile_manager.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/test/test_browser_thread.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "extensions/common/extension.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "testing/platform_test.h"
31 #include "url/gurl.h"
33 #if defined(ENABLE_NOTIFICATIONS)
34 #include "chrome/browser/notifications/message_center_notification_manager.h"
35 #include "chrome/browser/notifications/notification.h"
36 #include "ui/message_center/fake_message_center_tray_delegate.h"
37 #include "ui/message_center/message_center.h"
38 #include "ui/message_center/message_center_observer.h"
39 #endif
41 class BackgroundContentsServiceTest : public testing::Test {
42 public:
43 BackgroundContentsServiceTest() {}
44 ~BackgroundContentsServiceTest() override {}
45 void SetUp() override {
46 command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM));
47 BackgroundContentsService::DisableCloseBalloonForTesting(true);
50 void TearDown() override {
51 BackgroundContentsService::DisableCloseBalloonForTesting(false);
54 const base::DictionaryValue* GetPrefs(Profile* profile) {
55 return profile->GetPrefs()->GetDictionary(
56 prefs::kRegisteredBackgroundContents);
59 // Returns the stored pref URL for the passed app id.
60 std::string GetPrefURLForApp(Profile* profile, const base::string16& appid) {
61 const base::DictionaryValue* pref = GetPrefs(profile);
62 EXPECT_TRUE(pref->HasKey(base::UTF16ToUTF8(appid)));
63 const base::DictionaryValue* value;
64 pref->GetDictionaryWithoutPathExpansion(base::UTF16ToUTF8(appid), &value);
65 std::string url;
66 value->GetString("url", &url);
67 return url;
70 content::TestBrowserThreadBundle thread_bundle_;
71 scoped_ptr<base::CommandLine> command_line_;
74 class MockBackgroundContents : public BackgroundContents {
75 public:
76 explicit MockBackgroundContents(Profile* profile)
77 : appid_(base::ASCIIToUTF16("app_id")),
78 profile_(profile) {
80 MockBackgroundContents(Profile* profile, const std::string& id)
81 : appid_(base::ASCIIToUTF16(id)),
82 profile_(profile) {
85 void SendOpenedNotification(BackgroundContentsService* service) {
86 BackgroundContentsOpenedDetails details = {
87 this, "background", appid_ };
88 service->BackgroundContentsOpened(&details, profile_);
91 virtual void Navigate(GURL url) {
92 url_ = url;
93 content::NotificationService::current()->Notify(
94 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED,
95 content::Source<Profile>(profile_),
96 content::Details<BackgroundContents>(this));
98 const GURL& GetURL() const override { return url_; }
100 void MockClose(Profile* profile) {
101 content::NotificationService::current()->Notify(
102 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED,
103 content::Source<Profile>(profile),
104 content::Details<BackgroundContents>(this));
105 delete this;
108 ~MockBackgroundContents() override {
109 content::NotificationService::current()->Notify(
110 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED,
111 content::Source<Profile>(profile_),
112 content::Details<BackgroundContents>(this));
115 const base::string16& appid() { return appid_; }
117 private:
118 GURL url_;
120 // The ID of our parent application
121 base::string16 appid_;
123 // Parent profile
124 Profile* profile_;
127 #if defined(ENABLE_NOTIFICATIONS)
128 // Wait for the notification created.
129 class NotificationWaiter : public message_center::MessageCenterObserver {
130 public:
131 explicit NotificationWaiter(const std::string& target_id, Profile* profile)
132 : target_id_(target_id), profile_(profile) {}
133 ~NotificationWaiter() override {}
135 void WaitForNotificationAdded() {
136 DCHECK(!run_loop_.running());
137 message_center::MessageCenter* message_center =
138 message_center::MessageCenter::Get();
140 message_center->AddObserver(this);
141 run_loop_.Run();
142 message_center->RemoveObserver(this);
145 private:
146 // message_center::MessageCenterObserver overrides:
147 void OnNotificationAdded(const std::string& notification_id) override {
148 if (notification_id == FindNotificationIdFromDelegateId(target_id_))
149 run_loop_.Quit();
152 void OnNotificationUpdated(const std::string& notification_id) override {
153 if (notification_id == FindNotificationIdFromDelegateId(target_id_))
154 run_loop_.Quit();
157 std::string FindNotificationIdFromDelegateId(const std::string& delegate_id) {
158 MessageCenterNotificationManager* manager =
159 static_cast<MessageCenterNotificationManager*>(
160 g_browser_process->notification_ui_manager());
161 DCHECK(manager);
162 return manager->FindById(delegate_id, profile_)->id();
165 std::string target_id_;
166 Profile* profile_;
167 base::RunLoop run_loop_;
169 DISALLOW_COPY_AND_ASSIGN(NotificationWaiter);
172 class BackgroundContentsServiceNotificationTest
173 : public BrowserWithTestWindowTest {
174 public:
175 BackgroundContentsServiceNotificationTest() {}
176 ~BackgroundContentsServiceNotificationTest() override {}
178 // Overridden from testing::Test
179 void SetUp() override {
180 BrowserWithTestWindowTest::SetUp();
181 // In ChromeOS environment, BrowserWithTestWindowTest initializes
182 // MessageCenter.
183 #if !defined(OS_CHROMEOS)
184 message_center::MessageCenter::Initialize();
185 #endif
186 profile_manager_.reset(new TestingProfileManager(
187 TestingBrowserProcess::GetGlobal()));
188 ASSERT_TRUE(profile_manager_->SetUp());
189 MessageCenterNotificationManager* manager =
190 static_cast<MessageCenterNotificationManager*>(
191 g_browser_process->notification_ui_manager());
192 manager->SetMessageCenterTrayDelegateForTest(
193 new message_center::FakeMessageCenterTrayDelegate(
194 message_center::MessageCenter::Get()));
197 void TearDown() override {
198 g_browser_process->notification_ui_manager()->CancelAll();
199 profile_manager_.reset();
200 #if !defined(OS_CHROMEOS)
201 message_center::MessageCenter::Shutdown();
202 #endif
203 BrowserWithTestWindowTest::TearDown();
206 protected:
207 // Creates crash notification for the specified extension and returns
208 // the created one.
209 const Notification* CreateCrashNotification(
210 scoped_refptr<extensions::Extension> extension) {
211 std::string notification_id = BackgroundContentsService::
212 GetNotificationDelegateIdForExtensionForTesting(extension->id());
213 NotificationWaiter waiter(notification_id, profile());
214 BackgroundContentsService::ShowBalloonForTesting(
215 extension.get(), profile());
216 waiter.WaitForNotificationAdded();
218 return g_browser_process->notification_ui_manager()->FindById(
219 notification_id, profile());
222 private:
223 scoped_ptr<TestingProfileManager> profile_manager_;
225 DISALLOW_COPY_AND_ASSIGN(BackgroundContentsServiceNotificationTest);
227 #endif // ENABLE_NOTIFICATIONS
229 TEST_F(BackgroundContentsServiceTest, Create) {
230 // Check for creation and leaks.
231 TestingProfile profile;
232 BackgroundContentsService service(&profile, command_line_.get());
235 TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) {
236 TestingProfile profile;
237 BackgroundContentsService service(&profile, command_line_.get());
238 MockBackgroundContents* contents = new MockBackgroundContents(&profile);
239 EXPECT_FALSE(service.IsTracked(contents));
240 contents->SendOpenedNotification(&service);
241 EXPECT_TRUE(service.IsTracked(contents));
242 delete contents;
243 EXPECT_FALSE(service.IsTracked(contents));
246 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) {
247 TestingProfile profile;
248 BackgroundContentsService service(&profile, command_line_.get());
249 BackgroundContentsServiceFactory::GetInstance()->
250 RegisterUserPrefsOnBrowserContextForTest(&profile);
251 GURL orig_url;
252 GURL url("http://a/");
253 GURL url2("http://a/");
255 scoped_ptr<MockBackgroundContents> contents(
256 new MockBackgroundContents(&profile));
257 EXPECT_EQ(0U, GetPrefs(&profile)->size());
258 contents->SendOpenedNotification(&service);
260 contents->Navigate(url);
261 EXPECT_EQ(1U, GetPrefs(&profile)->size());
262 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid()));
264 // Navigate the contents to a new url, should not change url.
265 contents->Navigate(url2);
266 EXPECT_EQ(1U, GetPrefs(&profile)->size());
267 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid()));
269 // Contents are deleted, url should persist.
270 EXPECT_EQ(1U, GetPrefs(&profile)->size());
273 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) {
274 TestingProfile profile;
275 BackgroundContentsService service(&profile, command_line_.get());
276 BackgroundContentsServiceFactory::GetInstance()->
277 RegisterUserPrefsOnBrowserContextForTest(&profile);
279 GURL url("http://a/");
280 MockBackgroundContents* contents = new MockBackgroundContents(&profile);
281 EXPECT_EQ(0U, GetPrefs(&profile)->size());
282 contents->SendOpenedNotification(&service);
283 contents->Navigate(url);
284 EXPECT_EQ(1U, GetPrefs(&profile)->size());
285 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid()));
287 // Fake a window closed by script.
288 contents->MockClose(&profile);
289 EXPECT_EQ(0U, GetPrefs(&profile)->size());
292 // Test what happens if a BackgroundContents shuts down (say, due to a renderer
293 // crash) then is restarted. Should not persist URL twice.
294 TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) {
295 TestingProfile profile;
296 BackgroundContentsService service(&profile, command_line_.get());
297 BackgroundContentsServiceFactory::GetInstance()->
298 RegisterUserPrefsOnBrowserContextForTest(&profile);
300 GURL url("http://a/");
302 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents(
303 &profile, "appid"));
304 contents->SendOpenedNotification(&service);
305 contents->Navigate(url);
306 EXPECT_EQ(1U, GetPrefs(&profile)->size());
307 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid()));
309 // Contents deleted, url should be persisted.
310 EXPECT_EQ(1U, GetPrefs(&profile)->size());
313 // Reopen the BackgroundContents to the same URL, we should not register the
314 // URL again.
315 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents(
316 &profile, "appid"));
317 contents->SendOpenedNotification(&service);
318 contents->Navigate(url);
319 EXPECT_EQ(1U, GetPrefs(&profile)->size());
323 // Ensures that BackgroundContentsService properly tracks the association
324 // between a BackgroundContents and its parent extension, including
325 // unregistering the BC when the extension is uninstalled.
326 TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) {
327 TestingProfile profile;
328 BackgroundContentsService service(&profile, command_line_.get());
329 BackgroundContentsServiceFactory::GetInstance()->
330 RegisterUserPrefsOnBrowserContextForTest(&profile);
332 EXPECT_EQ(NULL,
333 service.GetAppBackgroundContents(base::ASCIIToUTF16("appid")));
334 MockBackgroundContents* contents = new MockBackgroundContents(&profile,
335 "appid");
336 scoped_ptr<MockBackgroundContents> contents2(
337 new MockBackgroundContents(&profile, "appid2"));
338 contents->SendOpenedNotification(&service);
339 EXPECT_EQ(contents, service.GetAppBackgroundContents(contents->appid()));
340 contents2->SendOpenedNotification(&service);
341 EXPECT_EQ(contents2.get(), service.GetAppBackgroundContents(
342 contents2->appid()));
343 EXPECT_EQ(0U, GetPrefs(&profile)->size());
345 // Navigate the contents, then make sure the one associated with the extension
346 // is unregistered.
347 GURL url("http://a/");
348 GURL url2("http://b/");
349 contents->Navigate(url);
350 EXPECT_EQ(1U, GetPrefs(&profile)->size());
351 contents2->Navigate(url2);
352 EXPECT_EQ(2U, GetPrefs(&profile)->size());
353 service.ShutdownAssociatedBackgroundContents(base::ASCIIToUTF16("appid"));
354 EXPECT_FALSE(service.IsTracked(contents));
355 EXPECT_EQ(NULL,
356 service.GetAppBackgroundContents(base::ASCIIToUTF16("appid")));
357 EXPECT_EQ(1U, GetPrefs(&profile)->size());
358 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid()));
361 #if defined(ENABLE_NOTIFICATIONS)
362 TEST_F(BackgroundContentsServiceNotificationTest, TestShowBalloon) {
363 scoped_refptr<extensions::Extension> extension =
364 extension_test_util::LoadManifest("image_loading_tracker", "app.json");
365 ASSERT_TRUE(extension.get());
366 ASSERT_TRUE(extension->GetManifestData("icons"));
368 const Notification* notification = CreateCrashNotification(extension);
369 EXPECT_FALSE(notification->icon().IsEmpty());
372 // Verify if a test notification can show the default extension icon for
373 // a crash notification for an extension without icon.
374 TEST_F(BackgroundContentsServiceNotificationTest, TestShowBalloonNoIcon) {
375 // Extension manifest file with no 'icon' field.
376 scoped_refptr<extensions::Extension> extension =
377 extension_test_util::LoadManifest("app", "manifest.json");
378 ASSERT_TRUE(extension.get());
379 ASSERT_FALSE(extension->GetManifestData("icons"));
381 const Notification* notification = CreateCrashNotification(extension);
382 EXPECT_FALSE(notification->icon().IsEmpty());
385 TEST_F(BackgroundContentsServiceNotificationTest, TestShowTwoBalloons) {
386 TestingProfile profile;
387 scoped_refptr<extensions::Extension> extension =
388 extension_test_util::LoadManifest("app", "manifest.json");
389 ASSERT_TRUE(extension.get());
390 CreateCrashNotification(extension);
391 CreateCrashNotification(extension);
393 message_center::MessageCenter* message_center =
394 message_center::MessageCenter::Get();
395 message_center::NotificationList::Notifications notifications =
396 message_center->GetVisibleNotifications();
397 ASSERT_EQ(1u, notifications.size());
399 #endif