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

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: Address code review comments. 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) {
Alexei Svitkine (slow) 2013/11/15 15:20:43 Nit: Align. Also, same thing for other tests below
yao 2013/11/15 18:51:49 Done.
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(0, id);
Alexei Svitkine (slow) 2013/11/15 15:20:43 Don't hardcode 0. Use EMPTY_ID instead. However,
yao 2013/11/15 18:51:49 Done.
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);
Alexei Svitkine (slow) 2013/11/15 15:20:43 Similar comment as a above, we shouldn't test the
yao 2013/11/15 18:51:49 Done.
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);
Alexei Svitkine (slow) 2013/11/15 15:20:43 EMPTY_ID
yao 2013/11/15 18:51:49 Done.
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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
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) {
Alexei Svitkine (slow) 2013/11/15 15:20:43 Nit: TestAllowVariationIdWithForcingFlag -> AllowV
yao 2013/11/15 18:51:49 The reason I put a test prefix here was trying to
Alexei Svitkine (slow) 2013/11/15 19:00:56 I think having the test be named the same as the f
yao 2013/11/15 19:12:25 Done.
643 VariationsSeedProcessor seed_processor;
644 Study study = CreateStudyWithFlagGroups(100, 0, 0);
645 EXPECT_FALSE(seed_processor.AllowVariationIdWithForcingFlag(study));
646
647 study.mutable_filter()->add_channel(Study_Channel_DEV);
648 EXPECT_FALSE(seed_processor.AllowVariationIdWithForcingFlag(study));
649
650 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_ANDROID);
651 EXPECT_TRUE(seed_processor.AllowVariationIdWithForcingFlag(study));
652
653 study.mutable_filter()->add_channel(Study_Channel_CANARY);
654 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_IOS);
655 EXPECT_TRUE(seed_processor.AllowVariationIdWithForcingFlag(study));
656
657 study.mutable_filter()->add_platform(Study_Platform_PLATFORM_WINDOWS);
658 EXPECT_FALSE(seed_processor.AllowVariationIdWithForcingFlag(study));
659 }
660
556 TEST_F(VariationsSeedProcessorTest, ValidateStudy) { 661 TEST_F(VariationsSeedProcessorTest, ValidateStudy) {
557 VariationsSeedProcessor seed_processor; 662 VariationsSeedProcessor seed_processor;
558 663
559 Study study; 664 Study study;
560 study.set_default_experiment_name("def"); 665 study.set_default_experiment_name("def");
561 AddExperiment("abc", 100, &study); 666 AddExperiment("abc", 100, &study);
562 Study_Experiment* default_group = AddExperiment("def", 200, &study); 667 Study_Experiment* default_group = AddExperiment("def", 200, &study);
563 668
564 base::FieldTrial::Probability total_probability = 0; 669 base::FieldTrial::Probability total_probability = 0;
565 bool valid = seed_processor.ValidateStudyAndComputeTotalProbability( 670 bool valid = seed_processor.ValidateStudyAndComputeTotalProbability(
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B")); 808 EXPECT_EQ("BB", base::FieldTrialList::FindFullName("B"));
704 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C")); 809 EXPECT_EQ("CC", base::FieldTrialList::FindFullName("C"));
705 810
706 // Now, all studies should be active. 811 // Now, all studies should be active.
707 EXPECT_TRUE(IsFieldTrialActive("A")); 812 EXPECT_TRUE(IsFieldTrialActive("A"));
708 EXPECT_TRUE(IsFieldTrialActive("B")); 813 EXPECT_TRUE(IsFieldTrialActive("B"));
709 EXPECT_TRUE(IsFieldTrialActive("C")); 814 EXPECT_TRUE(IsFieldTrialActive("C"));
710 } 815 }
711 816
712 } // namespace chrome_variations 817 } // namespace chrome_variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698