environment_recorder_unittest.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2017 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/metrics/environment_recorder.h"
  5. #include "components/metrics/metrics_pref_names.h"
  6. #include "components/prefs/testing_pref_service.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #include "third_party/metrics_proto/system_profile.pb.h"
  9. namespace metrics {
  10. class EnvironmentRecorderTest : public testing::Test {
  11. public:
  12. EnvironmentRecorderTest() {
  13. EnvironmentRecorder::RegisterPrefs(prefs_.registry());
  14. }
  15. EnvironmentRecorderTest(const EnvironmentRecorderTest&) = delete;
  16. EnvironmentRecorderTest& operator=(const EnvironmentRecorderTest&) = delete;
  17. ~EnvironmentRecorderTest() override {}
  18. protected:
  19. TestingPrefServiceSimple prefs_;
  20. };
  21. TEST_F(EnvironmentRecorderTest, LoadEnvironmentFromPrefs) {
  22. const char* kSystemProfilePref = prefs::kStabilitySavedSystemProfile;
  23. const char* kSystemProfileHashPref = prefs::kStabilitySavedSystemProfileHash;
  24. // The pref value is empty, so loading it from prefs should fail.
  25. {
  26. EnvironmentRecorder recorder(&prefs_);
  27. SystemProfileProto system_profile;
  28. EXPECT_FALSE(recorder.LoadEnvironmentFromPrefs(&system_profile));
  29. EXPECT_FALSE(system_profile.has_app_version());
  30. }
  31. // Do a RecordEnvironment() call and check whether the pref is recorded.
  32. {
  33. EnvironmentRecorder recorder(&prefs_);
  34. SystemProfileProto system_profile;
  35. system_profile.set_app_version("bogus version");
  36. std::string serialized_profile =
  37. recorder.SerializeAndRecordEnvironmentToPrefs(system_profile);
  38. EXPECT_FALSE(serialized_profile.empty());
  39. EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
  40. EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
  41. }
  42. // Load it and check that it has the right value.
  43. {
  44. EnvironmentRecorder recorder(&prefs_);
  45. SystemProfileProto system_profile;
  46. EXPECT_TRUE(recorder.LoadEnvironmentFromPrefs(&system_profile));
  47. EXPECT_EQ("bogus version", system_profile.app_version());
  48. // Ensure that the call did not clear the prefs.
  49. EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
  50. EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
  51. }
  52. // Ensure that a non-matching hash results in the pref being invalid.
  53. {
  54. // Set the hash to a bad value.
  55. prefs_.SetString(kSystemProfileHashPref, "deadbeef");
  56. EnvironmentRecorder recorder(&prefs_);
  57. SystemProfileProto system_profile;
  58. EXPECT_FALSE(recorder.LoadEnvironmentFromPrefs(&system_profile));
  59. EXPECT_FALSE(system_profile.has_app_version());
  60. }
  61. }
  62. } // namespace metrics