| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "core/dom/ScriptRunner.h" | 6 #include "core/dom/ScriptRunner.h" |
| 7 | 7 |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/Element.h" | 9 #include "core/dom/Element.h" |
| 10 #include "core/dom/ScriptLoader.h" | 10 #include "core/dom/ScriptLoader.h" |
| 11 #include "platform/scheduler/Scheduler.h" | |
| 12 #include "public/platform/Platform.h" | 11 #include "public/platform/Platform.h" |
| 13 #include <gmock/gmock.h> | 12 #include <gmock/gmock.h> |
| 14 #include <gtest/gtest.h> | 13 #include <gtest/gtest.h> |
| 15 | 14 |
| 16 using ::testing::Invoke; | 15 using ::testing::Invoke; |
| 17 using ::testing::ElementsAre; | 16 using ::testing::ElementsAre; |
| 18 using ::testing::Return; | 17 using ::testing::Return; |
| 19 | 18 |
| 20 namespace blink { | 19 namespace blink { |
| 21 | 20 |
| 22 class MockScriptLoader: public ScriptLoader { | 21 class MockScriptLoader: public ScriptLoader { |
| 23 public: | 22 public: |
| 24 explicit MockScriptLoader(Element* element) : ScriptLoader(element, false, f
alse) { } | 23 explicit MockScriptLoader(Element* element) : ScriptLoader(element, false, f
alse) { } |
| 25 | 24 |
| 26 ~MockScriptLoader() override { } | 25 ~MockScriptLoader() override { } |
| 27 | 26 |
| 28 MOCK_METHOD0(execute, void()); | 27 MOCK_METHOD0(execute, void()); |
| 29 MOCK_CONST_METHOD0(isReady, bool()); | 28 MOCK_CONST_METHOD0(isReady, bool()); |
| 30 }; | 29 }; |
| 31 | 30 |
| 31 class MockWebThread : public WebThread { |
| 32 public: |
| 33 explicit MockWebThread(WebScheduler* webScheduler) : m_webScheduler(webSched
uler) { } |
| 34 ~MockWebThread() override { } |
| 35 |
| 36 void postTask(const WebTraceLocation&, Task*) override { ASSERT_NOT_REACHED(
); } |
| 37 void postDelayedTask(const WebTraceLocation&, Task*, long long) override { A
SSERT_NOT_REACHED(); } |
| 38 |
| 39 bool isCurrentThread() const override |
| 40 { |
| 41 ASSERT_NOT_REACHED(); |
| 42 return false; |
| 43 } |
| 44 |
| 45 PlatformThreadId threadId() const override |
| 46 { |
| 47 ASSERT_NOT_REACHED(); |
| 48 return 0; |
| 49 } |
| 50 |
| 51 void addTaskObserver(TaskObserver*) override { ASSERT_NOT_REACHED(); } |
| 52 void removeTaskObserver(TaskObserver*) override { ASSERT_NOT_REACHED(); } |
| 53 |
| 54 WebScheduler* scheduler() const override { return m_webScheduler; } |
| 55 |
| 56 void enterRunLoop() override { ASSERT_NOT_REACHED(); } |
| 57 void exitRunLoop() override { ASSERT_NOT_REACHED(); } |
| 58 |
| 59 private: |
| 60 WebScheduler* m_webScheduler; |
| 61 }; |
| 62 |
| 32 class MockPlatform : public Platform, private WebScheduler { | 63 class MockPlatform : public Platform, private WebScheduler { |
| 33 public: | 64 public: |
| 34 MockPlatform() : m_shouldYield(false), m_shouldYieldEveryOtherTime(false) {
} | 65 MockPlatform() : m_mockWebThread(this), m_shouldYield(false), m_shouldYieldE
veryOtherTime(false) { } |
| 35 | |
| 36 WebScheduler* scheduler() override | |
| 37 { | |
| 38 return this; | |
| 39 } | |
| 40 | 66 |
| 41 void postLoadingTask(const WebTraceLocation&, WebThread::Task* task) overrid
e | 67 void postLoadingTask(const WebTraceLocation&, WebThread::Task* task) overrid
e |
| 42 { | 68 { |
| 43 m_tasks.append(adoptPtr(task)); | 69 m_tasks.append(adoptPtr(task)); |
| 44 } | 70 } |
| 45 | 71 |
| 46 void cryptographicallyRandomValues(unsigned char* buffer, size_t length) ove
rride { } | 72 void cryptographicallyRandomValues(unsigned char* buffer, size_t length) ove
rride { } |
| 47 | 73 |
| 74 WebThread* currentThread() override { return &m_mockWebThread; } |
| 75 |
| 48 void runSingleTask() | 76 void runSingleTask() |
| 49 { | 77 { |
| 50 if (m_tasks.isEmpty()) | 78 if (m_tasks.isEmpty()) |
| 51 return; | 79 return; |
| 52 m_tasks.takeFirst()->run(); | 80 m_tasks.takeFirst()->run(); |
| 53 } | 81 } |
| 54 | 82 |
| 55 void runAllTasks() | 83 void runAllTasks() |
| 56 { | 84 { |
| 57 while (!m_tasks.isEmpty()) | 85 while (!m_tasks.isEmpty()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 70 m_shouldYield = shouldYield; | 98 m_shouldYield = shouldYield; |
| 71 } | 99 } |
| 72 | 100 |
| 73 // NOTE if we yield 100% of the time, nothing will get run. | 101 // NOTE if we yield 100% of the time, nothing will get run. |
| 74 void setShouldYieldEveryOtherTime(bool shouldYieldEveryOtherTime) | 102 void setShouldYieldEveryOtherTime(bool shouldYieldEveryOtherTime) |
| 75 { | 103 { |
| 76 m_shouldYieldEveryOtherTime = shouldYieldEveryOtherTime; | 104 m_shouldYieldEveryOtherTime = shouldYieldEveryOtherTime; |
| 77 } | 105 } |
| 78 | 106 |
| 79 private: | 107 private: |
| 108 MockWebThread m_mockWebThread; |
| 80 Deque<OwnPtr<WebThread::Task>> m_tasks; | 109 Deque<OwnPtr<WebThread::Task>> m_tasks; |
| 81 bool m_shouldYield; | 110 bool m_shouldYield; |
| 82 bool m_shouldYieldEveryOtherTime; | 111 bool m_shouldYieldEveryOtherTime; |
| 83 }; | 112 }; |
| 84 | 113 |
| 85 class ScriptRunnerTest : public testing::Test { | 114 class ScriptRunnerTest : public testing::Test { |
| 86 public: | 115 public: |
| 87 void SetUp() override | 116 void SetUp() override |
| 88 { | 117 { |
| 89 m_document = Document::create(); | 118 m_document = Document::create(); |
| 90 m_element = m_document->createElement("foo", ASSERT_NO_EXCEPTION); | 119 m_element = m_document->createElement("foo", ASSERT_NO_EXCEPTION); |
| 91 | 120 |
| 92 m_scriptRunner = ScriptRunner::create(m_document.get()); | 121 m_scriptRunner = ScriptRunner::create(m_document.get()); |
| 93 m_oldPlatform = Platform::current(); | 122 m_oldPlatform = Platform::current(); |
| 94 m_oldScheduler = Scheduler::shared(); | |
| 95 | 123 |
| 96 // Force Platform::initialize to create a new one pointing at MockPlatfo
rm. | 124 // Force Platform::initialize to create a new one pointing at MockPlatfo
rm. |
| 97 Scheduler::setForTesting(nullptr); | |
| 98 Platform::initialize(&m_platform); | 125 Platform::initialize(&m_platform); |
| 99 m_platform.setShouldYield(false); | 126 m_platform.setShouldYield(false); |
| 100 m_platform.setShouldYieldEveryOtherTime(false); | 127 m_platform.setShouldYieldEveryOtherTime(false); |
| 101 } | 128 } |
| 102 | 129 |
| 103 void TearDown() override | 130 void TearDown() override |
| 104 { | 131 { |
| 105 m_scriptRunner.release(); | 132 m_scriptRunner.release(); |
| 106 Scheduler::shutdown(); | |
| 107 Scheduler::setForTesting(m_oldScheduler); | |
| 108 Platform::initialize(m_oldPlatform); | 133 Platform::initialize(m_oldPlatform); |
| 109 } | 134 } |
| 110 | 135 |
| 111 RefPtrWillBePersistent<Document> m_document; | 136 RefPtrWillBePersistent<Document> m_document; |
| 112 RefPtrWillBePersistent<Element> m_element; | 137 RefPtrWillBePersistent<Element> m_element; |
| 113 OwnPtrWillBePersistent<ScriptRunner> m_scriptRunner; | 138 OwnPtrWillBePersistent<ScriptRunner> m_scriptRunner; |
| 114 std::vector<int> m_order; // gmock matchers don't work nicely with WTF::Vect
or | 139 std::vector<int> m_order; // gmock matchers don't work nicely with WTF::Vect
or |
| 115 MockPlatform m_platform; | 140 MockPlatform m_platform; |
| 116 Platform* m_oldPlatform; // NOT OWNED | 141 Platform* m_oldPlatform; // NOT OWNED |
| 117 Scheduler* m_oldScheduler; // NOT OWNED | |
| 118 }; | 142 }; |
| 119 | 143 |
| 120 TEST_F(ScriptRunnerTest, QueueSingleScript_Async) | 144 TEST_F(ScriptRunnerTest, QueueSingleScript_Async) |
| 121 { | 145 { |
| 122 MockScriptLoader scriptLoader(m_element.get()); | 146 MockScriptLoader scriptLoader(m_element.get()); |
| 123 m_scriptRunner->queueScriptForExecution(&scriptLoader, ScriptRunner::ASYNC_E
XECUTION); | 147 m_scriptRunner->queueScriptForExecution(&scriptLoader, ScriptRunner::ASYNC_E
XECUTION); |
| 124 m_scriptRunner->notifyScriptReady(&scriptLoader, ScriptRunner::ASYNC_EXECUTI
ON); | 148 m_scriptRunner->notifyScriptReady(&scriptLoader, ScriptRunner::ASYNC_EXECUTI
ON); |
| 125 | 149 |
| 126 EXPECT_CALL(scriptLoader, execute()); | 150 EXPECT_CALL(scriptLoader, execute()); |
| 127 m_platform.runAllTasks(); | 151 m_platform.runAllTasks(); |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 testing::Mock::VerifyAndClear(&scriptLoader2); | 532 testing::Mock::VerifyAndClear(&scriptLoader2); |
| 509 testing::Mock::VerifyAndClear(&scriptLoader3); | 533 testing::Mock::VerifyAndClear(&scriptLoader3); |
| 510 EXPECT_CALL(scriptLoader2, execute()).Times(1); | 534 EXPECT_CALL(scriptLoader2, execute()).Times(1); |
| 511 EXPECT_CALL(scriptLoader3, execute()).Times(1); | 535 EXPECT_CALL(scriptLoader3, execute()).Times(1); |
| 512 EXPECT_CALL(scriptLoader2, isReady()).WillRepeatedly(Return(true)); | 536 EXPECT_CALL(scriptLoader2, isReady()).WillRepeatedly(Return(true)); |
| 513 EXPECT_CALL(scriptLoader3, isReady()).WillRepeatedly(Return(true)); | 537 EXPECT_CALL(scriptLoader3, isReady()).WillRepeatedly(Return(true)); |
| 514 m_platform.runAllTasks(); | 538 m_platform.runAllTasks(); |
| 515 } | 539 } |
| 516 | 540 |
| 517 } // namespace blink | 541 } // namespace blink |
| OLD | NEW |