Home | History | Annotate | Download | only in base
      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   }
     36   if (expected_event != entry.event.type) {
     37     return ::testing::AssertionFailure()
     38         << "Actual event: " << LoadLog::EventTypeToString(entry.event.type)
     39         << ". Expected event: " << LoadLog::EventTypeToString(expected_event)
     40         << ".";
     41   }
     42   if (expected_phase != entry.event.phase) {
     43     return ::testing::AssertionFailure()
     44         << "Actual phase: " << entry.event.phase
     45         << ". Expected phase: " << expected_phase << ".";
     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   }
     55   return ::testing::AssertionSuccess();
     56 }
     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 
     94 // Expect that the log contains an event, but don't care about where
     95 // as long as the index where it is found is greater than min_index.
     96 // Returns the position where the event was found.
     97 inline size_t ExpectLogContainsSomewhere(const LoadLog* log,
     98                                          size_t min_index,
     99                                          LoadLog::EventType expected_event,
    100                                          LoadLog::EventPhase expected_phase) {
    101   size_t i = 0;
    102   for (; i < log->entries().size(); ++i) {
    103     const LoadLog::Entry& entry = log->entries()[i];
    104     if (entry.type == LoadLog::Entry::TYPE_EVENT &&
    105         entry.event.type == expected_event &&
    106         entry.event.phase == expected_phase)
    107       break;
    108   }
    109   EXPECT_LT(i, log->entries().size());
    110   EXPECT_GE(i, min_index);
    111   return i;
    112 }
    113 
    114 }  // namespace net
    115 
    116 #endif  // NET_BASE_LOAD_LOG_UNITTEST_H_
    117