File: child_process_field_trial_syncer_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (98 lines) | stat: -rw-r--r-- 3,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/variations/child_process_field_trial_syncer.h"

#include <string>
#include <utility>
#include <vector>

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/field_trial.h"
#include "base/run_loop.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace variations {

namespace {

// TestFieldTrialObserver to listen to be notified by the child process syncer.
class TestFieldTrialObserver : public base::FieldTrialList::Observer {
 public:
  TestFieldTrialObserver() {}
  ~TestFieldTrialObserver() override {}

  // base::FieldTrial::Observer:
  void OnFieldTrialGroupFinalized(const std::string& trial_name,
                                  const std::string& group_name) override {
    observed_entries_.push_back(std::make_pair(trial_name, group_name));
  }

  size_t observed_entries_count() const { return observed_entries_.size(); }

  std::pair<std::string, std::string> get_observed_entry(int i) const {
    return observed_entries_[i];
  }

 private:
  std::vector<std::pair<std::string, std::string>> observed_entries_;

  DISALLOW_COPY_AND_ASSIGN(TestFieldTrialObserver);
};

// Needed because make_pair("a", "b") doesn't convert to std::string pair.
std::pair<std::string, std::string> MakeStringPair(const std::string& a,
                                                   const std::string& b) {
  return std::make_pair(a, b);
}

}  // namespace

TEST(ChildProcessFieldTrialSyncerTest, FieldTrialState) {
  base::MessageLoop message_loop;
  base::FieldTrialList field_trial_list(nullptr);
  // We don't use the descriptor here anyways so it's ok to pass -1.
  base::FieldTrialList::CreateTrialsFromCommandLine(
      *base::CommandLine::ForCurrentProcess(), "field_trial_handle_switch", -1);

  base::FieldTrial* trial1 = base::FieldTrialList::CreateFieldTrial("A", "G1");
  base::FieldTrial* trial2 = base::FieldTrialList::CreateFieldTrial("B", "G2");
  base::FieldTrial* trial3 = base::FieldTrialList::CreateFieldTrial("C", "G3");
  // Activate trial3 before command line is produced.
  trial1->group();

  std::string states_string;
  base::FieldTrialList::AllStatesToString(&states_string);

  base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
      switches::kForceFieldTrials, states_string);
  EXPECT_EQ("*A/G1/B/G2/C/G3/", states_string);

  // Active trial 2 before creating the syncer.
  trial2->group();

  TestFieldTrialObserver observer;
  ChildProcessFieldTrialSyncer syncer(&observer);
  syncer.InitFieldTrialObserving(*base::CommandLine::ForCurrentProcess(),
                                 "single_process");

  // The observer should be notified of activated entries that were not activate
  // on the command line. In this case, trial 2. (Trial 1 was already active via
  // command line and so its state shouldn't be notified.)
  ASSERT_EQ(1U, observer.observed_entries_count());
  EXPECT_EQ(MakeStringPair("B", "G2"), observer.get_observed_entry(0));

  // Now, activate trial 3, which should also get reflected.
  trial3->group();
  // Notifications from field trial activation actually happen via posted tasks,
  // so invoke the run loop.
  base::RunLoop().RunUntilIdle();

  ASSERT_EQ(2U, observer.observed_entries_count());
  EXPECT_EQ(MakeStringPair("C", "G3"), observer.get_observed_entry(1));
}

}  // namespace variations