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/android_metrics_helper.h"
9 #include "base/test/metrics/histogram_tester.h"
10 #include "components/prefs/testing_pref_service.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(AndroidMetricsHelperTest, CpuAbiBitnessSupport) {
16 std::unique_ptr<AndroidMetricsHelper> helper(
17 AndroidMetricsHelper::CreateInstanceForTest("", false, false));
18 EXPECT_EQ(helper->cpu_abi_bitness_support(), CpuAbiBitnessSupport::kNeither);
20 helper.reset(AndroidMetricsHelper::CreateInstanceForTest("", true, false));
21 EXPECT_EQ(helper->cpu_abi_bitness_support(),
22 CpuAbiBitnessSupport::k32bitOnly);
24 helper.reset(AndroidMetricsHelper::CreateInstanceForTest("", false, true));
25 EXPECT_EQ(helper->cpu_abi_bitness_support(),
26 CpuAbiBitnessSupport::k64bitOnly);
28 helper.reset(AndroidMetricsHelper::CreateInstanceForTest("", true, true));
29 EXPECT_EQ(helper->cpu_abi_bitness_support(),
30 CpuAbiBitnessSupport::k32And64bit);
33 TEST(AndroidMetricsHelperTest, VersionCode) {
34 std::unique_ptr<AndroidMetricsHelper> helper(
35 AndroidMetricsHelper::CreateInstanceForTest("588700000", true, true));
36 EXPECT_EQ(helper->version_code_int(), 588700000);
39 TEST(AndroidMetricsHelperTest, VersionCode_BadData) {
40 std::unique_ptr<AndroidMetricsHelper> helper(
41 AndroidMetricsHelper::CreateInstanceForTest("", true, true));
42 EXPECT_EQ(helper->version_code_int(), 0);
45 AndroidMetricsHelper::CreateInstanceForTest("5887_000_0_9", true, true));
46 EXPECT_EQ(helper->version_code_int(), 0);
49 class AndroidMetricsHelperHistTest : public testing::Test {
51 AndroidMetricsHelperHistTest() {
52 AndroidMetricsHelper::RegisterPrefs(pref_service.registry());
54 ~AndroidMetricsHelperHistTest() override {
55 AndroidMetricsHelper::ResetGlobalStateForTesting();
59 TestingPrefServiceSimple pref_service;
60 base::HistogramTester histogram_tester;
63 TEST_F(AndroidMetricsHelperHistTest, EmitHistograms_CurrentSession) {
64 std::unique_ptr<AndroidMetricsHelper> helper(
65 AndroidMetricsHelper::CreateInstanceForTest("588700002", true, true));
66 helper->EmitHistograms(&pref_service, /*current_session=*/true);
67 histogram_tester.ExpectTotalCount("Android.VersionCode", 1);
68 histogram_tester.ExpectUniqueSample("Android.VersionCode", 588700002, 1);
69 histogram_tester.ExpectTotalCount("Android.CpuAbiBitnessSupport", 1);
70 histogram_tester.ExpectUniqueSample("Android.CpuAbiBitnessSupport",
71 CpuAbiBitnessSupport::k32And64bit, 1);
74 TEST_F(AndroidMetricsHelperHistTest, EmitHistograms_LogPreviousSession) {
75 std::unique_ptr<AndroidMetricsHelper> helper(
76 AndroidMetricsHelper::CreateInstanceForTest("588700002", true, true));
77 helper->EmitHistograms(&pref_service, /*current_session=*/false);
79 histogram_tester.ExpectTotalCount("Android.VersionCode", 0);
80 histogram_tester.ExpectTotalCount("Android.CpuAbiBitnessSupport", 1);
81 histogram_tester.ExpectUniqueSample("Android.CpuAbiBitnessSupport",
82 CpuAbiBitnessSupport::k32And64bit, 1);
85 TEST_F(AndroidMetricsHelperHistTest,
86 EmitHistograms_LogPreviousSessionWithSavedLocalState) {
87 AndroidMetricsHelper::SaveLocalState(&pref_service, 588700002);
88 std::unique_ptr<AndroidMetricsHelper> helper(
89 AndroidMetricsHelper::CreateInstanceForTest("588700006", false, false));
90 helper->EmitHistograms(&pref_service, /*current_session=*/false);
91 // The previous value was used.
92 histogram_tester.ExpectTotalCount("Android.VersionCode", 1);
93 histogram_tester.ExpectUniqueSample("Android.VersionCode", 588700002, 1);
94 // We don't bother to save/restore CpuAbiBitnessSupport, as we assume that the
95 // value doesn't change across sessions. The test uses a different value
96 // artificially, solely for the purpose of testing this behavior.
97 histogram_tester.ExpectTotalCount("Android.CpuAbiBitnessSupport", 1);
98 histogram_tester.ExpectUniqueSample("Android.CpuAbiBitnessSupport",
99 CpuAbiBitnessSupport::kNeither, 1);
102 TEST_F(AndroidMetricsHelperHistTest, EmitHistograms_BadData) {
103 std::unique_ptr<AndroidMetricsHelper> helper(
104 AndroidMetricsHelper::CreateInstanceForTest("5887_000_0_2", true, false));
105 helper->EmitHistograms(&pref_service, /*current_session=*/true);
107 histogram_tester.ExpectTotalCount("Android.VersionCode", 0);
108 histogram_tester.ExpectTotalCount("Android.CpuAbiBitnessSupport", 1);
109 histogram_tester.ExpectUniqueSample("Android.CpuAbiBitnessSupport",
110 CpuAbiBitnessSupport::k32bitOnly, 1);
113 } // namespace metrics