Alexei Svitkine | e726dd52 | 2018-03-09 04:41:03 | [diff] [blame] | 1 | // 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 | |
Alexei Svitkine | e726dd52 | 2018-03-09 04:41:03 | [diff] [blame] | 9 | #include "base/metrics/field_trial.h" |
| 10 | #include "base/run_loop.h" |
Carlos Caballero | 954ca16 | 2018-11-12 10:47:09 | [diff] [blame] | 11 | #include "base/test/scoped_task_environment.h" |
Alexei Svitkine | ab87e83 | 2018-03-19 21:51:55 | [diff] [blame] | 12 | #include "build/build_config.h" |
Alexei Svitkine | e726dd52 | 2018-03-09 04:41:03 | [diff] [blame] | 13 | #include "components/crash/core/common/crash_key.h" |
| 14 | #include "components/variations/hashing.h" |
| 15 | #include "components/variations/synthetic_trial_registry.h" |
| 16 | #include "components/variations/synthetic_trials_active_group_id_provider.h" |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace variations { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | std::string GetVariationsCrashKey() { |
| 24 | return crash_reporter::GetCrashKeyValue("variations"); |
| 25 | } |
| 26 | |
| 27 | std::string GetNumExperimentsCrashKey() { |
| 28 | return crash_reporter::GetCrashKeyValue("num-experiments"); |
| 29 | } |
| 30 | |
| 31 | class VariationsCrashKeysTest : public ::testing::Test { |
| 32 | public: |
Alexei Svitkine | 06c498e | 2018-03-14 00:20:15 | [diff] [blame] | 33 | VariationsCrashKeysTest() : field_trial_list_(nullptr) { |
| 34 | crash_reporter::ResetCrashKeysForTesting(); |
| 35 | crash_reporter::InitializeCrashKeys(); |
| 36 | } |
Alexei Svitkine | e726dd52 | 2018-03-09 04:41:03 | [diff] [blame] | 37 | |
| 38 | ~VariationsCrashKeysTest() override { |
| 39 | SyntheticTrialsActiveGroupIdProvider::GetInstance()->ResetForTesting(); |
| 40 | ClearCrashKeysInstanceForTesting(); |
Alexei Svitkine | 06c498e | 2018-03-14 00:20:15 | [diff] [blame] | 41 | crash_reporter::ResetCrashKeysForTesting(); |
Alexei Svitkine | e726dd52 | 2018-03-09 04:41:03 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | private: |
Carlos Caballero | 954ca16 | 2018-11-12 10:47:09 | [diff] [blame] | 45 | base::test::ScopedTaskEnvironment task_environment_; |
Alexei Svitkine | e726dd52 | 2018-03-09 04:41:03 | [diff] [blame] | 46 | |
| 47 | base::FieldTrialList field_trial_list_; |
| 48 | |
| 49 | DISALLOW_COPY_AND_ASSIGN(VariationsCrashKeysTest); |
| 50 | }; |
| 51 | |
| 52 | } // namespace |
| 53 | |
Alexei Svitkine | ab87e83 | 2018-03-19 21:51:55 | [diff] [blame] | 54 | // TODO(crbug.com/821162): Test fails on iOS. Re-enable after fixing. |
| 55 | #if defined(OS_IOS) |
| 56 | #define MAYBE_BasicFunctionality DISABLED_BasicFunctionality |
| 57 | #else |
| 58 | #define MAYBE_BasicFunctionality BasicFunctionality |
| 59 | #endif |
| 60 | TEST_F(VariationsCrashKeysTest, MAYBE_BasicFunctionality) { |
Alexei Svitkine | e726dd52 | 2018-03-09 04:41:03 | [diff] [blame] | 61 | SyntheticTrialRegistry registry; |
| 62 | registry.AddSyntheticTrialObserver( |
| 63 | SyntheticTrialsActiveGroupIdProvider::GetInstance()); |
| 64 | |
| 65 | // Start with 2 trials, one active and one not |
| 66 | base::FieldTrialList::CreateFieldTrial("Trial1", "Group1")->group(); |
| 67 | base::FieldTrialList::CreateFieldTrial("Trial2", "Group2"); |
| 68 | |
| 69 | InitCrashKeys(); |
| 70 | |
| 71 | EXPECT_EQ("1", GetNumExperimentsCrashKey()); |
| 72 | EXPECT_EQ("8e7abfb0-c16397b7,", GetVariationsCrashKey()); |
| 73 | |
| 74 | // Now, active Trial2. |
| 75 | EXPECT_EQ("Group2", base::FieldTrialList::FindFullName("Trial2")); |
| 76 | base::RunLoop().RunUntilIdle(); |
| 77 | |
| 78 | EXPECT_EQ("2", GetNumExperimentsCrashKey()); |
| 79 | EXPECT_EQ("8e7abfb0-c16397b7,277f2a3d-d77354d0,", GetVariationsCrashKey()); |
| 80 | |
| 81 | // Add two synthetic trials and confirm that they show up in the list. |
| 82 | SyntheticTrialGroup synth_trial(HashName("Trial3"), HashName("Group3")); |
| 83 | registry.RegisterSyntheticFieldTrial(synth_trial); |
| 84 | |
| 85 | EXPECT_EQ("3", GetNumExperimentsCrashKey()); |
| 86 | EXPECT_EQ("8e7abfb0-c16397b7,277f2a3d-d77354d0,9f339c9d-746c2ad4,", |
| 87 | GetVariationsCrashKey()); |
| 88 | |
| 89 | // Add another regular trial. |
| 90 | base::FieldTrialList::CreateFieldTrial("Trial4", "Group4")->group(); |
| 91 | base::RunLoop().RunUntilIdle(); |
| 92 | EXPECT_EQ("4", GetNumExperimentsCrashKey()); |
| 93 | EXPECT_EQ( |
| 94 | "8e7abfb0-c16397b7,277f2a3d-d77354d0,21710f4c-99b90b01," |
| 95 | "9f339c9d-746c2ad4,", |
| 96 | GetVariationsCrashKey()); |
| 97 | |
| 98 | // Replace synthetic trial group and add one more. |
| 99 | SyntheticTrialGroup synth_trial2(HashName("Trial3"), HashName("Group3_A")); |
| 100 | registry.RegisterSyntheticFieldTrial(synth_trial2); |
| 101 | SyntheticTrialGroup synth_trial3(HashName("Trial4"), HashName("Group4")); |
| 102 | registry.RegisterSyntheticFieldTrial(synth_trial3); |
| 103 | |
| 104 | EXPECT_EQ("5", GetNumExperimentsCrashKey()); |
| 105 | EXPECT_EQ( |
| 106 | "8e7abfb0-c16397b7,277f2a3d-d77354d0,21710f4c-99b90b01," |
| 107 | "9f339c9d-3250dddc,21710f4c-99b90b01,", |
| 108 | GetVariationsCrashKey()); |
| 109 | } |
| 110 | |
| 111 | } // namespace variations |