device_count_metrics_provider_unittest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "components/sync_device_info/device_count_metrics_provider.h"
  5. #include <map>
  6. #include <string>
  7. #include "base/bind.h"
  8. #include "base/test/metrics/histogram_tester.h"
  9. #include "components/sync/protocol/sync_enums.pb.h"
  10. #include "components/sync_device_info/device_info.h"
  11. #include "components/sync_device_info/fake_device_info_tracker.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace syncer {
  14. class DeviceCountMetricsProviderTest : public testing::Test {
  15. public:
  16. DeviceCountMetricsProviderTest()
  17. : metrics_provider_(
  18. base::BindRepeating(&DeviceCountMetricsProviderTest::GetTrackers,
  19. base::Unretained(this))) {}
  20. void AddTracker(const std::map<sync_pb::SyncEnums_DeviceType, int>& count) {
  21. auto tracker = std::make_unique<FakeDeviceInfoTracker>();
  22. tracker->OverrideActiveDeviceCount(count);
  23. trackers_.emplace_back(std::move(tracker));
  24. }
  25. void GetTrackers(std::vector<const DeviceInfoTracker*>* trackers) {
  26. for (const auto& tracker : trackers_) {
  27. trackers->push_back(tracker.get());
  28. }
  29. }
  30. struct ExpectedCount {
  31. int total;
  32. int desktop_count;
  33. int phone_count;
  34. int tablet_count;
  35. };
  36. void TestProvider(const ExpectedCount& expected_count) {
  37. base::HistogramTester histogram_tester;
  38. metrics_provider_.ProvideCurrentSessionData(nullptr);
  39. histogram_tester.ExpectUniqueSample("Sync.DeviceCount2",
  40. expected_count.total, 1);
  41. histogram_tester.ExpectUniqueSample("Sync.DeviceCount2.Desktop",
  42. expected_count.desktop_count, 1);
  43. histogram_tester.ExpectUniqueSample("Sync.DeviceCount2.Phone",
  44. expected_count.phone_count, 1);
  45. histogram_tester.ExpectUniqueSample("Sync.DeviceCount2.Tablet",
  46. expected_count.tablet_count, 1);
  47. }
  48. private:
  49. DeviceCountMetricsProvider metrics_provider_;
  50. std::vector<std::unique_ptr<DeviceInfoTracker>> trackers_;
  51. };
  52. namespace {
  53. TEST_F(DeviceCountMetricsProviderTest, NoTrackers) {
  54. TestProvider(ExpectedCount{});
  55. }
  56. TEST_F(DeviceCountMetricsProviderTest, SingleTracker) {
  57. AddTracker({{sync_pb::SyncEnums_DeviceType_TYPE_WIN, 1},
  58. {sync_pb::SyncEnums_DeviceType_TYPE_PHONE, 1}});
  59. TestProvider(ExpectedCount{
  60. .total = 2, .desktop_count = 1, .phone_count = 1, .tablet_count = 0});
  61. }
  62. TEST_F(DeviceCountMetricsProviderTest, MultipileTrackers) {
  63. AddTracker({{sync_pb::SyncEnums_DeviceType_TYPE_PHONE, 1}});
  64. AddTracker({{sync_pb::SyncEnums_DeviceType_TYPE_TABLET, 3},
  65. {sync_pb::SyncEnums_DeviceType_TYPE_MAC, 2}});
  66. AddTracker({{sync_pb::SyncEnums_DeviceType_TYPE_WIN, -120}});
  67. AddTracker({{sync_pb::SyncEnums_DeviceType_TYPE_WIN, 3}});
  68. TestProvider(ExpectedCount{
  69. .total = 5, .desktop_count = 3, .phone_count = 1, .tablet_count = 3});
  70. }
  71. TEST_F(DeviceCountMetricsProviderTest, OnlyNegative) {
  72. AddTracker({{sync_pb::SyncEnums_DeviceType_TYPE_PHONE, -121}});
  73. TestProvider(ExpectedCount{
  74. .total = 0, .desktop_count = 0, .phone_count = 0, .tablet_count = 0});
  75. }
  76. TEST_F(DeviceCountMetricsProviderTest, VeryLarge) {
  77. AddTracker({{sync_pb::SyncEnums_DeviceType_TYPE_LINUX, 123456789},
  78. {sync_pb::SyncEnums_DeviceType_TYPE_PHONE, 1}});
  79. TestProvider(ExpectedCount{
  80. .total = 100, .desktop_count = 100, .phone_count = 1, .tablet_count = 0});
  81. }
  82. } // namespace
  83. } // namespace syncer