Chromium Code Reviews| 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 "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 platform_; | |
|
alex clarke (OOO till 29th)
2017/02/14 16:22:18
Sadly this is in blink, please rename to m_platfor
Dan Elphick
2017/02/14 17:11:03
Done.
| |
| 29 | |
| 30 gin::Converter<std::vector<double>> converter; | |
|
alex clarke (OOO till 29th)
2017/02/14 16:22:18
please rename to m_converter
Dan Elphick
2017/02/14 17:11:03
Done.
| |
| 31 | |
| 32 void SetUp() override { | |
| 33 platform_->setAutoAdvanceNowToPendingTasks(true); | |
| 34 // Advance timer manually as RenderingTest expects the time to be non-zero. | |
| 35 platform_->advanceClockSeconds(1.); | |
| 36 RenderingTest::SetUp(); | |
| 37 // Advance timer again as otherwise the time between the first call to | |
| 38 // setInterval and it running will be off by 5us. | |
| 39 platform_->advanceClockSeconds(1); | |
| 40 document().settings()->setScriptEnabled(true); | |
| 41 } | |
| 42 | |
| 43 v8::Local<v8::Value> EvalExpression(const char* expr) { | |
| 44 return document().frame()->script().executeScriptInMainWorldAndReturnValue( | |
| 45 ScriptSourceCode(expr)); | |
| 46 } | |
| 47 | |
| 48 void ExecuteScriptAndWaitUntilIdle(const char* scriptText) { | |
| 49 ScriptSourceCode script(scriptText); | |
| 50 document().frame()->script().executeScriptInMainWorld(script); | |
| 51 platform_->runUntilIdle(); | |
| 52 } | |
| 53 }; | |
| 54 | |
| 55 const char* kSetTimeoutScriptText = | |
|
alex clarke (OOO till 29th)
2017/02/14 16:22:18
We are allowed to use C++11 string literals now wh
Dan Elphick
2017/02/14 17:11:03
Looks like check-webkit-style hasn't got the memo:
| |
| 56 "var id;" | |
| 57 "var last = performance.now();" | |
| 58 "var times = [];" | |
| 59 "function nestSetTimeouts() {" | |
| 60 " var current = performance.now();" | |
| 61 " var elapsed = current - last;" | |
| 62 " last = current;" | |
| 63 " times.push(elapsed);" | |
| 64 " if (times.length < 6) {" | |
| 65 " setTimeout(nestSetTimeouts, 1);" | |
| 66 " }" | |
| 67 "}" | |
| 68 "setTimeout(nestSetTimeouts, 1);"; | |
| 69 | |
| 70 // Expected time between each iterator for setInterval(..., 1) or nested | |
| 71 // setTimeout(..., 1) are 1, 1, 1, 1, 4, 4, ... as a minumum clamp of 4ms | |
| 72 // is applied from the 5th iteration onwards. | |
| 73 const std::vector<Matcher<double>> kExpectedTimings = { | |
|
alex clarke (OOO till 29th)
2017/02/14 16:22:18
AFAIK the style guide does not allow globals that
| |
| 74 DoubleNear(1., 0.000001), DoubleNear(1., 0.000001), | |
| 75 DoubleNear(1., 0.000001), DoubleNear(1., 0.000001), | |
| 76 DoubleNear(4., 0.000001), DoubleNear(4., 0.000001), | |
| 77 }; | |
| 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 converter.FromV8(scope.GetIsolate(), EvalExpression("times"), ×); | |
| 86 | |
| 87 EXPECT_THAT(times, ElementsAreArray(kExpectedTimings)); | |
| 88 } | |
| 89 | |
| 90 const char* kSetIntervalScriptText = | |
| 91 "var last = performance.now();" | |
|
alex clarke (OOO till 29th)
2017/02/14 16:22:18
Ditto
Dan Elphick
2017/02/14 17:11:03
As above.
| |
| 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 converter.FromV8(scope.GetIsolate(), EvalExpression("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 converter.FromV8(scope.GetIsolate(), EvalExpression("times"), ×); | |
| 126 | |
| 127 EXPECT_THAT(times, ElementsAreArray(kExpectedTimings)); | |
| 128 } | |
| 129 | |
| 130 } // namespace | |
| 131 | |
| 132 } // namespace blink | |
| OLD | NEW |