[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / metrics / cloned_install_detector_unittest.cc
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.
4
5 #include "components/metrics/cloned_install_detector.h"
6
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"
13
14 namespace metrics {
15
16 namespace {
17
18 const std::string kTestRawId = "test";
19 // Hashed machine id for |kTestRawId|.
20 const int kTestHashedId = 2216819;
21
22 }  // namespace
23
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());
30
31   ClonedInstallDetector detector;
32   detector.SaveMachineId(&prefs, kTestRawId);
33
34   EXPECT_EQ(kTestHashedId, prefs.GetInteger(prefs::kMetricsMachineId));
35 }
36
37 TEST(ClonedInstallDetectorTest, DetectClone) {
38   TestingPrefServiceSimple prefs;
39   ClonedInstallDetector::RegisterPrefs(prefs.registry());
40
41   // Save a machine id that will cause a clone to be detected.
42   prefs.SetInteger(prefs::kMetricsMachineId, kTestHashedId + 1);
43
44   ClonedInstallDetector detector;
45   detector.SaveMachineId(&prefs, kTestRawId);
46
47   EXPECT_TRUE(prefs.GetBoolean(prefs::kMetricsResetIds));
48   EXPECT_TRUE(detector.ShouldResetClientIds(&prefs));
49 }
50
51 TEST(ClonedInstallDetectorTest, ShouldResetClientIds) {
52   TestingPrefServiceSimple prefs;
53   ClonedInstallDetector::RegisterPrefs(prefs.registry());
54
55   ClonedInstallDetector detector;
56   EXPECT_FALSE(detector.ShouldResetClientIds(&prefs));
57
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);
61
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));
66 }
67
68 TEST(ClonedInstallDetectorTest, ClonedInstallDetectedInCurrentSession) {
69   TestingPrefServiceSimple prefs;
70   ClonedInstallDetector::RegisterPrefs(prefs.registry());
71
72   ClonedInstallDetector detector;
73   EXPECT_FALSE(detector.ShouldResetClientIds(&prefs));
74   EXPECT_FALSE(detector.ClonedInstallDetectedInCurrentSession());
75
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);
79
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());
85 }
86
87 TEST(ClonedInstallDetectorTest, ClonedInstallDetectedCallback) {
88   TestingPrefServiceSimple prefs;
89   ClonedInstallDetector::RegisterPrefs(prefs.registry());
90
91   ClonedInstallDetector detector;
92
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; }));
99
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);
105
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);
111
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);
119 }
120
121 }  // namespace metrics