summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/scheduler/common/tracing_helper_unittest.cc
blob: 332310b374885d4746ff87e30509cac730380974 (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
98
// Copyright 2017 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.

#include "third_party/blink/renderer/platform/scheduler/common/tracing_helper.h"

#include <unordered_set>

#include "testing/gtest/include/gtest/gtest.h"

namespace blink {
namespace scheduler {

namespace {

const char* g_last_state = nullptr;

void ExpectTraced(const char* state) {
  EXPECT_TRUE(state);
  EXPECT_TRUE(g_last_state);
  EXPECT_STREQ(state, g_last_state);
  g_last_state = nullptr;
}

void ExpectNotTraced() {
  EXPECT_FALSE(g_last_state);
}

const char* SignOfInt(int value) {
  if (value > 0)
    return "positive";
  if (value < 0)
    return "negative";
  return "zero";
}

class TraceableStateForTest
    : public TraceableState<int, TracingCategoryName::kDefault> {
 public:
  TraceableStateForTest(TraceableVariableController* controller)
      : TraceableState(0, "State", controller, controller, SignOfInt) {
    // We shouldn't expect trace in constructor here because mock isn't set yet.
    mock_trace_for_test_ = &MockTrace;
  }

  TraceableStateForTest& operator=(const int& value) {
    Assign(value);
    return *this;
  }

  static void MockTrace(const char* state) {
    EXPECT_TRUE(state);
    EXPECT_FALSE(g_last_state);  // No unexpected traces.
    g_last_state = state;
  }
};

}  // namespace

// TODO(kraynov): TraceableCounter tests.

TEST(TracingHelperTest, TraceableState) {
  TraceableVariableController controller;
  TraceableStateForTest state(&controller);
  controller.OnTraceLogEnabled();
  ExpectTraced("zero");
  state = 0;
  ExpectNotTraced();
  state = 1;
  ExpectTraced("positive");
  state = -1;
  ExpectTraced("negative");
}

TEST(TracingHelperTest, TraceableStateOperators) {
  TraceableVariableController controller;
  TraceableState<int, TracingCategoryName::kDebug> x(-1, "X", &controller,
                                                     &controller, SignOfInt);
  TraceableState<int, TracingCategoryName::kDebug> y(1, "Y", &controller,
                                                     &controller, SignOfInt);
  EXPECT_EQ(0, x + y);
  EXPECT_FALSE(x == y);
  EXPECT_TRUE(x != y);
  x = 1;
  EXPECT_EQ(0, y - x);
  EXPECT_EQ(2, x + y);
  EXPECT_EQ(x, y);
  EXPECT_FALSE(x != y);
  EXPECT_NE(x + y, 3);
  EXPECT_EQ(2 - y + 1 + x, 3);
  x = 3;
  y = 2;
  int z = x = y;
  EXPECT_EQ(2, z);
}

}  // namespace scheduler
}  // namespace blink