File: device_count_metrics_provider_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (100 lines) | stat: -rw-r--r-- 3,552 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync_device_info/device_count_metrics_provider.h"

#include <map>
#include <string>

#include "base/functional/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "components/sync/protocol/sync_enums.pb.h"
#include "components/sync_device_info/device_info.h"
#include "components/sync_device_info/fake_device_info_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {

class DeviceCountMetricsProviderTest : public testing::Test {
 public:
  DeviceCountMetricsProviderTest()
      : metrics_provider_(
            base::BindRepeating(&DeviceCountMetricsProviderTest::GetTrackers,
                                base::Unretained(this))) {}

  void AddTracker(const std::map<DeviceInfo::FormFactor, int>& count) {
    auto tracker = std::make_unique<FakeDeviceInfoTracker>();
    tracker->OverrideActiveDeviceCount(count);
    trackers_.emplace_back(std::move(tracker));
  }

  void GetTrackers(std::vector<const DeviceInfoTracker*>* trackers) {
    for (const auto& tracker : trackers_) {
      trackers->push_back(tracker.get());
    }
  }

  struct ExpectedCount {
    int total;
    int desktop_count;
    int phone_count;
    int tablet_count;
  };
  void TestProvider(const ExpectedCount& expected_count) {
    base::HistogramTester histogram_tester;
    metrics_provider_.ProvideCurrentSessionData(nullptr);
    histogram_tester.ExpectUniqueSample("Sync.DeviceCount2",
                                        expected_count.total, 1);
    histogram_tester.ExpectUniqueSample("Sync.DeviceCount2.Desktop",
                                        expected_count.desktop_count, 1);
    histogram_tester.ExpectUniqueSample("Sync.DeviceCount2.Phone",
                                        expected_count.phone_count, 1);
    histogram_tester.ExpectUniqueSample("Sync.DeviceCount2.Tablet",
                                        expected_count.tablet_count, 1);
  }

 private:
  DeviceCountMetricsProvider metrics_provider_;
  std::vector<std::unique_ptr<DeviceInfoTracker>> trackers_;
};

namespace {

TEST_F(DeviceCountMetricsProviderTest, NoTrackers) {
  TestProvider(ExpectedCount{});
}

TEST_F(DeviceCountMetricsProviderTest, SingleTracker) {
  AddTracker({{DeviceInfo::FormFactor::kDesktop, 1},
              {DeviceInfo::FormFactor::kPhone, 1}});
  TestProvider(ExpectedCount{
      .total = 2, .desktop_count = 1, .phone_count = 1, .tablet_count = 0});
}

TEST_F(DeviceCountMetricsProviderTest, MultipileTrackers) {
  AddTracker({{DeviceInfo::FormFactor::kPhone, 1}});
  AddTracker({{DeviceInfo::FormFactor::kTablet, 3},
              {DeviceInfo::FormFactor::kDesktop, 2}});
  AddTracker({{DeviceInfo::FormFactor::kDesktop, -120}});
  AddTracker({{DeviceInfo::FormFactor::kDesktop, 3}});
  TestProvider(ExpectedCount{
      .total = 5, .desktop_count = 3, .phone_count = 1, .tablet_count = 3});
}

TEST_F(DeviceCountMetricsProviderTest, OnlyNegative) {
  AddTracker({{DeviceInfo::FormFactor::kPhone, -121}});
  TestProvider(ExpectedCount{
      .total = 0, .desktop_count = 0, .phone_count = 0, .tablet_count = 0});
}

TEST_F(DeviceCountMetricsProviderTest, VeryLarge) {
  AddTracker({{DeviceInfo::FormFactor::kDesktop, 123456789},
              {DeviceInfo::FormFactor::kPhone, 1}});
  TestProvider(ExpectedCount{
      .total = 100, .desktop_count = 100, .phone_count = 1, .tablet_count = 0});
}

}  // namespace

}  // namespace syncer