blob: 5a5cab56a1f935ba719347fa50428939008f740d [file] [log] [blame]
Alexei Svitkinee726dd522018-03-09 04:41:031// Copyright 2018 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
5#include "components/variations/variations_crash_keys.h"
6
7#include <string>
8
9#include "base/message_loop/message_loop.h"
10#include "base/metrics/field_trial.h"
11#include "base/run_loop.h"
12#include "components/crash/core/common/crash_key.h"
13#include "components/variations/hashing.h"
14#include "components/variations/synthetic_trial_registry.h"
15#include "components/variations/synthetic_trials_active_group_id_provider.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace variations {
19
20namespace {
21
22std::string GetVariationsCrashKey() {
23 return crash_reporter::GetCrashKeyValue("variations");
24}
25
26std::string GetNumExperimentsCrashKey() {
27 return crash_reporter::GetCrashKeyValue("num-experiments");
28}
29
30class VariationsCrashKeysTest : public ::testing::Test {
31 public:
32 VariationsCrashKeysTest() : field_trial_list_(nullptr) {}
33
34 ~VariationsCrashKeysTest() override {
35 SyntheticTrialsActiveGroupIdProvider::GetInstance()->ResetForTesting();
36 ClearCrashKeysInstanceForTesting();
37 }
38
39 private:
40 base::MessageLoop loop_;
41
42 base::FieldTrialList field_trial_list_;
43
44 DISALLOW_COPY_AND_ASSIGN(VariationsCrashKeysTest);
45};
46
47} // namespace
48
49TEST_F(VariationsCrashKeysTest, BasicFunctionality) {
50 SyntheticTrialRegistry registry;
51 registry.AddSyntheticTrialObserver(
52 SyntheticTrialsActiveGroupIdProvider::GetInstance());
53
54 // Start with 2 trials, one active and one not
55 base::FieldTrialList::CreateFieldTrial("Trial1", "Group1")->group();
56 base::FieldTrialList::CreateFieldTrial("Trial2", "Group2");
57
58 InitCrashKeys();
59
60 EXPECT_EQ("1", GetNumExperimentsCrashKey());
61 EXPECT_EQ("8e7abfb0-c16397b7,", GetVariationsCrashKey());
62
63 // Now, active Trial2.
64 EXPECT_EQ("Group2", base::FieldTrialList::FindFullName("Trial2"));
65 base::RunLoop().RunUntilIdle();
66
67 EXPECT_EQ("2", GetNumExperimentsCrashKey());
68 EXPECT_EQ("8e7abfb0-c16397b7,277f2a3d-d77354d0,", GetVariationsCrashKey());
69
70 // Add two synthetic trials and confirm that they show up in the list.
71 SyntheticTrialGroup synth_trial(HashName("Trial3"), HashName("Group3"));
72 registry.RegisterSyntheticFieldTrial(synth_trial);
73
74 EXPECT_EQ("3", GetNumExperimentsCrashKey());
75 EXPECT_EQ("8e7abfb0-c16397b7,277f2a3d-d77354d0,9f339c9d-746c2ad4,",
76 GetVariationsCrashKey());
77
78 // Add another regular trial.
79 base::FieldTrialList::CreateFieldTrial("Trial4", "Group4")->group();
80 base::RunLoop().RunUntilIdle();
81 EXPECT_EQ("4", GetNumExperimentsCrashKey());
82 EXPECT_EQ(
83 "8e7abfb0-c16397b7,277f2a3d-d77354d0,21710f4c-99b90b01,"
84 "9f339c9d-746c2ad4,",
85 GetVariationsCrashKey());
86
87 // Replace synthetic trial group and add one more.
88 SyntheticTrialGroup synth_trial2(HashName("Trial3"), HashName("Group3_A"));
89 registry.RegisterSyntheticFieldTrial(synth_trial2);
90 SyntheticTrialGroup synth_trial3(HashName("Trial4"), HashName("Group4"));
91 registry.RegisterSyntheticFieldTrial(synth_trial3);
92
93 EXPECT_EQ("5", GetNumExperimentsCrashKey());
94 EXPECT_EQ(
95 "8e7abfb0-c16397b7,277f2a3d-d77354d0,21710f4c-99b90b01,"
96 "9f339c9d-3250dddc,21710f4c-99b90b01,",
97 GetVariationsCrashKey());
98}
99
100} // namespace variations