[ServiceWorker] Set setSkipServiceWorker flag of the request from NPAPI plugins
[chromium-blink-merge.git] / base / debug / trace_event_synthetic_delay_unittest.cc
blob7833e7bbcdeb89d47bc2bb404e280cf962f48260
1 // Copyright 2014 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.
5 #include "base/debug/trace_event_synthetic_delay.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace base {
10 namespace debug {
11 namespace {
13 const int kTargetDurationMs = 100;
14 // Allow some leeway in timings to make it possible to run these tests with a
15 // wall clock time source too.
16 const int kShortDurationMs = 10;
18 } // namespace
20 class TraceEventSyntheticDelayTest : public testing::Test,
21 public TraceEventSyntheticDelayClock {
22 public:
23 TraceEventSyntheticDelayTest() {}
24 virtual ~TraceEventSyntheticDelayTest() {
25 ResetTraceEventSyntheticDelays();
28 // TraceEventSyntheticDelayClock implementation.
29 virtual base::TimeTicks Now() OVERRIDE {
30 AdvanceTime(base::TimeDelta::FromMilliseconds(kShortDurationMs / 10));
31 return now_;
34 TraceEventSyntheticDelay* ConfigureDelay(const char* name) {
35 TraceEventSyntheticDelay* delay = TraceEventSyntheticDelay::Lookup(name);
36 delay->SetClock(this);
37 delay->SetTargetDuration(
38 base::TimeDelta::FromMilliseconds(kTargetDurationMs));
39 return delay;
42 void AdvanceTime(base::TimeDelta delta) { now_ += delta; }
44 int TestFunction() {
45 base::TimeTicks start = Now();
46 { TRACE_EVENT_SYNTHETIC_DELAY("test.Delay"); }
47 return (Now() - start).InMilliseconds();
50 int AsyncTestFunctionBegin() {
51 base::TimeTicks start = Now();
52 { TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("test.AsyncDelay"); }
53 return (Now() - start).InMilliseconds();
56 int AsyncTestFunctionEnd() {
57 base::TimeTicks start = Now();
58 { TRACE_EVENT_SYNTHETIC_DELAY_END("test.AsyncDelay"); }
59 return (Now() - start).InMilliseconds();
62 private:
63 base::TimeTicks now_;
65 DISALLOW_COPY_AND_ASSIGN(TraceEventSyntheticDelayTest);
68 TEST_F(TraceEventSyntheticDelayTest, StaticDelay) {
69 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay");
70 delay->SetMode(TraceEventSyntheticDelay::STATIC);
71 EXPECT_GE(TestFunction(), kTargetDurationMs);
74 TEST_F(TraceEventSyntheticDelayTest, OneShotDelay) {
75 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay");
76 delay->SetMode(TraceEventSyntheticDelay::ONE_SHOT);
77 EXPECT_GE(TestFunction(), kTargetDurationMs);
78 EXPECT_LT(TestFunction(), kShortDurationMs);
80 delay->SetTargetDuration(
81 base::TimeDelta::FromMilliseconds(kTargetDurationMs));
82 EXPECT_GE(TestFunction(), kTargetDurationMs);
85 TEST_F(TraceEventSyntheticDelayTest, AlternatingDelay) {
86 TraceEventSyntheticDelay* delay = ConfigureDelay("test.Delay");
87 delay->SetMode(TraceEventSyntheticDelay::ALTERNATING);
88 EXPECT_GE(TestFunction(), kTargetDurationMs);
89 EXPECT_LT(TestFunction(), kShortDurationMs);
90 EXPECT_GE(TestFunction(), kTargetDurationMs);
91 EXPECT_LT(TestFunction(), kShortDurationMs);
94 TEST_F(TraceEventSyntheticDelayTest, AsyncDelay) {
95 ConfigureDelay("test.AsyncDelay");
96 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
97 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2);
100 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayExceeded) {
101 ConfigureDelay("test.AsyncDelay");
102 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
103 AdvanceTime(base::TimeDelta::FromMilliseconds(kTargetDurationMs));
104 EXPECT_LT(AsyncTestFunctionEnd(), kShortDurationMs);
107 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayNoActivation) {
108 ConfigureDelay("test.AsyncDelay");
109 EXPECT_LT(AsyncTestFunctionEnd(), kShortDurationMs);
112 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayNested) {
113 ConfigureDelay("test.AsyncDelay");
114 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
115 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
116 EXPECT_LT(AsyncTestFunctionEnd(), kShortDurationMs);
117 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2);
120 TEST_F(TraceEventSyntheticDelayTest, AsyncDelayUnbalanced) {
121 ConfigureDelay("test.AsyncDelay");
122 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
123 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2);
124 EXPECT_LT(AsyncTestFunctionEnd(), kShortDurationMs);
126 EXPECT_LT(AsyncTestFunctionBegin(), kShortDurationMs);
127 EXPECT_GE(AsyncTestFunctionEnd(), kTargetDurationMs / 2);
130 TEST_F(TraceEventSyntheticDelayTest, ResetDelays) {
131 ConfigureDelay("test.Delay");
132 ResetTraceEventSyntheticDelays();
133 EXPECT_LT(TestFunction(), kShortDurationMs);
136 TEST_F(TraceEventSyntheticDelayTest, BeginParallel) {
137 TraceEventSyntheticDelay* delay = ConfigureDelay("test.AsyncDelay");
138 base::TimeTicks end_times[2];
139 base::TimeTicks start_time = Now();
141 delay->BeginParallel(&end_times[0]);
142 EXPECT_FALSE(end_times[0].is_null());
144 delay->BeginParallel(&end_times[1]);
145 EXPECT_FALSE(end_times[1].is_null());
147 delay->EndParallel(end_times[0]);
148 EXPECT_GE((Now() - start_time).InMilliseconds(), kTargetDurationMs);
150 start_time = Now();
151 delay->EndParallel(end_times[1]);
152 EXPECT_LT((Now() - start_time).InMilliseconds(), kShortDurationMs);
155 } // namespace debug
156 } // namespace base