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

Side by Side Diff: components/variations/variations_seed_processor_unittest.cc

Issue 71753004: Allow variation id with forcing flag for special setup & unit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Allow variation id with forcing flag for special setup & unit tests. Created 7 years, 1 month 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/variations/variations_seed_processor.h" 5 #include "components/variations/variations_seed_processor.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/strings/string_split.h" 10 #include "base/strings/string_split.h"
(...skipping 11 matching lines...) Expand all
22 } 22 }
23 23
24 // Constants for testing associating command line flags with trial groups. 24 // Constants for testing associating command line flags with trial groups.
25 const char kFlagStudyName[] = "flag_test_trial"; 25 const char kFlagStudyName[] = "flag_test_trial";
26 const char kFlagGroup1Name[] = "flag_group1"; 26 const char kFlagGroup1Name[] = "flag_group1";
27 const char kFlagGroup2Name[] = "flag_group2"; 27 const char kFlagGroup2Name[] = "flag_group2";
28 const char kNonFlagGroupName[] = "non_flag_group"; 28 const char kNonFlagGroupName[] = "non_flag_group";
29 const char kForcingFlag1[] = "flag_test1"; 29 const char kForcingFlag1[] = "flag_test1";
30 const char kForcingFlag2[] = "flag_test2"; 30 const char kForcingFlag2[] = "flag_test2";
31 31
32 const VariationID kExperimentId = 123;
33
32 // Adds an experiment to |study| with the specified |name| and |probability|. 34 // Adds an experiment to |study| with the specified |name| and |probability|.
33 Study_Experiment* AddExperiment(const std::string& name, int probability, 35 Study_Experiment* AddExperiment(const std::string& name, int probability,
34 Study* study) { 36 Study* study) {
35 Study_Experiment* experiment = study->add_experiment(); 37 Study_Experiment* experiment = study->add_experiment();
36 experiment->set_name(name); 38 experiment->set_name(name);
37 experiment->set_probability_weight(probability); 39 experiment->set_probability_weight(probability);
38 return experiment; 40 return experiment;
39 } 41 }
40 42
41 // Populates |study| with test data used for testing associating command line 43 // Populates |study| with test data used for testing associating command line
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 // Ensure that the maps are cleared between tests, since they are stored as 85 // Ensure that the maps are cleared between tests, since they are stored as
84 // process singletons. 86 // process singletons.
85 testing::ClearAllVariationIDs(); 87 testing::ClearAllVariationIDs();
86 testing::ClearAllVariationParams(); 88 testing::ClearAllVariationParams();
87 } 89 }
88 90
89 private: 91 private:
90 DISALLOW_COPY_AND_ASSIGN(VariationsSeedProcessorTest); 92 DISALLOW_COPY_AND_ASSIGN(VariationsSeedProcessorTest);
91 }; 93 };
92 94
95 TEST_F(VariationsSeedProcessorTest,
96 AllowForceGroupAndVariationIdWithoutCommandLine) {
97 base::FieldTrialList field_trial_list(NULL);
98
99 Study study = CreateStudyWithFlagGroups(100, 0, 0);
100 study.mutable_experiment(1)->set_google_web_experiment_id(kExperimentId);
101 study.mutable_filter()->add_channel(Study_Channel_DEV);
102 study.mutable_filter()->add_channel(Study_Channel_CANARY);
103 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_ANDROID);
104
105 VariationsSeedProcessor().CreateTrialFromStudy(
106 ProcessedStudy(&study, 100, false));
107
108 EXPECT_EQ(kNonFlagGroupName,
109 base::FieldTrialList::FindFullName(kFlagStudyName));
110
111 VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, kFlagStudyName,
112 kFlagGroup1Name);
113 EXPECT_EQ(kExperimentId, id);
114 }
115
116 TEST_F(VariationsSeedProcessorTest,
117 AllowForceGroupAndVariationIdWithCommandLine) {
118 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
119
120 base::FieldTrialList field_trial_list(NULL);
121
122 Study study = CreateStudyWithFlagGroups(100, 0, 0);
123 study.mutable_experiment(1)->set_google_web_experiment_id(kExperimentId);
124 study.mutable_filter()->add_channel(Study_Channel_DEV);
125 study.mutable_filter()->add_channel(Study_Channel_CANARY);
126 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_ANDROID);
127
128 VariationsSeedProcessor().CreateTrialFromStudy(
129 ProcessedStudy(&study, 100, false));
130
131 EXPECT_EQ(kFlagGroup1Name,
132 base::FieldTrialList::FindFullName(kFlagStudyName));
133
134 VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, kFlagStudyName,
135 kFlagGroup1Name);
136 EXPECT_EQ(kExperimentId, id);
137 }
138
93 TEST_F(VariationsSeedProcessorTest, CheckStudyChannel) { 139 TEST_F(VariationsSeedProcessorTest, CheckStudyChannel) {
94 VariationsSeedProcessor seed_processor; 140 VariationsSeedProcessor seed_processor;
95 141
96 const Study_Channel channels[] = { 142 const Study_Channel channels[] = {
97 Study_Channel_CANARY, 143 Study_Channel_CANARY,
98 Study_Channel_DEV, 144 Study_Channel_DEV,
99 Study_Channel_BETA, 145 Study_Channel_BETA,
100 Study_Channel_STABLE, 146 Study_Channel_STABLE,
101 }; 147 };
102 bool channel_added[arraysize(channels)] = { 0 }; 148 bool channel_added[arraysize(channels)] = { 0 };
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 seed, "en-CA", base::Time::Now(), base::Version("20.0.0.0"), 463 seed, "en-CA", base::Time::Now(), base::Version("20.0.0.0"),
418 Study_Channel_STABLE, Study_FormFactor_DESKTOP, &processed_studies); 464 Study_Channel_STABLE, Study_FormFactor_DESKTOP, &processed_studies);
419 465
420 // Check that only the first kTrial1Name study was kept. 466 // Check that only the first kTrial1Name study was kept.
421 ASSERT_EQ(2U, processed_studies.size()); 467 ASSERT_EQ(2U, processed_studies.size());
422 EXPECT_EQ(kTrial1Name, processed_studies[0].study->name()); 468 EXPECT_EQ(kTrial1Name, processed_studies[0].study->name());
423 EXPECT_EQ(kGroup1Name, processed_studies[0].study->experiment(0).name()); 469 EXPECT_EQ(kGroup1Name, processed_studies[0].study->experiment(0).name());
424 EXPECT_EQ(kTrial3Name, processed_studies[1].study->name()); 470 EXPECT_EQ(kTrial3Name, processed_studies[1].study->name());
425 } 471 }
426 472
473 TEST_F(VariationsSeedProcessorTest,
474 ForbidForceGroupWithVariationIdWithoutCommandLine) {
475 base::FieldTrialList field_trial_list(NULL);
476
477 Study study = CreateStudyWithFlagGroups(100, 0, 0);
478 study.mutable_experiment(1)->set_google_web_experiment_id(kExperimentId);
479 // Add windows platform makes forcing_flag and variation Id incompatible.
480 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_WINDOWS);
481
482 VariationsSeedProcessor().CreateTrialFromStudy(
483 ProcessedStudy(&study, 100, false));
484
485 EXPECT_EQ(kNonFlagGroupName,
486 base::FieldTrialList::FindFullName(kFlagStudyName));
487 VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, kFlagStudyName,
488 kFlagGroup1Name);
489 EXPECT_EQ(0, id);
490 }
491
492 TEST_F(VariationsSeedProcessorTest,
493 ForbidForceGroupWithVariationIdWithCommandLine) {
494 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
495
496 base::FieldTrialList field_trial_list(NULL);
497
498 Study study = CreateStudyWithFlagGroups(100, 0, 0);
499 study.mutable_experiment(1)->set_google_web_experiment_id(kExperimentId);
500 // Add windows platform makes forcing_flag and variation Id incompatible.
501 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_WINDOWS);
502
503 VariationsSeedProcessor().CreateTrialFromStudy(
504 ProcessedStudy(&study, 100, false));
505
506 EXPECT_EQ(kFlagGroup1Name,
507 base::FieldTrialList::FindFullName(kFlagStudyName));
508 VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, kFlagStudyName,
509 kFlagGroup1Name);
510 EXPECT_EQ(0, id);
511 }
512
427 // Test that the group for kForcingFlag1 is forced. 513 // Test that the group for kForcingFlag1 is forced.
428 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag1) { 514 TEST_F(VariationsSeedProcessorTest, ForceGroupWithFlag1) {
429 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); 515 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
430 516
431 base::FieldTrialList field_trial_list(NULL); 517 base::FieldTrialList field_trial_list(NULL);
432 518
433 Study study = CreateStudyWithFlagGroups(100, 0, 0); 519 Study study = CreateStudyWithFlagGroups(100, 0, 0);
434 VariationsSeedProcessor().CreateTrialFromStudy( 520 VariationsSeedProcessor().CreateTrialFromStudy(
435 ProcessedStudy(&study, 100, false)); 521 ProcessedStudy(&study, 100, false));
436 522
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 587
502 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { 588 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) {
503 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); 589 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date));
504 const bool result = seed_processor.IsStudyExpired(study, now); 590 const bool result = seed_processor.IsStudyExpired(study, now);
505 EXPECT_EQ(expiry_test_cases[i].expected_result, result) 591 EXPECT_EQ(expiry_test_cases[i].expected_result, result)
506 << "Case " << i << " failed!"; 592 << "Case " << i << " failed!";
507 } 593 }
508 } 594 }
509 595
510 TEST_F(VariationsSeedProcessorTest, 596 TEST_F(VariationsSeedProcessorTest,
511 NonExpiredStudyPrioritizedOverExpiredStudy) { 597 NonExpiredStudyPrioritizedOverExpiredStudy) {
yao 2013/11/13 23:41:43 This test is not declared in variation_seed_proces
Alexei Svitkine (slow) 2013/11/14 16:54:15 Only tests that need to call private APIs need to
yao 2013/11/14 20:51:12 Ic, thanks:) On 2013/11/14 16:54:15, Alexei Svit
512 VariationsSeedProcessor seed_processor; 598 VariationsSeedProcessor seed_processor;
513 599
514 const std::string kTrialName = "A"; 600 const std::string kTrialName = "A";
515 const std::string kGroup1Name = "Group1"; 601 const std::string kGroup1Name = "Group1";
516 602
517 VariationsSeed seed; 603 VariationsSeed seed;
518 Study* study1 = seed.add_study(); 604 Study* study1 = seed.add_study();
519 study1->set_name(kTrialName); 605 study1->set_name(kTrialName);
520 study1->set_default_experiment_name("Default"); 606 study1->set_default_experiment_name("Default");
521 AddExperiment(kGroup1Name, 100, study1); 607 AddExperiment(kGroup1Name, 100, study1);
(...skipping 24 matching lines...) Expand all
546 base::FieldTrialList field_trial_list(NULL); 632 base::FieldTrialList field_trial_list(NULL);
547 study1->clear_expiry_date(); 633 study1->clear_expiry_date();
548 study2->set_expiry_date(TimeToProtoTime(year_ago)); 634 study2->set_expiry_date(TimeToProtoTime(year_ago));
549 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(), 635 seed_processor.CreateTrialsFromSeed(seed, "en-CA", base::Time::Now(),
550 version, Study_Channel_STABLE, 636 version, Study_Channel_STABLE,
551 Study_FormFactor_DESKTOP); 637 Study_FormFactor_DESKTOP);
552 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName)); 638 EXPECT_EQ(kGroup1Name, base::FieldTrialList::FindFullName(kTrialName));
553 } 639 }
554 } 640 }
555 641
642 TEST_F(VariationsSeedProcessorTest, TestAllowVariationIdWithForcingFlag) {
643 Study study = CreateStudyWithFlagGroups(100, 0, 0);
644 bool allowBoth = VariationsSeedProcessor().
645 AllowVariationIdWithForcingFlag(study);
646 EXPECT_FALSE(allowBoth);
647
648 study.mutable_filter()->add_channel(Study_Channel_DEV);
649 allowBoth = VariationsSeedProcessor().
650 AllowVariationIdWithForcingFlag(study);
651 EXPECT_FALSE(allowBoth);
652
653 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_ANDROID);
654 allowBoth = VariationsSeedProcessor().
655 AllowVariationIdWithForcingFlag(study);
656 EXPECT_TRUE(allowBoth);
657
658 study.mutable_filter()->add_channel(Study_Channel_CANARY);
659 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_IOS);
660 allowBoth = VariationsSeedProcessor().
661 AllowVariationIdWithForcingFlag(study);
662 EXPECT_TRUE(allowBoth);
663
664 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_WINDOWS);
665 allowBoth = VariationsSeedProcessor().
666 AllowVariationIdWithForcingFlag(study);
667 EXPECT_FALSE(allowBoth);
668 }
669
556 TEST_F(VariationsSeedProcessorTest, ValidateStudy) { 670 TEST_F(VariationsSeedProcessorTest, ValidateStudy) {
557 VariationsSeedProcessor seed_processor; 671 VariationsSeedProcessor seed_processor;
558 672
559 Study study; 673 Study study;
560 study.set_default_experiment_name("def"); 674 study.set_default_experiment_name("def");
561 AddExperiment("abc", 100, &study); 675 AddExperiment("abc", 100, &study);
562 Study_Experiment* default_group = AddExperiment("def", 200, &study); 676 Study_Experiment* default_group = AddExperiment("def", 200, &study);
563 677
564 base::FieldTrial::Probability total_probability = 0; 678 base::FieldTrial::Probability total_probability = 0;
565 bool valid = seed_processor.ValidateStudyAndComputeTotalProbability( 679 bool valid = seed_processor.ValidateStudyAndComputeTotalProbability(
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 param->set_value("y"); 770 param->set_value("y");
657 771
658 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1); 772 CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
659 base::FieldTrialList field_trial_list(NULL); 773 base::FieldTrialList field_trial_list(NULL);
660 VariationsSeedProcessor().CreateTrialFromStudy( 774 VariationsSeedProcessor().CreateTrialFromStudy(
661 ProcessedStudy(&study, 100, false)); 775 ProcessedStudy(&study, 100, false));
662 EXPECT_EQ(kFlagGroup1Name, base::FieldTrialList::FindFullName(study.name())); 776 EXPECT_EQ(kFlagGroup1Name, base::FieldTrialList::FindFullName(study.name()));
663 EXPECT_EQ("y", GetVariationParamValue(study.name(), "x")); 777 EXPECT_EQ("y", GetVariationParamValue(study.name(), "x"));
664 } 778 }
665 779
666 TEST_F(VariationsSeedProcessorTest, StartsActive) { 780 TEST_F(VariationsSeedProcessorTest, StartsActive) {
yao 2013/11/13 23:41:43 This one as well.
667 base::FieldTrialList field_trial_list(NULL); 781 base::FieldTrialList field_trial_list(NULL);
668 782
669 VariationsSeed seed; 783 VariationsSeed seed;
670 Study* study1 = seed.add_study(); 784 Study* study1 = seed.add_study();
671 study1->set_name("A"); 785 study1->set_name("A");
672 study1->set_default_experiment_name("Default"); 786 study1->set_default_experiment_name("Default");
673 AddExperiment("AA", 100, study1); 787 AddExperiment("AA", 100, study1);
674 AddExperiment("Default", 0, study1); 788 AddExperiment("Default", 0, study1);
675 789
676 Study* study2 = seed.add_study(); 790 Study* study2 = seed.add_study();
(...skipping 26 matching lines...) Expand all
703 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B")); 817 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B"));
704 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C")); 818 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C"));
705 819
706 // Now, all studies should be active. 820 // Now, all studies should be active.
707 EXPECT_TRUE(IsFieldTrialActive("A")); 821 EXPECT_TRUE(IsFieldTrialActive("A"));
708 EXPECT_TRUE(IsFieldTrialActive("B")); 822 EXPECT_TRUE(IsFieldTrialActive("B"));
709 EXPECT_TRUE(IsFieldTrialActive("C")); 823 EXPECT_TRUE(IsFieldTrialActive("C"));
710 } 824 }
711 825
712 } // namespace chrome_variations 826 } // namespace chrome_variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698