| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/background/background_contents_service.h" | 12 #include "chrome/browser/background/background_contents_service.h" |
| 13 #include "chrome/browser/background/background_contents_service_factory.h" | 13 #include "chrome/browser/background/background_contents_service_factory.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 16 #include "chrome/browser/tab_contents/background_contents.h" | 16 #include "chrome/browser/tab_contents/background_contents.h" |
| 17 #include "chrome/browser/ui/browser_list.h" | 17 #include "chrome/browser/ui/browser_list.h" |
| 18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/test/base/testing_browser_process.h" | 19 #include "chrome/test/base/testing_browser_process.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
| 25 #include "ui/message_center/message_center.h" |
| 24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 25 | 27 |
| 26 class BackgroundContentsServiceTest : public testing::Test { | 28 class BackgroundContentsServiceTest : public testing::Test { |
| 27 public: | 29 public: |
| 28 BackgroundContentsServiceTest() {} | |
| 29 virtual ~BackgroundContentsServiceTest() {} | |
| 30 virtual void SetUp() { | 30 virtual void SetUp() { |
| 31 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); | 31 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); |
| 32 } | 32 } |
| 33 | 33 |
| 34 static void SetUpTestCase() { |
| 35 message_center::MessageCenter::Initialize(); |
| 36 } |
| 37 static void TearDownTestCase() { |
| 38 message_center::MessageCenter::Shutdown(); |
| 39 } |
| 40 |
| 34 const DictionaryValue* GetPrefs(Profile* profile) { | 41 const DictionaryValue* GetPrefs(Profile* profile) { |
| 35 return profile->GetPrefs()->GetDictionary( | 42 return profile->GetPrefs()->GetDictionary( |
| 36 prefs::kRegisteredBackgroundContents); | 43 prefs::kRegisteredBackgroundContents); |
| 37 } | 44 } |
| 38 | 45 |
| 39 // Returns the stored pref URL for the passed app id. | 46 // Returns the stored pref URL for the passed app id. |
| 40 std::string GetPrefURLForApp(Profile* profile, const string16& appid) { | 47 std::string GetPrefURLForApp(Profile* profile, const string16& appid) { |
| 41 const DictionaryValue* pref = GetPrefs(profile); | 48 const DictionaryValue* pref = GetPrefs(profile); |
| 42 EXPECT_TRUE(pref->HasKey(UTF16ToUTF8(appid))); | 49 EXPECT_TRUE(pref->HasKey(UTF16ToUTF8(appid))); |
| 43 const DictionaryValue* value; | 50 const DictionaryValue* value; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 106 |
| 100 // The ID of our parent application | 107 // The ID of our parent application |
| 101 string16 appid_; | 108 string16 appid_; |
| 102 | 109 |
| 103 // Parent profile | 110 // Parent profile |
| 104 Profile* profile_; | 111 Profile* profile_; |
| 105 }; | 112 }; |
| 106 | 113 |
| 107 TEST_F(BackgroundContentsServiceTest, Create) { | 114 TEST_F(BackgroundContentsServiceTest, Create) { |
| 108 // Check for creation and leaks. | 115 // Check for creation and leaks. |
| 116 content::TestBrowserThreadBundle thread_bundle; |
| 109 TestingProfile profile; | 117 TestingProfile profile; |
| 110 BackgroundContentsService service(&profile, command_line_.get()); | 118 BackgroundContentsService service(&profile, command_line_.get()); |
| 111 } | 119 } |
| 112 | 120 |
| 113 TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) { | 121 TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) { |
| 122 content::TestBrowserThreadBundle thread_bundle; |
| 114 TestingProfile profile; | 123 TestingProfile profile; |
| 115 BackgroundContentsService service(&profile, command_line_.get()); | 124 BackgroundContentsService service(&profile, command_line_.get()); |
| 116 MockBackgroundContents* contents = new MockBackgroundContents(&profile); | 125 MockBackgroundContents* contents = new MockBackgroundContents(&profile); |
| 117 EXPECT_FALSE(service.IsTracked(contents)); | 126 EXPECT_FALSE(service.IsTracked(contents)); |
| 118 contents->SendOpenedNotification(&service); | 127 contents->SendOpenedNotification(&service); |
| 119 EXPECT_TRUE(service.IsTracked(contents)); | 128 EXPECT_TRUE(service.IsTracked(contents)); |
| 120 delete contents; | 129 delete contents; |
| 121 EXPECT_FALSE(service.IsTracked(contents)); | 130 EXPECT_FALSE(service.IsTracked(contents)); |
| 122 } | 131 } |
| 123 | 132 |
| 124 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) { | 133 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) { |
| 134 content::TestBrowserThreadBundle thread_bundle; |
| 125 TestingProfile profile; | 135 TestingProfile profile; |
| 126 BackgroundContentsService service(&profile, command_line_.get()); | 136 BackgroundContentsService service(&profile, command_line_.get()); |
| 127 BackgroundContentsServiceFactory::GetInstance()-> | 137 BackgroundContentsServiceFactory::GetInstance()-> |
| 128 RegisterUserPrefsOnBrowserContext(&profile); | 138 RegisterUserPrefsOnBrowserContext(&profile); |
| 129 GURL orig_url; | 139 GURL orig_url; |
| 130 GURL url("http://a/"); | 140 GURL url("http://a/"); |
| 131 GURL url2("http://a/"); | 141 GURL url2("http://a/"); |
| 132 { | 142 { |
| 133 scoped_ptr<MockBackgroundContents> contents( | 143 scoped_ptr<MockBackgroundContents> contents( |
| 134 new MockBackgroundContents(&profile)); | 144 new MockBackgroundContents(&profile)); |
| 135 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 145 EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
| 136 contents->SendOpenedNotification(&service); | 146 contents->SendOpenedNotification(&service); |
| 137 | 147 |
| 138 contents->Navigate(url); | 148 contents->Navigate(url); |
| 139 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 149 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 140 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 150 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
| 141 | 151 |
| 142 // Navigate the contents to a new url, should not change url. | 152 // Navigate the contents to a new url, should not change url. |
| 143 contents->Navigate(url2); | 153 contents->Navigate(url2); |
| 144 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 154 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 145 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 155 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
| 146 } | 156 } |
| 147 // Contents are deleted, url should persist. | 157 // Contents are deleted, url should persist. |
| 148 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 158 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 149 } | 159 } |
| 150 | 160 |
| 151 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) { | 161 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) { |
| 162 content::TestBrowserThreadBundle thread_bundle; |
| 152 TestingProfile profile; | 163 TestingProfile profile; |
| 153 BackgroundContentsService service(&profile, command_line_.get()); | 164 BackgroundContentsService service(&profile, command_line_.get()); |
| 154 BackgroundContentsServiceFactory::GetInstance()-> | 165 BackgroundContentsServiceFactory::GetInstance()-> |
| 155 RegisterUserPrefsOnBrowserContext(&profile); | 166 RegisterUserPrefsOnBrowserContext(&profile); |
| 156 | 167 |
| 157 GURL url("http://a/"); | 168 GURL url("http://a/"); |
| 158 MockBackgroundContents* contents = new MockBackgroundContents(&profile); | 169 MockBackgroundContents* contents = new MockBackgroundContents(&profile); |
| 159 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 170 EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
| 160 contents->SendOpenedNotification(&service); | 171 contents->SendOpenedNotification(&service); |
| 161 contents->Navigate(url); | 172 contents->Navigate(url); |
| 162 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 173 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 163 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 174 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
| 164 | 175 |
| 165 // Fake a window closed by script. | 176 // Fake a window closed by script. |
| 166 contents->MockClose(&profile); | 177 contents->MockClose(&profile); |
| 167 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 178 EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
| 168 } | 179 } |
| 169 | 180 |
| 170 // Test what happens if a BackgroundContents shuts down (say, due to a renderer | 181 // Test what happens if a BackgroundContents shuts down (say, due to a renderer |
| 171 // crash) then is restarted. Should not persist URL twice. | 182 // crash) then is restarted. Should not persist URL twice. |
| 172 TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) { | 183 TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) { |
| 184 content::TestBrowserThreadBundle thread_bundle; |
| 173 TestingProfile profile; | 185 TestingProfile profile; |
| 174 BackgroundContentsService service(&profile, command_line_.get()); | 186 BackgroundContentsService service(&profile, command_line_.get()); |
| 175 BackgroundContentsServiceFactory::GetInstance()-> | 187 BackgroundContentsServiceFactory::GetInstance()-> |
| 176 RegisterUserPrefsOnBrowserContext(&profile); | 188 RegisterUserPrefsOnBrowserContext(&profile); |
| 177 | 189 |
| 178 GURL url("http://a/"); | 190 GURL url("http://a/"); |
| 179 { | 191 { |
| 180 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( | 192 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( |
| 181 &profile, "appid")); | 193 &profile, "appid")); |
| 182 contents->SendOpenedNotification(&service); | 194 contents->SendOpenedNotification(&service); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 195 contents->SendOpenedNotification(&service); | 207 contents->SendOpenedNotification(&service); |
| 196 contents->Navigate(url); | 208 contents->Navigate(url); |
| 197 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 209 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 198 } | 210 } |
| 199 } | 211 } |
| 200 | 212 |
| 201 // Ensures that BackgroundContentsService properly tracks the association | 213 // Ensures that BackgroundContentsService properly tracks the association |
| 202 // between a BackgroundContents and its parent extension, including | 214 // between a BackgroundContents and its parent extension, including |
| 203 // unregistering the BC when the extension is uninstalled. | 215 // unregistering the BC when the extension is uninstalled. |
| 204 TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) { | 216 TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) { |
| 217 content::TestBrowserThreadBundle thread_bundle; |
| 205 TestingProfile profile; | 218 TestingProfile profile; |
| 206 BackgroundContentsService service(&profile, command_line_.get()); | 219 BackgroundContentsService service(&profile, command_line_.get()); |
| 207 BackgroundContentsServiceFactory::GetInstance()-> | 220 BackgroundContentsServiceFactory::GetInstance()-> |
| 208 RegisterUserPrefsOnBrowserContext(&profile); | 221 RegisterUserPrefsOnBrowserContext(&profile); |
| 209 | 222 |
| 210 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); | 223 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); |
| 211 MockBackgroundContents* contents = new MockBackgroundContents(&profile, | 224 MockBackgroundContents* contents = new MockBackgroundContents(&profile, |
| 212 "appid"); | 225 "appid"); |
| 213 scoped_ptr<MockBackgroundContents> contents2( | 226 scoped_ptr<MockBackgroundContents> contents2( |
| 214 new MockBackgroundContents(&profile, "appid2")); | 227 new MockBackgroundContents(&profile, "appid2")); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 226 contents->Navigate(url); | 239 contents->Navigate(url); |
| 227 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 240 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 228 contents2->Navigate(url2); | 241 contents2->Navigate(url2); |
| 229 EXPECT_EQ(2U, GetPrefs(&profile)->size()); | 242 EXPECT_EQ(2U, GetPrefs(&profile)->size()); |
| 230 service.ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); | 243 service.ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); |
| 231 EXPECT_FALSE(service.IsTracked(contents)); | 244 EXPECT_FALSE(service.IsTracked(contents)); |
| 232 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); | 245 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); |
| 233 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 246 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 234 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); | 247 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); |
| 235 } | 248 } |
| OLD | NEW |