Windows should animate when they are about to get docked at screen edges.
[chromium-blink-merge.git] / net / base / net_log_unittest.h
blob32fb5f82399a91d352cace88aec8ac2c79dccc25
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 #ifndef NET_BASE_NET_LOG_UNITTEST_H_
6 #define NET_BASE_NET_LOG_UNITTEST_H_
8 #include <cstddef>
10 #include "net/base/capturing_net_log.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace net {
15 // Create a timestamp with internal value of |t| milliseconds from the epoch.
16 inline base::TimeTicks MakeTime(int t) {
17 base::TimeTicks ticks; // initialized to 0.
18 ticks += base::TimeDelta::FromMilliseconds(t);
19 return ticks;
22 inline ::testing::AssertionResult LogContainsEventHelper(
23 const CapturingNetLog::CapturedEntryList& entries,
24 int i, // Negative indices are reverse indices.
25 const base::TimeTicks& expected_time,
26 bool check_time,
27 NetLog::EventType expected_event,
28 NetLog::EventPhase expected_phase) {
29 // Negative indices are reverse indices.
30 size_t j = (i < 0) ?
31 static_cast<size_t>(static_cast<int>(entries.size()) + i) :
32 static_cast<size_t>(i);
33 if (j >= entries.size())
34 return ::testing::AssertionFailure() << j << " is out of bounds.";
35 const CapturingNetLog::CapturedEntry& entry = entries[j];
36 if (expected_event != entry.type) {
37 return ::testing::AssertionFailure()
38 << "Actual event: " << NetLog::EventTypeToString(entry.type)
39 << ". Expected event: " << NetLog::EventTypeToString(expected_event)
40 << ".";
42 if (expected_phase != entry.phase) {
43 return ::testing::AssertionFailure()
44 << "Actual phase: " << entry.phase
45 << ". Expected phase: " << expected_phase << ".";
47 if (check_time) {
48 if (expected_time != entry.time) {
49 return ::testing::AssertionFailure()
50 << "Actual time: " << entry.time.ToInternalValue()
51 << ". Expected time: " << expected_time.ToInternalValue()
52 << ".";
55 return ::testing::AssertionSuccess();
58 inline ::testing::AssertionResult LogContainsEventAtTime(
59 const CapturingNetLog::CapturedEntryList& log,
60 int i, // Negative indices are reverse indices.
61 const base::TimeTicks& expected_time,
62 NetLog::EventType expected_event,
63 NetLog::EventPhase expected_phase) {
64 return LogContainsEventHelper(log, i, expected_time, true,
65 expected_event, expected_phase);
68 // Version without timestamp.
69 inline ::testing::AssertionResult LogContainsEvent(
70 const CapturingNetLog::CapturedEntryList& log,
71 int i, // Negative indices are reverse indices.
72 NetLog::EventType expected_event,
73 NetLog::EventPhase expected_phase) {
74 return LogContainsEventHelper(log, i, base::TimeTicks(), false,
75 expected_event, expected_phase);
78 // Version for PHASE_BEGIN (and no timestamp).
79 inline ::testing::AssertionResult LogContainsBeginEvent(
80 const CapturingNetLog::CapturedEntryList& log,
81 int i, // Negative indices are reverse indices.
82 NetLog::EventType expected_event) {
83 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_BEGIN);
86 // Version for PHASE_END (and no timestamp).
87 inline ::testing::AssertionResult LogContainsEndEvent(
88 const CapturingNetLog::CapturedEntryList& log,
89 int i, // Negative indices are reverse indices.
90 NetLog::EventType expected_event) {
91 return LogContainsEvent(log, i, expected_event, NetLog::PHASE_END);
94 inline ::testing::AssertionResult LogContainsEntryWithType(
95 const CapturingNetLog::CapturedEntryList& entries,
96 int i, // Negative indices are reverse indices.
97 NetLog::EventType type) {
98 // Negative indices are reverse indices.
99 size_t j = (i < 0) ?
100 static_cast<size_t>(static_cast<int>(entries.size()) + i) :
101 static_cast<size_t>(i);
102 if (j >= entries.size())
103 return ::testing::AssertionFailure() << j << " is out of bounds.";
104 const CapturingNetLog::CapturedEntry& entry = entries[j];
105 if (entry.type != type)
106 return ::testing::AssertionFailure() << "Type does not match.";
107 return ::testing::AssertionSuccess();
111 // Expect that the log contains an event, but don't care about where
112 // as long as the first index where it is found is at least |min_index|.
113 // Returns the position where the event was found.
114 inline size_t ExpectLogContainsSomewhere(
115 const CapturingNetLog::CapturedEntryList& entries,
116 size_t min_index,
117 NetLog::EventType expected_event,
118 NetLog::EventPhase expected_phase) {
119 size_t i = 0;
120 for (; i < entries.size(); ++i) {
121 const CapturingNetLog::CapturedEntry& entry = entries[i];
122 if (entry.type == expected_event &&
123 entry.phase == expected_phase)
124 break;
126 EXPECT_LT(i, entries.size());
127 EXPECT_GE(i, min_index);
128 return i;
131 // Expect that the log contains an event, but don't care about where
132 // as long as one index where it is found is at least |min_index|.
133 // Returns the first such position where the event was found.
134 inline size_t ExpectLogContainsSomewhereAfter(
135 const CapturingNetLog::CapturedEntryList& entries,
136 size_t min_index,
137 NetLog::EventType expected_event,
138 NetLog::EventPhase expected_phase) {
139 size_t i = min_index;
140 for (; i < entries.size(); ++i) {
141 const CapturingNetLog::CapturedEntry& entry = entries[i];
142 if (entry.type == expected_event &&
143 entry.phase == expected_phase)
144 break;
146 EXPECT_LT(i, entries.size());
147 return i;
150 } // namespace net
152 #endif // NET_BASE_NET_LOG_UNITTEST_H_