summaryrefslogtreecommitdiffstats
path: root/net/base/load_log_unittest.h
blob: ea9a37de4e3c5c6be5e8049c57bf014387688fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_BASE_LOAD_LOG_UNITTEST_H_
#define NET_BASE_LOAD_LOG_UNITTEST_H_

#include <cstddef>
#include "net/base/load_log.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

// Create a timestamp with internal value of |t| milliseconds from the epoch.
inline base::TimeTicks MakeTime(int t) {
  base::TimeTicks ticks;  // initialized to 0.
  ticks += base::TimeDelta::FromMilliseconds(t);
  return ticks;
}

// Call gtest's EXPECT_* to verify that |log| contains the specified entry
// at index |i|.
inline void ExpectLogContains(const LoadLog* log,
                              size_t i,
                              base::TimeTicks expected_time,
                              LoadLog::EventType expected_event,
                              LoadLog::EventPhase expected_phase) {
  ASSERT_LT(i, log->entries().size());
  const LoadLog::Entry& entry = log->entries()[i];
  EXPECT_EQ(LoadLog::Entry::TYPE_EVENT, entry.type);
  EXPECT_TRUE(expected_time == entry.time);
  EXPECT_EQ(expected_event, entry.event.type);
  EXPECT_EQ(expected_phase, entry.event.phase);
}

// Same as above, but without an expectation for the timestamp.
inline void ExpectLogContains(const LoadLog* log,
                              size_t i,
                              LoadLog::EventType expected_event,
                              LoadLog::EventPhase expected_phase) {
  ASSERT_LT(i, log->entries().size());
  const LoadLog::Entry& entry = log->entries()[i];
  EXPECT_EQ(LoadLog::Entry::TYPE_EVENT, entry.type);
  EXPECT_EQ(expected_event, entry.event.type);
  EXPECT_EQ(expected_phase, entry.event.phase);
}

inline ::testing::AssertionResult LogContains(
    const LoadLog& log,
    int i,  // Negative indices are reverse indices.
    LoadLog::EventType expected_event,
    LoadLog::EventPhase expected_phase) {
  // Negative indices are reverse indices.
  size_t j = (i < 0) ? log.entries().size() + i : i;
  if (j >= log.entries().size())
    return ::testing::AssertionFailure() << j << " is out of bounds.";
  const LoadLog::Entry& entry = log.entries()[j];
  if (entry.type != LoadLog::Entry::TYPE_EVENT) {
    return ::testing::AssertionFailure() << "Not a TYPE_EVENT entry";
  }
  if (expected_event != entry.event.type) {
    return ::testing::AssertionFailure()
        << "Actual event: " << LoadLog::EventTypeToString(entry.event.type)
        << ". Expected event: " << LoadLog::EventTypeToString(expected_event)
        << ".";
  }
  if (expected_phase != entry.event.phase) {
    return ::testing::AssertionFailure()
        << "Actual phase: " << entry.event.phase
        << ". Expected phase: " << expected_phase << ".";
  }
  return ::testing::AssertionSuccess();
}

// Expect that the log contains an event, but don't care about where
// as long as the index where it is found is greater than min_index.
// Returns the position where the event was found.
inline size_t ExpectLogContainsSomewhere(const LoadLog* log,
                                         size_t min_index,
                                         LoadLog::EventType expected_event,
                                         LoadLog::EventPhase expected_phase) {
  size_t i = 0;
  for (; i < log->entries().size(); ++i) {
    const LoadLog::Entry& entry = log->entries()[i];
    if (entry.type == LoadLog::Entry::TYPE_EVENT &&
        entry.event.type == expected_event &&
        entry.event.phase == expected_phase)
      break;
  }
  EXPECT_LT(i, log->entries().size());
  EXPECT_GE(i, min_index);
  return i;
}

}  // namespace net

#endif  // NET_BASE_LOAD_LOG_UNITTEST_H_