| 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 "chrome/browser/metrics/variations/variations_service.h" | 5 #include "chrome/browser/metrics/variations/variations_service.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/prefs/testing_pref_service.h" | 10 #include "base/prefs/testing_pref_service.h" |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 // Simulates a variations service response by setting a date header and the | 144 // Simulates a variations service response by setting a date header and the |
| 145 // specified HTTP |response_code| on |fetcher|. | 145 // specified HTTP |response_code| on |fetcher|. |
| 146 void SimulateServerResponse(int response_code, net::TestURLFetcher* fetcher) { | 146 void SimulateServerResponse(int response_code, net::TestURLFetcher* fetcher) { |
| 147 ASSERT_TRUE(fetcher); | 147 ASSERT_TRUE(fetcher); |
| 148 scoped_refptr<net::HttpResponseHeaders> headers( | 148 scoped_refptr<net::HttpResponseHeaders> headers( |
| 149 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0")); | 149 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0")); |
| 150 fetcher->set_response_headers(headers); | 150 fetcher->set_response_headers(headers); |
| 151 fetcher->set_response_code(response_code); | 151 fetcher->set_response_code(response_code); |
| 152 } | 152 } |
| 153 | 153 |
| 154 // Helper class that abstracts away platform-specific details relating to the |
| 155 // pref store used for the "restrict" param policy. |
| 156 class TestVariationsPrefsStore { |
| 157 public: |
| 158 TestVariationsPrefsStore() { |
| 159 #if defined(OS_ANDROID) |
| 160 // Android uses profile prefs as the PrefService to generate the URL. |
| 161 VariationsService::RegisterProfilePrefs(prefs_.registry()); |
| 162 #else |
| 163 VariationsService::RegisterPrefs(prefs_.registry()); |
| 164 #endif |
| 165 |
| 166 #if defined(OS_CHROMEOS) |
| 167 cros_settings_ = chromeos::CrosSettings::Get(); |
| 168 DCHECK(cros_settings_ != NULL); |
| 169 // Remove the real DeviceSettingsProvider and replace it with a stub that |
| 170 // allows modifications in a test. |
| 171 // TODO(asvitkine): Make a scoped helper class for this operation. |
| 172 device_settings_provider_ = cros_settings_->GetProvider( |
| 173 chromeos::kReportDeviceVersionInfo); |
| 174 EXPECT_TRUE(device_settings_provider_ != NULL); |
| 175 EXPECT_TRUE(cros_settings_->RemoveSettingsProvider( |
| 176 device_settings_provider_)); |
| 177 cros_settings_->AddSettingsProvider(&stub_settings_provider_); |
| 178 #endif |
| 179 } |
| 180 |
| 181 ~TestVariationsPrefsStore() { |
| 182 #if defined(OS_CHROMEOS) |
| 183 // Restore the real DeviceSettingsProvider. |
| 184 EXPECT_TRUE( |
| 185 cros_settings_->RemoveSettingsProvider(&stub_settings_provider_)); |
| 186 cros_settings_->AddSettingsProvider(device_settings_provider_); |
| 187 #endif |
| 188 } |
| 189 |
| 190 void SetVariationsRestrictParameterPolicyValue(const std::string& value) { |
| 191 #if defined(OS_CHROMEOS) |
| 192 cros_settings_->SetString(chromeos::kVariationsRestrictParameter, value); |
| 193 #else |
| 194 prefs_.SetString(prefs::kVariationsRestrictParameter, value); |
| 195 #endif |
| 196 } |
| 197 |
| 198 PrefService* prefs() { return &prefs_; } |
| 199 |
| 200 private: |
| 201 #if defined(OS_ANDROID) |
| 202 // Android uses profile prefs as the PrefService to generate the URL. |
| 203 TestingPrefServiceSyncable prefs_; |
| 204 #else |
| 205 TestingPrefServiceSimple prefs_; |
| 206 #endif |
| 207 |
| 208 #if defined(OS_CHROMEOS) |
| 209 chromeos::CrosSettings* cros_settings_; |
| 210 chromeos::StubCrosSettingsProvider stub_settings_provider_; |
| 211 chromeos::CrosSettingsProvider* device_settings_provider_; |
| 212 #endif |
| 213 |
| 214 DISALLOW_COPY_AND_ASSIGN(TestVariationsPrefsStore); |
| 215 }; |
| 216 |
| 154 } // namespace | 217 } // namespace |
| 155 | 218 |
| 156 class VariationsServiceTest : public ::testing::Test { | 219 class VariationsServiceTest : public ::testing::Test { |
| 157 protected: | 220 protected: |
| 158 VariationsServiceTest() {} | 221 VariationsServiceTest() {} |
| 159 | 222 |
| 160 private: | 223 private: |
| 161 #if defined(OS_CHROMEOS) | 224 #if defined(OS_CHROMEOS) |
| 162 // Not used directly. Initializes CrosSettings for testing. | 225 // Not used directly. Initializes CrosSettings for testing. |
| 163 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 226 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 164 chromeos::ScopedTestCrosSettings test_cros_settings_; | 227 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 165 #endif | 228 #endif |
| 166 | 229 |
| 167 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); | 230 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); |
| 168 }; | 231 }; |
| 169 | 232 |
| 170 #if !defined(OS_CHROMEOS) | 233 TEST_F(VariationsServiceTest, GetVariationsServerURL) { |
| 171 TEST_F(VariationsServiceTest, VariationsURLIsValid) { | 234 TestVariationsPrefsStore prefs_store; |
| 172 #if defined(OS_ANDROID) | 235 PrefService* prefs = prefs_store.prefs(); |
| 173 // Android uses profile prefs as the PrefService to generate the URL. | |
| 174 TestingPrefServiceSyncable prefs; | |
| 175 VariationsService::RegisterProfilePrefs(prefs.registry()); | |
| 176 #else | |
| 177 TestingPrefServiceSimple prefs; | |
| 178 VariationsService::RegisterPrefs(prefs.registry()); | |
| 179 #endif | |
| 180 const std::string default_variations_url = | 236 const std::string default_variations_url = |
| 181 VariationsService::GetDefaultVariationsServerURLForTesting(); | 237 VariationsService::GetDefaultVariationsServerURLForTesting(); |
| 182 | 238 |
| 183 std::string value; | 239 std::string value; |
| 184 GURL url = VariationsService::GetVariationsServerURL(&prefs); | 240 GURL url = VariationsService::GetVariationsServerURL(prefs, std::string()); |
| 185 EXPECT_TRUE(StartsWithASCII(url.spec(), default_variations_url, true)); | 241 EXPECT_TRUE(StartsWithASCII(url.spec(), default_variations_url, true)); |
| 186 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 242 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 187 | 243 |
| 188 prefs.SetString(prefs::kVariationsRestrictParameter, "restricted"); | 244 prefs_store.SetVariationsRestrictParameterPolicyValue("restricted"); |
| 189 url = VariationsService::GetVariationsServerURL(&prefs); | 245 url = VariationsService::GetVariationsServerURL(prefs, std::string()); |
| 190 EXPECT_TRUE(StartsWithASCII(url.spec(), default_variations_url, true)); | 246 EXPECT_TRUE(StartsWithASCII(url.spec(), default_variations_url, true)); |
| 191 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 247 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 192 EXPECT_EQ("restricted", value); | 248 EXPECT_EQ("restricted", value); |
| 193 } | |
| 194 #else | |
| 195 class VariationsServiceTestChromeOS : public VariationsServiceTest { | |
| 196 protected: | |
| 197 VariationsServiceTestChromeOS() {} | |
| 198 | 249 |
| 199 void SetUp() override { | 250 // The override value should take precedence over what's in prefs. |
| 200 cros_settings_ = chromeos::CrosSettings::Get(); | 251 url = VariationsService::GetVariationsServerURL(prefs, "override"); |
| 201 DCHECK(cros_settings_ != NULL); | |
| 202 // Remove the real DeviceSettingsProvider and replace it with a stub that | |
| 203 // allows modifications in a test. | |
| 204 device_settings_provider_ = cros_settings_->GetProvider( | |
| 205 chromeos::kReportDeviceVersionInfo); | |
| 206 EXPECT_TRUE(device_settings_provider_ != NULL); | |
| 207 EXPECT_TRUE(cros_settings_->RemoveSettingsProvider( | |
| 208 device_settings_provider_)); | |
| 209 cros_settings_->AddSettingsProvider(&stub_settings_provider_); | |
| 210 } | |
| 211 | |
| 212 void TearDown() override { | |
| 213 // Restore the real DeviceSettingsProvider. | |
| 214 EXPECT_TRUE( | |
| 215 cros_settings_->RemoveSettingsProvider(&stub_settings_provider_)); | |
| 216 cros_settings_->AddSettingsProvider(device_settings_provider_); | |
| 217 } | |
| 218 | |
| 219 void SetVariationsRestrictParameterPolicyValue(std::string value) { | |
| 220 cros_settings_->SetString(chromeos::kVariationsRestrictParameter, value); | |
| 221 } | |
| 222 | |
| 223 private: | |
| 224 chromeos::CrosSettings* cros_settings_; | |
| 225 chromeos::StubCrosSettingsProvider stub_settings_provider_; | |
| 226 chromeos::CrosSettingsProvider* device_settings_provider_; | |
| 227 | |
| 228 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTestChromeOS); | |
| 229 }; | |
| 230 | |
| 231 TEST_F(VariationsServiceTestChromeOS, VariationsURLIsValid) { | |
| 232 TestingPrefServiceSimple prefs; | |
| 233 VariationsService::RegisterPrefs(prefs.registry()); | |
| 234 const std::string default_variations_url = | |
| 235 VariationsService::GetDefaultVariationsServerURLForTesting(); | |
| 236 | |
| 237 std::string value; | |
| 238 GURL url = VariationsService::GetVariationsServerURL(&prefs); | |
| 239 EXPECT_TRUE(StartsWithASCII(url.spec(), default_variations_url, true)); | |
| 240 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); | |
| 241 | |
| 242 SetVariationsRestrictParameterPolicyValue("restricted"); | |
| 243 url = VariationsService::GetVariationsServerURL(&prefs); | |
| 244 EXPECT_TRUE(StartsWithASCII(url.spec(), default_variations_url, true)); | 252 EXPECT_TRUE(StartsWithASCII(url.spec(), default_variations_url, true)); |
| 245 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 253 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 246 EXPECT_EQ("restricted", value); | 254 EXPECT_EQ("override", value); |
| 247 } | 255 } |
| 248 #endif | |
| 249 | 256 |
| 250 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) { | 257 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) { |
| 251 TestingPrefServiceSimple prefs; | 258 TestingPrefServiceSimple prefs; |
| 252 VariationsService::RegisterPrefs(prefs.registry()); | 259 VariationsService::RegisterPrefs(prefs.registry()); |
| 253 const GURL url = VariationsService::GetVariationsServerURL(&prefs); | 260 const GURL url = |
| 261 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 254 | 262 |
| 255 std::string value; | 263 std::string value; |
| 256 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); | 264 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); |
| 257 EXPECT_FALSE(value.empty()); | 265 EXPECT_FALSE(value.empty()); |
| 258 } | 266 } |
| 259 | 267 |
| 260 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { | 268 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { |
| 261 base::MessageLoopForUI message_loop; | 269 base::MessageLoopForUI message_loop; |
| 262 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | 270 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 263 &message_loop); | 271 &message_loop); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 | 307 |
| 300 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { | 308 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { |
| 301 base::MessageLoop message_loop; | 309 base::MessageLoop message_loop; |
| 302 content::TestBrowserThread io_thread(content::BrowserThread::IO, | 310 content::TestBrowserThread io_thread(content::BrowserThread::IO, |
| 303 &message_loop); | 311 &message_loop); |
| 304 TestingPrefServiceSimple prefs; | 312 TestingPrefServiceSimple prefs; |
| 305 VariationsService::RegisterPrefs(prefs.registry()); | 313 VariationsService::RegisterPrefs(prefs.registry()); |
| 306 | 314 |
| 307 TestVariationsService service( | 315 TestVariationsService service( |
| 308 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); | 316 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); |
| 309 const GURL url = VariationsService::GetVariationsServerURL(&prefs); | 317 service.variations_server_url_ = |
| 310 service.variations_server_url_ = url; | 318 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 311 service.set_intercepts_fetch(false); | 319 service.set_intercepts_fetch(false); |
| 312 | 320 |
| 313 net::TestURLFetcherFactory factory; | 321 net::TestURLFetcherFactory factory; |
| 314 service.DoActualFetch(); | 322 service.DoActualFetch(); |
| 315 | 323 |
| 316 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 324 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 317 SimulateServerResponse(net::HTTP_OK, fetcher); | 325 SimulateServerResponse(net::HTTP_OK, fetcher); |
| 318 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); | 326 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); |
| 319 | 327 |
| 320 EXPECT_FALSE(service.seed_stored()); | 328 EXPECT_FALSE(service.seed_stored()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 332 }; | 340 }; |
| 333 | 341 |
| 334 base::MessageLoop message_loop; | 342 base::MessageLoop message_loop; |
| 335 content::TestBrowserThread io_thread(content::BrowserThread::IO, | 343 content::TestBrowserThread io_thread(content::BrowserThread::IO, |
| 336 &message_loop); | 344 &message_loop); |
| 337 TestingPrefServiceSimple prefs; | 345 TestingPrefServiceSimple prefs; |
| 338 VariationsService::RegisterPrefs(prefs.registry()); | 346 VariationsService::RegisterPrefs(prefs.registry()); |
| 339 | 347 |
| 340 VariationsService service( | 348 VariationsService service( |
| 341 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | 349 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
| 342 const GURL url = VariationsService::GetVariationsServerURL(&prefs); | 350 service.variations_server_url_ = |
| 343 service.variations_server_url_ = url; | 351 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 344 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { | 352 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { |
| 345 net::TestURLFetcherFactory factory; | 353 net::TestURLFetcherFactory factory; |
| 346 service.DoActualFetch(); | 354 service.DoActualFetch(); |
| 347 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 355 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 348 | 356 |
| 349 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 357 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 350 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 358 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
| 351 service.OnURLFetchComplete(fetcher); | 359 service.OnURLFetchComplete(fetcher); |
| 352 | 360 |
| 353 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 361 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 354 } | 362 } |
| 355 } | 363 } |
| 356 | 364 |
| 357 TEST_F(VariationsServiceTest, SeedDateUpdatedOn304Status) { | 365 TEST_F(VariationsServiceTest, SeedDateUpdatedOn304Status) { |
| 358 base::MessageLoop message_loop; | 366 base::MessageLoop message_loop; |
| 359 content::TestBrowserThread io_thread(content::BrowserThread::IO, | 367 content::TestBrowserThread io_thread(content::BrowserThread::IO, |
| 360 &message_loop); | 368 &message_loop); |
| 361 TestingPrefServiceSimple prefs; | 369 TestingPrefServiceSimple prefs; |
| 362 VariationsService::RegisterPrefs(prefs.registry()); | 370 VariationsService::RegisterPrefs(prefs.registry()); |
| 363 | 371 |
| 364 net::TestURLFetcherFactory factory; | 372 net::TestURLFetcherFactory factory; |
| 365 VariationsService service( | 373 VariationsService service( |
| 366 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | 374 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
| 367 const GURL url = VariationsService::GetVariationsServerURL(&prefs); | 375 service.variations_server_url_ = |
| 368 service.variations_server_url_ = url; | 376 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 369 service.DoActualFetch(); | 377 service.DoActualFetch(); |
| 370 EXPECT_TRUE( | 378 EXPECT_TRUE( |
| 371 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); | 379 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); |
| 372 | 380 |
| 373 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 381 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 374 SimulateServerResponse(net::HTTP_NOT_MODIFIED, fetcher); | 382 SimulateServerResponse(net::HTTP_NOT_MODIFIED, fetcher); |
| 375 service.OnURLFetchComplete(fetcher); | 383 service.OnURLFetchComplete(fetcher); |
| 376 EXPECT_FALSE( | 384 EXPECT_FALSE( |
| 377 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); | 385 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); |
| 378 } | 386 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 EXPECT_EQ(cases[i].expected_best_effort_notifications, | 424 EXPECT_EQ(cases[i].expected_best_effort_notifications, |
| 417 observer.best_effort_changes_notified()) << i; | 425 observer.best_effort_changes_notified()) << i; |
| 418 EXPECT_EQ(cases[i].expected_crtical_notifications, | 426 EXPECT_EQ(cases[i].expected_crtical_notifications, |
| 419 observer.crticial_changes_notified()) << i; | 427 observer.crticial_changes_notified()) << i; |
| 420 | 428 |
| 421 service.RemoveObserver(&observer); | 429 service.RemoveObserver(&observer); |
| 422 } | 430 } |
| 423 } | 431 } |
| 424 | 432 |
| 425 } // namespace chrome_variations | 433 } // namespace chrome_variations |
| OLD | NEW |