OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_LOG_NET_LOG_UNITTEST_H_ | 5 #ifndef NET_LOG_NET_LOG_UNITTEST_H_ |
6 #define NET_LOG_NET_LOG_UNITTEST_H_ | 6 #define NET_LOG_NET_LOG_UNITTEST_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 | 9 |
10 #include "net/log/captured_net_log_entry.h" | |
10 #include "net/log/test_net_log.h" | 11 #include "net/log/test_net_log.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace net { | 14 namespace net { |
14 | 15 |
15 // Create a timestamp with internal value of |t| milliseconds from the epoch. | 16 // Create a timestamp with internal value of |t| milliseconds from the epoch. |
16 inline base::TimeTicks MakeTime(int t) { | 17 inline base::TimeTicks MakeTime(int t) { |
17 base::TimeTicks ticks; // initialized to 0. | 18 base::TimeTicks ticks; // initialized to 0. |
18 ticks += base::TimeDelta::FromMilliseconds(t); | 19 ticks += base::TimeDelta::FromMilliseconds(t); |
19 return ticks; | 20 return ticks; |
20 } | 21 } |
21 | 22 |
22 inline ::testing::AssertionResult LogContainsEventHelper( | 23 inline ::testing::AssertionResult LogContainsEventHelper( |
mmenke
2015/04/24 22:31:39
Think I'll de-inline these in a followup CL.
| |
23 const TestNetLog::CapturedEntryList& entries, | 24 const CapturedNetLogEntry::List& entries, |
24 int i, // Negative indices are reverse indices. | 25 int i, // Negative indices are reverse indices. |
25 const base::TimeTicks& expected_time, | 26 const base::TimeTicks& expected_time, |
26 bool check_time, | 27 bool check_time, |
27 NetLog::EventType expected_event, | 28 NetLog::EventType expected_event, |
28 NetLog::EventPhase expected_phase) { | 29 NetLog::EventPhase expected_phase) { |
29 // Negative indices are reverse indices. | 30 // Negative indices are reverse indices. |
30 size_t j = (i < 0) ? static_cast<size_t>(static_cast<int>(entries.size()) + i) | 31 size_t j = (i < 0) ? static_cast<size_t>(static_cast<int>(entries.size()) + i) |
31 : static_cast<size_t>(i); | 32 : static_cast<size_t>(i); |
32 if (j >= entries.size()) | 33 if (j >= entries.size()) |
33 return ::testing::AssertionFailure() << j << " is out of bounds."; | 34 return ::testing::AssertionFailure() << j << " is out of bounds."; |
34 const TestNetLog::CapturedEntry& entry = entries[j]; | 35 const CapturedNetLogEntry& entry = entries[j]; |
35 if (expected_event != entry.type) { | 36 if (expected_event != entry.type) { |
36 return ::testing::AssertionFailure() | 37 return ::testing::AssertionFailure() |
37 << "Actual event: " << NetLog::EventTypeToString(entry.type) | 38 << "Actual event: " << NetLog::EventTypeToString(entry.type) |
38 << ". Expected event: " << NetLog::EventTypeToString(expected_event) | 39 << ". Expected event: " << NetLog::EventTypeToString(expected_event) |
39 << "."; | 40 << "."; |
40 } | 41 } |
41 if (expected_phase != entry.phase) { | 42 if (expected_phase != entry.phase) { |
42 return ::testing::AssertionFailure() | 43 return ::testing::AssertionFailure() |
43 << "Actual phase: " << entry.phase | 44 << "Actual phase: " << entry.phase |
44 << ". Expected phase: " << expected_phase << "."; | 45 << ". Expected phase: " << expected_phase << "."; |
45 } | 46 } |
46 if (check_time) { | 47 if (check_time) { |
47 if (expected_time != entry.time) { | 48 if (expected_time != entry.time) { |
48 return ::testing::AssertionFailure() | 49 return ::testing::AssertionFailure() |
49 << "Actual time: " << entry.time.ToInternalValue() | 50 << "Actual time: " << entry.time.ToInternalValue() |
50 << ". Expected time: " << expected_time.ToInternalValue() << "."; | 51 << ". Expected time: " << expected_time.ToInternalValue() << "."; |
51 } | 52 } |
52 } | 53 } |
53 return ::testing::AssertionSuccess(); | 54 return ::testing::AssertionSuccess(); |
54 } | 55 } |
55 | 56 |
56 inline ::testing::AssertionResult LogContainsEventAtTime( | 57 inline ::testing::AssertionResult LogContainsEventAtTime( |
57 const TestNetLog::CapturedEntryList& log, | 58 const CapturedNetLogEntry::List& log, |
58 int i, // Negative indices are reverse indices. | 59 int i, // Negative indices are reverse indices. |
59 const base::TimeTicks& expected_time, | 60 const base::TimeTicks& expected_time, |
60 NetLog::EventType expected_event, | 61 NetLog::EventType expected_event, |
61 NetLog::EventPhase expected_phase) { | 62 NetLog::EventPhase expected_phase) { |
62 return LogContainsEventHelper(log, i, expected_time, true, expected_event, | 63 return LogContainsEventHelper(log, i, expected_time, true, expected_event, |
63 expected_phase); | 64 expected_phase); |
64 } | 65 } |
65 | 66 |
66 // Version without timestamp. | 67 // Version without timestamp. |
67 inline ::testing::AssertionResult LogContainsEvent( | 68 inline ::testing::AssertionResult LogContainsEvent( |
68 const TestNetLog::CapturedEntryList& log, | 69 const CapturedNetLogEntry::List& log, |
69 int i, // Negative indices are reverse indices. | 70 int i, // Negative indices are reverse indices. |
70 NetLog::EventType expected_event, | 71 NetLog::EventType expected_event, |
71 NetLog::EventPhase expected_phase) { | 72 NetLog::EventPhase expected_phase) { |
72 return LogContainsEventHelper(log, i, base::TimeTicks(), false, | 73 return LogContainsEventHelper(log, i, base::TimeTicks(), false, |
73 expected_event, expected_phase); | 74 expected_event, expected_phase); |
74 } | 75 } |
75 | 76 |
76 // Version for PHASE_BEGIN (and no timestamp). | 77 // Version for PHASE_BEGIN (and no timestamp). |
77 inline ::testing::AssertionResult LogContainsBeginEvent( | 78 inline ::testing::AssertionResult LogContainsBeginEvent( |
78 const TestNetLog::CapturedEntryList& log, | 79 const CapturedNetLogEntry::List& log, |
79 int i, // Negative indices are reverse indices. | 80 int i, // Negative indices are reverse indices. |
80 NetLog::EventType expected_event) { | 81 NetLog::EventType expected_event) { |
81 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_BEGIN); | 82 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_BEGIN); |
82 } | 83 } |
83 | 84 |
84 // Version for PHASE_END (and no timestamp). | 85 // Version for PHASE_END (and no timestamp). |
85 inline ::testing::AssertionResult LogContainsEndEvent( | 86 inline ::testing::AssertionResult LogContainsEndEvent( |
86 const TestNetLog::CapturedEntryList& log, | 87 const CapturedNetLogEntry::List& log, |
87 int i, // Negative indices are reverse indices. | 88 int i, // Negative indices are reverse indices. |
88 NetLog::EventType expected_event) { | 89 NetLog::EventType expected_event) { |
89 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_END); | 90 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_END); |
90 } | 91 } |
91 | 92 |
92 inline ::testing::AssertionResult LogContainsEntryWithType( | 93 inline ::testing::AssertionResult LogContainsEntryWithType( |
93 const TestNetLog::CapturedEntryList& entries, | 94 const CapturedNetLogEntry::List& entries, |
94 int i, // Negative indices are reverse indices. | 95 int i, // Negative indices are reverse indices. |
95 NetLog::EventType type) { | 96 NetLog::EventType type) { |
96 // Negative indices are reverse indices. | 97 // Negative indices are reverse indices. |
97 size_t j = (i < 0) ? static_cast<size_t>(static_cast<int>(entries.size()) + i) | 98 size_t j = (i < 0) ? static_cast<size_t>(static_cast<int>(entries.size()) + i) |
98 : static_cast<size_t>(i); | 99 : static_cast<size_t>(i); |
99 if (j >= entries.size()) | 100 if (j >= entries.size()) |
100 return ::testing::AssertionFailure() << j << " is out of bounds."; | 101 return ::testing::AssertionFailure() << j << " is out of bounds."; |
101 const TestNetLog::CapturedEntry& entry = entries[j]; | 102 const CapturedNetLogEntry& entry = entries[j]; |
102 if (entry.type != type) | 103 if (entry.type != type) |
103 return ::testing::AssertionFailure() << "Type does not match."; | 104 return ::testing::AssertionFailure() << "Type does not match."; |
104 return ::testing::AssertionSuccess(); | 105 return ::testing::AssertionSuccess(); |
105 } | 106 } |
106 | 107 |
107 // Check if the log contains any entry of the given type at |min_index| or | 108 // Check if the log contains any entry of the given type at |min_index| or |
108 // after. | 109 // after. |
109 inline ::testing::AssertionResult LogContainsEntryWithTypeAfter( | 110 inline ::testing::AssertionResult LogContainsEntryWithTypeAfter( |
110 const TestNetLog::CapturedEntryList& entries, | 111 const CapturedNetLogEntry::List& entries, |
111 int min_index, // Negative indices are reverse indices. | 112 int min_index, // Negative indices are reverse indices. |
112 NetLog::EventType type) { | 113 NetLog::EventType type) { |
113 // Negative indices are reverse indices. | 114 // Negative indices are reverse indices. |
114 size_t real_index = | 115 size_t real_index = |
115 (min_index < 0) | 116 (min_index < 0) |
116 ? static_cast<size_t>(static_cast<int>(entries.size()) + min_index) | 117 ? static_cast<size_t>(static_cast<int>(entries.size()) + min_index) |
117 : static_cast<size_t>(min_index); | 118 : static_cast<size_t>(min_index); |
118 for (size_t i = real_index; i < entries.size(); ++i) { | 119 for (size_t i = real_index; i < entries.size(); ++i) { |
119 const TestNetLog::CapturedEntry& entry = entries[i]; | 120 const CapturedNetLogEntry& entry = entries[i]; |
120 if (entry.type == type) | 121 if (entry.type == type) |
121 return ::testing::AssertionSuccess(); | 122 return ::testing::AssertionSuccess(); |
122 } | 123 } |
123 return ::testing::AssertionFailure(); | 124 return ::testing::AssertionFailure(); |
124 } | 125 } |
125 | 126 |
126 // Expect that the log contains an event, but don't care about where | 127 // Expect that the log contains an event, but don't care about where |
127 // as long as the first index where it is found is at least |min_index|. | 128 // as long as the first index where it is found is at least |min_index|. |
128 // Returns the position where the event was found. | 129 // Returns the position where the event was found. |
129 inline size_t ExpectLogContainsSomewhere( | 130 inline size_t ExpectLogContainsSomewhere( |
130 const TestNetLog::CapturedEntryList& entries, | 131 const CapturedNetLogEntry::List& entries, |
131 size_t min_index, | 132 size_t min_index, |
132 NetLog::EventType expected_event, | 133 NetLog::EventType expected_event, |
133 NetLog::EventPhase expected_phase) { | 134 NetLog::EventPhase expected_phase) { |
134 size_t i = 0; | 135 size_t i = 0; |
135 for (; i < entries.size(); ++i) { | 136 for (; i < entries.size(); ++i) { |
136 const TestNetLog::CapturedEntry& entry = entries[i]; | 137 const CapturedNetLogEntry& entry = entries[i]; |
137 if (entry.type == expected_event && entry.phase == expected_phase) | 138 if (entry.type == expected_event && entry.phase == expected_phase) |
138 break; | 139 break; |
139 } | 140 } |
140 EXPECT_LT(i, entries.size()); | 141 EXPECT_LT(i, entries.size()); |
141 EXPECT_GE(i, min_index); | 142 EXPECT_GE(i, min_index); |
142 return i; | 143 return i; |
143 } | 144 } |
144 | 145 |
145 // Expect that the log contains an event, but don't care about where | 146 // Expect that the log contains an event, but don't care about where |
146 // as long as one index where it is found is at least |min_index|. | 147 // as long as one index where it is found is at least |min_index|. |
147 // Returns the first such position where the event was found. | 148 // Returns the first such position where the event was found. |
148 inline size_t ExpectLogContainsSomewhereAfter( | 149 inline size_t ExpectLogContainsSomewhereAfter( |
149 const TestNetLog::CapturedEntryList& entries, | 150 const CapturedNetLogEntry::List& entries, |
150 size_t min_index, | 151 size_t min_index, |
151 NetLog::EventType expected_event, | 152 NetLog::EventType expected_event, |
152 NetLog::EventPhase expected_phase) { | 153 NetLog::EventPhase expected_phase) { |
153 size_t i = min_index; | 154 size_t i = min_index; |
154 for (; i < entries.size(); ++i) { | 155 for (; i < entries.size(); ++i) { |
155 const TestNetLog::CapturedEntry& entry = entries[i]; | 156 const CapturedNetLogEntry& entry = entries[i]; |
156 if (entry.type == expected_event && entry.phase == expected_phase) | 157 if (entry.type == expected_event && entry.phase == expected_phase) |
157 break; | 158 break; |
158 } | 159 } |
159 EXPECT_LT(i, entries.size()); | 160 EXPECT_LT(i, entries.size()); |
160 return i; | 161 return i; |
161 } | 162 } |
162 | 163 |
163 } // namespace net | 164 } // namespace net |
164 | 165 |
165 #endif // NET_LOG_NET_LOG_UNITTEST_H_ | 166 #endif // NET_LOG_NET_LOG_UNITTEST_H_ |
OLD | NEW |