Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: third_party/WebKit/Source/core/frame/DOMTimerTest.cpp

Issue 2694743004: Reset the nesting level for setInterval after completion. (Closed)
Patch Set: Apply review comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 #include "core/frame/DOMTimer.h"
6
7 #include <vector>
8
9 #include "bindings/core/v8/ScriptController.h"
10 #include "core/dom/Document.h"
11 #include "core/layout/LayoutTestHelper.h"
12 #include "gin/converter.h"
13 #include "platform/testing/TestingPlatformSupport.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 using testing::DoubleNear;
18 using testing::ElementsAreArray;
19 using testing::Matcher;
20
21 namespace blink {
22
23 namespace {
24
25 class DOMTimerTest : public RenderingTest {
26 public:
27 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
28 m_platform;
29
30 gin::Converter<std::vector<double>> m_converter;
31
32 // Expected time between each iterator for setInterval(..., 1) or nested
33 // setTimeout(..., 1) are 1, 1, 1, 1, 4, 4, ... as a minumum clamp of 4m
34 // is applied from the 5th iteration onwards.
35 const std::vector<Matcher<double>> kExpectedTimings = {
36 DoubleNear(1., 0.000001), DoubleNear(1., 0.000001),
37 DoubleNear(1., 0.000001), DoubleNear(1., 0.000001),
38 DoubleNear(4., 0.000001), DoubleNear(4., 0.000001),
39 };
40
41 void SetUp() override {
42 m_platform->setAutoAdvanceNowToPendingTasks(true);
43 // Advance timer manually as RenderingTest expects the time to be non-zero.
44 m_platform->advanceClockSeconds(1.);
45 RenderingTest::SetUp();
46 // Advance timer again as otherwise the time between the first call to
47 // setInterval and it running will be off by 5us.
48 m_platform->advanceClockSeconds(1);
49 document().settings()->setScriptEnabled(true);
50 }
51
52 v8::Local<v8::Value> EvalExpression(const char* expr) {
53 return document().frame()->script().executeScriptInMainWorldAndReturnValue(
54 ScriptSourceCode(expr));
55 }
56
57 void ExecuteScriptAndWaitUntilIdle(const char* scriptText) {
58 ScriptSourceCode script(scriptText);
59 document().frame()->script().executeScriptInMainWorld(script);
60 m_platform->runUntilIdle();
61 }
62 };
63
64 const char* kSetTimeoutScriptText =
65 "var id;"
66 "var last = performance.now();"
67 "var times = [];"
68 "function nestSetTimeouts() {"
69 " var current = performance.now();"
70 " var elapsed = current - last;"
71 " last = current;"
72 " times.push(elapsed);"
73 " if (times.length < 6) {"
74 " setTimeout(nestSetTimeouts, 1);"
75 " }"
76 "}"
77 "setTimeout(nestSetTimeouts, 1);";
78
79 TEST_F(DOMTimerTest, setTimeout_ClampsAfter4Nestings) {
80 v8::HandleScope scope(v8::Isolate::GetCurrent());
81
82 ExecuteScriptAndWaitUntilIdle(kSetTimeoutScriptText);
83
84 std::vector<double> times;
85 m_converter.FromV8(scope.GetIsolate(), EvalExpression("times"), &times);
86
87 EXPECT_THAT(times, ElementsAreArray(kExpectedTimings));
88 }
89
90 const char* kSetIntervalScriptText =
91 "var last = performance.now();"
92 "var times = [];"
93 "var id = setInterval(function() {"
94 " var current = performance.now();"
95 " var elapsed = current - last;"
96 " last = current;"
97 " times.push(elapsed);"
98 " if (times.length > 5) {"
99 " clearInterval(id);"
100 " }"
101 "}, 1);";
102
103 TEST_F(DOMTimerTest, setInterval_ClampsAfter4Iterations) {
104 v8::HandleScope scope(v8::Isolate::GetCurrent());
105
106 ExecuteScriptAndWaitUntilIdle(kSetIntervalScriptText);
107
108 std::vector<double> times;
109 m_converter.FromV8(scope.GetIsolate(), EvalExpression("times"), &times);
110
111 EXPECT_THAT(times, ElementsAreArray(kExpectedTimings));
112 }
113
114 TEST_F(DOMTimerTest, setInterval_NestingResetsForLaterCalls) {
115 v8::HandleScope scope(v8::Isolate::GetCurrent());
116
117 ExecuteScriptAndWaitUntilIdle(kSetIntervalScriptText);
118
119 // Run the setIntervalScript again to verify that the clamp imposed for
120 // nesting beyond 4 levels is reset when setInterval is called again in the
121 // original scope but after the original setInterval has completed.
122 ExecuteScriptAndWaitUntilIdle(kSetIntervalScriptText);
123
124 std::vector<double> times;
125 m_converter.FromV8(scope.GetIsolate(), EvalExpression("times"), &times);
126
127 EXPECT_THAT(times, ElementsAreArray(kExpectedTimings));
128 }
129
130 } // namespace
131
132 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698