Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: trunk/src/chrome/browser/background/background_contents_service_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698