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