File: tab_ukm_test_helper.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 (205 lines) | stat: -rw-r--r-- 7,881 bytes parent folder | download | duplicates (6)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/tabs/tab_ukm_test_helper.h"

#include <algorithm>
#include <sstream>

#include "base/memory/raw_ptr.h"
#include "services/metrics/public/cpp/ukm_source.h"
#include "services/metrics/public/mojom/ukm_interface.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Verifies each expected metric's value. Metrics not in |expected_metrics| are
// ignored. A metric value of |nullopt| implies the metric shouldn't exist.
void ExpectEntryMetrics(const ukm::mojom::UkmEntry& entry,
                        const UkmMetricMap& expected_metrics) {
  // Each expected metric should match a named value in the UKM entry.
  for (const UkmMetricMap::value_type& pair : expected_metrics) {
    if (pair.second.has_value()) {
      ukm::TestUkmRecorder::ExpectEntryMetric(&entry, pair.first,
                                              pair.second.value());
    } else {
      // The metric shouldn't exist.
      EXPECT_FALSE(ukm::TestUkmRecorder::EntryHasMetric(&entry, pair.first))
          << " for metric: " << pair.first;
    }
  }
}

// Returns true if each metric in |expected_metrics| has the same value in the
// given entry. An expected metric value of |nullopt| implies a value shouldn't
// exist in the entry.
bool EntryContainsMetrics(const ukm::mojom::UkmEntry* entry,
                          const UkmMetricMap& expected_metrics) {
  for (const UkmMetricMap::value_type& expected_pair : expected_metrics) {
    const int64_t* metric =
        ukm::TestUkmRecorder::GetEntryMetric(entry, expected_pair.first);
    if (expected_pair.second.has_value()) {
      if (!metric || *metric != expected_pair.second.value()) {
        return false;
      }
    } else {
      // The metric shouldn't exist.
      if (ukm::TestUkmRecorder::EntryHasMetric(entry, expected_pair.first)) {
        return false;
      }
    }
  }
  return true;
}

// Returns an iterator to an entry whose metrics match |expected_metrics|,
// or end() if not found.
std::vector<
    raw_ptr<const ukm::mojom::UkmEntry, VectorExperimental>>::const_iterator
FindMatchingEntry(
    const std::vector<raw_ptr<const ukm::mojom::UkmEntry, VectorExperimental>>&
        entries,
    const UkmMetricMap& expected_metrics) {
  return std::ranges::find_if(
      entries, [&expected_metrics](const ukm::mojom::UkmEntry* entry) {
        return EntryContainsMetrics(entry, expected_metrics);
      });
}

}  // namespace

UkmEntryChecker::UkmEntryChecker() = default;

UkmEntryChecker::~UkmEntryChecker() {
  // Events under test should not have new, unchecked entries.
  for (const auto& pair : num_entries_) {
    const std::string& entry_name = pair.first;
    int num_unexpected_entries = NumNewEntriesRecorded(entry_name);
    // Could be negative if an expectation has already failed.
    if (num_unexpected_entries <= 0) {
      continue;
    }

    ADD_FAILURE() << "Found " << num_unexpected_entries
                  << " unexpected UKM entries at shutdown for: " << entry_name;
    size_t first_unexpected_index = num_entries_[entry_name];
    const ukm::mojom::UkmEntry* ukm_entry =
        ukm_recorder_.GetEntriesByName(entry_name)[first_unexpected_index];

    std::ostringstream entry_metrics;
    for (const auto& metric : ukm_entry->metrics) {
      entry_metrics << "\n" << metric.first << ": " << metric.second;
    }
    LOG(ERROR) << "First unexpected entry: " << entry_metrics.str();
  }
}

void UkmEntryChecker::ExpectNewEntry(const std::string& entry_name,
                                     const GURL& source_url,
                                     const UkmMetricMap& expected_metrics) {
  // There should be at least one new entry, which is the one we're checking.
  num_entries_[entry_name]++;
  std::vector<raw_ptr<const ukm::mojom::UkmEntry, VectorExperimental>> entries =
      ukm_recorder_.GetEntriesByName(entry_name);
  ASSERT_LE(num_entries_[entry_name], entries.size())
      << "Expected at least " << num_entries_[entry_name] << " entries, found "
      << entries.size() << " for " << entry_name;

  // Verify the entry is associated with the correct URL.
  const ukm::mojom::UkmEntry* entry = entries[num_entries_[entry_name] - 1];
  if (!source_url.is_empty()) {
    ukm_recorder_.ExpectEntrySourceHasUrl(entry, source_url);
  }

  ExpectEntryMetrics(*entry, expected_metrics);
}

void UkmEntryChecker::ExpectNewEntries(
    const std::string& entry_name,
    const std::vector<UkmMetricMap>& expected_entries) {
  std::vector<raw_ptr<const ukm::mojom::UkmEntry, VectorExperimental>> entries =
      ukm_recorder_.GetEntriesByName(entry_name);

  const size_t num_new_entries = expected_entries.size();
  num_entries_[entry_name] += num_new_entries;
  ASSERT_LE(num_entries_[entry_name], entries.size())
      << "Expected at least " << num_entries_[entry_name] << " entries, found "
      << entries.size() << " for " << entry_name;

  // Remove old entries from |entries| before matching new entries.
  entries.erase(entries.begin(), entries.end() - num_new_entries);
  for (size_t i = 0; i < expected_entries.size(); i++) {
    auto it = FindMatchingEntry(entries, expected_entries[i]);
    if (it == entries.end()) {
      ADD_FAILURE() << "Expected entry " << i << " not found.";
      continue;
    } else {
      // Remove the matched entry from the pool of actual entries.
      entries.erase(it);
    }
  }
}

void UkmEntryChecker::ExpectNewEntriesBySource(
    const std::string& entry_name,
    const SourceUkmMetricMap& expected_data) {
  std::vector<raw_ptr<const ukm::mojom::UkmEntry, VectorExperimental>> entries =
      ukm_recorder_.GetEntriesByName(entry_name);

  const size_t num_new_entries = expected_data.size();
  const size_t num_entries = entries.size();
  num_entries_[entry_name] += num_new_entries;

  ASSERT_LE(num_entries_[entry_name], entries.size());
  std::set<ukm::SourceId> found_source_ids;

  for (size_t i = 0; i < num_new_entries; ++i) {
    const ukm::mojom::UkmEntry* entry =
        entries[num_entries - num_new_entries + i];
    const ukm::SourceId& source_id = entry->source_id;
    const auto& expected_data_for_id = expected_data.find(source_id);
    EXPECT_TRUE(expected_data_for_id != expected_data.end());
    EXPECT_EQ(0u, found_source_ids.count(source_id));

    found_source_ids.insert(source_id);
    const std::pair<GURL, UkmMetricMap>& expected_url_metrics =
        expected_data_for_id->second;

    const GURL& source_url = expected_url_metrics.first;
    const UkmMetricMap& expected_metrics = expected_url_metrics.second;
    if (!source_url.is_empty()) {
      ukm_recorder_.ExpectEntrySourceHasUrl(entry, source_url);
    }

    // Each expected metric should match a named value in the UKM entry.
    ExpectEntryMetrics(*entry, expected_metrics);
  }
}

int UkmEntryChecker::NumNewEntriesRecorded(
    const std::string& entry_name) const {
  const size_t current_ukm_entries = NumEntries(entry_name);

  // If a value hasn't been inserted for |entry_name|, the test hasn't checked
  // for these entries before, so they all count as new.
  if (!num_entries_.count(entry_name)) {
    return current_ukm_entries;
  }

  size_t previous_num_entries = num_entries_.at(entry_name);
  EXPECT_GE(current_ukm_entries, previous_num_entries);
  return current_ukm_entries - previous_num_entries;
}

size_t UkmEntryChecker::NumEntries(const std::string& entry_name) const {
  return ukm_recorder_.GetEntriesByName(entry_name).size();
}

const ukm::mojom::UkmEntry* UkmEntryChecker::LastUkmEntry(
    const std::string& entry_name) const {
  std::vector<raw_ptr<const ukm::mojom::UkmEntry, VectorExperimental>> entries =
      ukm_recorder_.GetEntriesByName(entry_name);
  CHECK(!entries.empty());
  return entries.back();
}