OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 | |
5 #include "components/sync_driver/device_count_metrics_provider.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/test/histogram_tester.h" | |
13 #include "components/sync_driver/device_info.h" | |
14 #include "components/sync_driver/device_info_tracker.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace sync_driver { | |
18 | |
19 namespace { | |
20 | |
21 class FakeTracker : public DeviceInfoTracker { | |
22 public: | |
23 explicit FakeTracker(const int count) : count_(count) {} | |
24 | |
25 // DeviceInfoTracker | |
26 bool IsSyncing() const override { return false; } | |
27 std::unique_ptr<DeviceInfo> GetDeviceInfo( | |
28 const std::string& client_id) const override { | |
29 return std::unique_ptr<DeviceInfo>(); | |
30 } | |
31 ScopedVector<DeviceInfo> GetAllDeviceInfo() const override { | |
32 return ScopedVector<DeviceInfo>(); | |
33 } | |
34 void AddObserver(Observer* observer) override {} | |
35 void RemoveObserver(Observer* observer) override {} | |
36 int CountActiveDevices() const override { return count_; } | |
37 | |
38 private: | |
39 int count_; | |
40 }; | |
41 | |
42 } // namespace | |
43 | |
44 class DeviceCountMetricsProviderTest : public testing::Test { | |
45 public: | |
46 DeviceCountMetricsProviderTest() | |
47 : metrics_provider_( | |
48 base::Bind(&DeviceCountMetricsProviderTest::GetTrackers, | |
49 base::Unretained(this))) {} | |
50 | |
51 void AddTracker(const int count) { | |
52 trackers_.push_back( | |
53 std::unique_ptr<DeviceInfoTracker>(new FakeTracker(count))); | |
54 } | |
55 void GetTrackers(std::vector<const DeviceInfoTracker*>* trackers) { | |
56 for (const auto& tracker : trackers_) { | |
57 trackers->push_back(tracker.get()); | |
58 } | |
59 } | |
60 | |
61 void TestProvider(int expected_device_count) { | |
62 base::HistogramTester histogram_tester; | |
63 metrics_provider_.ProvideGeneralMetrics(nullptr); | |
64 histogram_tester.ExpectUniqueSample("Sync.DeviceCount", | |
65 expected_device_count, 1); | |
66 } | |
67 | |
68 private: | |
69 DeviceCountMetricsProvider metrics_provider_; | |
70 std::vector<std::unique_ptr<DeviceInfoTracker>> trackers_; | |
71 }; | |
72 | |
73 namespace { | |
74 | |
75 TEST_F(DeviceCountMetricsProviderTest, NoTrackers) { | |
76 TestProvider(0); | |
77 } | |
78 | |
79 TEST_F(DeviceCountMetricsProviderTest, SingleTracker) { | |
80 AddTracker(2); | |
81 TestProvider(2); | |
82 } | |
83 | |
84 TEST_F(DeviceCountMetricsProviderTest, MultipileTrackers) { | |
85 AddTracker(1); | |
86 AddTracker(5); | |
87 AddTracker(-123); | |
88 AddTracker(0); | |
89 TestProvider(5); | |
90 } | |
91 | |
92 TEST_F(DeviceCountMetricsProviderTest, OnlyNegative) { | |
93 AddTracker(-123); | |
94 TestProvider(0); | |
95 } | |
96 | |
97 TEST_F(DeviceCountMetricsProviderTest, VeryLarge) { | |
98 AddTracker(123456789); | |
99 TestProvider(100); | |
100 } | |
101 | |
102 } // namespace | |
103 | |
104 } // namespace sync_driver | |
OLD | NEW |