1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/cloned_install_detector.h"
7 #include "base/callback_list.h"
8 #include "base/test/bind.h"
9 #include "components/metrics/machine_id_provider.h"
10 #include "components/metrics/metrics_pref_names.h"
11 #include "components/prefs/testing_pref_service.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 const std::string kTestRawId = "test";
19 // Hashed machine id for |kTestRawId|.
20 const int kTestHashedId = 2216819;
24 // TODO(jwd): Change these test to test the full flow and histogram outputs. It
25 // should also remove the need to make the test a friend of
26 // ClonedInstallDetector.
27 TEST(ClonedInstallDetectorTest, SaveId) {
28 TestingPrefServiceSimple prefs;
29 ClonedInstallDetector::RegisterPrefs(prefs.registry());
31 ClonedInstallDetector detector;
32 detector.SaveMachineId(&prefs, kTestRawId);
34 EXPECT_EQ(kTestHashedId, prefs.GetInteger(prefs::kMetricsMachineId));
37 TEST(ClonedInstallDetectorTest, DetectClone) {
38 TestingPrefServiceSimple prefs;
39 ClonedInstallDetector::RegisterPrefs(prefs.registry());
41 // Save a machine id that will cause a clone to be detected.
42 prefs.SetInteger(prefs::kMetricsMachineId, kTestHashedId + 1);
44 ClonedInstallDetector detector;
45 detector.SaveMachineId(&prefs, kTestRawId);
47 EXPECT_TRUE(prefs.GetBoolean(prefs::kMetricsResetIds));
48 EXPECT_TRUE(detector.ShouldResetClientIds(&prefs));
51 TEST(ClonedInstallDetectorTest, ShouldResetClientIds) {
52 TestingPrefServiceSimple prefs;
53 ClonedInstallDetector::RegisterPrefs(prefs.registry());
55 ClonedInstallDetector detector;
56 EXPECT_FALSE(detector.ShouldResetClientIds(&prefs));
58 // Save a machine id that will cause a clone to be detected.
59 prefs.SetInteger(prefs::kMetricsMachineId, kTestHashedId + 1);
60 detector.SaveMachineId(&prefs, kTestRawId);
62 // Multiple different services may call into the cloned install detector, it
63 // needs to continue supporting giving the same answer more than once
64 EXPECT_TRUE(detector.ShouldResetClientIds(&prefs));
65 EXPECT_TRUE(detector.ShouldResetClientIds(&prefs));
68 TEST(ClonedInstallDetectorTest, ClonedInstallDetectedInCurrentSession) {
69 TestingPrefServiceSimple prefs;
70 ClonedInstallDetector::RegisterPrefs(prefs.registry());
72 ClonedInstallDetector detector;
73 EXPECT_FALSE(detector.ShouldResetClientIds(&prefs));
74 EXPECT_FALSE(detector.ClonedInstallDetectedInCurrentSession());
76 // Save a machine id that will cause a clone to be detected.
77 prefs.SetInteger(prefs::kMetricsMachineId, kTestHashedId + 1);
78 detector.SaveMachineId(&prefs, kTestRawId);
80 // Ensure that the current session call returns true both before things are
81 // modified by ShouldResetClientIds and after
82 EXPECT_TRUE(detector.ClonedInstallDetectedInCurrentSession());
83 EXPECT_TRUE(detector.ShouldResetClientIds(&prefs));
84 EXPECT_TRUE(detector.ClonedInstallDetectedInCurrentSession());
87 TEST(ClonedInstallDetectorTest, ClonedInstallDetectedCallback) {
88 TestingPrefServiceSimple prefs;
89 ClonedInstallDetector::RegisterPrefs(prefs.registry());
91 ClonedInstallDetector detector;
93 // Set up a callback that will set |callback_called| to true when a cloned
94 // install is detected.
95 bool callback_called = false;
96 base::CallbackListSubscription subscription =
97 detector.AddOnClonedInstallDetectedCallback(base::BindLambdaForTesting(
98 [&callback_called] { callback_called = true; }));
100 // Save a machine id that will not cause a clone to be detected.
101 prefs.SetInteger(prefs::kMetricsMachineId, kTestHashedId);
102 detector.SaveMachineId(&prefs, kTestRawId);
103 EXPECT_FALSE(detector.ClonedInstallDetectedInCurrentSession());
104 EXPECT_FALSE(callback_called);
106 // Save a machine id that will cause a clone to be detected.
107 prefs.SetInteger(prefs::kMetricsMachineId, kTestHashedId + 1);
108 detector.SaveMachineId(&prefs, kTestRawId);
109 EXPECT_TRUE(detector.ClonedInstallDetectedInCurrentSession());
110 EXPECT_TRUE(callback_called);
112 // Verify that if a callback is added after the cloned install has been
113 // detected, it is called immediately.
114 bool callback_called2 = false;
115 base::CallbackListSubscription subscription2 =
116 detector.AddOnClonedInstallDetectedCallback(base::BindLambdaForTesting(
117 [&callback_called2] { callback_called2 = true; }));
118 EXPECT_TRUE(callback_called2);
121 } // namespace metrics