Chromium Code Reviews| 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. Initializes CrosSettings for testing. | |
|
Alexei Svitkine (slow)
2013/04/18 19:27:53
Nit: "Not used" -> "Not used directly"
Mathieu
2013/04/18 19:51:14
Done.
Mathieu
2013/04/18 19:51:14
Done.
| |
| 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 testing::Test { | |
| 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_; | |
|
Alexei Svitkine (slow)
2013/04/18 19:27:53
This should be private.
Mathieu
2013/04/18 19:51:14
Compiler complains when it's used in the test if p
Alexei Svitkine (slow)
2013/04/18 20:01:27
Ah, I didn't see that you were using in that test.
| |
| 430 | |
| 431 private: | |
| 432 // Not used. Initializes CrosSettings for testing. | |
| 433 chromeos::ScopedTestCrosSettings scoped_test_cros_settings; | |
|
Alexei Svitkine (slow)
2013/04/18 19:27:53
You can remove this if you inherit from Variations
Mathieu
2013/04/18 19:51:14
Done.
| |
| 434 | |
| 435 chromeos::StubCrosSettingsProvider stub_settings_provider_; | |
| 436 chromeos::CrosSettingsProvider* device_settings_provider_; | |
| 437 | |
| 438 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTestChromeOS); | |
| 439 }; | |
| 440 | |
| 441 TEST_F(VariationsServiceTestChromeOS, VariationsURLIsValid) { | |
| 442 TestingPrefServiceSimple prefs; | |
| 443 VariationsService::RegisterPrefs(prefs.registry()); | |
| 444 const std::string default_variations_url = | |
| 445 VariationsService::GetDefaultVariationsServerURLForTesting(); | |
| 446 | |
| 447 EXPECT_EQ(default_variations_url, | |
| 448 VariationsService::GetVariationsServerURL(&prefs).spec()); | |
| 449 | |
| 450 cros_settings_->SetString(chromeos::kVariationsRestrictParameter, | |
| 451 "restricted"); | |
| 452 EXPECT_EQ(default_variations_url + "?restrict=restricted", | |
| 453 VariationsService::GetVariationsServerURL(&prefs).spec()); | |
| 454 } | |
| 455 #endif | |
| 456 | |
| 457 TEST_F(VariationsServiceTest, LoadSeed) { | |
| 387 // Store good seed data to test if loading from prefs works. | 458 // Store good seed data to test if loading from prefs works. |
| 388 const TrialsSeed seed = CreateTestSeed(); | 459 const TrialsSeed seed = CreateTestSeed(); |
| 389 const std::string base64_seed = SerializeSeedBase64(seed); | 460 const std::string base64_seed = SerializeSeedBase64(seed); |
| 390 | 461 |
| 391 TestingPrefServiceSimple prefs; | 462 TestingPrefServiceSimple prefs; |
| 392 VariationsService::RegisterPrefs(prefs.registry()); | 463 VariationsService::RegisterPrefs(prefs.registry()); |
| 393 prefs.SetString(prefs::kVariationsSeed, base64_seed); | 464 prefs.SetString(prefs::kVariationsSeed, base64_seed); |
| 394 | 465 |
| 395 TestVariationsService variations_service(new TestRequestAllowedNotifier, | 466 TestVariationsService variations_service(new TestRequestAllowedNotifier, |
| 396 &prefs); | 467 &prefs); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 408 prefs.SetString(prefs::kVariationsSeed, "this should fail"); | 479 prefs.SetString(prefs::kVariationsSeed, "this should fail"); |
| 409 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 480 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 410 EXPECT_FALSE(variations_service.LoadTrialsSeedFromPref(&prefs, &loaded_seed)); | 481 EXPECT_FALSE(variations_service.LoadTrialsSeedFromPref(&prefs, &loaded_seed)); |
| 411 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 482 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 412 | 483 |
| 413 // Check that having no seed in prefs results in a return value of false. | 484 // Check that having no seed in prefs results in a return value of false. |
| 414 prefs.ClearPref(prefs::kVariationsSeed); | 485 prefs.ClearPref(prefs::kVariationsSeed); |
| 415 EXPECT_FALSE(variations_service.LoadTrialsSeedFromPref(&prefs, &loaded_seed)); | 486 EXPECT_FALSE(variations_service.LoadTrialsSeedFromPref(&prefs, &loaded_seed)); |
| 416 } | 487 } |
| 417 | 488 |
| 418 TEST(VariationsServiceTest, StoreSeed) { | 489 TEST_F(VariationsServiceTest, StoreSeed) { |
| 419 const base::Time now = base::Time::Now(); | 490 const base::Time now = base::Time::Now(); |
| 420 const TrialsSeed seed = CreateTestSeed(); | 491 const TrialsSeed seed = CreateTestSeed(); |
| 421 const std::string serialized_seed = SerializeSeed(seed); | 492 const std::string serialized_seed = SerializeSeed(seed); |
| 422 | 493 |
| 423 TestingPrefServiceSimple prefs; | 494 TestingPrefServiceSimple prefs; |
| 424 VariationsService::RegisterPrefs(prefs.registry()); | 495 VariationsService::RegisterPrefs(prefs.registry()); |
| 425 | 496 |
| 426 TestVariationsService variations_service(new TestRequestAllowedNotifier, | 497 TestVariationsService variations_service(new TestRequestAllowedNotifier, |
| 427 &prefs); | 498 &prefs); |
| 428 | 499 |
| 429 EXPECT_TRUE(variations_service.StoreSeedData(serialized_seed, now, &prefs)); | 500 EXPECT_TRUE(variations_service.StoreSeedData(serialized_seed, now, &prefs)); |
| 430 // Make sure the pref was actually set. | 501 // Make sure the pref was actually set. |
| 431 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 502 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 432 | 503 |
| 433 std::string loaded_serialized_seed = prefs.GetString(prefs::kVariationsSeed); | 504 std::string loaded_serialized_seed = prefs.GetString(prefs::kVariationsSeed); |
| 434 std::string decoded_serialized_seed; | 505 std::string decoded_serialized_seed; |
| 435 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, | 506 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, |
| 436 &decoded_serialized_seed)); | 507 &decoded_serialized_seed)); |
| 437 // Make sure the stored seed from pref is the same as the seed we created. | 508 // Make sure the stored seed from pref is the same as the seed we created. |
| 438 EXPECT_EQ(serialized_seed, decoded_serialized_seed); | 509 EXPECT_EQ(serialized_seed, decoded_serialized_seed); |
| 439 | 510 |
| 440 // Check if trying to store a bad seed leaves the pref unchanged. | 511 // Check if trying to store a bad seed leaves the pref unchanged. |
| 441 prefs.ClearPref(prefs::kVariationsSeed); | 512 prefs.ClearPref(prefs::kVariationsSeed); |
| 442 EXPECT_FALSE(variations_service.StoreSeedData("should fail", now, &prefs)); | 513 EXPECT_FALSE(variations_service.StoreSeedData("should fail", now, &prefs)); |
| 443 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 514 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 444 } | 515 } |
| 445 | 516 |
| 446 TEST(VariationsServiceTest, ValidateStudy) { | 517 TEST_F(VariationsServiceTest, ValidateStudy) { |
| 447 Study study; | 518 Study study; |
| 448 study.set_default_experiment_name("def"); | 519 study.set_default_experiment_name("def"); |
| 449 | 520 |
| 450 Study_Experiment* experiment = study.add_experiment(); | 521 Study_Experiment* experiment = study.add_experiment(); |
| 451 experiment->set_name("abc"); | 522 experiment->set_name("abc"); |
| 452 experiment->set_probability_weight(100); | 523 experiment->set_probability_weight(100); |
| 453 | 524 |
| 454 Study_Experiment* default_group = study.add_experiment(); | 525 Study_Experiment* default_group = study.add_experiment(); |
| 455 default_group->set_name("def"); | 526 default_group->set_name("def"); |
| 456 default_group->set_probability_weight(200); | 527 default_group->set_probability_weight(200); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 510 &total_probability); | 581 &total_probability); |
| 511 ASSERT_TRUE(valid); | 582 ASSERT_TRUE(valid); |
| 512 Study_Experiment* repeated_group = study.add_experiment(); | 583 Study_Experiment* repeated_group = study.add_experiment(); |
| 513 repeated_group->set_name("abc"); | 584 repeated_group->set_name("abc"); |
| 514 repeated_group->set_probability_weight(1); | 585 repeated_group->set_probability_weight(1); |
| 515 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 586 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
| 516 &total_probability); | 587 &total_probability); |
| 517 EXPECT_FALSE(valid); | 588 EXPECT_FALSE(valid); |
| 518 } | 589 } |
| 519 | 590 |
| 520 TEST(VariationsServiceTest, RequestsInitiallyNotAllowed) { | 591 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { |
| 521 MessageLoopForUI message_loop; | 592 MessageLoopForUI message_loop; |
| 522 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | 593 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 523 &message_loop); | 594 &message_loop); |
| 524 TestingPrefServiceSimple prefs; | 595 TestingPrefServiceSimple prefs; |
| 525 VariationsService::RegisterPrefs(prefs.registry()); | 596 VariationsService::RegisterPrefs(prefs.registry()); |
| 526 | 597 |
| 527 // Pass ownership to TestVariationsService, but keep a weak pointer to | 598 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 528 // manipulate it for this test. | 599 // manipulate it for this test. |
| 529 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; | 600 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; |
| 530 TestVariationsService test_service(test_notifier, &prefs); | 601 TestVariationsService test_service(test_notifier, &prefs); |
| 531 | 602 |
| 532 // Force the notifier to initially disallow requests. | 603 // Force the notifier to initially disallow requests. |
| 533 test_notifier->SetRequestsAllowedOverride(false); | 604 test_notifier->SetRequestsAllowedOverride(false); |
| 534 test_service.StartRepeatedVariationsSeedFetch(); | 605 test_service.StartRepeatedVariationsSeedFetch(); |
| 535 EXPECT_FALSE(test_service.fetch_attempted()); | 606 EXPECT_FALSE(test_service.fetch_attempted()); |
| 536 | 607 |
| 537 test_notifier->NotifyObserver(); | 608 test_notifier->NotifyObserver(); |
| 538 EXPECT_TRUE(test_service.fetch_attempted()); | 609 EXPECT_TRUE(test_service.fetch_attempted()); |
| 539 } | 610 } |
| 540 | 611 |
| 541 TEST(VariationsServiceTest, RequestsInitiallyAllowed) { | 612 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) { |
| 542 MessageLoopForUI message_loop; | 613 MessageLoopForUI message_loop; |
| 543 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | 614 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 544 &message_loop); | 615 &message_loop); |
| 545 TestingPrefServiceSimple prefs; | 616 TestingPrefServiceSimple prefs; |
| 546 VariationsService::RegisterPrefs(prefs.registry()); | 617 VariationsService::RegisterPrefs(prefs.registry()); |
| 547 | 618 |
| 548 // Pass ownership to TestVariationsService, but keep a weak pointer to | 619 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 549 // manipulate it for this test. | 620 // manipulate it for this test. |
| 550 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; | 621 TestRequestAllowedNotifier* test_notifier = new TestRequestAllowedNotifier; |
| 551 TestVariationsService test_service(test_notifier, &prefs); | 622 TestVariationsService test_service(test_notifier, &prefs); |
| 552 | 623 |
| 553 test_notifier->SetRequestsAllowedOverride(true); | 624 test_notifier->SetRequestsAllowedOverride(true); |
| 554 test_service.StartRepeatedVariationsSeedFetch(); | 625 test_service.StartRepeatedVariationsSeedFetch(); |
| 555 EXPECT_TRUE(test_service.fetch_attempted()); | 626 EXPECT_TRUE(test_service.fetch_attempted()); |
| 556 } | 627 } |
| 557 | 628 |
| 558 TEST(VariationsServiceTest, SeedStoredWhenOKStatus) { | 629 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { |
| 559 MessageLoop message_loop; | 630 MessageLoop message_loop; |
| 560 content::TestBrowserThread io_thread(content::BrowserThread::IO, | 631 content::TestBrowserThread io_thread(content::BrowserThread::IO, |
| 561 &message_loop); | 632 &message_loop); |
| 562 TestingPrefServiceSimple prefs; | 633 TestingPrefServiceSimple prefs; |
| 563 VariationsService::RegisterPrefs(prefs.registry()); | 634 VariationsService::RegisterPrefs(prefs.registry()); |
| 564 | 635 |
| 565 VariationsService variations_service(new TestRequestAllowedNotifier, &prefs); | 636 VariationsService variations_service(new TestRequestAllowedNotifier, &prefs); |
| 566 | 637 |
| 567 net::TestURLFetcherFactory factory; | 638 net::TestURLFetcherFactory factory; |
| 568 variations_service.DoActualFetch(); | 639 variations_service.DoActualFetch(); |
| 569 | 640 |
| 570 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 641 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 571 SimulateServerResponse(net::HTTP_OK, fetcher); | 642 SimulateServerResponse(net::HTTP_OK, fetcher); |
| 572 const TrialsSeed seed = CreateTestSeed(); | 643 const TrialsSeed seed = CreateTestSeed(); |
| 573 fetcher->SetResponseString(SerializeSeed(seed)); | 644 fetcher->SetResponseString(SerializeSeed(seed)); |
| 574 | 645 |
| 575 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 646 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 576 variations_service.OnURLFetchComplete(fetcher); | 647 variations_service.OnURLFetchComplete(fetcher); |
| 577 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 648 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 578 EXPECT_EQ(SerializeSeedBase64(seed), prefs.GetString(prefs::kVariationsSeed)); | 649 EXPECT_EQ(SerializeSeedBase64(seed), prefs.GetString(prefs::kVariationsSeed)); |
| 579 } | 650 } |
| 580 | 651 |
| 581 TEST(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { | 652 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { |
| 582 const int non_ok_status_codes[] = { | 653 const int non_ok_status_codes[] = { |
| 583 net::HTTP_NO_CONTENT, | 654 net::HTTP_NO_CONTENT, |
| 584 net::HTTP_NOT_MODIFIED, | 655 net::HTTP_NOT_MODIFIED, |
| 585 net::HTTP_NOT_FOUND, | 656 net::HTTP_NOT_FOUND, |
| 586 net::HTTP_INTERNAL_SERVER_ERROR, | 657 net::HTTP_INTERNAL_SERVER_ERROR, |
| 587 net::HTTP_SERVICE_UNAVAILABLE, | 658 net::HTTP_SERVICE_UNAVAILABLE, |
| 588 }; | 659 }; |
| 589 | 660 |
| 590 MessageLoop message_loop; | 661 MessageLoop message_loop; |
| 591 content::TestBrowserThread io_thread(content::BrowserThread::IO, | 662 content::TestBrowserThread io_thread(content::BrowserThread::IO, |
| 592 &message_loop); | 663 &message_loop); |
| 593 TestingPrefServiceSimple prefs; | 664 TestingPrefServiceSimple prefs; |
| 594 VariationsService::RegisterPrefs(prefs.registry()); | 665 VariationsService::RegisterPrefs(prefs.registry()); |
| 595 | 666 |
| 596 VariationsService variations_service(new TestRequestAllowedNotifier, &prefs); | 667 VariationsService variations_service(new TestRequestAllowedNotifier, &prefs); |
| 597 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { | 668 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { |
| 598 net::TestURLFetcherFactory factory; | 669 net::TestURLFetcherFactory factory; |
| 599 variations_service.DoActualFetch(); | 670 variations_service.DoActualFetch(); |
| 600 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 671 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 601 | 672 |
| 602 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 673 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 603 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 674 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
| 604 variations_service.OnURLFetchComplete(fetcher); | 675 variations_service.OnURLFetchComplete(fetcher); |
| 605 | 676 |
| 606 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 677 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 607 } | 678 } |
| 608 } | 679 } |
| 609 | 680 |
| 610 } // namespace chrome_variations | 681 } // namespace chrome_variations |
| OLD | NEW |