Roll src/third_party/skia 4cf9e7e:8ee06f2
[chromium-blink-merge.git] / net / log / test_net_log_util.cc
blob66e482176555a62f5516570664d6308f8230587b
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"
7 #include <cstddef>
9 namespace net {
11 namespace {
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) {
17 if (offset >= 0)
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;
28 } // namespace
30 ::testing::AssertionResult LogContainsEvent(
31 const TestNetLogEntry::List& entries,
32 int offset,
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)
43 << ".";
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,
55 int offset,
56 NetLog::EventType expected_event) {
57 return LogContainsEvent(entries, offset, expected_event, NetLog::PHASE_BEGIN);
60 ::testing::AssertionResult LogContainsEndEvent(
61 const TestNetLogEntry::List& entries,
62 int offset,
63 NetLog::EventType expected_event) {
64 return LogContainsEvent(entries, offset, expected_event, NetLog::PHASE_END);
67 ::testing::AssertionResult LogContainsEntryWithType(
68 const TestNetLogEntry::List& entries,
69 int offset,
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,
82 int start_offset,
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,
93 size_t min_offset,
94 NetLog::EventType expected_event,
95 NetLog::EventPhase expected_phase) {
96 size_t min_index = GetIndex(entries, min_offset);
97 size_t i = 0;
98 for (; i < entries.size(); ++i) {
99 const TestNetLogEntry& entry = entries[i];
100 if (entry.type == expected_event && entry.phase == expected_phase)
101 break;
103 EXPECT_LT(i, entries.size());
104 EXPECT_GE(i, min_index);
105 return i;
108 size_t ExpectLogContainsSomewhereAfter(const TestNetLogEntry::List& entries,
109 size_t start_offset,
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)
116 break;
118 EXPECT_LT(i, entries.size());
119 return i;
122 } // namespace net