1 // Copyright 2023 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.
5 #include "components/metrics/gms_metrics_provider.h"
7 #include "base/android/build_info.h"
8 #include "base/test/metrics/histogram_tester.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 // Same as GmsMetricsProvider but Gms version is mocked for easy testing.
15 class MockedGmsMetricsProvider : public GmsMetricsProvider {
17 void SetGmsVersionForTesting(const std::string& version) {
22 // GmsMetricsProvider.
23 std::string GetGMSVersion() override { return version_; }
30 class GmsMetricsProviderTest : public testing::Test {
32 GmsMetricsProviderTest() = default;
34 base::HistogramTester& histogram_tester() { return histogram_tester_; }
35 MockedGmsMetricsProvider& gms_metrics_provider() {
36 return gms_metrics_provider_;
40 base::HistogramTester histogram_tester_;
41 MockedGmsMetricsProvider gms_metrics_provider_;
44 TEST_F(GmsMetricsProviderTest, TestMetricsReportedCorrectly) {
45 gms_metrics_provider().SetGmsVersionForTesting("234012000");
46 gms_metrics_provider().ProvideHistograms();
48 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
50 /*expected_bucket_count=*/1);
52 gms_metrics_provider().SetGmsVersionForTesting("234016000");
53 gms_metrics_provider().ProvideHistograms();
55 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
57 /*expected_bucket_count=*/2);
59 gms_metrics_provider().SetGmsVersionForTesting("234082000");
60 gms_metrics_provider().ProvideHistograms();
62 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
64 /*expected_bucket_count=*/3);
67 TEST_F(GmsMetricsProviderTest, TestMetricsReportedCorrectlyForBeta) {
68 gms_metrics_provider().SetGmsVersionForTesting("234002000");
69 gms_metrics_provider().ProvideHistograms();
71 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
73 /*expected_bucket_count=*/1);
75 gms_metrics_provider().SetGmsVersionForTesting("234008000");
76 gms_metrics_provider().ProvideHistograms();
78 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
80 /*expected_bucket_count=*/2);
82 gms_metrics_provider().SetGmsVersionForTesting("234010000");
83 gms_metrics_provider().ProvideHistograms();
85 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
87 /*expected_bucket_count=*/3);
90 TEST_F(GmsMetricsProviderTest, TestGMSNotInstalled) {
91 gms_metrics_provider().SetGmsVersionForTesting("0");
92 gms_metrics_provider().ProvideHistograms();
94 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
96 /*expected_bucket_count=*/1);
99 TEST_F(GmsMetricsProviderTest, TestGMSVersionInvalid) {
100 gms_metrics_provider().SetGmsVersionForTesting("aaaa");
101 gms_metrics_provider().ProvideHistograms();
103 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
105 /*expected_bucket_count=*/1);
108 TEST_F(GmsMetricsProviderTest, TestGMSVersionOutOfRange) {
109 gms_metrics_provider().SetGmsVersionForTesting("11111");
110 gms_metrics_provider().ProvideHistograms();
112 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
114 /*expected_bucket_count=*/1);
116 gms_metrics_provider().SetGmsVersionForTesting("999999999");
117 gms_metrics_provider().ProvideHistograms();
119 histogram_tester().ExpectUniqueSample("Android.PlayServices.ShortVersion",
121 /*expected_bucket_count=*/2);
124 } // namespace metrics