File: tab_ukm_test_helper.h

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 (78 lines) | stat: -rw-r--r-- 3,564 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
// 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.

#ifndef CHROME_BROWSER_UI_TABS_TAB_UKM_TEST_HELPER_H_
#define CHROME_BROWSER_UI_TABS_TAB_UKM_TEST_HELPER_H_

#include <map>
#include <optional>

#include "components/ukm/test_ukm_recorder.h"

// A UKM entry consists of named metrics with int64_t values. Use a map to
// specify expected metrics to test against an actual entry for tests.
// A value of |nullopt| implies a value shouldn't exist for the given metric
// name.
using UkmMetricMap = std::map<const char*, std::optional<int64_t>>;
using SourceUkmMetricMap =
    std::map<ukm::SourceId, std::pair<GURL, UkmMetricMap>>;

// Helper class to check entries have been logged as expected into UKM.
// Tests use this by validating new entries after they are logged. The helper
// skips already-validated entries when checking new entries, and expects new
// entries to be validated in the order they were logged. This ensures
// unexpected entries are not logged in between expected entries.
class UkmEntryChecker {
 public:
  UkmEntryChecker();
  UkmEntryChecker(const UkmEntryChecker&) = delete;
  UkmEntryChecker& operator=(const UkmEntryChecker&) = delete;
  ~UkmEntryChecker();

  // Expects that the next untested entry for |entry_name| matches the value
  // and the given URL if |source_url| is not empty.
  // Use this function to verify a single expected event.
  // This function increments |num_entries_[entry_name]| by 1, so entries after
  // this one will still be considered new/untested.
  void ExpectNewEntry(const std::string& entry_name,
                      const GURL& source_url,
                      const UkmMetricMap& expected_metrics);

  // Expects that |expected_entries.size()| new entries have been recorded for
  // |entry_name|, in any order. For each expected entry, checks that its
  // metrics match one of the newly recorded entries.
  // Use this function when expecting multiple entries to be logged at once.
  void ExpectNewEntries(const std::string& entry_name,
                        const std::vector<UkmMetricMap>& expected_entries);

  // Like ExpectNewEntries(), but entries are keyed by source ID. For each
  // recorded entry (as identified by its source ID), checks the values and the
  // source's URL if the expected URL is not empty.
  void ExpectNewEntriesBySource(const std::string& entry_name,
                                const SourceUkmMetricMap& expected_data);

  // Returns number of new entries that have been recorded for |entry_name|.
  // Entries are considered new until they have been validated with
  // ExpectNewEntries() or similar.
  // Thus, this returns the difference between the number of entries in UKM and
  // the number of entries that have been validated.
  int NumNewEntriesRecorded(const std::string& entry_name) const;

  // Returns number of entries for |entry_name|.
  size_t NumEntries(const std::string& entry_name) const;

  // Returns the last recorded entry for |entry_name|.
  const ukm::mojom::UkmEntry* LastUkmEntry(const std::string& entry_name) const;

 private:
  ukm::TestAutoSetUkmRecorder ukm_recorder_;

  // Keyed by entry name, and tracks the expected number of entries to ensure we
  // don't log duplicate or incorrect entries.
  // |num_entries_| records the number of entries that have been expected via
  // calls to ExpectNewEntries() or similar.
  std::map<std::string, size_t> num_entries_;
};

#endif  // CHROME_BROWSER_UI_TABS_TAB_UKM_TEST_HELPER_H_