Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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(0, 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 Loading... | |
| 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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(). | |
|
Alexei Svitkine (slow)
2013/11/14 22:34:33
Use C++ naming convention.
yao
2013/11/14 23:41:15
Done.
| |
| 645 AllowVariationIdWithForcingFlag(study); | |
|
Alexei Svitkine (slow)
2013/11/14 22:34:33
This should be indented 2 more.
Also, can you re-
yao
2013/11/14 23:41:15
Done.
| |
| 646 EXPECT_FALSE(allowBoth); | |
|
Alexei Svitkine (slow)
2013/11/14 22:34:33
You can just inline this, without a variable.
EXP
yao
2013/11/14 23:41:15
Done.
| |
| 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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |
| OLD | NEW |