child_process_field_trial_syncer_unittest.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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
  3. // found in the LICENSE file.
  4. #include "components/variations/child_process_field_trial_syncer.h"
  5. #include <string>
  6. #include <vector>
  7. #include "base/base_switches.h"
  8. #include "base/command_line.h"
  9. #include "base/metrics/field_trial.h"
  10. #include "base/test/bind.h"
  11. #include "base/test/scoped_feature_list.h"
  12. #include "base/test/task_environment.h"
  13. #include "testing/gmock/include/gmock/gmock.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. namespace variations {
  16. TEST(ChildProcessFieldTrialSyncerTest, FieldTrialState) {
  17. base::test::TaskEnvironment task_environment;
  18. base::test::ScopedFeatureList scoped_feature_list;
  19. // We expect there are no field trials, because we creates 3 field trials
  20. // A, B and C, and checks EXPECT_EQ("*A/G1/B/G2/C/G3/", states_string).
  21. // So we need to create a new scope with empty feature and field trial lists.
  22. scoped_feature_list.InitWithEmptyFeatureAndFieldTrialLists();
  23. // We don't use the descriptor here anyways so it's ok to pass -1.
  24. base::FieldTrialList::CreateTrialsFromCommandLine(
  25. *base::CommandLine::ForCurrentProcess(), -1);
  26. base::FieldTrial* trial1 = base::FieldTrialList::CreateFieldTrial("A", "G1");
  27. base::FieldTrial* trial2 = base::FieldTrialList::CreateFieldTrial("B", "G2");
  28. base::FieldTrial* trial3 = base::FieldTrialList::CreateFieldTrial("C", "G3");
  29. // Activate trial3 before command line is produced.
  30. trial1->group();
  31. std::string states_string;
  32. base::FieldTrialList::AllStatesToString(&states_string, false);
  33. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  34. switches::kForceFieldTrials, states_string);
  35. EXPECT_EQ("*A/G1/B/G2/C/G3/", states_string);
  36. // Active trial 2 before creating the syncer.
  37. trial2->group();
  38. std::vector<std::string> observed_trial_names;
  39. auto callback =
  40. base::BindLambdaForTesting([&](const std::string& trial_name) {
  41. observed_trial_names.push_back(trial_name);
  42. });
  43. ChildProcessFieldTrialSyncer::CreateInstance(callback);
  44. // The callback should be invoked for activated trials that were not specified
  45. // on the command line. In this case, trial 2. (Trial 1 was already active via
  46. // command line and so its state shouldn't be notified.)
  47. EXPECT_THAT(observed_trial_names, testing::ElementsAre("B"));
  48. // Now, activate trial 3, which should also get reflected.
  49. trial3->group();
  50. EXPECT_THAT(observed_trial_names, testing::ElementsAre("B", "C"));
  51. ChildProcessFieldTrialSyncer::DeleteInstanceForTesting();
  52. }
  53. } // namespace variations