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
3 // found in the LICENSE file.
5 #include "net/log/test_net_log_util.h"
13 // Takes the list of entries and an offset, and returns an index into the array.
14 // If |offset| is positive, just returns |offset|. If it's negative, it
15 // indicates a position relative to the end of the array.
16 size_t GetIndex(const TestNetLogEntry::List
& entries
, int offset
) {
18 return static_cast<size_t>(offset
);
20 size_t abs_offset
= static_cast<size_t>(-offset
);
21 // If offset indicates a position before the start of the array, just return
22 // the end of the list.
23 if (abs_offset
> entries
.size())
24 return entries
.size();
25 return entries
.size() - abs_offset
;
30 ::testing::AssertionResult
LogContainsEvent(
31 const TestNetLogEntry::List
& entries
,
33 NetLog::EventType expected_event
,
34 NetLog::EventPhase expected_phase
) {
35 size_t index
= GetIndex(entries
, offset
);
36 if (index
>= entries
.size())
37 return ::testing::AssertionFailure() << index
<< " is out of bounds.";
38 const TestNetLogEntry
& entry
= entries
[index
];
39 if (expected_event
!= entry
.type
) {
40 return ::testing::AssertionFailure()
41 << "Actual event: " << NetLog::EventTypeToString(entry
.type
)
42 << ". Expected event: " << NetLog::EventTypeToString(expected_event
)
45 if (expected_phase
!= entry
.phase
) {
46 return ::testing::AssertionFailure()
47 << "Actual phase: " << entry
.phase
48 << ". Expected phase: " << expected_phase
<< ".";
50 return ::testing::AssertionSuccess();
53 ::testing::AssertionResult
LogContainsBeginEvent(
54 const TestNetLogEntry::List
& entries
,
56 NetLog::EventType expected_event
) {
57 return LogContainsEvent(entries
, offset
, expected_event
, NetLog::PHASE_BEGIN
);
60 ::testing::AssertionResult
LogContainsEndEvent(
61 const TestNetLogEntry::List
& entries
,
63 NetLog::EventType expected_event
) {
64 return LogContainsEvent(entries
, offset
, expected_event
, NetLog::PHASE_END
);
67 ::testing::AssertionResult
LogContainsEntryWithType(
68 const TestNetLogEntry::List
& entries
,
70 NetLog::EventType type
) {
71 size_t index
= GetIndex(entries
, offset
);
72 if (index
>= entries
.size())
73 return ::testing::AssertionFailure() << index
<< " is out of bounds.";
74 const TestNetLogEntry
& entry
= entries
[index
];
75 if (entry
.type
!= type
)
76 return ::testing::AssertionFailure() << "Type does not match.";
77 return ::testing::AssertionSuccess();
80 ::testing::AssertionResult
LogContainsEntryWithTypeAfter(
81 const TestNetLogEntry::List
& entries
,
83 NetLog::EventType type
) {
84 for (size_t i
= GetIndex(entries
, start_offset
); i
< entries
.size(); ++i
) {
85 const TestNetLogEntry
& entry
= entries
[i
];
86 if (entry
.type
== type
)
87 return ::testing::AssertionSuccess();
89 return ::testing::AssertionFailure();
92 size_t ExpectLogContainsSomewhere(const TestNetLogEntry::List
& entries
,
94 NetLog::EventType expected_event
,
95 NetLog::EventPhase expected_phase
) {
96 size_t min_index
= GetIndex(entries
, min_offset
);
98 for (; i
< entries
.size(); ++i
) {
99 const TestNetLogEntry
& entry
= entries
[i
];
100 if (entry
.type
== expected_event
&& entry
.phase
== expected_phase
)
103 EXPECT_LT(i
, entries
.size());
104 EXPECT_GE(i
, min_index
);
108 size_t ExpectLogContainsSomewhereAfter(const TestNetLogEntry::List
& entries
,
110 NetLog::EventType expected_event
,
111 NetLog::EventPhase expected_phase
) {
112 size_t i
= GetIndex(entries
, start_offset
);
113 for (; i
< entries
.size(); ++i
) {
114 const TestNetLogEntry
& entry
= entries
[i
];
115 if (entry
.type
== expected_event
&& entry
.phase
== expected_phase
)
118 EXPECT_LT(i
, entries
.size());