| 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" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 EXPECT_FALSE(service.IsTracked(contents)); | 117 EXPECT_FALSE(service.IsTracked(contents)); |
| 118 contents->SendOpenedNotification(&service); | 118 contents->SendOpenedNotification(&service); |
| 119 EXPECT_TRUE(service.IsTracked(contents)); | 119 EXPECT_TRUE(service.IsTracked(contents)); |
| 120 delete contents; | 120 delete contents; |
| 121 EXPECT_FALSE(service.IsTracked(contents)); | 121 EXPECT_FALSE(service.IsTracked(contents)); |
| 122 } | 122 } |
| 123 | 123 |
| 124 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) { | 124 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAdded) { |
| 125 TestingProfile profile; | 125 TestingProfile profile; |
| 126 BackgroundContentsService service(&profile, command_line_.get()); | 126 BackgroundContentsService service(&profile, command_line_.get()); |
| 127 BackgroundContentsServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( | 127 BackgroundContentsServiceFactory::GetInstance()-> |
| 128 &profile); | 128 RegisterUserPrefsOnBrowserContext(&profile); |
| 129 GURL orig_url; | 129 GURL orig_url; |
| 130 GURL url("http://a/"); | 130 GURL url("http://a/"); |
| 131 GURL url2("http://a/"); | 131 GURL url2("http://a/"); |
| 132 { | 132 { |
| 133 scoped_ptr<MockBackgroundContents> contents( | 133 scoped_ptr<MockBackgroundContents> contents( |
| 134 new MockBackgroundContents(&profile)); | 134 new MockBackgroundContents(&profile)); |
| 135 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 135 EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
| 136 contents->SendOpenedNotification(&service); | 136 contents->SendOpenedNotification(&service); |
| 137 | 137 |
| 138 contents->Navigate(url); | 138 contents->Navigate(url); |
| 139 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 139 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 140 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 140 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
| 141 | 141 |
| 142 // Navigate the contents to a new url, should not change url. | 142 // Navigate the contents to a new url, should not change url. |
| 143 contents->Navigate(url2); | 143 contents->Navigate(url2); |
| 144 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 144 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 145 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 145 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
| 146 } | 146 } |
| 147 // Contents are deleted, url should persist. | 147 // Contents are deleted, url should persist. |
| 148 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 148 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 149 } | 149 } |
| 150 | 150 |
| 151 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) { | 151 TEST_F(BackgroundContentsServiceTest, BackgroundContentsUrlAddedAndClosed) { |
| 152 TestingProfile profile; | 152 TestingProfile profile; |
| 153 BackgroundContentsService service(&profile, command_line_.get()); | 153 BackgroundContentsService service(&profile, command_line_.get()); |
| 154 BackgroundContentsServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( | 154 BackgroundContentsServiceFactory::GetInstance()-> |
| 155 &profile); | 155 RegisterUserPrefsOnBrowserContext(&profile); |
| 156 | 156 |
| 157 GURL url("http://a/"); | 157 GURL url("http://a/"); |
| 158 MockBackgroundContents* contents = new MockBackgroundContents(&profile); | 158 MockBackgroundContents* contents = new MockBackgroundContents(&profile); |
| 159 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 159 EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
| 160 contents->SendOpenedNotification(&service); | 160 contents->SendOpenedNotification(&service); |
| 161 contents->Navigate(url); | 161 contents->Navigate(url); |
| 162 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 162 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 163 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 163 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
| 164 | 164 |
| 165 // Fake a window closed by script. | 165 // Fake a window closed by script. |
| 166 contents->MockClose(&profile); | 166 contents->MockClose(&profile); |
| 167 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 167 EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
| 168 } | 168 } |
| 169 | 169 |
| 170 // 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 |
| 171 // crash) then is restarted. Should not persist URL twice. | 171 // crash) then is restarted. Should not persist URL twice. |
| 172 TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) { | 172 TEST_F(BackgroundContentsServiceTest, RestartBackgroundContents) { |
| 173 TestingProfile profile; | 173 TestingProfile profile; |
| 174 BackgroundContentsService service(&profile, command_line_.get()); | 174 BackgroundContentsService service(&profile, command_line_.get()); |
| 175 BackgroundContentsServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( | 175 BackgroundContentsServiceFactory::GetInstance()-> |
| 176 &profile); | 176 RegisterUserPrefsOnBrowserContext(&profile); |
| 177 | 177 |
| 178 GURL url("http://a/"); | 178 GURL url("http://a/"); |
| 179 { | 179 { |
| 180 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( | 180 scoped_ptr<MockBackgroundContents> contents(new MockBackgroundContents( |
| 181 &profile, "appid")); | 181 &profile, "appid")); |
| 182 contents->SendOpenedNotification(&service); | 182 contents->SendOpenedNotification(&service); |
| 183 contents->Navigate(url); | 183 contents->Navigate(url); |
| 184 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 184 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 185 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); | 185 EXPECT_EQ(url.spec(), GetPrefURLForApp(&profile, contents->appid())); |
| 186 } | 186 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 197 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 197 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 | 200 |
| 201 // Ensures that BackgroundContentsService properly tracks the association | 201 // Ensures that BackgroundContentsService properly tracks the association |
| 202 // between a BackgroundContents and its parent extension, including | 202 // between a BackgroundContents and its parent extension, including |
| 203 // unregistering the BC when the extension is uninstalled. | 203 // unregistering the BC when the extension is uninstalled. |
| 204 TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) { | 204 TEST_F(BackgroundContentsServiceTest, TestApplicationIDLinkage) { |
| 205 TestingProfile profile; | 205 TestingProfile profile; |
| 206 BackgroundContentsService service(&profile, command_line_.get()); | 206 BackgroundContentsService service(&profile, command_line_.get()); |
| 207 BackgroundContentsServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( | 207 BackgroundContentsServiceFactory::GetInstance()-> |
| 208 &profile); | 208 RegisterUserPrefsOnBrowserContext(&profile); |
| 209 | 209 |
| 210 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); | 210 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); |
| 211 MockBackgroundContents* contents = new MockBackgroundContents(&profile, | 211 MockBackgroundContents* contents = new MockBackgroundContents(&profile, |
| 212 "appid"); | 212 "appid"); |
| 213 scoped_ptr<MockBackgroundContents> contents2( | 213 scoped_ptr<MockBackgroundContents> contents2( |
| 214 new MockBackgroundContents(&profile, "appid2")); | 214 new MockBackgroundContents(&profile, "appid2")); |
| 215 contents->SendOpenedNotification(&service); | 215 contents->SendOpenedNotification(&service); |
| 216 EXPECT_EQ(contents, service.GetAppBackgroundContents(contents->appid())); | 216 EXPECT_EQ(contents, service.GetAppBackgroundContents(contents->appid())); |
| 217 contents2->SendOpenedNotification(&service); | 217 contents2->SendOpenedNotification(&service); |
| 218 EXPECT_EQ(contents2.get(), service.GetAppBackgroundContents( | 218 EXPECT_EQ(contents2.get(), service.GetAppBackgroundContents( |
| 219 contents2->appid())); | 219 contents2->appid())); |
| 220 EXPECT_EQ(0U, GetPrefs(&profile)->size()); | 220 EXPECT_EQ(0U, GetPrefs(&profile)->size()); |
| 221 | 221 |
| 222 // 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 |
| 223 // is unregistered. | 223 // is unregistered. |
| 224 GURL url("http://a/"); | 224 GURL url("http://a/"); |
| 225 GURL url2("http://b/"); | 225 GURL url2("http://b/"); |
| 226 contents->Navigate(url); | 226 contents->Navigate(url); |
| 227 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 227 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 228 contents2->Navigate(url2); | 228 contents2->Navigate(url2); |
| 229 EXPECT_EQ(2U, GetPrefs(&profile)->size()); | 229 EXPECT_EQ(2U, GetPrefs(&profile)->size()); |
| 230 service.ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); | 230 service.ShutdownAssociatedBackgroundContents(ASCIIToUTF16("appid")); |
| 231 EXPECT_FALSE(service.IsTracked(contents)); | 231 EXPECT_FALSE(service.IsTracked(contents)); |
| 232 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); | 232 EXPECT_EQ(NULL, service.GetAppBackgroundContents(ASCIIToUTF16("appid"))); |
| 233 EXPECT_EQ(1U, GetPrefs(&profile)->size()); | 233 EXPECT_EQ(1U, GetPrefs(&profile)->size()); |
| 234 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); | 234 EXPECT_EQ(url2.spec(), GetPrefURLForApp(&profile, contents2->appid())); |
| 235 } | 235 } |
| OLD | NEW |