| 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" |
| 11 #include "base/sha1.h" | 11 #include "base/sha1.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/test/base/testing_browser_process.h" | 15 #include "chrome/test/base/testing_browser_process.h" |
| 16 #include "chrome/test/base/testing_pref_service_syncable.h" | 16 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 17 #include "components/variations/proto/study.pb.h" | 17 #include "components/variations/proto/study.pb.h" |
| 18 #include "components/variations/proto/variations_seed.pb.h" | 18 #include "components/variations/proto/variations_seed.pb.h" |
| 19 #include "components/web_resource/resource_request_allowed_notifier_test_util.h" | 19 #include "components/web_resource/resource_request_allowed_notifier_test_util.h" |
| 20 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "net/base/url_util.h" | 22 #include "net/base/url_util.h" |
| 22 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
| 23 #include "net/http/http_status_code.h" | 24 #include "net/http/http_status_code.h" |
| 24 #include "net/url_request/test_url_fetcher_factory.h" | 25 #include "net/url_request/test_url_fetcher_factory.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 27 |
| 27 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
| 28 #include "chrome/browser/chromeos/settings/cros_settings.h" | 29 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 29 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 30 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 30 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 31 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 DISALLOW_COPY_AND_ASSIGN(TestVariationsPrefsStore); | 215 DISALLOW_COPY_AND_ASSIGN(TestVariationsPrefsStore); |
| 215 }; | 216 }; |
| 216 | 217 |
| 217 } // namespace | 218 } // namespace |
| 218 | 219 |
| 219 class VariationsServiceTest : public ::testing::Test { | 220 class VariationsServiceTest : public ::testing::Test { |
| 220 protected: | 221 protected: |
| 221 VariationsServiceTest() {} | 222 VariationsServiceTest() {} |
| 222 | 223 |
| 223 private: | 224 private: |
| 225 content::TestBrowserThreadBundle thread_bundle_; |
| 224 #if defined(OS_CHROMEOS) | 226 #if defined(OS_CHROMEOS) |
| 225 // Not used directly. Initializes CrosSettings for testing. | 227 // Not used directly. Initializes CrosSettings for testing. |
| 226 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 228 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 227 chromeos::ScopedTestCrosSettings test_cros_settings_; | 229 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 228 #endif | 230 #endif |
| 229 | 231 |
| 230 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); | 232 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); |
| 231 }; | 233 }; |
| 232 | 234 |
| 233 TEST_F(VariationsServiceTest, GetVariationsServerURL) { | 235 TEST_F(VariationsServiceTest, GetVariationsServerURL) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 259 VariationsService::RegisterPrefs(prefs.registry()); | 261 VariationsService::RegisterPrefs(prefs.registry()); |
| 260 const GURL url = | 262 const GURL url = |
| 261 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 263 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 262 | 264 |
| 263 std::string value; | 265 std::string value; |
| 264 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); | 266 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); |
| 265 EXPECT_FALSE(value.empty()); | 267 EXPECT_FALSE(value.empty()); |
| 266 } | 268 } |
| 267 | 269 |
| 268 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { | 270 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { |
| 269 base::MessageLoopForUI message_loop; | |
| 270 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 271 &message_loop); | |
| 272 TestingPrefServiceSimple prefs; | 271 TestingPrefServiceSimple prefs; |
| 273 VariationsService::RegisterPrefs(prefs.registry()); | 272 VariationsService::RegisterPrefs(prefs.registry()); |
| 274 | 273 |
| 275 // Pass ownership to TestVariationsService, but keep a weak pointer to | 274 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 276 // manipulate it for this test. | 275 // manipulate it for this test. |
| 277 web_resource::TestRequestAllowedNotifier* test_notifier = | 276 web_resource::TestRequestAllowedNotifier* test_notifier = |
| 278 new web_resource::TestRequestAllowedNotifier(&prefs); | 277 new web_resource::TestRequestAllowedNotifier(&prefs); |
| 279 TestVariationsService test_service(test_notifier, &prefs); | 278 TestVariationsService test_service(test_notifier, &prefs); |
| 280 | 279 |
| 281 // Force the notifier to initially disallow requests. | 280 // Force the notifier to initially disallow requests. |
| 282 test_notifier->SetRequestsAllowedOverride(false); | 281 test_notifier->SetRequestsAllowedOverride(false); |
| 283 test_service.StartRepeatedVariationsSeedFetch(); | 282 test_service.StartRepeatedVariationsSeedFetch(); |
| 284 EXPECT_FALSE(test_service.fetch_attempted()); | 283 EXPECT_FALSE(test_service.fetch_attempted()); |
| 285 | 284 |
| 286 test_notifier->NotifyObserver(); | 285 test_notifier->NotifyObserver(); |
| 287 EXPECT_TRUE(test_service.fetch_attempted()); | 286 EXPECT_TRUE(test_service.fetch_attempted()); |
| 288 } | 287 } |
| 289 | 288 |
| 290 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) { | 289 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) { |
| 291 base::MessageLoopForUI message_loop; | |
| 292 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
| 293 &message_loop); | |
| 294 TestingPrefServiceSimple prefs; | 290 TestingPrefServiceSimple prefs; |
| 295 VariationsService::RegisterPrefs(prefs.registry()); | 291 VariationsService::RegisterPrefs(prefs.registry()); |
| 296 | 292 |
| 297 // Pass ownership to TestVariationsService, but keep a weak pointer to | 293 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 298 // manipulate it for this test. | 294 // manipulate it for this test. |
| 299 web_resource::TestRequestAllowedNotifier* test_notifier = | 295 web_resource::TestRequestAllowedNotifier* test_notifier = |
| 300 new web_resource::TestRequestAllowedNotifier(&prefs); | 296 new web_resource::TestRequestAllowedNotifier(&prefs); |
| 301 TestVariationsService test_service(test_notifier, &prefs); | 297 TestVariationsService test_service(test_notifier, &prefs); |
| 302 | 298 |
| 303 test_notifier->SetRequestsAllowedOverride(true); | 299 test_notifier->SetRequestsAllowedOverride(true); |
| 304 test_service.StartRepeatedVariationsSeedFetch(); | 300 test_service.StartRepeatedVariationsSeedFetch(); |
| 305 EXPECT_TRUE(test_service.fetch_attempted()); | 301 EXPECT_TRUE(test_service.fetch_attempted()); |
| 306 } | 302 } |
| 307 | 303 |
| 308 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { | 304 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { |
| 309 base::MessageLoop message_loop; | |
| 310 content::TestBrowserThread io_thread(content::BrowserThread::IO, | |
| 311 &message_loop); | |
| 312 TestingPrefServiceSimple prefs; | 305 TestingPrefServiceSimple prefs; |
| 313 VariationsService::RegisterPrefs(prefs.registry()); | 306 VariationsService::RegisterPrefs(prefs.registry()); |
| 314 | 307 |
| 315 TestVariationsService service( | 308 TestVariationsService service( |
| 316 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); | 309 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); |
| 317 service.variations_server_url_ = | 310 service.variations_server_url_ = |
| 318 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 311 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 319 service.set_intercepts_fetch(false); | 312 service.set_intercepts_fetch(false); |
| 320 | 313 |
| 321 net::TestURLFetcherFactory factory; | 314 net::TestURLFetcherFactory factory; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 332 | 325 |
| 333 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { | 326 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { |
| 334 const int non_ok_status_codes[] = { | 327 const int non_ok_status_codes[] = { |
| 335 net::HTTP_NO_CONTENT, | 328 net::HTTP_NO_CONTENT, |
| 336 net::HTTP_NOT_MODIFIED, | 329 net::HTTP_NOT_MODIFIED, |
| 337 net::HTTP_NOT_FOUND, | 330 net::HTTP_NOT_FOUND, |
| 338 net::HTTP_INTERNAL_SERVER_ERROR, | 331 net::HTTP_INTERNAL_SERVER_ERROR, |
| 339 net::HTTP_SERVICE_UNAVAILABLE, | 332 net::HTTP_SERVICE_UNAVAILABLE, |
| 340 }; | 333 }; |
| 341 | 334 |
| 342 base::MessageLoop message_loop; | |
| 343 content::TestBrowserThread io_thread(content::BrowserThread::IO, | |
| 344 &message_loop); | |
| 345 TestingPrefServiceSimple prefs; | 335 TestingPrefServiceSimple prefs; |
| 346 VariationsService::RegisterPrefs(prefs.registry()); | 336 VariationsService::RegisterPrefs(prefs.registry()); |
| 347 | 337 |
| 348 VariationsService service( | 338 VariationsService service( |
| 349 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | 339 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
| 350 service.variations_server_url_ = | 340 service.variations_server_url_ = |
| 351 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 341 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 352 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { | 342 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { |
| 353 net::TestURLFetcherFactory factory; | 343 net::TestURLFetcherFactory factory; |
| 354 service.DoActualFetch(); | 344 service.DoActualFetch(); |
| 355 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 345 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 356 | 346 |
| 357 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 347 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 358 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 348 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
| 359 service.OnURLFetchComplete(fetcher); | 349 service.OnURLFetchComplete(fetcher); |
| 360 | 350 |
| 361 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 351 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 362 } | 352 } |
| 363 } | 353 } |
| 364 | 354 |
| 365 TEST_F(VariationsServiceTest, SeedDateUpdatedOn304Status) { | 355 TEST_F(VariationsServiceTest, SeedDateUpdatedOn304Status) { |
| 366 base::MessageLoop message_loop; | |
| 367 content::TestBrowserThread io_thread(content::BrowserThread::IO, | |
| 368 &message_loop); | |
| 369 TestingPrefServiceSimple prefs; | 356 TestingPrefServiceSimple prefs; |
| 370 VariationsService::RegisterPrefs(prefs.registry()); | 357 VariationsService::RegisterPrefs(prefs.registry()); |
| 371 | 358 |
| 372 net::TestURLFetcherFactory factory; | 359 net::TestURLFetcherFactory factory; |
| 373 VariationsService service( | 360 VariationsService service( |
| 374 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | 361 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
| 375 service.variations_server_url_ = | 362 service.variations_server_url_ = |
| 376 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 363 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 377 service.DoActualFetch(); | 364 service.DoActualFetch(); |
| 378 EXPECT_TRUE( | 365 EXPECT_TRUE( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 EXPECT_EQ(cases[i].expected_best_effort_notifications, | 411 EXPECT_EQ(cases[i].expected_best_effort_notifications, |
| 425 observer.best_effort_changes_notified()) << i; | 412 observer.best_effort_changes_notified()) << i; |
| 426 EXPECT_EQ(cases[i].expected_crtical_notifications, | 413 EXPECT_EQ(cases[i].expected_crtical_notifications, |
| 427 observer.crticial_changes_notified()) << i; | 414 observer.crticial_changes_notified()) << i; |
| 428 | 415 |
| 429 service.RemoveObserver(&observer); | 416 service.RemoveObserver(&observer); |
| 430 } | 417 } |
| 431 } | 418 } |
| 432 | 419 |
| 433 } // namespace chrome_variations | 420 } // namespace chrome_variations |
| OLD | NEW |