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