| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/common/variations/child_process_field_trial_syncer.h" | 5 #include "components/variations/child_process_field_trial_syncer.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/base_switches.h" | 11 #include "base/base_switches.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace chrome_variations { | 18 namespace variations { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // TestFieldTrialObserver to listen to be notified by the child process syncer. | 22 // TestFieldTrialObserver to listen to be notified by the child process syncer. |
| 23 class TestFieldTrialObserver : public base::FieldTrialList::Observer { | 23 class TestFieldTrialObserver : public base::FieldTrialList::Observer { |
| 24 public: | 24 public: |
| 25 TestFieldTrialObserver() {} | 25 TestFieldTrialObserver() {} |
| 26 ~TestFieldTrialObserver() override {} | 26 ~TestFieldTrialObserver() override {} |
| 27 | 27 |
| 28 // base::FieldTrial::Observer: | 28 // base::FieldTrial::Observer: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 48 const std::string& b) { | 48 const std::string& b) { |
| 49 return std::make_pair(a, b); | 49 return std::make_pair(a, b); |
| 50 } | 50 } |
| 51 | 51 |
| 52 } // namespace | 52 } // namespace |
| 53 | 53 |
| 54 TEST(ChildProcessFieldTrialSyncerTest, FieldTrialState) { | 54 TEST(ChildProcessFieldTrialSyncerTest, FieldTrialState) { |
| 55 base::MessageLoop message_loop; | 55 base::MessageLoop message_loop; |
| 56 base::FieldTrialList field_trial_list(nullptr); | 56 base::FieldTrialList field_trial_list(nullptr); |
| 57 base::FieldTrialList::CreateTrialsFromCommandLine( | 57 base::FieldTrialList::CreateTrialsFromCommandLine( |
| 58 *base::CommandLine::ForCurrentProcess(), "field_trial_handle_switch"); | 58 *base::CommandLine::ForCurrentProcess(), "field_trial_handle_switch"); |
| 59 | 59 |
| 60 base::FieldTrial* trial1 = base::FieldTrialList::CreateFieldTrial("A", "G1"); | 60 base::FieldTrial* trial1 = base::FieldTrialList::CreateFieldTrial("A", "G1"); |
| 61 base::FieldTrial* trial2 = base::FieldTrialList::CreateFieldTrial("B", "G2"); | 61 base::FieldTrial* trial2 = base::FieldTrialList::CreateFieldTrial("B", "G2"); |
| 62 base::FieldTrial* trial3 = base::FieldTrialList::CreateFieldTrial("C", "G3"); | 62 base::FieldTrial* trial3 = base::FieldTrialList::CreateFieldTrial("C", "G3"); |
| 63 // Activate trial3 before command line is produced. | 63 // Activate trial3 before command line is produced. |
| 64 trial1->group(); | 64 trial1->group(); |
| 65 | 65 |
| 66 std::string states_string; | 66 std::string states_string; |
| 67 base::FieldTrialList::AllStatesToString(&states_string); | 67 base::FieldTrialList::AllStatesToString(&states_string); |
| 68 | 68 |
| 69 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 69 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 70 switches::kForceFieldTrials, states_string); | 70 switches::kForceFieldTrials, states_string); |
| 71 EXPECT_EQ("*A/G1/B/G2/C/G3/", states_string); | 71 EXPECT_EQ("*A/G1/B/G2/C/G3/", states_string); |
| 72 | 72 |
| 73 // Active trial 2 before creating the syncer. | 73 // Active trial 2 before creating the syncer. |
| 74 trial2->group(); | 74 trial2->group(); |
| 75 | 75 |
| 76 TestFieldTrialObserver observer; | 76 TestFieldTrialObserver observer; |
| 77 ChildProcessFieldTrialSyncer syncer(&observer); | 77 ChildProcessFieldTrialSyncer syncer(&observer); |
| 78 syncer.InitFieldTrialObserving(*base::CommandLine::ForCurrentProcess()); | 78 syncer.InitFieldTrialObserving(*base::CommandLine::ForCurrentProcess(), |
| 79 "single_process"); |
| 79 | 80 |
| 80 // The observer should be notified of activated entries that were not activate | 81 // The observer should be notified of activated entries that were not activate |
| 81 // on the command line. In this case, trial 2. (Trial 1 was already active via | 82 // on the command line. In this case, trial 2. (Trial 1 was already active via |
| 82 // command line and so its state shouldn't be notified.) | 83 // command line and so its state shouldn't be notified.) |
| 83 ASSERT_EQ(1U, observer.observed_entries_count()); | 84 ASSERT_EQ(1U, observer.observed_entries_count()); |
| 84 EXPECT_EQ(MakeStringPair("B", "G2"), observer.get_observed_entry(0)); | 85 EXPECT_EQ(MakeStringPair("B", "G2"), observer.get_observed_entry(0)); |
| 85 | 86 |
| 86 // Now, activate trial 3, which should also get reflected. | 87 // Now, activate trial 3, which should also get reflected. |
| 87 trial3->group(); | 88 trial3->group(); |
| 88 // Notifications from field trial activation actually happen via posted tasks, | 89 // Notifications from field trial activation actually happen via posted tasks, |
| 89 // so invoke the run loop. | 90 // so invoke the run loop. |
| 90 base::RunLoop().RunUntilIdle(); | 91 base::RunLoop().RunUntilIdle(); |
| 91 | 92 |
| 92 ASSERT_EQ(2U, observer.observed_entries_count()); | 93 ASSERT_EQ(2U, observer.observed_entries_count()); |
| 93 EXPECT_EQ(MakeStringPair("C", "G3"), observer.get_observed_entry(1)); | 94 EXPECT_EQ(MakeStringPair("C", "G3"), observer.get_observed_entry(1)); |
| 94 } | 95 } |
| 95 | 96 |
| 96 } // namespace chrome_variations | 97 } // namespace variations |
| OLD | NEW |