1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_BASE_NET_LOG_UNITTEST_H_
6 #define NET_BASE_NET_LOG_UNITTEST_H_
11 #include "net/base/capturing_net_log.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 // Create a timestamp with internal value of |t| milliseconds from the epoch.
17 inline base::TimeTicks
MakeTime(int t
) {
18 base::TimeTicks ticks
; // initialized to 0.
19 ticks
+= base::TimeDelta::FromMilliseconds(t
);
23 inline ::testing::AssertionResult
LogContainsEventHelper(
24 const CapturingNetLog::EntryList
& entries
,
25 int i
, // Negative indices are reverse indices.
26 const base::TimeTicks
& expected_time
,
28 NetLog::EventType expected_event
,
29 NetLog::EventPhase expected_phase
) {
30 // Negative indices are reverse indices.
32 static_cast<size_t>(static_cast<int>(entries
.size()) + i
) :
33 static_cast<size_t>(i
);
34 if (j
>= entries
.size())
35 return ::testing::AssertionFailure() << j
<< " is out of bounds.";
36 const CapturingNetLog::Entry
& entry
= entries
[j
];
37 if (expected_event
!= entry
.type
) {
38 return ::testing::AssertionFailure()
39 << "Actual event: " << NetLog::EventTypeToString(entry
.type
)
40 << ". Expected event: " << NetLog::EventTypeToString(expected_event
)
43 if (expected_phase
!= entry
.phase
) {
44 return ::testing::AssertionFailure()
45 << "Actual phase: " << entry
.phase
46 << ". Expected phase: " << expected_phase
<< ".";
49 if (expected_time
!= entry
.time
) {
50 return ::testing::AssertionFailure()
51 << "Actual time: " << entry
.time
.ToInternalValue()
52 << ". Expected time: " << expected_time
.ToInternalValue()
56 return ::testing::AssertionSuccess();
59 inline ::testing::AssertionResult
LogContainsEventAtTime(
60 const CapturingNetLog::EntryList
& log
,
61 int i
, // Negative indices are reverse indices.
62 const base::TimeTicks
& expected_time
,
63 NetLog::EventType expected_event
,
64 NetLog::EventPhase expected_phase
) {
65 return LogContainsEventHelper(log
, i
, expected_time
, true,
66 expected_event
, expected_phase
);
69 // Version without timestamp.
70 inline ::testing::AssertionResult
LogContainsEvent(
71 const CapturingNetLog::EntryList
& log
,
72 int i
, // Negative indices are reverse indices.
73 NetLog::EventType expected_event
,
74 NetLog::EventPhase expected_phase
) {
75 return LogContainsEventHelper(log
, i
, base::TimeTicks(), false,
76 expected_event
, expected_phase
);
79 // Version for PHASE_BEGIN (and no timestamp).
80 inline ::testing::AssertionResult
LogContainsBeginEvent(
81 const CapturingNetLog::EntryList
& log
,
82 int i
, // Negative indices are reverse indices.
83 NetLog::EventType expected_event
) {
84 return LogContainsEvent(log
, i
, expected_event
, NetLog::PHASE_BEGIN
);
87 // Version for PHASE_END (and no timestamp).
88 inline ::testing::AssertionResult
LogContainsEndEvent(
89 const CapturingNetLog::EntryList
& log
,
90 int i
, // Negative indices are reverse indices.
91 NetLog::EventType expected_event
) {
92 return LogContainsEvent(log
, i
, expected_event
, NetLog::PHASE_END
);
95 inline ::testing::AssertionResult
LogContainsEntryWithType(
96 const CapturingNetLog::EntryList
& entries
,
97 int i
, // Negative indices are reverse indices.
98 NetLog::EventType type
) {
99 // Negative indices are reverse indices.
101 static_cast<size_t>(static_cast<int>(entries
.size()) + i
) :
102 static_cast<size_t>(i
);
103 if (j
>= entries
.size())
104 return ::testing::AssertionFailure() << j
<< " is out of bounds.";
105 const CapturingNetLog::Entry
& entry
= entries
[j
];
106 if (entry
.type
!= type
)
107 return ::testing::AssertionFailure() << "Type does not match.";
108 return ::testing::AssertionSuccess();
112 // Expect that the log contains an event, but don't care about where
113 // as long as the first index where it is found is at least |min_index|.
114 // Returns the position where the event was found.
115 inline size_t ExpectLogContainsSomewhere(
116 const CapturingNetLog::EntryList
& entries
,
118 NetLog::EventType expected_event
,
119 NetLog::EventPhase expected_phase
) {
121 for (; i
< entries
.size(); ++i
) {
122 const CapturingNetLog::Entry
& entry
= entries
[i
];
123 if (entry
.type
== expected_event
&&
124 entry
.phase
== expected_phase
)
127 EXPECT_LT(i
, entries
.size());
128 EXPECT_GE(i
, min_index
);
132 // Expect that the log contains an event, but don't care about where
133 // as long as one index where it is found is at least |min_index|.
134 // Returns the first such position where the event was found.
135 inline size_t ExpectLogContainsSomewhereAfter(
136 const CapturingNetLog::EntryList
& entries
,
138 NetLog::EventType expected_event
,
139 NetLog::EventPhase expected_phase
) {
140 size_t i
= min_index
;
141 for (; i
< entries
.size(); ++i
) {
142 const CapturingNetLog::Entry
& entry
= entries
[i
];
143 if (entry
.type
== expected_event
&&
144 entry
.phase
== expected_phase
)
147 EXPECT_LT(i
, entries
.size());
153 #endif // NET_BASE_NET_LOG_UNITTEST_H_