variations_crash_keys_chromeos_unittest.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2021 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/variations_crash_keys_chromeos.h"
  5. #include "base/base_paths.h"
  6. #include "base/files/file_util.h"
  7. #include "base/files/scoped_temp_dir.h"
  8. #include "base/metrics/field_trial.h"
  9. #include "base/run_loop.h"
  10. #include "base/test/scoped_path_override.h"
  11. #include "base/test/task_environment.h"
  12. #include "components/crash/core/common/crash_key.h"
  13. #include "components/variations/synthetic_trials_active_group_id_provider.h"
  14. #include "components/variations/variations_crash_keys.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace variations {
  17. namespace {
  18. class VariationsCrashKeysChromeOsTest : public ::testing::Test {
  19. public:
  20. VariationsCrashKeysChromeOsTest() {
  21. crash_reporter::ResetCrashKeysForTesting();
  22. crash_reporter::InitializeCrashKeysForTesting();
  23. }
  24. VariationsCrashKeysChromeOsTest(const VariationsCrashKeysChromeOsTest&) =
  25. delete;
  26. VariationsCrashKeysChromeOsTest& operator=(
  27. const VariationsCrashKeysChromeOsTest&) = delete;
  28. ~VariationsCrashKeysChromeOsTest() override {
  29. SyntheticTrialsActiveGroupIdProvider::GetInstance()->ResetForTesting();
  30. ClearCrashKeysInstanceForTesting();
  31. crash_reporter::ResetCrashKeysForTesting();
  32. }
  33. protected:
  34. base::test::TaskEnvironment task_environment_;
  35. };
  36. TEST_F(VariationsCrashKeysChromeOsTest, WritesVariationsList) {
  37. // Override the homedir so that the class writes to a known location we can
  38. // check.
  39. base::ScopedTempDir dir;
  40. ASSERT_TRUE(dir.CreateUniqueTempDir());
  41. const base::FilePath& home_path = dir.GetPath();
  42. base::ScopedPathOverride home_override(base::DIR_HOME, home_path);
  43. base::FilePath variations_list_path =
  44. home_path.Append(".variations-list.txt");
  45. // Start with 2 trials, one active and one not
  46. base::FieldTrialList::CreateFieldTrial("Trial1", "Group1")->group();
  47. base::FieldTrialList::CreateFieldTrial("Trial2", "Group2");
  48. InitCrashKeys();
  49. base::RunLoop().RunUntilIdle();
  50. task_environment_.RunUntilIdle();
  51. ExperimentListInfo info = GetExperimentListInfo();
  52. EXPECT_EQ(1, info.num_experiments);
  53. EXPECT_EQ("8e7abfb0-c16397b7,", info.experiment_list);
  54. std::string contents;
  55. ASSERT_TRUE(base::ReadFileToString(variations_list_path, &contents));
  56. EXPECT_EQ(contents,
  57. "num-experiments=1\n"
  58. "variations=8e7abfb0-c16397b7,\n");
  59. // Now, activate Trial2.
  60. EXPECT_EQ("Group2", base::FieldTrialList::FindFullName("Trial2"));
  61. base::RunLoop().RunUntilIdle();
  62. task_environment_.RunUntilIdle();
  63. info = GetExperimentListInfo();
  64. EXPECT_EQ(2, info.num_experiments);
  65. EXPECT_EQ("8e7abfb0-c16397b7,277f2a3d-d77354d0,", info.experiment_list);
  66. ASSERT_TRUE(base::ReadFileToString(variations_list_path, &contents));
  67. EXPECT_EQ(contents,
  68. "num-experiments=2\n"
  69. "variations=8e7abfb0-c16397b7,277f2a3d-d77354d0,\n");
  70. }
  71. } // namespace
  72. } // namespace variations