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.
5 #include "chrome/browser/metrics/extensions_metrics_provider.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "components/metrics/client_info.h"
12 #include "components/metrics/metrics_service.h"
13 #include "components/metrics/metrics_state_manager.h"
14 #include "components/metrics/proto/system_profile.pb.h"
15 #include "extensions/common/extension.h"
16 #include "extensions/common/extension_builder.h"
17 #include "extensions/common/extension_set.h"
18 #include "testing/gtest/include/gtest/gtest.h"
22 bool IsMetricsReportingEnabled() {
26 void StoreNoClientInfoBackup(const metrics::ClientInfo
& /* client_info */) {
29 scoped_ptr
<metrics::ClientInfo
> ReturnNoBackup() {
30 return scoped_ptr
<metrics::ClientInfo
>();
33 class TestExtensionsMetricsProvider
: public ExtensionsMetricsProvider
{
35 explicit TestExtensionsMetricsProvider(
36 metrics::MetricsStateManager
* metrics_state_manager
)
37 : ExtensionsMetricsProvider(metrics_state_manager
) {}
39 // Makes the protected HashExtension method available to testing code.
40 using ExtensionsMetricsProvider::HashExtension
;
43 // Override the GetInstalledExtensions method to return a set of extensions
45 scoped_ptr
<extensions::ExtensionSet
> GetInstalledExtensions(
46 Profile
* profile
) override
{
47 scoped_ptr
<extensions::ExtensionSet
> extensions(
48 new extensions::ExtensionSet());
49 scoped_refptr
<const extensions::Extension
> extension
;
50 extension
= extensions::ExtensionBuilder()
51 .SetManifest(extensions::DictionaryBuilder()
52 .Set("name", "Test extension")
53 .Set("version", "1.0.0")
54 .Set("manifest_version", 2))
55 .SetID("ahfgeienlihckogmohjhadlkjgocpleb")
57 extensions
->Insert(extension
);
58 extension
= extensions::ExtensionBuilder()
59 .SetManifest(extensions::DictionaryBuilder()
60 .Set("name", "Test extension 2")
61 .Set("version", "1.0.0")
62 .Set("manifest_version", 2))
63 .SetID("pknkgggnfecklokoggaggchhaebkajji")
65 extensions
->Insert(extension
);
66 extension
= extensions::ExtensionBuilder()
67 .SetManifest(extensions::DictionaryBuilder()
68 .Set("name", "Colliding Extension")
69 .Set("version", "1.0.0")
70 .Set("manifest_version", 2))
71 .SetID("mdhofdjgenpkhlmddfaegdjddcecipmo")
73 extensions
->Insert(extension
);
74 return extensions
.Pass();
77 // Override GetClientID() to return a specific value on which test
78 // expectations are based.
79 uint64
GetClientID() override
{ return 0x3f1bfee9; }
84 // Checks that the hash function used to hide precise extension IDs produces
85 // the expected values.
86 TEST(ExtensionsMetricsProvider
, HashExtension
) {
88 TestExtensionsMetricsProvider::HashExtension(
89 "ahfgeienlihckogmohjhadlkjgocpleb", 0));
91 TestExtensionsMetricsProvider::HashExtension(
92 "ahfgeienlihckogmohjhadlkjgocpleb", 3817));
94 TestExtensionsMetricsProvider::HashExtension(
95 "pknkgggnfecklokoggaggchhaebkajji", 3817));
97 TestExtensionsMetricsProvider::HashExtension(
98 "mdhofdjgenpkhlmddfaegdjddcecipmo", 3817));
101 // Checks that the fake set of extensions provided by
102 // TestExtensionsMetricsProvider is encoded properly.
103 TEST(ExtensionsMetricsProvider
, SystemProtoEncoding
) {
104 metrics::SystemProfileProto system_profile
;
105 TestingPrefServiceSimple local_state
;
106 metrics::MetricsService::RegisterPrefs(local_state
.registry());
107 scoped_ptr
<metrics::MetricsStateManager
> metrics_state_manager(
108 metrics::MetricsStateManager::Create(
110 base::Bind(&IsMetricsReportingEnabled
),
111 base::Bind(&StoreNoClientInfoBackup
),
112 base::Bind(&ReturnNoBackup
)));
113 TestExtensionsMetricsProvider
extension_metrics(metrics_state_manager
.get());
114 extension_metrics
.ProvideSystemProfileMetrics(&system_profile
);
115 ASSERT_EQ(2, system_profile
.occupied_extension_bucket_size());
116 EXPECT_EQ(10, system_profile
.occupied_extension_bucket(0));
117 EXPECT_EQ(1007, system_profile
.occupied_extension_bucket(1));