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/run_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/background/background_contents_service.h" | 13 #include "chrome/browser/background/background_contents_service.h" |
13 #include "chrome/browser/background/background_contents_service_factory.h" | 14 #include "chrome/browser/background/background_contents_service_factory.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 16 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
16 #include "chrome/browser/tab_contents/background_contents.h" | 17 #include "chrome/browser/tab_contents/background_contents.h" |
17 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/test/base/scoped_testing_local_state.h" |
19 #include "chrome/test/base/testing_browser_process.h" | 21 #include "chrome/test/base/testing_browser_process.h" |
20 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
| 23 #include "chrome/test/base/testing_profile_manager.h" |
21 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/test/test_browser_thread_bundle.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "testing/platform_test.h" | 27 #include "testing/platform_test.h" |
| 28 #include "ui/message_center/message_center.h" |
24 #include "url/gurl.h" | 29 #include "url/gurl.h" |
25 | 30 |
26 class BackgroundContentsServiceTest : public testing::Test { | 31 class BackgroundContentsServiceTest : public testing::Test { |
27 public: | 32 protected: |
28 BackgroundContentsServiceTest() {} | 33 BackgroundContentsServiceTest() |
29 virtual ~BackgroundContentsServiceTest() {} | 34 : command_line_(CommandLine::NO_PROGRAM), |
30 virtual void SetUp() { | 35 profile_manager_(TestingBrowserProcess::GetGlobal()), |
31 command_line_.reset(new CommandLine(CommandLine::NO_PROGRAM)); | 36 profile_(NULL) { |
| 37 CHECK(profile_manager_.SetUp()); |
| 38 profile_ = profile_manager_.CreateTestingProfile("TestProfile"); |
| 39 service_.reset(new BackgroundContentsService(profile_, &command_line_)); |
| 40 BackgroundContentsServiceFactory::GetInstance()-> |
| 41 RegisterUserPrefsOnBrowserContext(profile_); |
| 42 } |
| 43 |
| 44 virtual ~BackgroundContentsServiceTest() { |
| 45 base::RunLoop().RunUntilIdle(); |
| 46 } |
| 47 |
| 48 static void SetUpTestCase() { |
| 49 message_center::MessageCenter::Initialize(); |
| 50 } |
| 51 static void TearDownTestCase() { |
| 52 message_center::MessageCenter::Shutdown(); |
32 } | 53 } |
33 | 54 |
34 const DictionaryValue* GetPrefs(Profile* profile) { | 55 const DictionaryValue* GetPrefs(Profile* profile) { |
35 return profile->GetPrefs()->GetDictionary( | 56 return profile->GetPrefs()->GetDictionary( |
36 prefs::kRegisteredBackgroundContents); | 57 prefs::kRegisteredBackgroundContents); |
37 } | 58 } |
38 | 59 |
39 // Returns the stored pref URL for the passed app id. | 60 // Returns the stored pref URL for the passed app id. |
40 std::string GetPrefURLForApp(Profile* profile, const string16& appid) { | 61 std::string GetPrefURLForApp(Profile* profile, const string16& appid) { |
41 const DictionaryValue* pref = GetPrefs(profile); | 62 const DictionaryValue* pref = GetPrefs(profile); |
42 EXPECT_TRUE(pref->HasKey(UTF16ToUTF8(appid))); | 63 EXPECT_TRUE(pref->HasKey(UTF16ToUTF8(appid))); |
43 const DictionaryValue* value; | 64 const DictionaryValue* value; |
44 pref->GetDictionaryWithoutPathExpansion(UTF16ToUTF8(appid), &value); | 65 pref->GetDictionaryWithoutPathExpansion(UTF16ToUTF8(appid), &value); |
45 std::string url; | 66 std::string url; |
46 value->GetString("url", &url); | 67 value->GetString("url", &url); |
47 return url; | 68 return url; |
48 } | 69 } |
49 | 70 |
50 scoped_ptr<CommandLine> command_line_; | 71 content::TestBrowserThreadBundle thread_bundle; |
| 72 CommandLine command_line_; |
| 73 TestingProfileManager profile_manager_; |
| 74 TestingProfile* profile_; // Not owned. |
| 75 scoped_ptr<BackgroundContentsService> service_; |
51 }; | 76 }; |
52 | 77 |
53 class MockBackgroundContents : public BackgroundContents { | 78 class MockBackgroundContents : public BackgroundContents { |
54 public: | 79 public: |
55 explicit MockBackgroundContents(Profile* profile) | 80 explicit MockBackgroundContents(Profile* profile) |
56 : appid_(ASCIIToUTF16("app_id")), | 81 : appid_(ASCIIToUTF16("app_id")), |
57 profile_(profile) { | 82 profile_(profile) { |
58 } | 83 } |
59 MockBackgroundContents(Profile* profile, const std::string& id) | 84 MockBackgroundContents(Profile* profile, const std::string& id) |
60 : appid_(ASCIIToUTF16(id)), | 85 : appid_(ASCIIToUTF16(id)), |
(...skipping 29 matching lines...) Expand all Loading... |
90 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, | 115 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED, |
91 content::Source<Profile>(profile_), | 116 content::Source<Profile>(profile_), |
92 content::Details<BackgroundContents>(this)); | 117 content::Details<BackgroundContents>(this)); |
93 } | 118 } |
94 | 119 |
95 const string16& appid() { return appid_; } | 120 const string16& appid() { return appid_; } |
96 | 121 |
97 private: | 122 private: |
98 GURL url_; | 123 GURL url_; |
99 | 124 |
100 // The ID of our parent application | 125 // The ID of our parent application. |
101 string16 appid_; | 126 string16 appid_; |
102 | 127 |
103 // Parent profile | 128 // Parent profile. Not owned. |
104 Profile* profile_; | 129 Profile* profile_; |
105 }; | 130 }; |
106 | 131 |
107 TEST_F(BackgroundContentsServiceTest, Create) { | 132 TEST_F(BackgroundContentsServiceTest, Create) { |
108 // Check for creation and leaks. | 133 // Check for creation and leaks when the basic objects in the |
109 TestingProfile profile; | 134 // fixtures are created/destructed. |
110 BackgroundContentsService service(&profile, command_line_.get()); | |
111 } | 135 } |
112 | 136 |
113 TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) { | 137 TEST_F(BackgroundContentsServiceTest, BackgroundContentsCreateDestroy) { |
114 TestingProfile profile; | 138 MockBackgroundContents* contents = new MockBackgroundContents(profile_); |
115 BackgroundContentsService service(&profile, command_line_.get()); | 139 EXPECT_FALSE(service_->IsTracked(contents)); |
116 MockBackgroundContents* contents = new MockBackgroundContents(&profile); | 140 contents->SendOpenedNotification(service_.get()); |
117 EXPECT_FALSE(service.IsTracked(contents)); | 141 EXPECT_TRUE(service_->IsTracked(contents)); |
118 contents->SendOpenedNotification(&service); | |
119 EXPECT_TRUE(service.IsTracked(contents)); | |
120 delete contents; | 142 delete contents; |
121 EXPECT_FALSE(service.IsTracked(contents)); | 143 EXPECT_FALSE(service_->IsTracked(contents)); |
122 } | 144 } |
123 | 145 |
124 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) { | 146 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) { |
125 TestingProfile profile; | |
126 BackgroundContentsService service(&profile, command_line_.get()); | |
127 BackgroundContentsServiceFactory::GetInstance()-> | |
128 RegisterUserPrefsOnBrowserContext(&profile); | |
129 GURL orig_url; | 147 GURL orig_url; |
130 GURL url("http://a/"); | 148 GURL url("http://a/"); |
131 GURL url2("http://a/"); | 149 GURL url2("http://a/"); |
132 { | 150 { |
133 scoped_ptr<MockBackgroundContents> contents( | 151 scoped_ptr<MockBackgroundContents> contents( |
134 new MockBackgroundContents(&profile)); | 152 new MockBackgroundContents(profile_)); |
135 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 153 EXPECT_EQ(0U, GetPrefs(profile_)->size()); |
136 contents->SendOpenedNotification(&service); | 154 contents->SendOpenedNotification(service_.get()); |
137 | 155 |
138 contents->Navigate(url); | 156 contents->Navigate(url); |
139 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 157 EXPECT_EQ(1U, GetPrefs(profile_)->size()); |
140 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 158 EXPECT_EQ(url.spec(), GetPrefURLForApp(profile_, contents->appid())); |
141 | 159 |
142 // Navigate the contents to a new url, should not change url. | 160 // Navigate the contents to a new url, should not change url. |
143 contents->Navigate(url2); | 161 contents->Navigate(url2); |
144 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 162 EXPECT_EQ(1U, GetPrefs(profile_)->size()); |
145 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 163 EXPECT_EQ(url.spec(), GetPrefURLForApp(profile_, contents->appid())); |
146 } | 164 } |
147 // Contents are deleted, url should persist. | 165 // Contents are deleted, url should persist. |
148 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 166 EXPECT_EQ(1U, GetPrefs(profile_)->size()); |
149 } | 167 } |
150 | 168 |
151 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) { | 169 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) { |
152 TestingProfile profile; | |
153 BackgroundContentsService service(&profile, command_line_.get()); | |
154 BackgroundContentsServiceFactory::GetInstance()-> | |
155 RegisterUserPrefsOnBrowserContext(&profile); | |
156 | |
157 GURL url("http://a/"); | 170 GURL url("http://a/"); |
158 MockBackgroundContents* contents = new MockBackgroundContents(&profile); | 171 MockBackgroundContents* contents = new MockBackgroundContents(profile_); |
159 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 172 EXPECT_EQ(0U, GetPrefs(profile_)->size()); |
160 contents->SendOpenedNotification(&service); | 173 contents->SendOpenedNotification(service_.get()); |
161 contents->Navigate(url); | 174 contents->Navigate(url); |
162 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 175 EXPECT_EQ(1U, GetPrefs(profile_)->size()); |
163 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 176 EXPECT_EQ(url.spec(), GetPrefURLForApp(profile_, contents->appid())); |
164 | 177 |
165 // Fake a window closed by script. | 178 // Fake a window closed by script. |
166 contents->MockClose(&profile); | 179 contents->MockClose(profile_); |
167 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 180 EXPECT_EQ(0U, GetPrefs(profile_)->size()); |
168 } | 181 } |
169 | 182 |
170 // Test what happens if a BackgroundContents shuts down (say, due to a renderer | 183 // Test what happens if a BackgroundContents shuts down (say, due to a renderer |
171 // crash) then is restarted. Should not persist URL twice. | 184 // crash) then is restarted. Should not persist URL twice. |
172 TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) { | 185 TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) { |
173 TestingProfile profile; | |
174 BackgroundContentsService service(&profile, command_line_.get()); | |
175 BackgroundContentsServiceFactory::GetInstance()-> | |
176 RegisterUserPrefsOnBrowserContext(&profile); | |
177 | |
178 GURL url("http://a/"); | 186 GURL url("http://a/"); |
179 { | 187 { |
180 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( | 188 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( |
181 &profile, "appid")); | 189 profile_, "appid")); |
182 contents->SendOpenedNotification(&service); | 190 contents->SendOpenedNotification(service_.get()); |
183 contents->Navigate(url); | 191 contents->Navigate(url); |
184 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 192 EXPECT_EQ(1U, GetPrefs(profile_)->size()); |
185 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 193 EXPECT_EQ(url.spec(), GetPrefURLForApp(profile_, contents->appid())); |
186 } | 194 } |
187 // Contents deleted, url should be persisted. | 195 // Contents deleted, url should be persisted. |
188 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 196 EXPECT_EQ(1U, GetPrefs(profile_)->size()); |
189 | 197 |
190 { | 198 { |
191 // Reopen the BackgroundContents to the same URL, we should not register the | 199 // Reopen the BackgroundContents to the same URL, we should not register the |
192 // URL again. | 200 // URL again. |
193 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( | 201 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( |
194 &profile, "appid")); | 202 profile_, "appid")); |
195 contents->SendOpenedNotification(&service); | 203 contents->SendOpenedNotification(service_.get()); |
196 contents->Navigate(url); | 204 contents->Navigate(url); |
197 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 205 EXPECT_EQ(1U, GetPrefs(profile_)->size()); |
198 } | 206 } |
199 } | 207 } |
200 | 208 |
201 // Ensures that BackgroundContentsService properly tracks the association | 209 // Ensures that BackgroundContentsService properly tracks the association |
202 // between a BackgroundContents and its parent extension, including | 210 // between a BackgroundContents and its parent extension, including |
203 // unregistering the BC when the extension is uninstalled. | 211 // unregistering the BC when the extension is uninstalled. |
204 TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) { | 212 TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) { |
205 TestingProfile profile; | 213 EXPECT_EQ(NULL, service_->GetAppBackgroundContents(ASCIIToUTF16("appid"))); |
206 BackgroundContentsService service(&profile, command_line_.get()); | 214 MockBackgroundContents* contents = new MockBackgroundContents(profile_, |
207 BackgroundContentsServiceFactory::GetInstance()-> | |
208 RegisterUserPrefsOnBrowserContext(&profile); | |
209 | |
210 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); | |
211 MockBackgroundContents* contents = new MockBackgroundContents(&profile, | |
212 "appid"); | 215 "appid"); |
213 scoped_ptr<MockBackgroundContents> contents2( | 216 scoped_ptr<MockBackgroundContents> contents2( |
214 new MockBackgroundContents(&profile, "appid2")); | 217 new MockBackgroundContents(profile_, "appid2")); |
215 contents->SendOpenedNotification(&service); | 218 contents->SendOpenedNotification(service_.get()); |
216 EXPECT_EQ(contents, service.GetAppBackgroundContents(contents->appid())); | 219 EXPECT_EQ(contents, service_->GetAppBackgroundContents(contents->appid())); |
217 contents2->SendOpenedNotification(&service); | 220 contents2->SendOpenedNotification(service_.get()); |
218 EXPECT_EQ(contents2.get(), service.GetAppBackgroundContents( | 221 EXPECT_EQ(contents2.get(), service_->GetAppBackgroundContents( |
219 contents2->appid())); | 222 contents2->appid())); |
220 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 223 EXPECT_EQ(0U, GetPrefs(profile_)->size()); |
221 | 224 |
222 // Navigate the contents, then make sure the one associated with the extension | 225 // Navigate the contents, then make sure the one associated with the extension |
223 // is unregistered. | 226 // is unregistered. |
224 GURL url("http://a/"); | 227 GURL url("http://a/"); |
225 GURL url2("http://b/"); | 228 GURL url2("http://b/"); |
226 contents->Navigate(url); | 229 contents->Navigate(url); |
227 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 230 EXPECT_EQ(1U, GetPrefs(profile_)->size()); |
228 contents2->Navigate(url2); | 231 contents2->Navigate(url2); |
229 EXPECT_EQ(2U, GetPrefs(&profile)->size()); | 232 EXPECT_EQ(2U, GetPrefs(profile_)->size()); |
230 service.ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); | 233 service_->ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); |
231 EXPECT_FALSE(service.IsTracked(contents)); | 234 EXPECT_FALSE(service_->IsTracked(contents)); |
232 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); | 235 EXPECT_EQ(NULL, service_->GetAppBackgroundContents(ASCIIToUTF16("appid"))); |
233 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 236 EXPECT_EQ(1U, GetPrefs(profile_)->size()); |
234 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); | 237 EXPECT_EQ(url2.spec(), GetPrefURLForApp(profile_, contents2->appid())); |
235 } | 238 } |
OLD | NEW |