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