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