File: gtest_util.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 (216 lines) | stat: -rw-r--r-- 8,017 bytes parent folder | download | duplicates (5)
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
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_RESOURCE_ATTRIBUTION_GTEST_UTIL_H_
#define COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_RESOURCE_ATTRIBUTION_GTEST_UTIL_H_

#include <optional>
#include <ostream>
#include <variant>

#include "components/performance_manager/public/resource_attribution/query_results.h"
#include "components/performance_manager/public/resource_attribution/resource_contexts.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace resource_attribution {

namespace internal {

// Returns a reference to the QueryResults member holding a ResultType.
template <typename ResultType>
constexpr const std::optional<ResultType>& QueryResultsMember(
    const QueryResults& results);

template <>
constexpr const std::optional<CPUTimeResult>& QueryResultsMember(
    const QueryResults& results) {
  return results.cpu_time_result;
}

template <>
constexpr const std::optional<MemorySummaryResult>& QueryResultsMember(
    const QueryResults& results) {
  return results.memory_summary_result;
}

// Returns the name of a type at compile-time, for better error messages.
template <typename T>
constexpr const char* TypeNameString();

template <>
constexpr const char* TypeNameString<CPUTimeResult>() {
  return "CPUTimeResult";
}

template <>
constexpr const char* TypeNameString<MemorySummaryResult>() {
  return "MemorySummaryResult";
}

}  // namespace internal

// gMock matcher expecting that a given ResultType object (which can be any
// object with a ResultMetadata field) has a ResultMetadata member whose fields
// match `measurement_time_matcher` and `algorithm_matcher`.
template <typename ResultType, typename Matcher1, typename Matcher2>
auto ResultMetadataMatches(Matcher1 measurement_time_matcher,
                           Matcher2 algorithm_matcher) {
  return ::testing::Field(
      "metadata", &ResultType::metadata,
      ::testing::AllOf(::testing::Field("measurement_time",
                                        &ResultMetadata::measurement_time,
                                        measurement_time_matcher),
                       ::testing::Field("algorithm", &ResultMetadata::algorithm,
                                        algorithm_matcher)));
}

// gMock matcher expecting that a given QueryResults object contains a result of
// type ResultType that matches `matcher`.
//
// Usage:
//
//  // Match any CPUTimeResult:
//  EXPECT_THAT(results, QueryResultsMatch<CPUTimeResult>(_));
//
//  // Match an exact MemorySummaryResult:
//  EXPECT_THAT(results, QueryResultsMatch<MemorySummaryResult>(
//                           MemorySummaryResult{...});
template <typename ResultType, typename Matcher>
auto QueryResultsMatch(Matcher matcher) {
  return ::testing::ResultOf(
      // Error messages will be formatted "whose ResultType value..."
      internal::TypeNameString<ResultType>(),
      &internal::QueryResultsMember<ResultType>, ::testing::Optional(matcher));
}

// As QueryResultsMatch() but expects that the QueryResults object contains
// multiple results.
//
// Usage:
//
//  // Match an exact MemorySummaryResult and any CPUTimeResult:
//  EXPECT_THAT(results,
//              QueryResultsMatchAll<MemorySummaryResult, CPUTimeResult>(
//                  MemorySummaryResult{...}, _));
template <typename... ResultTypes, typename... Matchers>
auto QueryResultsMatchAll(Matchers... matchers) {
  return ::testing::AllOf(QueryResultsMatch<ResultTypes>(matchers)...);
}

// gMock matcher expecting that a given QueryResultMap entry has a key of
// `resource_context` whose value contains a result of type ResultType that
// matches `matcher`.
//
// Usage:
//
//   QueryResultMap result_map;
//   result_map[context1] = {CPUTimeResult{...}};
//   result_map[context2] = {CPUTimeResult{...}, MemorySummaryResult{...}};
//
//   // Tests that the map contains `context1` and possibly other elements.
//   EXPECT_THAT(result_map, ::testing::Contains(
//       ResultForContextMatches<CPUTimeResult>(context1, _));
//
//   // Tests that the map contains exactly `context1` and `context2`.
//   // `context2` contains at least a MemorySummaryResult and possibly others.
//   EXPECT_THAT(result_map, ::testing::UnorderedElementsAre(
//       ResultForContextMatches<CPUTimeResult>(context1, _),
//       ResultForContextMatches<MemorySummaryResult>(context2, _)));
template <typename ResultType, typename Matcher>
auto ResultForContextMatches(const ResourceContext& resource_context,
                             Matcher matcher) {
  // Pair matches the key and value of a map entry.
  return ::testing::Pair(resource_context,
                         QueryResultsMatch<ResultType>(matcher));
}

// As ResultForContextMatches() but expects that the QueryResultMap entry
// contains multiple results.
//
// Usage:
//
//   QueryResultMap result_map;
//   result_map[context1] = {CPUTimeResult{...}};
//   result_map[context2] = {CPUTimeResult{...}, MemorySummaryResult{...}};
//
//   // Tests that the map contains exactly `context1` and `context2`.
//   // `context2` contains at a CPUTimeResult and a MemorySummaryResult.
//   EXPECT_THAT(result_map, ::testing::UnorderedElementsAre(
//       ResultForContextMatches<CPUTimeResult>(context1, _),
//       ResultForContextMatchesAll<CPUTimeResult, MemorySummaryResult>(
//           context2, _, _)));
template <typename... ResultTypes, typename... Matchers>
auto ResultForContextMatchesAll(const ResourceContext& resource_context,
                                Matchers... matchers) {
  // Pair matches the key and value of a map entry.
  return ::testing::Pair(
      resource_context,
      ::testing::AllOf(QueryResultsMatch<ResultTypes>(matchers)...));
}

// Test result printers. These need to go in the same namespace as the type
// being printed.

inline void PrintTo(MeasurementAlgorithm algorithm, std::ostream* os) {
  switch (algorithm) {
    case MeasurementAlgorithm::kDirectMeasurement:
      *os << "DirectMeasurement";
      return;
    case MeasurementAlgorithm::kSplit:
      *os << "Split";
      return;
    case MeasurementAlgorithm::kSum:
      *os << "Sum";
      return;
  }
  *os << "UnknownAlgorithm:" << static_cast<int>(algorithm);
}

inline void PrintTo(const ResultMetadata& metadata, std::ostream* os) {
  *os << "measurement_time:" << metadata.measurement_time
      << ",algorithm:" << ::testing::PrintToString(metadata.algorithm);
}

inline void PrintTo(const CPUTimeResult& result, std::ostream* os) {
  *os << "cumulative_cpu:" << result.cumulative_cpu
      << ",cumulative_background_cpu:" << result.cumulative_background_cpu
      << ",start_time:" << result.start_time
      << ",metadata:" << ::testing::PrintToString(result.metadata) << " ("
      << (result.metadata.measurement_time - result.start_time) << ")";
}

inline void PrintTo(const MemorySummaryResult& result, std::ostream* os) {
  *os << "rss:" << result.resident_set_size_kb
      << ",pmf:" << result.private_footprint_kb
      << ",metadata:" << ::testing::PrintToString(result.metadata);
}

// PrintTo() matches the generic std::variant overload before ResourceContext,
// so each context type needs a separate printer.

inline void PrintTo(const FrameContext& context, std::ostream* os) {
  *os << context.ToString();
}

inline void PrintTo(const PageContext& context, std::ostream* os) {
  *os << context.ToString();
}

inline void PrintTo(const ProcessContext& context, std::ostream* os) {
  *os << context.ToString();
}

inline void PrintTo(const WorkerContext& context, std::ostream* os) {
  *os << context.ToString();
}

inline void PrintTo(const OriginInBrowsingInstanceContext& context,
                    std::ostream* os) {
  *os << context.ToString();
}

}  // namespace resource_attribution

#endif  // COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_RESOURCE_ATTRIBUTION_GTEST_UTIL_H_