Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: net/base/net_log_unittest.h

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_BASE_NET_LOG_UNITTEST_H_ 5 #ifndef NET_BASE_NET_LOG_UNITTEST_H_
6 #define NET_BASE_NET_LOG_UNITTEST_H_ 6 #define NET_BASE_NET_LOG_UNITTEST_H_
7 7
8 #include <cstddef> 8 #include <cstddef>
9 9
10 #include "net/base/capturing_net_log.h" 10 #include "net/base/capturing_net_log.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace net { 13 namespace net {
14 14
15 // Create a timestamp with internal value of |t| milliseconds from the epoch. 15 // Create a timestamp with internal value of |t| milliseconds from the epoch.
16 inline base::TimeTicks MakeTime(int t) { 16 inline base::TimeTicks MakeTime(int t) {
17 base::TimeTicks ticks; // initialized to 0. 17 base::TimeTicks ticks; // initialized to 0.
18 ticks += base::TimeDelta::FromMilliseconds(t); 18 ticks += base::TimeDelta::FromMilliseconds(t);
19 return ticks; 19 return ticks;
20 } 20 }
21 21
22 inline ::testing::AssertionResult LogContainsEventHelper( 22 inline ::testing::AssertionResult LogContainsEventHelper(
23 const CapturingNetLog::CapturedEntryList& entries, 23 const CapturingNetLog::CapturedEntryList& entries,
24 int i, // Negative indices are reverse indices. 24 int i, // Negative indices are reverse indices.
25 const base::TimeTicks& expected_time, 25 const base::TimeTicks& expected_time,
26 bool check_time, 26 bool check_time,
27 NetLog::EventType expected_event, 27 NetLog::EventType expected_event,
28 NetLog::EventPhase expected_phase) { 28 NetLog::EventPhase expected_phase) {
29 // Negative indices are reverse indices. 29 // Negative indices are reverse indices.
30 size_t j = (i < 0) ? 30 size_t j = (i < 0) ? static_cast<size_t>(static_cast<int>(entries.size()) + i)
31 static_cast<size_t>(static_cast<int>(entries.size()) + i) : 31 : static_cast<size_t>(i);
32 static_cast<size_t>(i);
33 if (j >= entries.size()) 32 if (j >= entries.size())
34 return ::testing::AssertionFailure() << j << " is out of bounds."; 33 return ::testing::AssertionFailure() << j << " is out of bounds.";
35 const CapturingNetLog::CapturedEntry& entry = entries[j]; 34 const CapturingNetLog::CapturedEntry& entry = entries[j];
36 if (expected_event != entry.type) { 35 if (expected_event != entry.type) {
37 return ::testing::AssertionFailure() 36 return ::testing::AssertionFailure()
38 << "Actual event: " << NetLog::EventTypeToString(entry.type) 37 << "Actual event: " << NetLog::EventTypeToString(entry.type)
39 << ". Expected event: " << NetLog::EventTypeToString(expected_event) 38 << ". Expected event: " << NetLog::EventTypeToString(expected_event)
40 << "."; 39 << ".";
41 } 40 }
42 if (expected_phase != entry.phase) { 41 if (expected_phase != entry.phase) {
43 return ::testing::AssertionFailure() 42 return ::testing::AssertionFailure()
44 << "Actual phase: " << entry.phase 43 << "Actual phase: " << entry.phase
45 << ". Expected phase: " << expected_phase << "."; 44 << ". Expected phase: " << expected_phase << ".";
46 } 45 }
47 if (check_time) { 46 if (check_time) {
48 if (expected_time != entry.time) { 47 if (expected_time != entry.time) {
49 return ::testing::AssertionFailure() 48 return ::testing::AssertionFailure()
50 << "Actual time: " << entry.time.ToInternalValue() 49 << "Actual time: " << entry.time.ToInternalValue()
51 << ". Expected time: " << expected_time.ToInternalValue() 50 << ". Expected time: " << expected_time.ToInternalValue() << ".";
52 << ".";
53 } 51 }
54 } 52 }
55 return ::testing::AssertionSuccess(); 53 return ::testing::AssertionSuccess();
56 } 54 }
57 55
58 inline ::testing::AssertionResult LogContainsEventAtTime( 56 inline ::testing::AssertionResult LogContainsEventAtTime(
59 const CapturingNetLog::CapturedEntryList& log, 57 const CapturingNetLog::CapturedEntryList& log,
60 int i, // Negative indices are reverse indices. 58 int i, // Negative indices are reverse indices.
61 const base::TimeTicks& expected_time, 59 const base::TimeTicks& expected_time,
62 NetLog::EventType expected_event, 60 NetLog::EventType expected_event,
63 NetLog::EventPhase expected_phase) { 61 NetLog::EventPhase expected_phase) {
64 return LogContainsEventHelper(log, i, expected_time, true, 62 return LogContainsEventHelper(
65 expected_event, expected_phase); 63 log, i, expected_time, true, expected_event, expected_phase);
66 } 64 }
67 65
68 // Version without timestamp. 66 // Version without timestamp.
69 inline ::testing::AssertionResult LogContainsEvent( 67 inline ::testing::AssertionResult LogContainsEvent(
70 const CapturingNetLog::CapturedEntryList& log, 68 const CapturingNetLog::CapturedEntryList& log,
71 int i, // Negative indices are reverse indices. 69 int i, // Negative indices are reverse indices.
72 NetLog::EventType expected_event, 70 NetLog::EventType expected_event,
73 NetLog::EventPhase expected_phase) { 71 NetLog::EventPhase expected_phase) {
74 return LogContainsEventHelper(log, i, base::TimeTicks(), false, 72 return LogContainsEventHelper(
75 expected_event, expected_phase); 73 log, i, base::TimeTicks(), false, expected_event, expected_phase);
76 } 74 }
77 75
78 // Version for PHASE_BEGIN (and no timestamp). 76 // Version for PHASE_BEGIN (and no timestamp).
79 inline ::testing::AssertionResult LogContainsBeginEvent( 77 inline ::testing::AssertionResult LogContainsBeginEvent(
80 const CapturingNetLog::CapturedEntryList& log, 78 const CapturingNetLog::CapturedEntryList& log,
81 int i, // Negative indices are reverse indices. 79 int i, // Negative indices are reverse indices.
82 NetLog::EventType expected_event) { 80 NetLog::EventType expected_event) {
83 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_BEGIN); 81 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_BEGIN);
84 } 82 }
85 83
86 // Version for PHASE_END (and no timestamp). 84 // Version for PHASE_END (and no timestamp).
87 inline ::testing::AssertionResult LogContainsEndEvent( 85 inline ::testing::AssertionResult LogContainsEndEvent(
88 const CapturingNetLog::CapturedEntryList& log, 86 const CapturingNetLog::CapturedEntryList& log,
89 int i, // Negative indices are reverse indices. 87 int i, // Negative indices are reverse indices.
90 NetLog::EventType expected_event) { 88 NetLog::EventType expected_event) {
91 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_END); 89 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_END);
92 } 90 }
93 91
94 inline ::testing::AssertionResult LogContainsEntryWithType( 92 inline ::testing::AssertionResult LogContainsEntryWithType(
95 const CapturingNetLog::CapturedEntryList& entries, 93 const CapturingNetLog::CapturedEntryList& entries,
96 int i, // Negative indices are reverse indices. 94 int i, // Negative indices are reverse indices.
97 NetLog::EventType type) { 95 NetLog::EventType type) {
98 // Negative indices are reverse indices. 96 // Negative indices are reverse indices.
99 size_t j = (i < 0) ? 97 size_t j = (i < 0) ? static_cast<size_t>(static_cast<int>(entries.size()) + i)
100 static_cast<size_t>(static_cast<int>(entries.size()) + i) : 98 : static_cast<size_t>(i);
101 static_cast<size_t>(i);
102 if (j >= entries.size()) 99 if (j >= entries.size())
103 return ::testing::AssertionFailure() << j << " is out of bounds."; 100 return ::testing::AssertionFailure() << j << " is out of bounds.";
104 const CapturingNetLog::CapturedEntry& entry = entries[j]; 101 const CapturingNetLog::CapturedEntry& entry = entries[j];
105 if (entry.type != type) 102 if (entry.type != type)
106 return ::testing::AssertionFailure() << "Type does not match."; 103 return ::testing::AssertionFailure() << "Type does not match.";
107 return ::testing::AssertionSuccess(); 104 return ::testing::AssertionSuccess();
108 } 105 }
109 106
110 // Check if the log contains any entry of the given type at |min_index| or 107 // Check if the log contains any entry of the given type at |min_index| or
111 // after. 108 // after.
112 inline ::testing::AssertionResult LogContainsEntryWithTypeAfter( 109 inline ::testing::AssertionResult LogContainsEntryWithTypeAfter(
113 const CapturingNetLog::CapturedEntryList& entries, 110 const CapturingNetLog::CapturedEntryList& entries,
114 int min_index, // Negative indices are reverse indices. 111 int min_index, // Negative indices are reverse indices.
115 NetLog::EventType type) { 112 NetLog::EventType type) {
116 // Negative indices are reverse indices. 113 // Negative indices are reverse indices.
117 size_t real_index = (min_index < 0) ? 114 size_t real_index =
118 static_cast<size_t>(static_cast<int>(entries.size()) + min_index) : 115 (min_index < 0)
119 static_cast<size_t>(min_index); 116 ? static_cast<size_t>(static_cast<int>(entries.size()) + min_index)
117 : static_cast<size_t>(min_index);
120 for (size_t i = real_index; i < entries.size(); ++i) { 118 for (size_t i = real_index; i < entries.size(); ++i) {
121 const CapturingNetLog::CapturedEntry& entry = entries[i]; 119 const CapturingNetLog::CapturedEntry& entry = entries[i];
122 if (entry.type == type) 120 if (entry.type == type)
123 return ::testing::AssertionSuccess(); 121 return ::testing::AssertionSuccess();
124 } 122 }
125 return ::testing::AssertionFailure(); 123 return ::testing::AssertionFailure();
126 } 124 }
127 125
128 // Expect that the log contains an event, but don't care about where 126 // Expect that the log contains an event, but don't care about where
129 // as long as the first index where it is found is at least |min_index|. 127 // as long as the first index where it is found is at least |min_index|.
130 // Returns the position where the event was found. 128 // Returns the position where the event was found.
131 inline size_t ExpectLogContainsSomewhere( 129 inline size_t ExpectLogContainsSomewhere(
132 const CapturingNetLog::CapturedEntryList& entries, 130 const CapturingNetLog::CapturedEntryList& entries,
133 size_t min_index, 131 size_t min_index,
134 NetLog::EventType expected_event, 132 NetLog::EventType expected_event,
135 NetLog::EventPhase expected_phase) { 133 NetLog::EventPhase expected_phase) {
136 size_t i = 0; 134 size_t i = 0;
137 for (; i < entries.size(); ++i) { 135 for (; i < entries.size(); ++i) {
138 const CapturingNetLog::CapturedEntry& entry = entries[i]; 136 const CapturingNetLog::CapturedEntry& entry = entries[i];
139 if (entry.type == expected_event && 137 if (entry.type == expected_event && entry.phase == expected_phase)
140 entry.phase == expected_phase)
141 break; 138 break;
142 } 139 }
143 EXPECT_LT(i, entries.size()); 140 EXPECT_LT(i, entries.size());
144 EXPECT_GE(i, min_index); 141 EXPECT_GE(i, min_index);
145 return i; 142 return i;
146 } 143 }
147 144
148 // Expect that the log contains an event, but don't care about where 145 // Expect that the log contains an event, but don't care about where
149 // as long as one index where it is found is at least |min_index|. 146 // as long as one index where it is found is at least |min_index|.
150 // Returns the first such position where the event was found. 147 // Returns the first such position where the event was found.
151 inline size_t ExpectLogContainsSomewhereAfter( 148 inline size_t ExpectLogContainsSomewhereAfter(
152 const CapturingNetLog::CapturedEntryList& entries, 149 const CapturingNetLog::CapturedEntryList& entries,
153 size_t min_index, 150 size_t min_index,
154 NetLog::EventType expected_event, 151 NetLog::EventType expected_event,
155 NetLog::EventPhase expected_phase) { 152 NetLog::EventPhase expected_phase) {
156 size_t i = min_index; 153 size_t i = min_index;
157 for (; i < entries.size(); ++i) { 154 for (; i < entries.size(); ++i) {
158 const CapturingNetLog::CapturedEntry& entry = entries[i]; 155 const CapturingNetLog::CapturedEntry& entry = entries[i];
159 if (entry.type == expected_event && 156 if (entry.type == expected_event && entry.phase == expected_phase)
160 entry.phase == expected_phase)
161 break; 157 break;
162 } 158 }
163 EXPECT_LT(i, entries.size()); 159 EXPECT_LT(i, entries.size());
164 return i; 160 return i;
165 } 161 }
166 162
167 } // namespace net 163 } // namespace net
168 164
169 #endif // NET_BASE_NET_LOG_UNITTEST_H_ 165 #endif // NET_BASE_NET_LOG_UNITTEST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698