1 // Copyright 2017 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/component_metrics_provider.h"
6
7 #include "base/hash/hash.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/version.h"
10 #include "components/component_updater/component_updater_service.h"
11 #include "components/component_updater/mock_component_updater_service.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/metrics_proto/system_profile.pb.h"
14
15 namespace metrics {
16
17 using component_updater::ComponentInfo;
18
19 namespace {
20 class TestComponentMetricsProviderDelegate
21 : public ComponentMetricsProviderDelegate {
22 public:
TestComponentMetricsProviderDelegate(std::vector<ComponentInfo> & components)23 explicit TestComponentMetricsProviderDelegate(
24 std::vector<ComponentInfo>& components)
25 : components_(components) {}
26 ~TestComponentMetricsProviderDelegate() override = default;
27
GetComponents()28 std::vector<ComponentInfo> GetComponents() override { return components_; }
29
30 private:
31 std::vector<ComponentInfo> components_;
32 };
33 } // namespace
34
35 class ComponentMetricsProviderTest : public testing::Test {
36 public:
ComponentMetricsProviderTest()37 ComponentMetricsProviderTest() {}
38
39 ComponentMetricsProviderTest(const ComponentMetricsProviderTest&) = delete;
40 ComponentMetricsProviderTest& operator=(const ComponentMetricsProviderTest&) =
41 delete;
42
~ComponentMetricsProviderTest()43 ~ComponentMetricsProviderTest() override {}
44 };
45
TEST_F(ComponentMetricsProviderTest,ProvideComponentMetrics)46 TEST_F(ComponentMetricsProviderTest, ProvideComponentMetrics) {
47 std::vector<ComponentInfo> components = {
48 ComponentInfo(
49 "hfnkpimlhhgieaddgfemjhofmfblmnib",
50 "1.0846414bf2025bbc067b6fa5b61b16eda2269d8712b8fec0973b4c71fdc65ca0",
51 u"name1", base::Version("1.2.3.4"), ""),
52 ComponentInfo(
53 "oimompecagnajdejgnnjijobebaeigek",
54 "1.adc9207a4a88ee98bf9ddf0330f35818386f1adc006bc8eee94dc59d43c0f5d6",
55 u"name2", base::Version("5.6.7.8"), "1:1bcZ:[email protected],[email protected]"),
56 ComponentInfo(
57 "thiscomponentfilteredfromresults",
58 "1.b5268dc93e08d68d0be26bd8fbbb15c7b7f805cc06b4abd9d49381bc178e78cf",
59 u"name3", base::Version("9.9.9.9"), "")};
60
61 ComponentMetricsProvider component_provider(
62 std::make_unique<TestComponentMetricsProviderDelegate>(components));
63 SystemProfileProto system_profile;
64 component_provider.ProvideSystemProfileMetrics(&system_profile);
65
66 EXPECT_EQ(2, system_profile.chrome_component_size());
67 EXPECT_EQ(SystemProfileProto_ComponentId_CRL_SET,
68 system_profile.chrome_component(0).component_id());
69 EXPECT_EQ("1.2.3.4", system_profile.chrome_component(0).version());
70 EXPECT_EQ(138821963u, system_profile.chrome_component(0).omaha_fingerprint());
71 EXPECT_EQ(SystemProfileProto_ComponentId_WIDEVINE_CDM,
72 system_profile.chrome_component(1).component_id());
73 EXPECT_EQ("5.6.7.8", system_profile.chrome_component(1).version());
74 EXPECT_EQ(2915639418u,
75 system_profile.chrome_component(1).omaha_fingerprint());
76 EXPECT_EQ(system_profile.chrome_component(0).cohort_hash(),
77 base::PersistentHash(""));
78 EXPECT_EQ(system_profile.chrome_component(1).cohort_hash(),
79 base::PersistentHash("1:1bcZ"));
80 }
81
82 } // namespace metrics
83