Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: chrome/browser/metrics/variations/variations_service_unittest.cc

Issue 1313213005: Turn VariationsService::GetVariationsServerURL into instance method (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/metrics/variations/variations_service.h" 5 #include "chrome/browser/metrics/variations/variations_service.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/json/json_string_value_serializer.h" 10 #include "base/json/json_string_value_serializer.h"
(...skipping 25 matching lines...) Expand all
36 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" 36 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
37 #endif 37 #endif
38 38
39 namespace chrome_variations { 39 namespace chrome_variations {
40 40
41 namespace { 41 namespace {
42 42
43 // A test class used to validate expected functionality in VariationsService. 43 // A test class used to validate expected functionality in VariationsService.
44 class TestVariationsService : public VariationsService { 44 class TestVariationsService : public VariationsService {
45 public: 45 public:
46 TestVariationsService(web_resource::TestRequestAllowedNotifier* test_notifier, 46 TestVariationsService(
47 PrefService* local_state) 47 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier,
48 PrefService* local_state)
48 : VariationsService(make_scoped_ptr(new ChromeVariationsServiceClient()), 49 : VariationsService(make_scoped_ptr(new ChromeVariationsServiceClient()),
49 test_notifier, 50 test_notifier.Pass(),
50 local_state, 51 local_state,
51 NULL), 52 NULL),
52 intercepts_fetch_(true), 53 intercepts_fetch_(true),
53 fetch_attempted_(false), 54 fetch_attempted_(false),
54 seed_stored_(false) { 55 seed_stored_(false) {
55 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. 56 // Set this so StartRepeatedVariationsSeedFetch can be called in tests.
56 SetCreateTrialsFromSeedCalledForTesting(true); 57 SetCreateTrialsFromSeedCalledForTesting(true);
57 } 58 }
58 59
59 ~TestVariationsService() override {} 60 ~TestVariationsService() override {}
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); 258 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest);
258 }; 259 };
259 260
260 TEST_F(VariationsServiceTest, GetVariationsServerURL) { 261 TEST_F(VariationsServiceTest, GetVariationsServerURL) {
261 TestVariationsPrefsStore prefs_store; 262 TestVariationsPrefsStore prefs_store;
262 PrefService* prefs = prefs_store.prefs(); 263 PrefService* prefs = prefs_store.prefs();
263 const std::string default_variations_url = 264 const std::string default_variations_url =
264 VariationsService::GetDefaultVariationsServerURLForTesting(); 265 VariationsService::GetDefaultVariationsServerURLForTesting();
265 266
266 std::string value; 267 std::string value;
267 GURL url = VariationsService::GetVariationsServerURL(prefs, std::string()); 268 TestVariationsService service(
269 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(prefs)),
270 prefs);
271 GURL url = service.GetVariationsServerURL(prefs, std::string());
268 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, 272 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url,
269 base::CompareCase::SENSITIVE)); 273 base::CompareCase::SENSITIVE));
270 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); 274 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value));
271 275
272 prefs_store.SetVariationsRestrictParameterPolicyValue("restricted"); 276 prefs_store.SetVariationsRestrictParameterPolicyValue("restricted");
273 url = VariationsService::GetVariationsServerURL(prefs, std::string()); 277 url = service.GetVariationsServerURL(prefs, std::string());
274 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, 278 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url,
275 base::CompareCase::SENSITIVE)); 279 base::CompareCase::SENSITIVE));
276 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); 280 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value));
277 EXPECT_EQ("restricted", value); 281 EXPECT_EQ("restricted", value);
278 282
279 // The override value should take precedence over what's in prefs. 283 // The override value should take precedence over what's in prefs.
280 url = VariationsService::GetVariationsServerURL(prefs, "override"); 284 url = service.GetVariationsServerURL(prefs, "override");
281 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, 285 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url,
282 base::CompareCase::SENSITIVE)); 286 base::CompareCase::SENSITIVE));
283 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); 287 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value));
284 EXPECT_EQ("override", value); 288 EXPECT_EQ("override", value);
285 } 289 }
286 290
287 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) { 291 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) {
288 TestingPrefServiceSimple prefs; 292 TestingPrefServiceSimple prefs;
289 VariationsService::RegisterPrefs(prefs.registry()); 293 VariationsService::RegisterPrefs(prefs.registry());
290 const GURL url = 294 TestVariationsService service(
291 VariationsService::GetVariationsServerURL(&prefs, std::string()); 295 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
296 &prefs);
297 const GURL url = service.GetVariationsServerURL(&prefs, std::string());
292 298
293 std::string value; 299 std::string value;
294 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); 300 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value));
295 EXPECT_FALSE(value.empty()); 301 EXPECT_FALSE(value.empty());
296 } 302 }
297 303
298 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { 304 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) {
299 TestingPrefServiceSimple prefs; 305 TestingPrefServiceSimple prefs;
300 VariationsService::RegisterPrefs(prefs.registry()); 306 VariationsService::RegisterPrefs(prefs.registry());
301 307
302 // Pass ownership to TestVariationsService, but keep a weak pointer to 308 // Pass ownership to TestVariationsService, but keep a weak pointer to
303 // manipulate it for this test. 309 // manipulate it for this test.
304 web_resource::TestRequestAllowedNotifier* test_notifier = 310 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier =
305 new web_resource::TestRequestAllowedNotifier(&prefs); 311 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs));
306 TestVariationsService test_service(test_notifier, &prefs); 312 web_resource::TestRequestAllowedNotifier* raw_notifier = test_notifier.get();
313 TestVariationsService test_service(test_notifier.Pass(), &prefs);
307 314
308 // Force the notifier to initially disallow requests. 315 // Force the notifier to initially disallow requests.
309 test_notifier->SetRequestsAllowedOverride(false); 316 raw_notifier->SetRequestsAllowedOverride(false);
310 test_service.StartRepeatedVariationsSeedFetch(); 317 test_service.StartRepeatedVariationsSeedFetch();
311 EXPECT_FALSE(test_service.fetch_attempted()); 318 EXPECT_FALSE(test_service.fetch_attempted());
312 319
313 test_notifier->NotifyObserver(); 320 raw_notifier->NotifyObserver();
314 EXPECT_TRUE(test_service.fetch_attempted()); 321 EXPECT_TRUE(test_service.fetch_attempted());
315 } 322 }
316 323
317 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) { 324 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) {
318 TestingPrefServiceSimple prefs; 325 TestingPrefServiceSimple prefs;
319 VariationsService::RegisterPrefs(prefs.registry()); 326 VariationsService::RegisterPrefs(prefs.registry());
320 327
321 // Pass ownership to TestVariationsService, but keep a weak pointer to 328 // Pass ownership to TestVariationsService, but keep a weak pointer to
322 // manipulate it for this test. 329 // manipulate it for this test.
323 web_resource::TestRequestAllowedNotifier* test_notifier = 330 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier =
324 new web_resource::TestRequestAllowedNotifier(&prefs); 331 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs));
325 TestVariationsService test_service(test_notifier, &prefs); 332 web_resource::TestRequestAllowedNotifier* raw_notifier = test_notifier.get();
333 TestVariationsService test_service(test_notifier.Pass(), &prefs);
326 334
327 test_notifier->SetRequestsAllowedOverride(true); 335 raw_notifier->SetRequestsAllowedOverride(true);
328 test_service.StartRepeatedVariationsSeedFetch(); 336 test_service.StartRepeatedVariationsSeedFetch();
329 EXPECT_TRUE(test_service.fetch_attempted()); 337 EXPECT_TRUE(test_service.fetch_attempted());
330 } 338 }
331 339
332 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { 340 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) {
333 TestingPrefServiceSimple prefs; 341 TestingPrefServiceSimple prefs;
334 VariationsService::RegisterPrefs(prefs.registry()); 342 VariationsService::RegisterPrefs(prefs.registry());
335 343
336 TestVariationsService service( 344 TestVariationsService service(
337 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); 345 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
346 &prefs);
338 service.variations_server_url_ = 347 service.variations_server_url_ =
339 VariationsService::GetVariationsServerURL(&prefs, std::string()); 348 service.GetVariationsServerURL(&prefs, std::string());
340 service.set_intercepts_fetch(false); 349 service.set_intercepts_fetch(false);
341 350
342 net::TestURLFetcherFactory factory; 351 net::TestURLFetcherFactory factory;
343 service.DoActualFetch(); 352 service.DoActualFetch();
344 353
345 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 354 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
346 SimulateServerResponse(net::HTTP_OK, fetcher); 355 SimulateServerResponse(net::HTTP_OK, fetcher);
347 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); 356 fetcher->SetResponseString(SerializeSeed(CreateTestSeed()));
348 357
349 EXPECT_FALSE(service.seed_stored()); 358 EXPECT_FALSE(service.seed_stored());
350 service.OnURLFetchComplete(fetcher); 359 service.OnURLFetchComplete(fetcher);
351 EXPECT_TRUE(service.seed_stored()); 360 EXPECT_TRUE(service.seed_stored());
352 } 361 }
353 362
354 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { 363 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) {
355 const int non_ok_status_codes[] = { 364 const int non_ok_status_codes[] = {
356 net::HTTP_NO_CONTENT, 365 net::HTTP_NO_CONTENT,
357 net::HTTP_NOT_MODIFIED, 366 net::HTTP_NOT_MODIFIED,
358 net::HTTP_NOT_FOUND, 367 net::HTTP_NOT_FOUND,
359 net::HTTP_INTERNAL_SERVER_ERROR, 368 net::HTTP_INTERNAL_SERVER_ERROR,
360 net::HTTP_SERVICE_UNAVAILABLE, 369 net::HTTP_SERVICE_UNAVAILABLE,
361 }; 370 };
362 371
363 TestingPrefServiceSimple prefs; 372 TestingPrefServiceSimple prefs;
364 VariationsService::RegisterPrefs(prefs.registry()); 373 VariationsService::RegisterPrefs(prefs.registry());
365 374
366 VariationsService service( 375 VariationsService service(
367 make_scoped_ptr(new ChromeVariationsServiceClient()), 376 make_scoped_ptr(new ChromeVariationsServiceClient()),
368 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); 377 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
378 &prefs, NULL);
369 service.variations_server_url_ = 379 service.variations_server_url_ =
370 VariationsService::GetVariationsServerURL(&prefs, std::string()); 380 service.GetVariationsServerURL(&prefs, std::string());
371 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { 381 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) {
372 net::TestURLFetcherFactory factory; 382 net::TestURLFetcherFactory factory;
373 service.DoActualFetch(); 383 service.DoActualFetch();
374 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 384 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
375 385
376 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 386 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
377 SimulateServerResponse(non_ok_status_codes[i], fetcher); 387 SimulateServerResponse(non_ok_status_codes[i], fetcher);
378 service.OnURLFetchComplete(fetcher); 388 service.OnURLFetchComplete(fetcher);
379 389
380 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 390 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
381 } 391 }
382 } 392 }
383 393
384 TEST_F(VariationsServiceTest, CountryHeader) { 394 TEST_F(VariationsServiceTest, CountryHeader) {
385 TestingPrefServiceSimple prefs; 395 TestingPrefServiceSimple prefs;
386 VariationsService::RegisterPrefs(prefs.registry()); 396 VariationsService::RegisterPrefs(prefs.registry());
387 397
388 TestVariationsService service( 398 TestVariationsService service(
389 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); 399 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
400 &prefs);
390 service.variations_server_url_ = 401 service.variations_server_url_ =
391 VariationsService::GetVariationsServerURL(&prefs, std::string()); 402 service.GetVariationsServerURL(&prefs, std::string());
392 service.set_intercepts_fetch(false); 403 service.set_intercepts_fetch(false);
393 404
394 net::TestURLFetcherFactory factory; 405 net::TestURLFetcherFactory factory;
395 service.DoActualFetch(); 406 service.DoActualFetch();
396 407
397 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 408 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
398 scoped_refptr<net::HttpResponseHeaders> headers = 409 scoped_refptr<net::HttpResponseHeaders> headers =
399 SimulateServerResponse(net::HTTP_OK, fetcher); 410 SimulateServerResponse(net::HTTP_OK, fetcher);
400 headers->AddHeader("X-Country: test"); 411 headers->AddHeader("X-Country: test");
401 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); 412 fetcher->SetResponseString(SerializeSeed(CreateTestSeed()));
402 413
403 EXPECT_FALSE(service.seed_stored()); 414 EXPECT_FALSE(service.seed_stored());
404 service.OnURLFetchComplete(fetcher); 415 service.OnURLFetchComplete(fetcher);
405 EXPECT_TRUE(service.seed_stored()); 416 EXPECT_TRUE(service.seed_stored());
406 EXPECT_EQ("test", service.stored_country()); 417 EXPECT_EQ("test", service.stored_country());
407 } 418 }
408 419
409 TEST_F(VariationsServiceTest, Observer) { 420 TEST_F(VariationsServiceTest, Observer) {
410 TestingPrefServiceSimple prefs; 421 TestingPrefServiceSimple prefs;
411 VariationsService::RegisterPrefs(prefs.registry()); 422 VariationsService::RegisterPrefs(prefs.registry());
412 VariationsService service( 423 VariationsService service(
413 make_scoped_ptr(new ChromeVariationsServiceClient()), 424 make_scoped_ptr(new ChromeVariationsServiceClient()),
414 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); 425 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
426 &prefs, NULL);
415 427
416 struct { 428 struct {
417 int normal_count; 429 int normal_count;
418 int best_effort_count; 430 int best_effort_count;
419 int critical_count; 431 int critical_count;
420 int expected_best_effort_notifications; 432 int expected_best_effort_notifications;
421 int expected_crtical_notifications; 433 int expected_crtical_notifications;
422 } cases[] = { 434 } cases[] = {
423 {0, 0, 0, 0, 0}, 435 {0, 0, 0, 0, 0},
424 {1, 0, 0, 0, 0}, 436 {1, 0, 0, 0, 0},
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 VariationsService::LOAD_COUNTRY_INVALID_PREF_HAS_SEED}, 515 VariationsService::LOAD_COUNTRY_INVALID_PREF_HAS_SEED},
504 {"badversion,ca", "20.0.0.0", nullptr, "", "", 516 {"badversion,ca", "20.0.0.0", nullptr, "", "",
505 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED}, 517 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED},
506 }; 518 };
507 519
508 for (const auto& test : test_cases) { 520 for (const auto& test : test_cases) {
509 TestingPrefServiceSimple prefs; 521 TestingPrefServiceSimple prefs;
510 VariationsService::RegisterPrefs(prefs.registry()); 522 VariationsService::RegisterPrefs(prefs.registry());
511 VariationsService service( 523 VariationsService service(
512 make_scoped_ptr(new ChromeVariationsServiceClient()), 524 make_scoped_ptr(new ChromeVariationsServiceClient()),
513 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); 525 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
526 &prefs, NULL);
514 527
515 if (test.pref_value_before) { 528 if (test.pref_value_before) {
516 base::ListValue list_value; 529 base::ListValue list_value;
517 for (const std::string& component : 530 for (const std::string& component :
518 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, 531 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE,
519 base::SPLIT_WANT_ALL)) { 532 base::SPLIT_WANT_ALL)) {
520 list_value.AppendString(component); 533 list_value.AppendString(component);
521 } 534 }
522 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); 535 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value);
523 } 536 }
(...skipping 23 matching lines...) Expand all
547 << test.pref_value_before << ", " << test.version << ", " 560 << test.pref_value_before << ", " << test.version << ", "
548 << test.latest_country_code; 561 << test.latest_country_code;
549 562
550 histogram_tester.ExpectUniqueSample( 563 histogram_tester.ExpectUniqueSample(
551 "Variations.LoadPermanentConsistencyCountryResult", 564 "Variations.LoadPermanentConsistencyCountryResult",
552 test.expected_result, 1); 565 test.expected_result, 1);
553 } 566 }
554 } 567 }
555 568
556 } // namespace chrome_variations 569 } // namespace chrome_variations
OLDNEW
« no previous file with comments | « chrome/browser/metrics/variations/variations_service.cc ('k') | chrome/browser/policy/policy_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698