| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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. | |
| 4 | |
| 5 #ifndef NET_BASE_LOAD_LOG_UNITTEST_H_ | |
| 6 #define NET_BASE_LOAD_LOG_UNITTEST_H_ | |
| 7 | |
| 8 #include <cstddef> | |
| 9 #include "net/base/load_log.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace net { | |
| 13 | |
| 14 // Create a timestamp with internal value of |t| milliseconds from the epoch. | |
| 15 inline base::TimeTicks MakeTime(int t) { | |
| 16 base::TimeTicks ticks; // initialized to 0. | |
| 17 ticks += base::TimeDelta::FromMilliseconds(t); | |
| 18 return ticks; | |
| 19 } | |
| 20 | |
| 21 inline ::testing::AssertionResult LogContainsEventHelper( | |
| 22 const LoadLog& log, | |
| 23 int i, // Negative indices are reverse indices. | |
| 24 const base::TimeTicks& expected_time, | |
| 25 bool check_time, | |
| 26 LoadLog::EventType expected_event, | |
| 27 LoadLog::EventPhase expected_phase) { | |
| 28 // Negative indices are reverse indices. | |
| 29 size_t j = (i < 0) ? log.entries().size() + i : i; | |
| 30 if (j >= log.entries().size()) | |
| 31 return ::testing::AssertionFailure() << j << " is out of bounds."; | |
| 32 const LoadLog::Entry& entry = log.entries()[j]; | |
| 33 if (entry.type != LoadLog::Entry::TYPE_EVENT) | |
| 34 return ::testing::AssertionFailure() << "Not a TYPE_EVENT entry"; | |
| 35 if (expected_event != entry.event.type) { | |
| 36 return ::testing::AssertionFailure() | |
| 37 << "Actual event: " << LoadLog::EventTypeToString(entry.event.type) | |
| 38 << ". Expected event: " << LoadLog::EventTypeToString(expected_event) | |
| 39 << "."; | |
| 40 } | |
| 41 if (expected_phase != entry.event.phase) { | |
| 42 return ::testing::AssertionFailure() | |
| 43 << "Actual phase: " << entry.event.phase | |
| 44 << ". Expected phase: " << expected_phase << "."; | |
| 45 } | |
| 46 if (check_time) { | |
| 47 if (expected_time != entry.time) { | |
| 48 return ::testing::AssertionFailure() | |
| 49 << "Actual time: " << entry.time.ToInternalValue() | |
| 50 << ". Expected time: " << expected_time.ToInternalValue() | |
| 51 << "."; | |
| 52 } | |
| 53 } | |
| 54 return ::testing::AssertionSuccess(); | |
| 55 } | |
| 56 | |
| 57 inline ::testing::AssertionResult LogContainsEventAtTime( | |
| 58 const LoadLog& log, | |
| 59 int i, // Negative indices are reverse indices. | |
| 60 const base::TimeTicks& expected_time, | |
| 61 LoadLog::EventType expected_event, | |
| 62 LoadLog::EventPhase expected_phase) { | |
| 63 return LogContainsEventHelper(log, i, expected_time, true, | |
| 64 expected_event, expected_phase); | |
| 65 } | |
| 66 | |
| 67 // Version without timestamp. | |
| 68 inline ::testing::AssertionResult LogContainsEvent( | |
| 69 const LoadLog& log, | |
| 70 int i, // Negative indices are reverse indices. | |
| 71 LoadLog::EventType expected_event, | |
| 72 LoadLog::EventPhase expected_phase) { | |
| 73 return LogContainsEventHelper(log, i, base::TimeTicks(), false, | |
| 74 expected_event, expected_phase); | |
| 75 } | |
| 76 | |
| 77 // Version for PHASE_BEGIN (and no timestamp). | |
| 78 inline ::testing::AssertionResult LogContainsBeginEvent( | |
| 79 const LoadLog& log, | |
| 80 int i, // Negative indices are reverse indices. | |
| 81 LoadLog::EventType expected_event) { | |
| 82 return LogContainsEvent(log, i, expected_event, LoadLog::PHASE_BEGIN); | |
| 83 } | |
| 84 | |
| 85 // Version for PHASE_END (and no timestamp). | |
| 86 inline ::testing::AssertionResult LogContainsEndEvent( | |
| 87 const LoadLog& log, | |
| 88 int i, // Negative indices are reverse indices. | |
| 89 LoadLog::EventType expected_event) { | |
| 90 return LogContainsEvent(log, i, expected_event, LoadLog::PHASE_END); | |
| 91 } | |
| 92 | |
| 93 inline ::testing::AssertionResult LogContainsEntryWithType( | |
| 94 const LoadLog& log, | |
| 95 int i, // Negative indices are reverse indices. | |
| 96 LoadLog::Entry::Type type) { | |
| 97 // Negative indices are reverse indices. | |
| 98 size_t j = (i < 0) ? log.entries().size() + i : i; | |
| 99 if (j >= log.entries().size()) | |
| 100 return ::testing::AssertionFailure() << j << " is out of bounds."; | |
| 101 const LoadLog::Entry& entry = log.entries()[j]; | |
| 102 if (entry.type != type) | |
| 103 return ::testing::AssertionFailure() << "Type does not match."; | |
| 104 return ::testing::AssertionSuccess(); | |
| 105 } | |
| 106 | |
| 107 | |
| 108 // Expect that the log contains an event, but don't care about where | |
| 109 // as long as the index where it is found is greater than min_index. | |
| 110 // Returns the position where the event was found. | |
| 111 inline size_t ExpectLogContainsSomewhere(const LoadLog* log, | |
| 112 size_t min_index, | |
| 113 LoadLog::EventType expected_event, | |
| 114 LoadLog::EventPhase expected_phase) { | |
| 115 size_t i = 0; | |
| 116 for (; i < log->entries().size(); ++i) { | |
| 117 const LoadLog::Entry& entry = log->entries()[i]; | |
| 118 if (entry.type == LoadLog::Entry::TYPE_EVENT && | |
| 119 entry.event.type == expected_event && | |
| 120 entry.event.phase == expected_phase) | |
| 121 break; | |
| 122 } | |
| 123 EXPECT_LT(i, log->entries().size()); | |
| 124 EXPECT_GE(i, min_index); | |
| 125 return i; | |
| 126 } | |
| 127 | |
| 128 } // namespace net | |
| 129 | |
| 130 #endif // NET_BASE_LOAD_LOG_UNITTEST_H_ | |
| OLD | NEW |