[email protected] | a61890e | 2012-07-27 22:27:11 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 5 | #include "chrome/browser/background/background_contents_service.h" |
| 6 | |
| 7 | #include <memory> |
[email protected] | 0bc24b5 | 2011-01-11 01:19:47 | [diff] [blame] | 8 | #include <string> |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 9 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 10 | #include "base/command_line.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | 5190a29 | 2014-02-26 15:50:32 | [diff] [blame] | 12 | #include "base/run_loop.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 13 | #include "build/build_config.h" |
avi | 71d0f76 | 2014-12-15 16:08:47 | [diff] [blame] | 14 | #include "chrome/browser/background/background_contents.h" |
[email protected] | a42f2e2 | 2011-10-10 22:28:17 | [diff] [blame] | 15 | #include "chrome/browser/background/background_contents_service_factory.h" |
[email protected] | fdf40f3e | 2013-07-11 23:55:46 | [diff] [blame] | 16 | #include "chrome/browser/chrome_notification_types.h" |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 17 | #include "chrome/browser/notifications/notification_display_service_tester.h" |
[email protected] | 71b73f0 | 2011-04-06 15:57:29 | [diff] [blame] | 18 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 19 | #include "chrome/common/extensions/extension_test_util.h" |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 20 | #include "chrome/common/pref_names.h" |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 21 | #include "chrome/test/base/browser_with_test_window_test.h" |
[email protected] | bf925774 | 2011-08-11 21:01:15 | [diff] [blame] | 22 | #include "chrome/test/base/testing_browser_process.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 23 | #include "chrome/test/base/testing_profile.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 24 | #include "components/prefs/pref_service.h" |
| 25 | #include "components/prefs/scoped_user_pref_update.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 26 | #include "content/public/browser/notification_service.h" |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 27 | #include "content/public/test/test_browser_thread.h" |
Gabriel Charette | b69fcd4 | 2019-08-23 02:13:29 | [diff] [blame^] | 28 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 29 | #include "extensions/common/extension.h" |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 30 | #include "testing/gtest/include/gtest/gtest.h" |
| 31 | #include "testing/platform_test.h" |
Evan Stade | 889ce471 | 2018-01-28 15:26:26 | [diff] [blame] | 32 | #include "ui/message_center/public/cpp/notification.h" |
brettw | 1f92eed | 2016-12-07 01:12:58 | [diff] [blame] | 33 | #include "url/gurl.h" |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 34 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 35 | class BackgroundContentsServiceTest : public testing::Test { |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 36 | public: |
| 37 | BackgroundContentsServiceTest() {} |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 38 | ~BackgroundContentsServiceTest() override {} |
| 39 | void SetUp() override { |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 40 | command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM)); |
skyostil | 0becb33 | 2015-04-27 17:59:37 | [diff] [blame] | 41 | BackgroundContentsService::DisableCloseBalloonForTesting(true); |
| 42 | } |
| 43 | |
| 44 | void TearDown() override { |
| 45 | BackgroundContentsService::DisableCloseBalloonForTesting(false); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 48 | const base::DictionaryValue* GetPrefs(Profile* profile) { |
[email protected] | f33bee5 | 2011-03-31 09:18:43 | [diff] [blame] | 49 | return profile->GetPrefs()->GetDictionary( |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 50 | prefs::kRegisteredBackgroundContents); |
| 51 | } |
| 52 | |
| 53 | // Returns the stored pref URL for the passed app id. |
Evan Stade | 092530b | 2017-11-21 17:17:05 | [diff] [blame] | 54 | std::string GetPrefURLForApp(Profile* profile, const std::string& appid) { |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 55 | const base::DictionaryValue* pref = GetPrefs(profile); |
Evan Stade | 092530b | 2017-11-21 17:17:05 | [diff] [blame] | 56 | EXPECT_TRUE(pref->HasKey(appid)); |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 57 | const base::DictionaryValue* value; |
Evan Stade | 092530b | 2017-11-21 17:17:05 | [diff] [blame] | 58 | pref->GetDictionaryWithoutPathExpansion(appid, &value); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 59 | std::string url; |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame] | 60 | value->GetString("url", &url); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 61 | return url; |
| 62 | } |
| 63 | |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 64 | content::BrowserTaskEnvironment task_environment_; |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 65 | std::unique_ptr<base::CommandLine> command_line_; |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | class MockBackgroundContents : public BackgroundContents { |
| 69 | public: |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 70 | MockBackgroundContents(BackgroundContentsService* service, |
| 71 | Profile* profile, |
| 72 | const std::string& id) |
| 73 | : service_(service), appid_(id), profile_(profile) {} |
| 74 | MockBackgroundContents(BackgroundContentsService* service, Profile* profile) |
| 75 | : MockBackgroundContents(service, profile, "app_id") {} |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 76 | |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 77 | void SendOpenedNotification() { |
Nigel Tao | 28338b8 | 2018-11-27 21:40:37 | [diff] [blame] | 78 | BackgroundContentsOpenedDetails details = {this, "background", appid_}; |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 79 | service_->BackgroundContentsOpened(&details, profile_); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 80 | } |
| 81 | |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 82 | void Navigate(GURL url) { |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 83 | url_ = url; |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 84 | service_->OnBackgroundContentsNavigated(this); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 85 | } |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 86 | const GURL& GetURL() const override { return url_; } |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 87 | |
| 88 | void MockClose(Profile* profile) { |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 89 | service_->OnBackgroundContentsClosed(this); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 90 | delete this; |
| 91 | } |
| 92 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 93 | ~MockBackgroundContents() override { |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 94 | service_->OnBackgroundContentsDeleted(this); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 95 | } |
| 96 | |
Evan Stade | 092530b | 2017-11-21 17:17:05 | [diff] [blame] | 97 | const std::string& appid() { return appid_; } |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 98 | |
| 99 | private: |
| 100 | GURL url_; |
| 101 | |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 102 | BackgroundContentsService* service_; |
| 103 | |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 104 | // The ID of our parent application |
Evan Stade | 092530b | 2017-11-21 17:17:05 | [diff] [blame] | 105 | std::string appid_; |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 106 | |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 107 | // Parent profile |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 108 | Profile* profile_; |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 109 | |
| 110 | DISALLOW_COPY_AND_ASSIGN(MockBackgroundContents); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 111 | }; |
| 112 | |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 113 | class BackgroundContentsServiceNotificationTest |
| 114 | : public BrowserWithTestWindowTest { |
| 115 | public: |
[email protected] | 5190a29 | 2014-02-26 15:50:32 | [diff] [blame] | 116 | BackgroundContentsServiceNotificationTest() {} |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 117 | ~BackgroundContentsServiceNotificationTest() override {} |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 118 | |
| 119 | // Overridden from testing::Test |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 120 | void SetUp() override { |
Evan Stade | ef1543f | 2017-11-17 20:20:13 | [diff] [blame] | 121 | BrowserWithTestWindowTest::SetUp(); |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 122 | display_service_ = |
| 123 | std::make_unique<NotificationDisplayServiceTester>(profile()); |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | protected: |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 127 | // Creates crash notification for the specified extension and returns |
| 128 | // the created one. |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 129 | const message_center::Notification CreateCrashNotification( |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 130 | scoped_refptr<extensions::Extension> extension) { |
juyik | 048931af | 2014-10-06 17:56:30 | [diff] [blame] | 131 | std::string notification_id = BackgroundContentsService:: |
| 132 | GetNotificationDelegateIdForExtensionForTesting(extension->id()); |
Nigel Tao | 28338b8 | 2018-11-27 21:40:37 | [diff] [blame] | 133 | BackgroundContentsService::ShowBalloonForTesting(extension.get(), |
| 134 | profile()); |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 135 | base::RunLoop run_loop; |
| 136 | display_service_->SetNotificationAddedClosure(run_loop.QuitClosure()); |
| 137 | run_loop.Run(); |
| 138 | display_service_->SetNotificationAddedClosure(base::RepeatingClosure()); |
| 139 | return *display_service_->GetNotification(notification_id); |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 140 | } |
| 141 | |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 142 | std::unique_ptr<NotificationDisplayServiceTester> display_service_; |
| 143 | |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 144 | private: |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 145 | DISALLOW_COPY_AND_ASSIGN(BackgroundContentsServiceNotificationTest); |
| 146 | }; |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 147 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 148 | TEST_F(BackgroundContentsServiceTest, Create) { |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 149 | // Check for creation and leaks. |
| 150 | TestingProfile profile; |
| 151 | BackgroundContentsService service(&profile, command_line_.get()); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) { |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 155 | TestingProfile profile; |
| 156 | BackgroundContentsService service(&profile, command_line_.get()); |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 157 | MockBackgroundContents* contents = |
| 158 | new MockBackgroundContents(&service, &profile); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 159 | EXPECT_FALSE(service.IsTracked(contents)); |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 160 | contents->SendOpenedNotification(); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 161 | EXPECT_TRUE(service.IsTracked(contents)); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 162 | delete contents; |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 163 | EXPECT_FALSE(service.IsTracked(contents)); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) { |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 167 | TestingProfile profile; |
| 168 | BackgroundContentsService service(&profile, command_line_.get()); |
Sylvain Defresne | 94a3752c | 2019-02-15 18:53:54 | [diff] [blame] | 169 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 170 | GURL orig_url; |
| 171 | GURL url("http://a/"); |
| 172 | GURL url2("http://a/"); |
| 173 | { |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 174 | std::unique_ptr<MockBackgroundContents> contents( |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 175 | new MockBackgroundContents(&service, &profile)); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 176 | EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 177 | contents->SendOpenedNotification(); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 178 | |
| 179 | contents->Navigate(url); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 180 | EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 181 | EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 182 | |
| 183 | // Navigate the contents to a new url, should not change url. |
| 184 | contents->Navigate(url2); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 185 | EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 186 | EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 187 | } |
| 188 | // Contents are deleted, url should persist. |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 189 | EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) { |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 193 | TestingProfile profile; |
| 194 | BackgroundContentsService service(&profile, command_line_.get()); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 195 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 196 | GURL url("http://a/"); |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 197 | MockBackgroundContents* contents = |
| 198 | new MockBackgroundContents(&service, &profile); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 199 | EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 200 | contents->SendOpenedNotification(); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 201 | contents->Navigate(url); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 202 | EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 203 | EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 204 | |
| 205 | // Fake a window closed by script. |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 206 | contents->MockClose(&profile); |
| 207 | EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Test what happens if a BackgroundContents shuts down (say, due to a renderer |
| 211 | // crash) then is restarted. Should not persist URL twice. |
| 212 | TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) { |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 213 | TestingProfile profile; |
| 214 | BackgroundContentsService service(&profile, command_line_.get()); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 215 | |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 216 | GURL url("http://a/"); |
| 217 | { |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 218 | std::unique_ptr<MockBackgroundContents> contents( |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 219 | new MockBackgroundContents(&service, &profile, "appid")); |
| 220 | contents->SendOpenedNotification(); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 221 | contents->Navigate(url); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 222 | EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 223 | EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 224 | } |
| 225 | // Contents deleted, url should be persisted. |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 226 | EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 227 | |
| 228 | { |
| 229 | // Reopen the BackgroundContents to the same URL, we should not register the |
| 230 | // URL again. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 231 | std::unique_ptr<MockBackgroundContents> contents( |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 232 | new MockBackgroundContents(&service, &profile, "appid")); |
| 233 | contents->SendOpenedNotification(); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 234 | contents->Navigate(url); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 235 | EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | // Ensures that BackgroundContentsService properly tracks the association |
| 240 | // between a BackgroundContents and its parent extension, including |
| 241 | // unregistering the BC when the extension is uninstalled. |
| 242 | TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) { |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 243 | TestingProfile profile; |
| 244 | BackgroundContentsService service(&profile, command_line_.get()); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 245 | |
Evan Stade | 092530b | 2017-11-21 17:17:05 | [diff] [blame] | 246 | EXPECT_EQ(NULL, service.GetAppBackgroundContents("appid")); |
Nigel Tao | 28338b8 | 2018-11-27 21:40:37 | [diff] [blame] | 247 | MockBackgroundContents* contents = |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 248 | new MockBackgroundContents(&service, &profile, "appid"); |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 249 | std::unique_ptr<MockBackgroundContents> contents2( |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 250 | new MockBackgroundContents(&service, &profile, "appid2")); |
| 251 | contents->SendOpenedNotification(); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 252 | EXPECT_EQ(contents, service.GetAppBackgroundContents(contents->appid())); |
Evan Stade | 6670c33 | 2019-06-18 20:56:12 | [diff] [blame] | 253 | contents2->SendOpenedNotification(); |
Nigel Tao | 28338b8 | 2018-11-27 21:40:37 | [diff] [blame] | 254 | EXPECT_EQ(contents2.get(), |
| 255 | service.GetAppBackgroundContents(contents2->appid())); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 256 | EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 257 | |
| 258 | // Navigate the contents, then make sure the one associated with the extension |
| 259 | // is unregistered. |
| 260 | GURL url("http://a/"); |
| 261 | GURL url2("http://b/"); |
| 262 | contents->Navigate(url); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 263 | EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 264 | contents2->Navigate(url2); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 265 | EXPECT_EQ(2U, GetPrefs(&profile)->size()); |
Evan Stade | 092530b | 2017-11-21 17:17:05 | [diff] [blame] | 266 | service.ShutdownAssociatedBackgroundContents("appid"); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 267 | EXPECT_FALSE(service.IsTracked(contents)); |
Evan Stade | 092530b | 2017-11-21 17:17:05 | [diff] [blame] | 268 | EXPECT_EQ(NULL, service.GetAppBackgroundContents("appid")); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 269 | EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 270 | EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); |
[email protected] | 398206c | 2010-06-21 01:46:08 | [diff] [blame] | 271 | } |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 272 | |
[email protected] | 44aae2f7 | 2014-02-28 14:40:09 | [diff] [blame] | 273 | TEST_F(BackgroundContentsServiceNotificationTest, TestShowBalloon) { |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 274 | scoped_refptr<extensions::Extension> extension = |
| 275 | extension_test_util::LoadManifest("image_loading_tracker", "app.json"); |
| 276 | ASSERT_TRUE(extension.get()); |
| 277 | ASSERT_TRUE(extension->GetManifestData("icons")); |
| 278 | |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 279 | const message_center::Notification notification = |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 280 | CreateCrashNotification(extension); |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 281 | EXPECT_FALSE(notification.icon().IsEmpty()); |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 282 | } |
| 283 | |
dgn | 49f1ca9 | 2016-05-26 20:02:36 | [diff] [blame] | 284 | TEST_F(BackgroundContentsServiceNotificationTest, TestShowBalloonShutdown) { |
| 285 | scoped_refptr<extensions::Extension> extension = |
| 286 | extension_test_util::LoadManifest("image_loading_tracker", "app.json"); |
| 287 | ASSERT_TRUE(extension.get()); |
| 288 | ASSERT_TRUE(extension->GetManifestData("icons")); |
| 289 | |
| 290 | std::string notification_id = BackgroundContentsService:: |
| 291 | GetNotificationDelegateIdForExtensionForTesting(extension->id()); |
| 292 | |
| 293 | static_cast<TestingBrowserProcess*>(g_browser_process)->SetShuttingDown(true); |
| 294 | BackgroundContentsService::ShowBalloonForTesting(extension.get(), profile()); |
| 295 | base::RunLoop().RunUntilIdle(); |
| 296 | static_cast<TestingBrowserProcess*>(g_browser_process) |
| 297 | ->SetShuttingDown(false); |
| 298 | |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 299 | EXPECT_FALSE(display_service_->GetNotification(notification_id)); |
dgn | 49f1ca9 | 2016-05-26 20:02:36 | [diff] [blame] | 300 | } |
| 301 | |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 302 | // Verify if a test notification can show the default extension icon for |
| 303 | // a crash notification for an extension without icon. |
[email protected] | 44aae2f7 | 2014-02-28 14:40:09 | [diff] [blame] | 304 | TEST_F(BackgroundContentsServiceNotificationTest, TestShowBalloonNoIcon) { |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 305 | // Extension manifest file with no 'icon' field. |
| 306 | scoped_refptr<extensions::Extension> extension = |
| 307 | extension_test_util::LoadManifest("app", "manifest.json"); |
| 308 | ASSERT_TRUE(extension.get()); |
| 309 | ASSERT_FALSE(extension->GetManifestData("icons")); |
| 310 | |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 311 | const message_center::Notification notification = |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 312 | CreateCrashNotification(extension); |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 313 | EXPECT_FALSE(notification.icon().IsEmpty()); |
[email protected] | 84695f2 | 2014-02-18 02:29:32 | [diff] [blame] | 314 | } |
[email protected] | 05d611865 | 2014-05-21 23:14:05 | [diff] [blame] | 315 | |
| 316 | TEST_F(BackgroundContentsServiceNotificationTest, TestShowTwoBalloons) { |
| 317 | TestingProfile profile; |
| 318 | scoped_refptr<extensions::Extension> extension = |
| 319 | extension_test_util::LoadManifest("app", "manifest.json"); |
| 320 | ASSERT_TRUE(extension.get()); |
| 321 | CreateCrashNotification(extension); |
| 322 | CreateCrashNotification(extension); |
| 323 | |
Evan Stade | 7af7199 | 2018-01-20 02:03:06 | [diff] [blame] | 324 | ASSERT_EQ(1u, display_service_ |
| 325 | ->GetDisplayedNotificationsForType( |
| 326 | NotificationHandler::Type::TRANSIENT) |
| 327 | .size()); |
[email protected] | 05d611865 | 2014-05-21 23:14:05 | [diff] [blame] | 328 | } |