File: search_metrics_reporter_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (136 lines) | stat: -rw-r--r-- 5,566 bytes parent folder | download | duplicates (9)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/local_search_service/search_metrics_reporter.h"

#include <memory>

#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/local_search_service/pref_names.h"
#include "chromeos/ash/components/local_search_service/shared_structs.h"
#include "components/metrics/daily_event.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::local_search_service {

class SearchMetricsReporterTest : public testing::Test {
 public:
  SearchMetricsReporterTest() = default;
  ~SearchMetricsReporterTest() override = default;

  void SetUp() override {
    SearchMetricsReporter::RegisterLocalStatePrefs(pref_service_.registry());
  }

  void TearDown() override { reporter_.reset(); }

 protected:
  void SetReporter() {
    reporter_ = std::make_unique<SearchMetricsReporter>(&pref_service_);
  }

  // Notifies |reporter_| that a search is performed.
  void SendOnSearchPerformedAndCheck(IndexId index_id) {
    bool callback_done = false;
    reporter_->OnSearchPerformed(
        index_id,
        base::BindOnce([](bool* callback_done) { *callback_done = true; },
                       &callback_done));
    task_environment_.RunUntilIdle();
    ASSERT_TRUE(callback_done);
  }

  // Instructs |reporter_| to report daily metrics for reason |type|.
  void TriggerDailyEvent(metrics::DailyEvent::IntervalType type) {
    reporter_->ReportDailyMetricsForTesting(type);
  }

  // Instructs |reporter_| to report daily metrics due to the passage of a day
  // and verifies that it reports one sample with each of the passed values.
  void TriggerDailyEventAndVerifyHistograms(const std::string& histogram_name,
                                            int expected_count) {
    base::HistogramTester histogram_tester;

    TriggerDailyEvent(metrics::DailyEvent::IntervalType::DAY_ELAPSED);
    histogram_tester.ExpectUniqueSample(histogram_name, expected_count, 1);
  }

  base::test::TaskEnvironment task_environment_;
  TestingPrefServiceSimple pref_service_;
  std::unique_ptr<SearchMetricsReporter> reporter_;
};

TEST_F(SearchMetricsReporterTest, CountAndReportEvents) {
  SetReporter();
  base::HistogramTester tester;

  SendOnSearchPerformedAndCheck(IndexId::kCrosSettings);
  SendOnSearchPerformedAndCheck(IndexId::kCrosSettings);
  SendOnSearchPerformedAndCheck(IndexId::kCrosSettings);
  TriggerDailyEvent(metrics::DailyEvent::IntervalType::DAY_ELAPSED);
  tester.ExpectUniqueSample(SearchMetricsReporter::kHelpAppName, 0, 1);
  tester.ExpectUniqueSample(SearchMetricsReporter::kCrosSettingsName, 3, 1);

  // The next day, another two searches.
  SendOnSearchPerformedAndCheck(IndexId::kCrosSettings);
  SendOnSearchPerformedAndCheck(IndexId::kCrosSettings);
  SendOnSearchPerformedAndCheck(IndexId::kHelpApp);
  TriggerDailyEvent(metrics::DailyEvent::IntervalType::DAY_ELAPSED);
  tester.ExpectBucketCount(SearchMetricsReporter::kHelpAppName, 1, 1);
  tester.ExpectBucketCount(SearchMetricsReporter::kCrosSettingsName, 2, 1);

  // Next day, CLOCK_CHANGED event happens, nothing more is logged.
  SendOnSearchPerformedAndCheck(IndexId::kHelpApp);
  SendOnSearchPerformedAndCheck(IndexId::kCrosSettings);
  SendOnSearchPerformedAndCheck(IndexId::kHelpApp);
  SendOnSearchPerformedAndCheck(IndexId::kCrosSettings);
  SendOnSearchPerformedAndCheck(IndexId::kHelpApp);
  TriggerDailyEvent(metrics::DailyEvent::IntervalType::CLOCK_CHANGED);
  tester.ExpectTotalCount(SearchMetricsReporter::kHelpAppName, 2);
  tester.ExpectTotalCount(SearchMetricsReporter::kCrosSettingsName, 2);
}

TEST_F(SearchMetricsReporterTest, LoadInitialCountsFromPrefs) {
  // Create a new reporter and check that it loads its initial event counts from
  // prefs.
  pref_service_.SetInteger(prefs::kLocalSearchServiceMetricsCrosSettingsCount,
                           2);
  SetReporter();
  TriggerDailyEventAndVerifyHistograms(SearchMetricsReporter::kCrosSettingsName,
                                       2);

  // The previous report should've cleared the prefs, so a new reporter should
  // start out at zero.
  TriggerDailyEventAndVerifyHistograms(SearchMetricsReporter::kCrosSettingsName,
                                       0);
}

TEST_F(SearchMetricsReporterTest, IgnoreDailyEventFirstRun) {
  SetReporter();
  // metrics::DailyEvent notifies observers immediately on first run. Histograms
  // shouldn't be sent in this case.
  base::HistogramTester tester;
  TriggerDailyEvent(metrics::DailyEvent::IntervalType::FIRST_RUN);
  tester.ExpectTotalCount(SearchMetricsReporter::kCrosSettingsName, 0);
}

TEST_F(SearchMetricsReporterTest, IgnoreDailyEventClockChanged) {
  SetReporter();
  SendOnSearchPerformedAndCheck(IndexId::kCrosSettings);

  // metrics::DailyEvent notifies observers if it sees that the system clock has
  // jumped back. Histograms shouldn't be sent in this case.
  base::HistogramTester tester;
  TriggerDailyEvent(metrics::DailyEvent::IntervalType::CLOCK_CHANGED);
  tester.ExpectTotalCount(SearchMetricsReporter::kCrosSettingsName, 0);

  // The existing stats should be cleared when the clock change notification is
  // received, so the next report should only contain zeros.
  TriggerDailyEventAndVerifyHistograms(SearchMetricsReporter::kCrosSettingsName,
                                       0);
}

}  // namespace ash::local_search_service