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
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_BASE_NET_LOG_UNITTEST_H_
#define NET_BASE_NET_LOG_UNITTEST_H_
#pragma once
#include <cstddef>
#include "net/base/capturing_net_log.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
// Create a timestamp with internal value of |t| milliseconds from the epoch.
inline base::TimeTicks MakeTime(int t) {
base::TimeTicks ticks; // initialized to 0.
ticks += base::TimeDelta::FromMilliseconds(t);
return ticks;
}
inline ::testing::AssertionResult LogContainsEventHelper(
const CapturingNetLog::EntryList& entries,
int i, // Negative indices are reverse indices.
const base::TimeTicks& expected_time,
bool check_time,
NetLog::EventType expected_event,
NetLog::EventPhase expected_phase) {
// Negative indices are reverse indices.
size_t j = (i < 0) ?
static_cast<size_t>(static_cast<int>(entries.size()) + i) :
static_cast<size_t>(i);
if (j >= entries.size())
return ::testing::AssertionFailure() << j << " is out of bounds.";
const CapturingNetLog::Entry& entry = entries[j];
if (expected_event != entry.type) {
return ::testing::AssertionFailure()
<< "Actual event: " << NetLog::EventTypeToString(entry.type)
<< ". Expected event: " << NetLog::EventTypeToString(expected_event)
<< ".";
}
if (expected_phase != entry.phase) {
return ::testing::AssertionFailure()
<< "Actual phase: " << entry.phase
<< ". Expected phase: " << expected_phase << ".";
}
if (check_time) {
if (expected_time != entry.time) {
return ::testing::AssertionFailure()
<< "Actual time: " << entry.time.ToInternalValue()
<< ". Expected time: " << expected_time.ToInternalValue()
<< ".";
}
}
return ::testing::AssertionSuccess();
}
inline ::testing::AssertionResult LogContainsEventAtTime(
const CapturingNetLog::EntryList& log,
int i, // Negative indices are reverse indices.
const base::TimeTicks& expected_time,
NetLog::EventType expected_event,
NetLog::EventPhase expected_phase) {
return LogContainsEventHelper(log, i, expected_time, true,
expected_event, expected_phase);
}
// Version without timestamp.
inline ::testing::AssertionResult LogContainsEvent(
const CapturingNetLog::EntryList& log,
int i, // Negative indices are reverse indices.
NetLog::EventType expected_event,
NetLog::EventPhase expected_phase) {
return LogContainsEventHelper(log, i, base::TimeTicks(), false,
expected_event, expected_phase);
}
// Version for PHASE_BEGIN (and no timestamp).
inline ::testing::AssertionResult LogContainsBeginEvent(
const CapturingNetLog::EntryList& log,
int i, // Negative indices are reverse indices.
NetLog::EventType expected_event) {
return LogContainsEvent(log, i, expected_event, NetLog::PHASE_BEGIN);
}
// Version for PHASE_END (and no timestamp).
inline ::testing::AssertionResult LogContainsEndEvent(
const CapturingNetLog::EntryList& log,
int i, // Negative indices are reverse indices.
NetLog::EventType expected_event) {
return LogContainsEvent(log, i, expected_event, NetLog::PHASE_END);
}
inline ::testing::AssertionResult LogContainsEntryWithType(
const CapturingNetLog::EntryList& entries,
int i, // Negative indices are reverse indices.
NetLog::EventType type) {
// Negative indices are reverse indices.
size_t j = (i < 0) ?
static_cast<size_t>(static_cast<int>(entries.size()) + i) :
static_cast<size_t>(i);
if (j >= entries.size())
return ::testing::AssertionFailure() << j << " is out of bounds.";
const CapturingNetLog::Entry& entry = entries[j];
if (entry.type != type)
return ::testing::AssertionFailure() << "Type does not match.";
return ::testing::AssertionSuccess();
}
// Expect that the log contains an event, but don't care about where
// as long as the first index where it is found is at least |min_index|.
// Returns the position where the event was found.
inline size_t ExpectLogContainsSomewhere(
const CapturingNetLog::EntryList& entries,
size_t min_index,
NetLog::EventType expected_event,
NetLog::EventPhase expected_phase) {
size_t i = 0;
for (; i < entries.size(); ++i) {
const CapturingNetLog::Entry& entry = entries[i];
if (entry.type == expected_event &&
entry.phase == expected_phase)
break;
}
EXPECT_LT(i, entries.size());
EXPECT_GE(i, min_index);
return i;
}
// Expect that the log contains an event, but don't care about where
// as long as one index where it is found is at least |min_index|.
// Returns the first such position where the event was found.
inline size_t ExpectLogContainsSomewhereAfter(
const CapturingNetLog::EntryList& entries,
size_t min_index,
NetLog::EventType expected_event,
NetLog::EventPhase expected_phase) {
size_t i = min_index;
for (; i < entries.size(); ++i) {
const CapturingNetLog::Entry& entry = entries[i];
if (entry.type == expected_event &&
entry.phase == expected_phase)
break;
}
EXPECT_LT(i, entries.size());
return i;
}
} // namespace net
#endif // NET_BASE_NET_LOG_UNITTEST_H_
|