| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/prefs/testing_pref_service.h" | 8 #include "base/prefs/testing_pref_service.h" |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "chrome/browser/metrics/proto/study.pb.h" | 10 #include "chrome/browser/metrics/proto/study.pb.h" |
| 11 #include "chrome/browser/metrics/variations/resource_request_allowed_notifier_te
st_util.h" | 11 #include "chrome/browser/metrics/variations/resource_request_allowed_notifier_te
st_util.h" |
| 12 #include "chrome/browser/metrics/variations/variations_service.h" | 12 #include "chrome/browser/metrics/variations/variations_service.h" |
| 13 #include "chrome/common/chrome_version_info.h" | 13 #include "chrome/common/chrome_version_info.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 "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 17 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 18 #include "net/http/http_status_code.h" | 18 #include "net/http/http_status_code.h" |
| 19 #include "net/url_request/test_url_fetcher_factory.h" | 19 #include "net/url_request/test_url_fetcher_factory.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 #if defined(OS_CHROMEOS) |
| 23 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 24 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| 25 #endif |
| 26 |
| 22 namespace chrome_variations { | 27 namespace chrome_variations { |
| 23 | 28 |
| 24 namespace { | 29 namespace { |
| 25 | 30 |
| 26 // A test class used to validate expected functionality in VariationsService. | 31 // A test class used to validate expected functionality in VariationsService. |
| 27 class TestVariationsService : public VariationsService { | 32 class TestVariationsService : public VariationsService { |
| 28 public: | 33 public: |
| 29 TestVariationsService(TestRequestAllowedNotifier* test_notifier, | 34 TestVariationsService(TestRequestAllowedNotifier* test_notifier, |
| 30 PrefService* local_state) | 35 PrefService* local_state) |
| 31 : VariationsService(test_notifier, local_state), | 36 : VariationsService(test_notifier, local_state), |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 void SimulateServerResponse(int response_code, net::TestURLFetcher* fetcher) { | 96 void SimulateServerResponse(int response_code, net::TestURLFetcher* fetcher) { |
| 92 ASSERT_TRUE(fetcher); | 97 ASSERT_TRUE(fetcher); |
| 93 scoped_refptr<net::HttpResponseHeaders> headers( | 98 scoped_refptr<net::HttpResponseHeaders> headers( |
| 94 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0")); | 99 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0")); |
| 95 fetcher->set_response_headers(headers); | 100 fetcher->set_response_headers(headers); |
| 96 fetcher->set_response_code(response_code); | 101 fetcher->set_response_code(response_code); |
| 97 } | 102 } |
| 98 | 103 |
| 99 } // namespace | 104 } // namespace |
| 100 | 105 |
| 101 TEST(VariationsServiceTest, CheckStudyChannel) { | 106 class VariationsServiceTest : public testing::Test { |
| 107 protected: |
| 108 VariationsServiceTest() {} |
| 109 |
| 110 private: |
| 111 #if defined(OS_CHROMEOS) |
| 112 // Not used directly. Initializes CrosSettings for testing. |
| 113 chromeos::ScopedTestCrosSettings scoped_test_cros_settings; |
| 114 #endif |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); |
| 117 }; |
| 118 |
| 119 TEST_F(VariationsServiceTest, CheckStudyChannel) { |
| 102 const chrome::VersionInfo::Channel channels[] = { | 120 const chrome::VersionInfo::Channel channels[] = { |
| 103 chrome::VersionInfo::CHANNEL_CANARY, | 121 chrome::VersionInfo::CHANNEL_CANARY, |
| 104 chrome::VersionInfo::CHANNEL_DEV, | 122 chrome::VersionInfo::CHANNEL_DEV, |
| 105 chrome::VersionInfo::CHANNEL_BETA, | 123 chrome::VersionInfo::CHANNEL_BETA, |
| 106 chrome::VersionInfo::CHANNEL_STABLE, | 124 chrome::VersionInfo::CHANNEL_STABLE, |
| 107 }; | 125 }; |
| 108 const Study_Channel study_channels[] = { | 126 const Study_Channel study_channels[] = { |
| 109 Study_Channel_CANARY, | 127 Study_Channel_CANARY, |
| 110 Study_Channel_DEV, | 128 Study_Channel_DEV, |
| 111 Study_Channel_BETA, | 129 Study_Channel_BETA, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 } | 162 } |
| 145 | 163 |
| 146 if (i < arraysize(study_channels)) { | 164 if (i < arraysize(study_channels)) { |
| 147 const int index = arraysize(study_channels) - i - 1; | 165 const int index = arraysize(study_channels) - i - 1; |
| 148 filter.add_channel(study_channels[index]); | 166 filter.add_channel(study_channels[index]); |
| 149 channel_added[index] = true; | 167 channel_added[index] = true; |
| 150 } | 168 } |
| 151 } | 169 } |
| 152 } | 170 } |
| 153 | 171 |
| 154 TEST(VariationsServiceTest, CheckStudyLocale) { | 172 TEST_F(VariationsServiceTest, CheckStudyLocale) { |
| 155 struct { | 173 struct { |
| 156 const char* filter_locales; | 174 const char* filter_locales; |
| 157 bool en_us_result; | 175 bool en_us_result; |
| 158 bool en_ca_result; | 176 bool en_ca_result; |
| 159 bool fr_result; | 177 bool fr_result; |
| 160 } test_cases[] = { | 178 } test_cases[] = { |
| 161 {"en-US", true, false, false}, | 179 {"en-US", true, false, false}, |
| 162 {"en-US,en-CA,fr", true, true, true}, | 180 {"en-US,en-CA,fr", true, true, true}, |
| 163 {"en-US,en-CA,en-GB", true, true, false}, | 181 {"en-US,en-CA,en-GB", true, true, false}, |
| 164 {"en-GB,en-CA,en-US", true, true, false}, | 182 {"en-GB,en-CA,en-US", true, true, false}, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 175 filter.add_locale(filter_locales[j]); | 193 filter.add_locale(filter_locales[j]); |
| 176 EXPECT_EQ(test_cases[i].en_us_result, | 194 EXPECT_EQ(test_cases[i].en_us_result, |
| 177 VariationsService::CheckStudyLocale(filter, "en-US")); | 195 VariationsService::CheckStudyLocale(filter, "en-US")); |
| 178 EXPECT_EQ(test_cases[i].en_ca_result, | 196 EXPECT_EQ(test_cases[i].en_ca_result, |
| 179 VariationsService::CheckStudyLocale(filter, "en-CA")); | 197 VariationsService::CheckStudyLocale(filter, "en-CA")); |
| 180 EXPECT_EQ(test_cases[i].fr_result, | 198 EXPECT_EQ(test_cases[i].fr_result, |
| 181 VariationsService::CheckStudyLocale(filter, "fr")); | 199 VariationsService::CheckStudyLocale(filter, "fr")); |
| 182 } | 200 } |
| 183 } | 201 } |
| 184 | 202 |
| 185 TEST(VariationsServiceTest, CheckStudyPlatform) { | 203 TEST_F(VariationsServiceTest, CheckStudyPlatform) { |
| 186 const Study_Platform platforms[] = { | 204 const Study_Platform platforms[] = { |
| 187 Study_Platform_PLATFORM_WINDOWS, | 205 Study_Platform_PLATFORM_WINDOWS, |
| 188 Study_Platform_PLATFORM_MAC, | 206 Study_Platform_PLATFORM_MAC, |
| 189 Study_Platform_PLATFORM_LINUX, | 207 Study_Platform_PLATFORM_LINUX, |
| 190 Study_Platform_PLATFORM_CHROMEOS, | 208 Study_Platform_PLATFORM_CHROMEOS, |
| 191 Study_Platform_PLATFORM_ANDROID, | 209 Study_Platform_PLATFORM_ANDROID, |
| 192 Study_Platform_PLATFORM_IOS, | 210 Study_Platform_PLATFORM_IOS, |
| 193 }; | 211 }; |
| 194 ASSERT_EQ(Study_Platform_Platform_ARRAYSIZE, | 212 ASSERT_EQ(Study_Platform_Platform_ARRAYSIZE, |
| 195 static_cast<int>(arraysize(platforms))); | 213 static_cast<int>(arraysize(platforms))); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 225 } | 243 } |
| 226 | 244 |
| 227 if (i < arraysize(platforms)) { | 245 if (i < arraysize(platforms)) { |
| 228 const int index = arraysize(platforms) - i - 1; | 246 const int index = arraysize(platforms) - i - 1; |
| 229 filter.add_platform(platforms[index]); | 247 filter.add_platform(platforms[index]); |
| 230 platform_added[index] = true; | 248 platform_added[index] = true; |
| 231 } | 249 } |
| 232 } | 250 } |
| 233 } | 251 } |
| 234 | 252 |
| 235 TEST(VariationsServiceTest, CheckStudyVersion) { | 253 TEST_F(VariationsServiceTest, CheckStudyVersion) { |
| 236 const struct { | 254 const struct { |
| 237 const char* min_version; | 255 const char* min_version; |
| 238 const char* version; | 256 const char* version; |
| 239 bool expected_result; | 257 bool expected_result; |
| 240 } min_test_cases[] = { | 258 } min_test_cases[] = { |
| 241 { "1.2.2", "1.2.3", true }, | 259 { "1.2.2", "1.2.3", true }, |
| 242 { "1.2.3", "1.2.3", true }, | 260 { "1.2.3", "1.2.3", true }, |
| 243 { "1.2.4", "1.2.3", false }, | 261 { "1.2.4", "1.2.3", false }, |
| 244 { "1.3.2", "1.2.3", false }, | 262 { "1.3.2", "1.2.3", false }, |
| 245 { "2.1.2", "1.2.3", false }, | 263 { "2.1.2", "1.2.3", false }, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 if (!max_test_cases[j].expected_result) { | 330 if (!max_test_cases[j].expected_result) { |
| 313 const bool result = | 331 const bool result = |
| 314 VariationsService::CheckStudyVersion(filter, | 332 VariationsService::CheckStudyVersion(filter, |
| 315 max_test_cases[j].version); | 333 max_test_cases[j].version); |
| 316 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; | 334 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; |
| 317 } | 335 } |
| 318 } | 336 } |
| 319 } | 337 } |
| 320 } | 338 } |
| 321 | 339 |
| 322 TEST(VariationsServiceTest, CheckStudyStartDate) { | 340 TEST_F(VariationsServiceTest, CheckStudyStartDate) { |
| 323 const base::Time now = base::Time::Now(); | 341 const base::Time now = base::Time::Now(); |
| 324 const base::TimeDelta delta = base::TimeDelta::FromHours(1); | 342 const base::TimeDelta delta = base::TimeDelta::FromHours(1); |
| 325 const struct { | 343 const struct { |
| 326 const base::Time start_date; | 344 const base::Time start_date; |
| 327 bool expected_result; | 345 bool expected_result; |
| 328 } start_test_cases[] = { | 346 } start_test_cases[] = { |
| 329 { now - delta, true }, | 347 { now - delta, true }, |
| 330 { now, true }, | 348 { now, true }, |
| 331 { now + delta, false }, | 349 { now + delta, false }, |
| 332 }; | 350 }; |
| 333 | 351 |
| 334 Study_Filter filter; | 352 Study_Filter filter; |
| 335 | 353 |
| 336 // Start date not set should result in true. | 354 // Start date not set should result in true. |
| 337 EXPECT_TRUE(VariationsService::CheckStudyStartDate(filter, now)); | 355 EXPECT_TRUE(VariationsService::CheckStudyStartDate(filter, now)); |
| 338 | 356 |
| 339 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(start_test_cases); ++i) { | 357 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(start_test_cases); ++i) { |
| 340 filter.set_start_date(TimeToProtoTime(start_test_cases[i].start_date)); | 358 filter.set_start_date(TimeToProtoTime(start_test_cases[i].start_date)); |
| 341 const bool result = VariationsService::CheckStudyStartDate(filter, now); | 359 const bool result = VariationsService::CheckStudyStartDate(filter, now); |
| 342 EXPECT_EQ(start_test_cases[i].expected_result, result) | 360 EXPECT_EQ(start_test_cases[i].expected_result, result) |
| 343 << "Case " << i << " failed!"; | 361 << "Case " << i << " failed!"; |
| 344 } | 362 } |
| 345 } | 363 } |
| 346 | 364 |
| 347 TEST(VariationsServiceTest, IsStudyExpired) { | 365 TEST_F(VariationsServiceTest, IsStudyExpired) { |
| 348 const base::Time now = base::Time::Now(); | 366 const base::Time now = base::Time::Now(); |
| 349 const base::TimeDelta delta = base::TimeDelta::FromHours(1); | 367 const base::TimeDelta delta = base::TimeDelta::FromHours(1); |
| 350 const struct { | 368 const struct { |
| 351 const base::Time expiry_date; | 369 const base::Time expiry_date; |
| 352 bool expected_result; | 370 bool expected_result; |
| 353 } expiry_test_cases[] = { | 371 } expiry_test_cases[] = { |
| 354 { now - delta, true }, | 372 { now - delta, true }, |
| 355 { now, true }, | 373 { now, true }, |
| 356 { now + delta, false }, | 374 { now + delta, false }, |
| 357 }; | 375 }; |
| 358 | 376 |
| 359 Study study; | 377 Study study; |
| 360 | 378 |
| 361 // Expiry date not set should result in false. | 379 // Expiry date not set should result in false. |
| 362 EXPECT_FALSE(VariationsService::IsStudyExpired(study, now)); | 380 EXPECT_FALSE(VariationsService::IsStudyExpired(study, now)); |
| 363 | 381 |
| 364 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { | 382 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { |
| 365 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); | 383 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); |
| 366 const bool result = VariationsService::IsStudyExpired(study, now); | 384 const bool result = VariationsService::IsStudyExpired(study, now); |
| 367 EXPECT_EQ(expiry_test_cases[i].expected_result, result) | 385 EXPECT_EQ(expiry_test_cases[i].expected_result, result) |
| 368 << "Case " << i << " failed!"; | 386 << "Case " << i << " failed!"; |
| 369 } | 387 } |
| 370 } | 388 } |
| 371 | 389 |
| 372 TEST(VariationsServiceTest, VariationsURLIsValid) { | 390 #if !defined(OS_CHROMEOS) |
| 391 TEST_F(VariationsServiceTest, VariationsURLIsValid) { |
| 373 TestingPrefServiceSimple prefs; | 392 TestingPrefServiceSimple prefs; |
| 374 VariationsService::RegisterPrefs(prefs.registry()); | 393 VariationsService::RegisterPrefs(prefs.registry()); |
| 375 const std::string default_variations_url = | 394 const std::string default_variations_url = |
| 376 VariationsService::GetDefaultVariationsServerURLForTesting(); | 395 VariationsService::GetDefaultVariationsServerURLForTesting(); |
| 377 | 396 |
| 378 EXPECT_EQ(default_variations_url, | 397 EXPECT_EQ(default_variations_url, |
| 379 VariationsService::GetVariationsServerURL(&prefs).spec()); | 398 VariationsService::GetVariationsServerURL(&prefs).spec()); |
| 380 | 399 |
| 381 prefs.SetString(prefs::kVariationsRestrictParameter, "restricted"); | 400 prefs.SetString(prefs::kVariationsRestrictParameter, "restricted"); |
| 382 EXPECT_EQ(default_variations_url + "?restrict=restricted", | 401 EXPECT_EQ(default_variations_url + "?restrict=restricted", |
| 383 VariationsService::GetVariationsServerURL(&prefs).spec()); | 402 VariationsService::GetVariationsServerURL(&prefs).spec()); |
| 384 } | 403 } |
| 404 #else |
| 405 class VariationsServiceTestChromeOS : public VariationsServiceTest { |
| 406 protected: |
| 407 VariationsServiceTestChromeOS() {} |
| 385 | 408 |
| 386 TEST(VariationsServiceTest, LoadSeed) { | 409 virtual void SetUp() OVERRIDE { |
| 410 cros_settings_ = chromeos::CrosSettings::Get(); |
| 411 DCHECK(cros_settings_ != NULL); |
| 412 // Remove the real DeviceSettingsProvider and replace it with a stub that |
| 413 // allows modifications in a test. |
| 414 device_settings_provider_ = cros_settings_->GetProvider( |
| 415 chromeos::kReportDeviceVersionInfo); |
| 416 EXPECT_TRUE(device_settings_provider_ != NULL); |
| 417 EXPECT_TRUE(cros_settings_->RemoveSettingsProvider( |
| 418 device_settings_provider_)); |
| 419 cros_settings_->AddSettingsProvider(&stub_settings_provider_); |
| 420 } |
| 421 |
| 422 virtual void TearDown() OVERRIDE { |
| 423 // Restore the real DeviceSettingsProvider. |
| 424 EXPECT_TRUE( |
| 425 cros_settings_->RemoveSettingsProvider(&stub_settings_provider_)); |
| 426 cros_settings_->AddSettingsProvider(device_settings_provider_); |
| 427 } |
| 428 |
| 429 chromeos::CrosSettings* cros_settings_; |
| 430 |
| 431 private: |
| 432 chromeos::StubCrosSettingsProvider stub_settings_provider_; |
| 433 chromeos::CrosSettingsProvider* device_settings_provider_; |
| 434 |
| 435 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTestChromeOS); |
| 436 }; |
| 437 |
| 438 TEST_F(VariationsServiceTestChromeOS, VariationsURLIsValid) { |
| 439 TestingPrefServiceSimple prefs; |
| 440 VariationsService::RegisterPrefs(prefs.registry()); |
| 441 const std::string default_variations_url = |
| 442 VariationsService::GetDefaultVariationsServerURLForTesting(); |
| 443 |
| 444 EXPECT_EQ(default_variations_url, |
| 445 VariationsService::GetVariationsServerURL(&prefs).spec()); |
| 446 |
| 447 cros_settings_->SetString(chromeos::kVariationsRestrictParameter, |
| 448 "restricted"); |
| 449 EXPECT_EQ(default_variations_url + "?restrict=restricted", |
| 450 VariationsService::GetVariationsServerURL(&prefs).spec()); |
| 451 } |
| 452 #endif |
| 453 |
| 454 TEST_F(VariationsServiceTest, LoadSeed) { |
| 387 // Store good seed data to test if loading from prefs works. | 455 // Store good seed data to test if loading from prefs works. |
| 388 const TrialsSeed seed = CreateTestSeed(); | 456 const TrialsSeed seed = CreateTestSeed(); |
| 389 const std::string base64_seed = SerializeSeedBase64(seed); | 457 const std::string base64_seed = SerializeSeedBase64(seed); |
| 390 | 458 |
| 391 TestingPrefServiceSimple prefs; | 459 TestingPrefServiceSimple prefs; |
| 392 VariationsService::RegisterPrefs(prefs.registry()); | 460 VariationsService::RegisterPrefs(prefs.registry()); |
| 393 prefs.SetString(prefs::kVariationsSeed, base64_seed); | 461 prefs.SetString(prefs::kVariationsSeed, base64_seed); |
| 394 | 462 |
| 395 TestVariationsService variations_service(new TestRequestAllowedNotifier, | 463 TestVariationsService variations_service(new TestRequestAllowedNotifier, |
| 396 &prefs); | 464 &prefs); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 408 prefs.SetString(prefs::kVariationsSeed, "this should fail"); | 476 prefs.SetString(prefs::kVariationsSeed, "this should fail"); |
| 409 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 477 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 410 EXPECT_FALSE(variations_service.LoadTrialsSeedFromPref(&prefs, &loaded_seed)); | 478 EXPECT_FALSE(variations_service.LoadTrialsSeedFromPref(&prefs, &loaded_seed)); |
| 411 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 479 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 412 | 480 |
| 413 // Check that having no seed in prefs results in a return value of false. | 481 // Check that having no seed in prefs results in a return value of false. |
| 414 prefs.ClearPref(prefs::kVariationsSeed); | 482 prefs.ClearPref(prefs::kVariationsSeed); |
| 415 EXPECT_FALSE(variations_service.LoadTrialsSeedFromPref(&prefs, &loaded_seed)); | 483 EXPECT_FALSE(variations_service.LoadTrialsSeedFromPref(&prefs, &loaded_seed)); |
| 416 } | 484 } |
| 417 | 485 |
| 418 TEST(VariationsServiceTest, StoreSeed) { | 486 TEST_F(VariationsServiceTest, StoreSeed) { |
| 419 const base::Time now = base::Time::Now(); | 487 const base::Time now = base::Time::Now(); |
| 420 const TrialsSeed seed = CreateTestSeed(); | 488 const TrialsSeed seed = CreateTestSeed(); |
| 421 const std::string serialized_seed = SerializeSeed(seed); | 489 const std::string serialized_seed = SerializeSeed(seed); |
| 422 | 490 |
| 423 TestingPrefServiceSimple prefs; | 491 TestingPrefServiceSimple prefs; |
| 424 VariationsService::RegisterPrefs(prefs.registry()); | 492 VariationsService::RegisterPrefs(prefs.registry()); |
| 425 | 493 |
| 426 TestVariationsService variations_service(new TestRequestAllowedNotifier, | 494 TestVariationsService variations_service(new TestRequestAllowedNotifier, |
| 427 &prefs); | 495 &prefs); |
| 428 | 496 |
| 429 EXPECT_TRUE(variations_service.StoreSeedData(serialized_seed, now, &prefs)); | 497 EXPECT_TRUE(variations_service.StoreSeedData(serialized_seed, now, &prefs)); |
| 430 // Make sure the pref was actually set. | 498 // Make sure the pref was actually set. |
| 431 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 499 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 432 | 500 |
| 433 std::string loaded_serialized_seed = prefs.GetString(prefs::kVariationsSeed); | 501 std::string loaded_serialized_seed = prefs.GetString(prefs::kVariationsSeed); |
| 434 std::string decoded_serialized_seed; | 502 std::string decoded_serialized_seed; |
| 435 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, | 503 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, |
| 436 &decoded_serialized_seed)); | 504 &decoded_serialized_seed)); |
| 437 // Make sure the stored seed from pref is the same as the seed we created. | 505 // Make sure the stored seed from pref is the same as the seed we created. |
| 438 EXPECT_EQ(serialized_seed, decoded_serialized_seed); | 506 EXPECT_EQ(serialized_seed, decoded_serialized_seed); |
| 439 | 507 |
| 440 // Check if trying to store a bad seed leaves the pref unchanged. | 508 // Check if trying to store a bad seed leaves the pref unchanged. |
| 441 prefs.ClearPref(prefs::kVariationsSeed); | 509 prefs.ClearPref(prefs::kVariationsSeed); |
| 442 EXPECT_FALSE(variations_service.StoreSeedData("should fail", now, &prefs)); | 510 EXPECT_FALSE(variations_service.StoreSeedData("should fail", now, &prefs)); |
| 443 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 511 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 444 } | 512 } |
| 445 | 513 |
| 446 TEST(VariationsServiceTest, ValidateStudy) { | 514 TEST_F(VariationsServiceTest, ValidateStudy) { |
| 447 Study study; | 515 Study study; |
| 448 study.set_default_experiment_name("def"); | 516 study.set_default_experiment_name("def"); |
| 449 | 517 |
| 450 Study_Experiment* experiment = study.add_experiment(); | 518 Study_Experiment* experiment = study.add_experiment(); |
| 451 experiment->set_name("abc"); | 519 experiment->set_name("abc"); |
| 452 experiment->set_probability_weight(100); | 520 experiment->set_probability_weight(100); |
| 453 | 521 |
| 454 Study_Experiment* default_group = study.add_experiment(); | 522 Study_Experiment* default_group = study.add_experiment(); |
| 455 default_group->set_name("def"); | 523 default_group->set_name("def"); |
| 456 default_group->set_probability_weight(200); | 524 default_group->set_probability_weight(200); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 &total_probability); | 578 &total_probability); |
| 511 ASSERT_TRUE(valid); | 579 ASSERT_TRUE(valid); |
| 512 Study_Experiment* repeated_group = study.add_experiment(); | 580 Study_Experiment* repeated_group = study.add_experiment(); |
| 513 repeated_group->set_name("abc"); | 581 repeated_group->set_name("abc"); |
| 514 repeated_group->set_probability_weight(1); | 582 repeated_group->set_probability_weight(1); |
| 515 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 583 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
| 516 &total_probability); | 584 &total_probability); |
| 517 EXPECT_FALSE(valid); | 585 EXPECT_FALSE(valid); |
| 518 } | 586 } |
| 519 | 587 |
| 520 TEST(VariationsServiceTest, RequestsInitiallyNotAllowed) { | 588 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { |
| 521 MessageLoopForUI message_loop; | 589 MessageLoopForUI message_loop; |
| 522 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | 590 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 523 &message_loop); | 591 &message_loop); |
| 524 TestingPrefServiceSimple prefs; | 592 TestingPrefServiceSimple prefs; |
| 525 VariationsService::RegisterPrefs(prefs.registry()); | 593 VariationsService::RegisterPrefs(prefs.registry()); |
| 526 | 594 |
| 527 // Pass ownership to TestVariationsService, but keep a weak pointer to | 595 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 528 // manipulate it for this test. | 596 // manipulate it for this test. |
| 529 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; | 597 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; |
| 530 TestVariationsService test_service(test_notifier, &prefs); | 598 TestVariationsService test_service(test_notifier, &prefs); |
| 531 | 599 |
| 532 // Force the notifier to initially disallow requests. | 600 // Force the notifier to initially disallow requests. |
| 533 test_notifier->SetRequestsAllowedOverride(false); | 601 test_notifier->SetRequestsAllowedOverride(false); |
| 534 test_service.StartRepeatedVariationsSeedFetch(); | 602 test_service.StartRepeatedVariationsSeedFetch(); |
| 535 EXPECT_FALSE(test_service.fetch_attempted()); | 603 EXPECT_FALSE(test_service.fetch_attempted()); |
| 536 | 604 |
| 537 test_notifier->NotifyObserver(); | 605 test_notifier->NotifyObserver(); |
| 538 EXPECT_TRUE(test_service.fetch_attempted()); | 606 EXPECT_TRUE(test_service.fetch_attempted()); |
| 539 } | 607 } |
| 540 | 608 |
| 541 TEST(VariationsServiceTest, RequestsInitiallyAllowed) { | 609 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) { |
| 542 MessageLoopForUI message_loop; | 610 MessageLoopForUI message_loop; |
| 543 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | 611 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 544 &message_loop); | 612 &message_loop); |
| 545 TestingPrefServiceSimple prefs; | 613 TestingPrefServiceSimple prefs; |
| 546 VariationsService::RegisterPrefs(prefs.registry()); | 614 VariationsService::RegisterPrefs(prefs.registry()); |
| 547 | 615 |
| 548 // Pass ownership to TestVariationsService, but keep a weak pointer to | 616 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 549 // manipulate it for this test. | 617 // manipulate it for this test. |
| 550 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; | 618 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; |
| 551 TestVariationsService test_service(test_notifier, &prefs); | 619 TestVariationsService test_service(test_notifier, &prefs); |
| 552 | 620 |
| 553 test_notifier->SetRequestsAllowedOverride(true); | 621 test_notifier->SetRequestsAllowedOverride(true); |
| 554 test_service.StartRepeatedVariationsSeedFetch(); | 622 test_service.StartRepeatedVariationsSeedFetch(); |
| 555 EXPECT_TRUE(test_service.fetch_attempted()); | 623 EXPECT_TRUE(test_service.fetch_attempted()); |
| 556 } | 624 } |
| 557 | 625 |
| 558 TEST(VariationsServiceTest, SeedStoredWhenOKStatus) { | 626 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { |
| 559 MessageLoop message_loop; | 627 MessageLoop message_loop; |
| 560 content::TestBrowserThread io_thread(content::BrowserThread::IO, | 628 content::TestBrowserThread io_thread(content::BrowserThread::IO, |
| 561 &message_loop); | 629 &message_loop); |
| 562 TestingPrefServiceSimple prefs; | 630 TestingPrefServiceSimple prefs; |
| 563 VariationsService::RegisterPrefs(prefs.registry()); | 631 VariationsService::RegisterPrefs(prefs.registry()); |
| 564 | 632 |
| 565 VariationsService variations_service(new TestRequestAllowedNotifier, &prefs); | 633 VariationsService variations_service(new TestRequestAllowedNotifier, &prefs); |
| 566 | 634 |
| 567 net::TestURLFetcherFactory factory; | 635 net::TestURLFetcherFactory factory; |
| 568 variations_service.DoActualFetch(); | 636 variations_service.DoActualFetch(); |
| 569 | 637 |
| 570 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 638 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 571 SimulateServerResponse(net::HTTP_OK, fetcher); | 639 SimulateServerResponse(net::HTTP_OK, fetcher); |
| 572 const TrialsSeed seed = CreateTestSeed(); | 640 const TrialsSeed seed = CreateTestSeed(); |
| 573 fetcher->SetResponseString(SerializeSeed(seed)); | 641 fetcher->SetResponseString(SerializeSeed(seed)); |
| 574 | 642 |
| 575 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 643 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 576 variations_service.OnURLFetchComplete(fetcher); | 644 variations_service.OnURLFetchComplete(fetcher); |
| 577 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 645 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 578 EXPECT_EQ(SerializeSeedBase64(seed), prefs.GetString(prefs::kVariationsSeed)); | 646 EXPECT_EQ(SerializeSeedBase64(seed), prefs.GetString(prefs::kVariationsSeed)); |
| 579 } | 647 } |
| 580 | 648 |
| 581 TEST(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { | 649 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { |
| 582 const int non_ok_status_codes[] = { | 650 const int non_ok_status_codes[] = { |
| 583 net::HTTP_NO_CONTENT, | 651 net::HTTP_NO_CONTENT, |
| 584 net::HTTP_NOT_MODIFIED, | 652 net::HTTP_NOT_MODIFIED, |
| 585 net::HTTP_NOT_FOUND, | 653 net::HTTP_NOT_FOUND, |
| 586 net::HTTP_INTERNAL_SERVER_ERROR, | 654 net::HTTP_INTERNAL_SERVER_ERROR, |
| 587 net::HTTP_SERVICE_UNAVAILABLE, | 655 net::HTTP_SERVICE_UNAVAILABLE, |
| 588 }; | 656 }; |
| 589 | 657 |
| 590 MessageLoop message_loop; | 658 MessageLoop message_loop; |
| 591 content::TestBrowserThread io_thread(content::BrowserThread::IO, | 659 content::TestBrowserThread io_thread(content::BrowserThread::IO, |
| 592 &message_loop); | 660 &message_loop); |
| 593 TestingPrefServiceSimple prefs; | 661 TestingPrefServiceSimple prefs; |
| 594 VariationsService::RegisterPrefs(prefs.registry()); | 662 VariationsService::RegisterPrefs(prefs.registry()); |
| 595 | 663 |
| 596 VariationsService variations_service(new TestRequestAllowedNotifier, &prefs); | 664 VariationsService variations_service(new TestRequestAllowedNotifier, &prefs); |
| 597 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { | 665 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { |
| 598 net::TestURLFetcherFactory factory; | 666 net::TestURLFetcherFactory factory; |
| 599 variations_service.DoActualFetch(); | 667 variations_service.DoActualFetch(); |
| 600 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 668 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 601 | 669 |
| 602 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 670 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 603 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 671 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
| 604 variations_service.OnURLFetchComplete(fetcher); | 672 variations_service.OnURLFetchComplete(fetcher); |
| 605 | 673 |
| 606 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 674 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 607 } | 675 } |
| 608 } | 676 } |
| 609 | 677 |
| 610 } // namespace chrome_variations | 678 } // namespace chrome_variations |
| OLD | NEW |