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.
7 #include "base/basictypes.h"
8 #include "base/command_line.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/prefs/scoped_user_pref_update.h"
13 #include "base/run_loop.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/background/background_contents.h"
16 #include "chrome/browser/background/background_contents_service.h"
17 #include "chrome/browser/background/background_contents_service_factory.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/ui/browser_list.h"
20 #include "chrome/common/extensions/extension_test_util.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/test/base/browser_with_test_window_test.h"
23 #include "chrome/test/base/testing_browser_process.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "chrome/test/base/testing_profile_manager.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/test/test_browser_thread.h"
28 #include "extensions/common/extension.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "testing/platform_test.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"
41 class BackgroundContentsServiceTest
: public testing::Test
{
43 BackgroundContentsServiceTest() {}
44 ~BackgroundContentsServiceTest() override
{}
45 void SetUp() override
{
46 command_line_
.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM
));
49 const base::DictionaryValue
* GetPrefs(Profile
* profile
) {
50 return profile
->GetPrefs()->GetDictionary(
51 prefs::kRegisteredBackgroundContents
);
54 // Returns the stored pref URL for the passed app id.
55 std::string
GetPrefURLForApp(Profile
* profile
, const base::string16
& appid
) {
56 const base::DictionaryValue
* pref
= GetPrefs(profile
);
57 EXPECT_TRUE(pref
->HasKey(base::UTF16ToUTF8(appid
)));
58 const base::DictionaryValue
* value
;
59 pref
->GetDictionaryWithoutPathExpansion(base::UTF16ToUTF8(appid
), &value
);
61 value
->GetString("url", &url
);
65 scoped_ptr
<base::CommandLine
> command_line_
;
68 class MockBackgroundContents
: public BackgroundContents
{
70 explicit MockBackgroundContents(Profile
* profile
)
71 : appid_(base::ASCIIToUTF16("app_id")),
74 MockBackgroundContents(Profile
* profile
, const std::string
& id
)
75 : appid_(base::ASCIIToUTF16(id
)),
79 void SendOpenedNotification(BackgroundContentsService
* service
) {
80 base::string16 frame_name
= base::ASCIIToUTF16("background");
81 BackgroundContentsOpenedDetails details
= {
82 this, frame_name
, appid_
};
83 service
->BackgroundContentsOpened(&details
, profile_
);
86 virtual void Navigate(GURL url
) {
88 content::NotificationService::current()->Notify(
89 chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED
,
90 content::Source
<Profile
>(profile_
),
91 content::Details
<BackgroundContents
>(this));
93 const GURL
& GetURL() const override
{ return url_
; }
95 void MockClose(Profile
* profile
) {
96 content::NotificationService::current()->Notify(
97 chrome::NOTIFICATION_BACKGROUND_CONTENTS_CLOSED
,
98 content::Source
<Profile
>(profile
),
99 content::Details
<BackgroundContents
>(this));
103 ~MockBackgroundContents() override
{
104 content::NotificationService::current()->Notify(
105 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED
,
106 content::Source
<Profile
>(profile_
),
107 content::Details
<BackgroundContents
>(this));
110 const base::string16
& appid() { return appid_
; }
115 // The ID of our parent application
116 base::string16 appid_
;
122 #if defined(ENABLE_NOTIFICATIONS)
123 // Wait for the notification created.
124 class NotificationWaiter
: public message_center::MessageCenterObserver
{
126 explicit NotificationWaiter(const std::string
& target_id
, Profile
* profile
)
127 : target_id_(target_id
), profile_(profile
) {}
128 ~NotificationWaiter() override
{}
130 void WaitForNotificationAdded() {
131 DCHECK(!run_loop_
.running());
132 message_center::MessageCenter
* message_center
=
133 message_center::MessageCenter::Get();
135 message_center
->AddObserver(this);
137 message_center
->RemoveObserver(this);
141 // message_center::MessageCenterObserver overrides:
142 void OnNotificationAdded(const std::string
& notification_id
) override
{
143 if (notification_id
== FindNotificationIdFromDelegateId(target_id_
))
147 void OnNotificationUpdated(const std::string
& notification_id
) override
{
148 if (notification_id
== FindNotificationIdFromDelegateId(target_id_
))
152 std::string
FindNotificationIdFromDelegateId(const std::string
& delegate_id
) {
153 MessageCenterNotificationManager
* manager
=
154 static_cast<MessageCenterNotificationManager
*>(
155 g_browser_process
->notification_ui_manager());
157 return manager
->FindById(delegate_id
, profile_
)->id();
160 std::string target_id_
;
162 base::RunLoop run_loop_
;
164 DISALLOW_COPY_AND_ASSIGN(NotificationWaiter
);
167 class BackgroundContentsServiceNotificationTest
168 : public BrowserWithTestWindowTest
{
170 BackgroundContentsServiceNotificationTest() {}
171 ~BackgroundContentsServiceNotificationTest() override
{}
173 // Overridden from testing::Test
174 void SetUp() override
{
175 BrowserWithTestWindowTest::SetUp();
176 // In ChromeOS environment, BrowserWithTestWindowTest initializes
178 #if !defined(OS_CHROMEOS)
179 message_center::MessageCenter::Initialize();
181 profile_manager_
.reset(new TestingProfileManager(
182 TestingBrowserProcess::GetGlobal()));
183 ASSERT_TRUE(profile_manager_
->SetUp());
184 MessageCenterNotificationManager
* manager
=
185 static_cast<MessageCenterNotificationManager
*>(
186 g_browser_process
->notification_ui_manager());
187 manager
->SetMessageCenterTrayDelegateForTest(
188 new message_center::FakeMessageCenterTrayDelegate(
189 message_center::MessageCenter::Get(), base::Closure()));
192 void TearDown() override
{
193 g_browser_process
->notification_ui_manager()->CancelAll();
194 profile_manager_
.reset();
195 #if !defined(OS_CHROMEOS)
196 message_center::MessageCenter::Shutdown();
198 BrowserWithTestWindowTest::TearDown();
202 // Creates crash notification for the specified extension and returns
204 const Notification
* CreateCrashNotification(
205 scoped_refptr
<extensions::Extension
> extension
) {
206 std::string notification_id
= BackgroundContentsService::
207 GetNotificationDelegateIdForExtensionForTesting(extension
->id());
208 NotificationWaiter
waiter(notification_id
, profile());
209 BackgroundContentsService::ShowBalloonForTesting(
210 extension
.get(), profile());
211 waiter
.WaitForNotificationAdded();
213 return g_browser_process
->notification_ui_manager()->FindById(
214 notification_id
, profile());
218 scoped_ptr
<TestingProfileManager
> profile_manager_
;
220 DISALLOW_COPY_AND_ASSIGN(BackgroundContentsServiceNotificationTest
);
222 #endif // ENABLE_NOTIFICATIONS
224 TEST_F(BackgroundContentsServiceTest
, Create
) {
225 // Check for creation and leaks.
226 TestingProfile profile
;
227 BackgroundContentsService
service(&profile
, command_line_
.get());
230 TEST_F(BackgroundContentsServiceTest
, BackgroundContentsCreateDestroy
) {
231 TestingProfile profile
;
232 BackgroundContentsService
service(&profile
, command_line_
.get());
233 MockBackgroundContents
* contents
= new MockBackgroundContents(&profile
);
234 EXPECT_FALSE(service
.IsTracked(contents
));
235 contents
->SendOpenedNotification(&service
);
236 EXPECT_TRUE(service
.IsTracked(contents
));
238 EXPECT_FALSE(service
.IsTracked(contents
));
241 TEST_F(BackgroundContentsServiceTest
, BackgroundContentsUrlAdded
) {
242 TestingProfile profile
;
243 BackgroundContentsService
service(&profile
, command_line_
.get());
244 BackgroundContentsServiceFactory::GetInstance()->
245 RegisterUserPrefsOnBrowserContextForTest(&profile
);
247 GURL
url("http://a/");
248 GURL
url2("http://a/");
250 scoped_ptr
<MockBackgroundContents
> contents(
251 new MockBackgroundContents(&profile
));
252 EXPECT_EQ(0U, GetPrefs(&profile
)->size());
253 contents
->SendOpenedNotification(&service
);
255 contents
->Navigate(url
);
256 EXPECT_EQ(1U, GetPrefs(&profile
)->size());
257 EXPECT_EQ(url
.spec(), GetPrefURLForApp(&profile
, contents
->appid()));
259 // Navigate the contents to a new url, should not change url.
260 contents
->Navigate(url2
);
261 EXPECT_EQ(1U, GetPrefs(&profile
)->size());
262 EXPECT_EQ(url
.spec(), GetPrefURLForApp(&profile
, contents
->appid()));
264 // Contents are deleted, url should persist.
265 EXPECT_EQ(1U, GetPrefs(&profile
)->size());
268 TEST_F(BackgroundContentsServiceTest
, BackgroundContentsUrlAddedAndClosed
) {
269 TestingProfile profile
;
270 BackgroundContentsService
service(&profile
, command_line_
.get());
271 BackgroundContentsServiceFactory::GetInstance()->
272 RegisterUserPrefsOnBrowserContextForTest(&profile
);
274 GURL
url("http://a/");
275 MockBackgroundContents
* contents
= new MockBackgroundContents(&profile
);
276 EXPECT_EQ(0U, GetPrefs(&profile
)->size());
277 contents
->SendOpenedNotification(&service
);
278 contents
->Navigate(url
);
279 EXPECT_EQ(1U, GetPrefs(&profile
)->size());
280 EXPECT_EQ(url
.spec(), GetPrefURLForApp(&profile
, contents
->appid()));
282 // Fake a window closed by script.
283 contents
->MockClose(&profile
);
284 EXPECT_EQ(0U, GetPrefs(&profile
)->size());
287 // Test what happens if a BackgroundContents shuts down (say, due to a renderer
288 // crash) then is restarted. Should not persist URL twice.
289 TEST_F(BackgroundContentsServiceTest
, RestartBackgroundContents
) {
290 TestingProfile profile
;
291 BackgroundContentsService
service(&profile
, command_line_
.get());
292 BackgroundContentsServiceFactory::GetInstance()->
293 RegisterUserPrefsOnBrowserContextForTest(&profile
);
295 GURL
url("http://a/");
297 scoped_ptr
<MockBackgroundContents
> contents(new MockBackgroundContents(
299 contents
->SendOpenedNotification(&service
);
300 contents
->Navigate(url
);
301 EXPECT_EQ(1U, GetPrefs(&profile
)->size());
302 EXPECT_EQ(url
.spec(), GetPrefURLForApp(&profile
, contents
->appid()));
304 // Contents deleted, url should be persisted.
305 EXPECT_EQ(1U, GetPrefs(&profile
)->size());
308 // Reopen the BackgroundContents to the same URL, we should not register the
310 scoped_ptr
<MockBackgroundContents
> contents(new MockBackgroundContents(
312 contents
->SendOpenedNotification(&service
);
313 contents
->Navigate(url
);
314 EXPECT_EQ(1U, GetPrefs(&profile
)->size());
318 // Ensures that BackgroundContentsService properly tracks the association
319 // between a BackgroundContents and its parent extension, including
320 // unregistering the BC when the extension is uninstalled.
321 TEST_F(BackgroundContentsServiceTest
, TestApplicationIDLinkage
) {
322 TestingProfile profile
;
323 BackgroundContentsService
service(&profile
, command_line_
.get());
324 BackgroundContentsServiceFactory::GetInstance()->
325 RegisterUserPrefsOnBrowserContextForTest(&profile
);
328 service
.GetAppBackgroundContents(base::ASCIIToUTF16("appid")));
329 MockBackgroundContents
* contents
= new MockBackgroundContents(&profile
,
331 scoped_ptr
<MockBackgroundContents
> contents2(
332 new MockBackgroundContents(&profile
, "appid2"));
333 contents
->SendOpenedNotification(&service
);
334 EXPECT_EQ(contents
, service
.GetAppBackgroundContents(contents
->appid()));
335 contents2
->SendOpenedNotification(&service
);
336 EXPECT_EQ(contents2
.get(), service
.GetAppBackgroundContents(
337 contents2
->appid()));
338 EXPECT_EQ(0U, GetPrefs(&profile
)->size());
340 // Navigate the contents, then make sure the one associated with the extension
342 GURL
url("http://a/");
343 GURL
url2("http://b/");
344 contents
->Navigate(url
);
345 EXPECT_EQ(1U, GetPrefs(&profile
)->size());
346 contents2
->Navigate(url2
);
347 EXPECT_EQ(2U, GetPrefs(&profile
)->size());
348 service
.ShutdownAssociatedBackgroundContents(base::ASCIIToUTF16("appid"));
349 EXPECT_FALSE(service
.IsTracked(contents
));
351 service
.GetAppBackgroundContents(base::ASCIIToUTF16("appid")));
352 EXPECT_EQ(1U, GetPrefs(&profile
)->size());
353 EXPECT_EQ(url2
.spec(), GetPrefURLForApp(&profile
, contents2
->appid()));
356 #if defined(ENABLE_NOTIFICATIONS)
357 TEST_F(BackgroundContentsServiceNotificationTest
, TestShowBalloon
) {
358 scoped_refptr
<extensions::Extension
> extension
=
359 extension_test_util::LoadManifest("image_loading_tracker", "app.json");
360 ASSERT_TRUE(extension
.get());
361 ASSERT_TRUE(extension
->GetManifestData("icons"));
363 const Notification
* notification
= CreateCrashNotification(extension
);
364 EXPECT_FALSE(notification
->icon().IsEmpty());
367 // Verify if a test notification can show the default extension icon for
368 // a crash notification for an extension without icon.
369 TEST_F(BackgroundContentsServiceNotificationTest
, TestShowBalloonNoIcon
) {
370 // Extension manifest file with no 'icon' field.
371 scoped_refptr
<extensions::Extension
> extension
=
372 extension_test_util::LoadManifest("app", "manifest.json");
373 ASSERT_TRUE(extension
.get());
374 ASSERT_FALSE(extension
->GetManifestData("icons"));
376 const Notification
* notification
= CreateCrashNotification(extension
);
377 EXPECT_FALSE(notification
->icon().IsEmpty());
380 TEST_F(BackgroundContentsServiceNotificationTest
, TestShowTwoBalloons
) {
381 TestingProfile profile
;
382 scoped_refptr
<extensions::Extension
> extension
=
383 extension_test_util::LoadManifest("app", "manifest.json");
384 ASSERT_TRUE(extension
.get());
385 CreateCrashNotification(extension
);
386 CreateCrashNotification(extension
);
388 message_center::MessageCenter
* message_center
=
389 message_center::MessageCenter::Get();
390 message_center::NotificationList::Notifications notifications
=
391 message_center
->GetVisibleNotifications();
392 ASSERT_EQ(1u, notifications
.size());