cloned_install_detector_unittest.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2014 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/cloned_install_detector.h"
  5. #include "components/metrics/machine_id_provider.h"
  6. #include "components/metrics/metrics_pref_names.h"
  7. #include "components/prefs/testing_pref_service.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace metrics {
  10. namespace {
  11. const std::string kTestRawId = "test";
  12. // Hashed machine id for |kTestRawId|.
  13. const int kTestHashedId = 2216819;
  14. } // namespace
  15. // TODO(jwd): Change these test to test the full flow and histogram outputs. It
  16. // should also remove the need to make the test a friend of
  17. // ClonedInstallDetector.
  18. TEST(ClonedInstallDetectorTest, SaveId) {
  19. TestingPrefServiceSimple prefs;
  20. ClonedInstallDetector::RegisterPrefs(prefs.registry());
  21. ClonedInstallDetector detector;
  22. detector.SaveMachineId(&prefs, kTestRawId);
  23. EXPECT_EQ(kTestHashedId, prefs.GetInteger(prefs::kMetricsMachineId));
  24. }
  25. TEST(ClonedInstallDetectorTest, DetectClone) {
  26. TestingPrefServiceSimple prefs;
  27. ClonedInstallDetector::RegisterPrefs(prefs.registry());
  28. // Save a machine id that will cause a clone to be detected.
  29. prefs.SetInteger(prefs::kMetricsMachineId, kTestHashedId + 1);
  30. ClonedInstallDetector detector;
  31. detector.SaveMachineId(&prefs, kTestRawId);
  32. EXPECT_TRUE(prefs.GetBoolean(prefs::kMetricsResetIds));
  33. EXPECT_TRUE(detector.ShouldResetClientIds(&prefs));
  34. }
  35. TEST(ClonedInstallDetectorTest, ShouldResetClientIds) {
  36. TestingPrefServiceSimple prefs;
  37. ClonedInstallDetector::RegisterPrefs(prefs.registry());
  38. ClonedInstallDetector detector;
  39. EXPECT_FALSE(detector.ShouldResetClientIds(&prefs));
  40. // Save a machine id that will cause a clone to be detected.
  41. prefs.SetInteger(prefs::kMetricsMachineId, kTestHashedId + 1);
  42. detector.SaveMachineId(&prefs, kTestRawId);
  43. // Multiple different services may call into the cloned install detector, it
  44. // needs to continue supporting giving the same answer more than once
  45. EXPECT_TRUE(detector.ShouldResetClientIds(&prefs));
  46. EXPECT_TRUE(detector.ShouldResetClientIds(&prefs));
  47. }
  48. TEST(ClonedInstallDetectorTest, ClonedInstallDetectedInCurrentSession) {
  49. TestingPrefServiceSimple prefs;
  50. ClonedInstallDetector::RegisterPrefs(prefs.registry());
  51. ClonedInstallDetector detector;
  52. EXPECT_FALSE(detector.ShouldResetClientIds(&prefs));
  53. EXPECT_FALSE(detector.ClonedInstallDetectedInCurrentSession());
  54. // Save a machine id that will cause a clone to be detected.
  55. prefs.SetInteger(prefs::kMetricsMachineId, kTestHashedId + 1);
  56. detector.SaveMachineId(&prefs, kTestRawId);
  57. // Ensure that the current session call returns true both before things are
  58. // modified by ShouldResetClientIds and after
  59. EXPECT_TRUE(detector.ClonedInstallDetectedInCurrentSession());
  60. EXPECT_TRUE(detector.ShouldResetClientIds(&prefs));
  61. EXPECT_TRUE(detector.ClonedInstallDetectedInCurrentSession());
  62. }
  63. } // namespace metrics