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

Side by Side Diff: third_party/WebKit/Source/core/dom/ScriptRunnerTest.cpp

Issue 1456873003: More regular Platform implementations in unit tests (reland.) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: have GN disable c4267 also Created 5 years 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
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/heap/Handle.h" 11 #include "platform/heap/Handle.h"
12 #include "platform/scheduler/CancellableTaskFactory.h" 12 #include "platform/scheduler/CancellableTaskFactory.h"
13 #include "platform/testing/TestingPlatformSupport.h"
13 #include "public/platform/Platform.h" 14 #include "public/platform/Platform.h"
14 #include "public/platform/WebViewScheduler.h" 15 #include "public/platform/WebViewScheduler.h"
15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 using ::testing::Invoke; 19 using ::testing::Invoke;
19 using ::testing::ElementsAre; 20 using ::testing::ElementsAre;
20 using ::testing::Return; 21 using ::testing::Return;
21 using ::testing::WhenSorted; 22 using ::testing::WhenSorted;
22 using ::testing::ElementsAreArray; 23 using ::testing::ElementsAreArray;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 89
89 WebTaskRunner* clone() override 90 WebTaskRunner* clone() override
90 { 91 {
91 ASSERT_NOT_REACHED(); 92 ASSERT_NOT_REACHED();
92 return nullptr; 93 return nullptr;
93 } 94 }
94 95
95 Deque<OwnPtr<WebTaskRunner::Task>>* m_tasks; // NOT OWNED 96 Deque<OwnPtr<WebTaskRunner::Task>>* m_tasks; // NOT OWNED
96 }; 97 };
97 98
98 class MockPlatform : public Platform, public WebScheduler { 99 class MockPlatform : public TestingPlatformSupport, public WebScheduler {
99 public: 100 public:
100 MockPlatform() 101 MockPlatform()
101 : m_mockWebThread(this) 102 : m_mockWebThread(this)
102 , m_mockWebTaskRunner(&m_tasks) 103 , m_mockWebTaskRunner(&m_tasks)
103 { 104 {
104 } 105 }
105 106
106 void cryptographicallyRandomValues(unsigned char* buffer, size_t length) ove rride
107 {
108 RELEASE_ASSERT_NOT_REACHED();
109 }
110
111 const unsigned char* getTraceCategoryEnabledFlag(const char* categoryName) o verride
112 {
113 static const unsigned char tracingIsDisabled = 0;
114 return &tracingIsDisabled;
115 }
116
117 WebThread* currentThread() override { return &m_mockWebThread; } 107 WebThread* currentThread() override { return &m_mockWebThread; }
118 108
119 void runSingleTask() 109 void runSingleTask()
120 { 110 {
121 if (m_tasks.isEmpty()) 111 if (m_tasks.isEmpty())
122 return; 112 return;
123 m_tasks.takeFirst()->run(); 113 m_tasks.takeFirst()->run();
124 } 114 }
125 115
126 void runAllTasks() 116 void runAllTasks()
(...skipping 29 matching lines...) Expand all
156 void onNavigationStarted() override { } 146 void onNavigationStarted() override { }
157 147
158 private: 148 private:
159 MockWebThread m_mockWebThread; 149 MockWebThread m_mockWebThread;
160 Deque<OwnPtr<WebTaskRunner::Task>> m_tasks; 150 Deque<OwnPtr<WebTaskRunner::Task>> m_tasks;
161 MockWebTaskRunner m_mockWebTaskRunner; 151 MockWebTaskRunner m_mockWebTaskRunner;
162 }; 152 };
163 153
164 class ScriptRunnerTest : public testing::Test { 154 class ScriptRunnerTest : public testing::Test {
165 public: 155 public:
156 ScriptRunnerTest()
157 : m_document(Document::create())
158 , m_element(m_document->createElement("foo", ASSERT_NO_EXCEPTION))
159 {
160 }
161
166 void SetUp() override 162 void SetUp() override
167 { 163 {
168 m_document = Document::create();
169 m_element = m_document->createElement("foo", ASSERT_NO_EXCEPTION);
170
171 m_oldPlatform = Platform::current();
172
173 // Force Platform::initialize to create a new one pointing at MockPlatfo rm.
174 Platform::initialize(&m_platform);
175
176 // We have to create ScriptRunner after initializing platform, because w e need 164 // We have to create ScriptRunner after initializing platform, because w e need
177 // Platform::current()->currentThread()->scheduler()->loadingTaskRunner( ) 165 // Platform::current()->currentThread()->scheduler()->loadingTaskRunner( )
178 // to be initialized before creating ScriptRunner to save it in construc tor. 166 // to be initialized before creating ScriptRunner to save it in construc tor.
179 m_scriptRunner = ScriptRunner::create(m_document.get()); 167 m_scriptRunner = ScriptRunner::create(m_document.get());
180 } 168 }
181 169
182 void TearDown() override 170 void TearDown() override
183 { 171 {
184 m_scriptRunner.release(); 172 m_scriptRunner.release();
185 Platform::initialize(m_oldPlatform);
186 } 173 }
187 174
188 RefPtrWillBePersistent<Document> m_document; 175 RefPtrWillBePersistent<Document> m_document;
189 RefPtrWillBePersistent<Element> m_element; 176 RefPtrWillBePersistent<Element> m_element;
177 MockPlatform m_platform;
190 OwnPtrWillBePersistent<ScriptRunner> m_scriptRunner; 178 OwnPtrWillBePersistent<ScriptRunner> m_scriptRunner;
191 WTF::Vector<int> m_order; 179 WTF::Vector<int> m_order;
192 MockPlatform m_platform;
193 Platform* m_oldPlatform; // NOT OWNED
194 }; 180 };
195 181
196 TEST_F(ScriptRunnerTest, QueueSingleScript_Async) 182 TEST_F(ScriptRunnerTest, QueueSingleScript_Async)
197 { 183 {
198 OwnPtrWillBeRawPtr<MockScriptLoader> scriptLoader = MockScriptLoader::create (m_element.get()); 184 OwnPtrWillBeRawPtr<MockScriptLoader> scriptLoader = MockScriptLoader::create (m_element.get());
199 m_scriptRunner->queueScriptForExecution(scriptLoader.get(), ScriptRunner::AS YNC_EXECUTION); 185 m_scriptRunner->queueScriptForExecution(scriptLoader.get(), ScriptRunner::AS YNC_EXECUTION);
200 m_scriptRunner->notifyScriptReady(scriptLoader.get(), ScriptRunner::ASYNC_EX ECUTION); 186 m_scriptRunner->notifyScriptReady(scriptLoader.get(), ScriptRunner::ASYNC_EX ECUTION);
201 187
202 EXPECT_CALL(*scriptLoader, execute()); 188 EXPECT_CALL(*scriptLoader, execute());
203 m_platform.runAllTasks(); 189 m_platform.runAllTasks();
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 #endif 554 #endif
569 555
570 // m_scriptRunner is gone. We need to make sure that ScriptRunner::Task do n ot access dead object. 556 // m_scriptRunner is gone. We need to make sure that ScriptRunner::Task do n ot access dead object.
571 EXPECT_CALL(*scriptLoader1, execute()).Times(0); 557 EXPECT_CALL(*scriptLoader1, execute()).Times(0);
572 EXPECT_CALL(*scriptLoader2, execute()).Times(0); 558 EXPECT_CALL(*scriptLoader2, execute()).Times(0);
573 559
574 m_platform.runAllTasks(); 560 m_platform.runAllTasks();
575 } 561 }
576 562
577 } // namespace blink 563 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698