Commit f651acf2 authored by toyoshim's avatar toyoshim Committed by Commit bot

TestingPlatformSupport: register Platform instance correctly

Now, TestingPlatformSupport registers itself as the current Platform
instance, but it is inside the constructor.

On the other hand, a child class TestingPlatformSupportWithMockScheduler
expects the parent class reigsters it for children.

But, to register a Platform instance, virtual methods should be ready
to call. Rephrasing it, we can not register a Platform instance
correctly inside a super-class's constructor.

This patch provides ScopedTestingPlatformSupport class template that
manages TestingPlatformSupport instance and Platform instance
installation.

Without this patch, Platform::current()->mainThread() returns a
wrong WebThread, and it makes new tests that I will add fail.

BUG=n/a
TEST=platform_blink_unittests, webkit_unit_tests

Review-Url: https://codereview.chromium.org/2588403002
Cr-Commit-Position: refs/heads/master@{#443219}
parent 552a72f3
......@@ -75,7 +75,7 @@ TEST_F(IdleDeadlineTest, deadlineInPast) {
}
TEST_F(IdleDeadlineTest, yieldForHighPriorityWork) {
MockPlatform platform;
ScopedTestingPlatformSupport<MockPlatform> platform;
IdleDeadline* deadline =
IdleDeadline::create(1.25, IdleDeadline::CallbackType::CalledWhenIdle);
......
......@@ -43,7 +43,10 @@ class ScriptRunnerTest : public testing::Test {
: m_document(Document::create()),
m_element(m_document->createElement("foo")) {}
void SetUp() override {
void TearDown() override { m_scriptRunner.release(); }
protected:
void initialize() {
// We have to create ScriptRunner after initializing platform, because we
// need Platform::current()->currentThread()->scheduler()->
// loadingTaskRunner() to be initialized before creating ScriptRunner to
......@@ -51,25 +54,30 @@ class ScriptRunnerTest : public testing::Test {
m_scriptRunner = ScriptRunner::create(m_document.get());
}
void TearDown() override { m_scriptRunner.release(); }
Persistent<Document> m_document;
Persistent<Element> m_element;
TestingPlatformSupportWithMockScheduler m_platform;
Persistent<ScriptRunner> m_scriptRunner;
WTF::Vector<int> m_order;
};
TEST_F(ScriptRunnerTest, QueueSingleScript_Async) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoader = MockScriptLoader::create(m_element.get());
m_scriptRunner->queueScriptForExecution(scriptLoader, ScriptRunner::Async);
m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::Async);
EXPECT_CALL(*scriptLoader, execute());
m_platform.runUntilIdle();
platform->runUntilIdle();
}
TEST_F(ScriptRunnerTest, QueueSingleScript_InOrder) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoader = MockScriptLoader::create(m_element.get());
m_scriptRunner->queueScriptForExecution(scriptLoader, ScriptRunner::InOrder);
......@@ -78,10 +86,14 @@ TEST_F(ScriptRunnerTest, QueueSingleScript_InOrder) {
m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::InOrder);
m_platform.runUntilIdle();
platform->runUntilIdle();
}
TEST_F(ScriptRunnerTest, QueueMultipleScripts_InOrder) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
......@@ -113,7 +125,7 @@ TEST_F(ScriptRunnerTest, QueueMultipleScripts_InOrder) {
for (int i = 2; i >= 0; i--) {
isReady[i] = true;
m_scriptRunner->notifyScriptReady(scriptLoaders[i], ScriptRunner::InOrder);
m_platform.runUntilIdle();
platform->runUntilIdle();
}
// But ensure the scripts were run in the expected order.
......@@ -121,6 +133,10 @@ TEST_F(ScriptRunnerTest, QueueMultipleScripts_InOrder) {
}
TEST_F(ScriptRunnerTest, QueueMixedScripts) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
......@@ -163,13 +179,17 @@ TEST_F(ScriptRunnerTest, QueueMixedScripts) {
m_order.push_back(5);
}));
m_platform.runUntilIdle();
platform->runUntilIdle();
// Async tasks are expected to run first.
EXPECT_THAT(m_order, ElementsAre(4, 5, 1, 2, 3));
}
TEST_F(ScriptRunnerTest, QueueReentrantScript_Async) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
......@@ -197,17 +217,21 @@ TEST_F(ScriptRunnerTest, QueueReentrantScript_Async) {
// Make sure that re-entrant calls to notifyScriptReady don't cause
// ScriptRunner::execute to do more work than expected.
m_platform.runSingleTask();
platform->runSingleTask();
EXPECT_THAT(m_order, ElementsAre(1));
m_platform.runSingleTask();
platform->runSingleTask();
EXPECT_THAT(m_order, ElementsAre(1, 2));
m_platform.runSingleTask();
platform->runSingleTask();
EXPECT_THAT(m_order, ElementsAre(1, 2, 3));
}
TEST_F(ScriptRunnerTest, QueueReentrantScript_InOrder) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
......@@ -243,17 +267,21 @@ TEST_F(ScriptRunnerTest, QueueReentrantScript_InOrder) {
// Make sure that re-entrant calls to queueScriptForExecution don't cause
// ScriptRunner::execute to do more work than expected.
m_platform.runSingleTask();
platform->runSingleTask();
EXPECT_THAT(m_order, ElementsAre(1));
m_platform.runSingleTask();
platform->runSingleTask();
EXPECT_THAT(m_order, ElementsAre(1, 2));
m_platform.runSingleTask();
platform->runSingleTask();
EXPECT_THAT(m_order, ElementsAre(1, 2, 3));
}
TEST_F(ScriptRunnerTest, QueueReentrantScript_ManyAsyncScripts) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoaders[20];
for (int i = 0; i < 20; i++)
scriptLoaders[i] = nullptr;
......@@ -283,7 +311,7 @@ TEST_F(ScriptRunnerTest, QueueReentrantScript_ManyAsyncScripts) {
m_order.push_back(0);
}));
m_platform.runUntilIdle();
platform->runUntilIdle();
int expected[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
......@@ -292,6 +320,10 @@ TEST_F(ScriptRunnerTest, QueueReentrantScript_ManyAsyncScripts) {
}
TEST_F(ScriptRunnerTest, ResumeAndSuspend_InOrder) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
......@@ -324,16 +356,20 @@ TEST_F(ScriptRunnerTest, ResumeAndSuspend_InOrder) {
EXPECT_CALL(*scriptLoader3, isReady()).WillRepeatedly(Return(true));
m_scriptRunner->notifyScriptReady(scriptLoader3, ScriptRunner::InOrder);
m_platform.runSingleTask();
platform->runSingleTask();
m_scriptRunner->suspend();
m_scriptRunner->resume();
m_platform.runUntilIdle();
platform->runUntilIdle();
// Make sure elements are correct and in right order.
EXPECT_THAT(m_order, ElementsAre(1, 2, 3));
}
TEST_F(ScriptRunnerTest, ResumeAndSuspend_Async) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
......@@ -356,16 +392,20 @@ TEST_F(ScriptRunnerTest, ResumeAndSuspend_Async) {
m_order.push_back(3);
}));
m_platform.runSingleTask();
platform->runSingleTask();
m_scriptRunner->suspend();
m_scriptRunner->resume();
m_platform.runUntilIdle();
platform->runUntilIdle();
// Make sure elements are correct.
EXPECT_THAT(m_order, WhenSorted(ElementsAre(1, 2, 3)));
}
TEST_F(ScriptRunnerTest, LateNotifications) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
......@@ -383,17 +423,21 @@ TEST_F(ScriptRunnerTest, LateNotifications) {
}));
m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::InOrder);
m_platform.runUntilIdle();
platform->runUntilIdle();
// At this moment all tasks can be already executed. Make sure that we do not
// crash here.
m_scriptRunner->notifyScriptReady(scriptLoader2, ScriptRunner::InOrder);
m_platform.runUntilIdle();
platform->runUntilIdle();
EXPECT_THAT(m_order, ElementsAre(1, 2));
}
TEST_F(ScriptRunnerTest, TasksWithDeadScriptRunner) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
initialize();
Persistent<MockScriptLoader> scriptLoader1 =
MockScriptLoader::create(m_element.get());
Persistent<MockScriptLoader> scriptLoader2 =
......@@ -417,7 +461,7 @@ TEST_F(ScriptRunnerTest, TasksWithDeadScriptRunner) {
EXPECT_CALL(*scriptLoader1, execute()).Times(0);
EXPECT_CALL(*scriptLoader2, execute()).Times(0);
m_platform.runUntilIdle();
platform->runUntilIdle();
}
} // namespace blink
......@@ -55,20 +55,20 @@ void createTestResourceAndSetCachedMetadata(const ResourceResponse& response) {
} // anonymous namespace
TEST(ResourceTest, SetCachedMetadata_SendsMetadataToPlatform) {
MockPlatform mock;
ScopedTestingPlatformSupport<MockPlatform> mock;
ResourceResponse response(createTestResourceResponse());
createTestResourceAndSetCachedMetadata(response);
EXPECT_EQ(1u, mock.cachedURLs().size());
EXPECT_EQ(1u, mock->cachedURLs().size());
}
TEST(
ResourceTest,
SetCachedMetadata_DoesNotSendMetadataToPlatformWhenFetchedViaServiceWorker) {
MockPlatform mock;
ScopedTestingPlatformSupport<MockPlatform> mock;
ResourceResponse response(createTestResourceResponse());
response.setWasFetchedViaServiceWorker(true);
createTestResourceAndSetCachedMetadata(response);
EXPECT_EQ(0u, mock.cachedURLs().size());
EXPECT_EQ(0u, mock->cachedURLs().size());
}
TEST(ResourceTest, RevalidateWithFragment) {
......
......@@ -1161,7 +1161,8 @@ TEST(ImageResourceTest,
}
TEST(ImageResourceTest, PeriodicFlushTest) {
TestingPlatformSupportWithMockScheduler platform;
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.html");
URLTestHelpers::registerMockedURLLoad(testURL, "cancelTest.html",
"text/html");
......@@ -1191,8 +1192,8 @@ TEST(ImageResourceTest, PeriodicFlushTest) {
EXPECT_TRUE(imageResource->getContent()->hasImage());
EXPECT_EQ(1, client->imageChangedCount());
platform.runForPeriodSeconds(1.);
platform.advanceClockSeconds(1.);
platform->runForPeriodSeconds(1.);
platform->advanceClockSeconds(1.);
// Sanity check that we created an image after appending |meaningfulImageSize|
// bytes just once.
......@@ -1216,13 +1217,13 @@ TEST(ImageResourceTest, PeriodicFlushTest) {
EXPECT_EQ(flushCount, client->imageChangedCount());
++bytesSent;
platform.runForPeriodSeconds(0.2001);
platform->runForPeriodSeconds(0.2001);
}
}
// Increasing time by a large number only causes one extra flush.
platform.runForPeriodSeconds(10.);
platform.advanceClockSeconds(10.);
platform->runForPeriodSeconds(10.);
platform->advanceClockSeconds(10.);
EXPECT_FALSE(imageResource->errorOccurred());
ASSERT_TRUE(imageResource->getContent()->hasImage());
EXPECT_FALSE(imageResource->getContent()->getImage()->isNull());
......
......@@ -128,10 +128,11 @@ class AnimationWorkletThreadTest : public ::testing::Test {
RefPtr<SecurityOrigin> m_securityOrigin;
std::unique_ptr<WorkerReportingProxy> m_reportingProxy;
AnimationWorkletTestPlatform m_testPlatform;
};
TEST_F(AnimationWorkletThreadTest, Basic) {
ScopedTestingPlatformSupport<AnimationWorkletTestPlatform> platform;
std::unique_ptr<AnimationWorkletThread> worklet =
createAnimationWorkletThread();
checkWorkletCanExecuteScript(worklet.get());
......@@ -141,6 +142,8 @@ TEST_F(AnimationWorkletThreadTest, Basic) {
// Tests that the same WebThread is used for new worklets if the WebThread is
// still alive.
TEST_F(AnimationWorkletThreadTest, CreateSecondAndTerminateFirst) {
ScopedTestingPlatformSupport<AnimationWorkletTestPlatform> platform;
// Create the first worklet and wait until it is initialized.
std::unique_ptr<AnimationWorkletThread> firstWorklet =
createAnimationWorkletThread();
......@@ -176,6 +179,8 @@ TEST_F(AnimationWorkletThreadTest, CreateSecondAndTerminateFirst) {
// Tests that a new WebThread is created if all existing worklets are
// terminated before a new worklet is created.
TEST_F(AnimationWorkletThreadTest, TerminateFirstAndCreateSecond) {
ScopedTestingPlatformSupport<AnimationWorkletTestPlatform> platform;
// Create the first worklet, wait until it is initialized, and terminate it.
std::unique_ptr<AnimationWorkletThread> worklet =
createAnimationWorkletThread();
......@@ -200,6 +205,8 @@ TEST_F(AnimationWorkletThreadTest, TerminateFirstAndCreateSecond) {
// Tests that v8::Isolate and WebThread are correctly set-up if a worklet is
// created while another is terminating.
TEST_F(AnimationWorkletThreadTest, CreatingSecondDuringTerminationOfFirst) {
ScopedTestingPlatformSupport<AnimationWorkletTestPlatform> platform;
std::unique_ptr<AnimationWorkletThread> firstWorklet =
createAnimationWorkletThread();
checkWorkletCanExecuteScript(firstWorklet.get());
......
......@@ -143,10 +143,11 @@ class CompositorWorkerThreadTest : public ::testing::Test {
RefPtr<SecurityOrigin> m_securityOrigin;
std::unique_ptr<InProcessWorkerObjectProxy> m_objectProxy;
CompositorWorkerTestPlatform m_testPlatform;
};
TEST_F(CompositorWorkerThreadTest, Basic) {
ScopedTestingPlatformSupport<CompositorWorkerTestPlatform> platform;
std::unique_ptr<CompositorWorkerThread> compositorWorker =
createCompositorWorker();
checkWorkerCanExecuteScript(compositorWorker.get());
......@@ -156,6 +157,8 @@ TEST_F(CompositorWorkerThreadTest, Basic) {
// Tests that the same WebThread is used for new workers if the WebThread is
// still alive.
TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) {
ScopedTestingPlatformSupport<CompositorWorkerTestPlatform> platform;
// Create the first worker and wait until it is initialized.
std::unique_ptr<CompositorWorkerThread> firstWorker =
createCompositorWorker();
......@@ -191,6 +194,8 @@ TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) {
// Tests that a new WebThread is created if all existing workers are terminated
// before a new worker is created.
TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) {
ScopedTestingPlatformSupport<CompositorWorkerTestPlatform> platform;
// Create the first worker, wait until it is initialized, and terminate it.
std::unique_ptr<CompositorWorkerThread> compositorWorker =
createCompositorWorker();
......@@ -215,6 +220,8 @@ TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) {
// Tests that v8::Isolate and WebThread are correctly set-up if a worker is
// created while another is terminating.
TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) {
ScopedTestingPlatformSupport<CompositorWorkerTestPlatform> platform;
std::unique_ptr<CompositorWorkerThread> firstWorker =
createCompositorWorker();
checkWorkerCanExecuteScript(firstWorker.get());
......
......@@ -97,22 +97,24 @@ TEST_F(NotificationImageLoaderTest, SuccessTest) {
}
TEST_F(NotificationImageLoaderTest, TimeoutTest) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform;
// To test for a timeout, this needs to override the clock in the platform.
// Just creating the mock platform will do everything to set it up.
TestingPlatformSupportWithMockScheduler testingPlatform;
KURL url = registerMockedURL(kIcon500x500);
loadImage(url);
// Run the platform for kImageFetchTimeoutInMs-1 seconds. This should not
// result in a timeout.
testingPlatform.runForPeriodSeconds(kImageFetchTimeoutInMs / 1000 - 1);
platform->runForPeriodSeconds(kImageFetchTimeoutInMs / 1000 - 1);
EXPECT_EQ(LoadState::kNotLoaded, loaded());
m_histogramTester.expectTotalCount("Notifications.LoadFinishTime.Icon", 0);
m_histogramTester.expectTotalCount("Notifications.LoadFileSize.Icon", 0);
m_histogramTester.expectTotalCount("Notifications.LoadFailTime.Icon", 0);
// Now advance time until a timeout should be expected.
testingPlatform.runForPeriodSeconds(2);
platform->runForPeriodSeconds(2);
// If the loader times out, it calls the callback and returns an empty bitmap.
EXPECT_EQ(LoadState::kLoadFailed, loaded());
......
......@@ -123,11 +123,10 @@ class BaseAudioContextTest : public ::testing::Test {
Persistent<DummyFrameOwner> m_dummyFrameOwner;
Persistent<LocalFrame> m_childFrame;
BaseAudioContextTestPlatform m_testPlatform;
};
TEST_F(BaseAudioContextTest, AutoplayMetrics_NoRestriction) {
ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> platform;
HistogramTester histogramTester;
BaseAudioContext* audioContext =
......@@ -138,6 +137,7 @@ TEST_F(BaseAudioContextTest, AutoplayMetrics_NoRestriction) {
}
TEST_F(BaseAudioContextTest, AutoplayMetrics_CreateNoGesture) {
ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> platform;
HistogramTester histogramTester;
createChildFrame();
childDocument().settings()->setMediaPlaybackRequiresUserGesture(true);
......@@ -152,6 +152,7 @@ TEST_F(BaseAudioContextTest, AutoplayMetrics_CreateNoGesture) {
}
TEST_F(BaseAudioContextTest, AutoplayMetrics_CallResumeNoGesture) {
ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> platform;
HistogramTester histogramTester;
createChildFrame();
childDocument().settings()->setMediaPlaybackRequiresUserGesture(true);
......@@ -170,6 +171,7 @@ TEST_F(BaseAudioContextTest, AutoplayMetrics_CallResumeNoGesture) {
}
TEST_F(BaseAudioContextTest, AutoplayMetrics_CreateGesture) {
ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> platform;
HistogramTester histogramTester;
createChildFrame();
childDocument().settings()->setMediaPlaybackRequiresUserGesture(true);
......@@ -187,6 +189,7 @@ TEST_F(BaseAudioContextTest, AutoplayMetrics_CreateGesture) {
}
TEST_F(BaseAudioContextTest, AutoplayMetrics_CallResumeGesture) {
ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> platform;
HistogramTester histogramTester;
createChildFrame();
childDocument().settings()->setMediaPlaybackRequiresUserGesture(true);
......@@ -209,6 +212,7 @@ TEST_F(BaseAudioContextTest, AutoplayMetrics_CallResumeGesture) {
}
TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartNoGesture) {
ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> platform;
HistogramTester histogramTester;
createChildFrame();
childDocument().settings()->setMediaPlaybackRequiresUserGesture(true);
......@@ -224,6 +228,7 @@ TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartNoGesture) {
}
TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartGesture) {
ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> platform;
HistogramTester histogramTester;
createChildFrame();
childDocument().settings()->setMediaPlaybackRequiresUserGesture(true);
......@@ -242,6 +247,7 @@ TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartGesture) {
}
TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartNoGestureThenSuccess) {
ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> platform;
HistogramTester histogramTester;
createChildFrame();
childDocument().settings()->setMediaPlaybackRequiresUserGesture(true);
......@@ -264,6 +270,7 @@ TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartNoGestureThenSuccess) {
}
TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartGestureThenSucces) {
ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> platform;
HistogramTester histogramTester;
createChildFrame();
childDocument().settings()->setMediaPlaybackRequiresUserGesture(true);
......
......@@ -12,18 +12,10 @@
namespace blink {
class EmptyPlatform : public TestingPlatformSupport {
public:
EmptyPlatform() {}
~EmptyPlatform() override {}
};
TEST(FontCache, getLastResortFallbackFont) {
FontCache* fontCache = FontCache::fontCache();
ASSERT_TRUE(fontCache);
EmptyPlatform platform;
FontDescription fontDescription;
fontDescription.setGenericFamily(FontDescription::StandardFamily);
RefPtr<SimpleFontData> fontData =
......
......@@ -1289,7 +1289,7 @@ TEST_F(Canvas2DLayerBridgeTest, DISABLED_DeleteIOSurfaceAfterTeardown)
#endif
{
FakeGLES2InterfaceWithImageSupport gl;
FakePlatformSupport testingPlatformSupport;
ScopedTestingPlatformSupport<FakePlatformSupport> platform;
std::unique_ptr<FakeWebGraphicsContext3DProvider> contextProvider =
WTF::wrapUnique(new FakeWebGraphicsContext3DProvider(&gl));
......
......@@ -231,14 +231,15 @@ class RecordingImageBufferSurfaceTest : public Test {
std::unique_ptr<ImageBuffer> m_imageBuffer;
};
#define CALL_TEST_TASK_WRAPPER(TEST_METHOD) \
{ \
TestingPlatformSupportWithMockScheduler testingPlatform; \
Platform::current()->currentThread()->getWebTaskRunner()->postTask( \
BLINK_FROM_HERE, \
WTF::bind(&RecordingImageBufferSurfaceTest::TEST_METHOD, \
WTF::unretained(this))); \
testingPlatform.runUntilIdle(); \
#define CALL_TEST_TASK_WRAPPER(TEST_METHOD) \
{ \
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> \
platform; \
Platform::current()->currentThread()->getWebTaskRunner()->postTask( \
BLINK_FROM_HERE, \
WTF::bind(&RecordingImageBufferSurfaceTest::TEST_METHOD, \
WTF::unretained(this))); \
platform->runUntilIdle(); \
}
TEST_F(RecordingImageBufferSurfaceTest, testEmptyPicture) {
......
......@@ -35,6 +35,10 @@ class ScrollbarThemeWithMockInvalidation : public ScrollbarThemeMock {
class ScrollableAreaTest : public ScrollbarTestSuite {};
TEST_F(ScrollableAreaTest, ScrollAnimatorCurrentPositionShouldBeSync) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
MockScrollableArea* scrollableArea =
MockScrollableArea::create(ScrollOffset(0, 100));
scrollableArea->setScrollOffset(ScrollOffset(0, 10000), CompositorScroll);
......@@ -42,6 +46,10 @@ TEST_F(ScrollableAreaTest, ScrollAnimatorCurrentPositionShouldBeSync) {
}
TEST_F(ScrollableAreaTest, ScrollbarTrackAndThumbRepaint) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
ScrollbarThemeWithMockInvalidation theme;
MockScrollableArea* scrollableArea =
MockScrollableArea::create(ScrollOffset(0, 100));
......@@ -81,6 +89,10 @@ TEST_F(ScrollableAreaTest, ScrollbarTrackAndThumbRepaint) {
}
TEST_F(ScrollableAreaTest, ScrollbarGraphicsLayerInvalidation) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
ScrollbarTheme::setMockScrollbarsEnabled(true);
MockScrollableArea* scrollableArea =
MockScrollableArea::create(ScrollOffset(0, 100));
......@@ -104,6 +116,10 @@ TEST_F(ScrollableAreaTest, ScrollbarGraphicsLayerInvalidation) {
}
TEST_F(ScrollableAreaTest, InvalidatesNonCompositedScrollbarsWhenThumbMoves) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
ScrollbarThemeWithMockInvalidation theme;
MockScrollableArea* scrollableArea =
MockScrollableArea::create(ScrollOffset(100, 100));
......@@ -144,6 +160,10 @@ TEST_F(ScrollableAreaTest, InvalidatesNonCompositedScrollbarsWhenThumbMoves) {
}
TEST_F(ScrollableAreaTest, InvalidatesCompositedScrollbarsIfPartsNeedRepaint) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
ScrollbarThemeWithMockInvalidation theme;
MockScrollableArea* scrollableArea =
MockScrollableArea::create(ScrollOffset(100, 100));
......@@ -222,6 +242,10 @@ TEST_F(ScrollableAreaTest, InvalidatesCompositedScrollbarsIfPartsNeedRepaint) {
}
TEST_F(ScrollableAreaTest, RecalculatesScrollbarOverlayIfBackgroundChanges) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
MockScrollableArea* scrollableArea =
MockScrollableArea::create(ScrollOffset(0, 100));
......
......@@ -87,16 +87,11 @@ class ScrollbarTestSuite : public testing::Test {
ScrollbarTestSuite() {}
void SetUp() override {
TestingPlatformSupport::Config config;
config.compositorSupport = Platform::current()->compositorSupport();
m_fakePlatform =
WTF::makeUnique<TestingPlatformSupportWithMockScheduler>(config);
m_config.compositorSupport = Platform::current()->compositorSupport();
}
void TearDown() override { m_fakePlatform = nullptr; }
private:
std::unique_ptr<TestingPlatformSupportWithMockScheduler> m_fakePlatform;
protected:
TestingPlatformSupport::Config m_config;
};
} // namespace blink
......
......@@ -31,6 +31,10 @@ class ScrollbarThemeAuraButtonOverride final : public ScrollbarThemeAura {
class ScrollbarThemeAuraTest : public ScrollbarTestSuite {};
TEST_F(ScrollbarThemeAuraTest, ButtonSizeHorizontal) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
MockScrollableArea* mockScrollableArea = MockScrollableArea::create();
ScrollbarThemeMock mockTheme;
Scrollbar* scrollbar = Scrollbar::createForTesting(
......@@ -53,6 +57,10 @@ TEST_F(ScrollbarThemeAuraTest, ButtonSizeHorizontal) {
}
TEST_F(ScrollbarThemeAuraTest, ButtonSizeVertical) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
MockScrollableArea* mockScrollableArea = MockScrollableArea::create();
ScrollbarThemeMock mockTheme;
Scrollbar* scrollbar = Scrollbar::createForTesting(
......@@ -75,6 +83,10 @@ TEST_F(ScrollbarThemeAuraTest, ButtonSizeVertical) {
}
TEST_F(ScrollbarThemeAuraTest, NoButtonsReturnsSize0) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
MockScrollableArea* mockScrollableArea = MockScrollableArea::create();
ScrollbarThemeMock mockTheme;
Scrollbar* scrollbar = Scrollbar::createForTesting(
......
......@@ -14,6 +14,10 @@ using testing::Return;
class ScrollbarThemeOverlayTest : public ScrollbarTestSuite {};
TEST_F(ScrollbarThemeOverlayTest, PaintInvalidation) {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler,
const TestingPlatformSupport::Config&>
platform(m_config);
NiceMock<MockScrollableArea>* mockScrollableArea =
new NiceMock<MockScrollableArea>(ScrollOffset(100, 100));
ScrollbarThemeOverlay theme(14, 0, ScrollbarThemeOverlay::AllowHitTest);
......
......@@ -141,12 +141,11 @@ TestingPlatformSupport::TestingPlatformSupport(const Config& config)
: m_config(config),
m_oldPlatform(Platform::current()),
m_interfaceProvider(new TestingInterfaceProvider) {
ASSERT(m_oldPlatform);
Platform::setCurrentPlatformForTesting(this);
DCHECK(m_oldPlatform);
}
TestingPlatformSupport::~TestingPlatformSupport() {
Platform::setCurrentPlatformForTesting(m_oldPlatform);
DCHECK_EQ(this, Platform::current());
}
WebString TestingPlatformSupport::defaultLocale() {
......@@ -323,8 +322,8 @@ ScopedUnittestsEnvironmentSetup::ScopedUnittestsEnvironmentSetup(int argc,
m_discardableMemoryAllocator.get());
base::StatisticsRecorder::Initialize();
m_platform = WTF::wrapUnique(new DummyPlatform);
Platform::setCurrentPlatformForTesting(m_platform.get());
m_dummyPlatform = WTF::wrapUnique(new DummyPlatform);
Platform::setCurrentPlatformForTesting(m_dummyPlatform.get());
WTF::Partitions::initialize(nullptr);
WTF::setTimeFunctionsForTesting(dummyCurrentTime);
......@@ -333,7 +332,8 @@ ScopedUnittestsEnvironmentSetup::ScopedUnittestsEnvironmentSetup(int argc,
m_compositorSupport = WTF::wrapUnique(new cc_blink::WebCompositorSupportImpl);
m_testingPlatformConfig.compositorSupport = m_compositorSupport.get();
m_testingPlatformSupport =
WTF::makeUnique<TestingPlatformSupport>(m_testingPlatformConfig);
WTF::wrapUnique(new TestingPlatformSupport(m_testingPlatformConfig));
Platform::setCurrentPlatformForTesting(m_testingPlatformSupport.get());
ProcessHeap::init();
ThreadState::attachMainThread();
......
......@@ -37,8 +37,12 @@
#include "public/platform/WebCompositorSupport.h"
#include "public/platform/WebScheduler.h"
#include "public/platform/WebThread.h"
#include "wtf/Allocator.h"
#include "wtf/Assertions.h"
#include "wtf/PtrUtil.h"
#include "wtf/Vector.h"
#include <memory>
#include <utility>
namespace base {
class SimpleTestTickClock;
......@@ -107,6 +111,9 @@ class TestingPlatformMockScheduler : public WebScheduler {
void removePendingNavigation(WebScheduler::NavigatingFrameType) override {}
};
// A base class to override Platform methods for testing. You can override the
// behavior by subclassing TestingPlatformSupport or using
// ScopedTestingPlatformSupport (see below).
class TestingPlatformSupport : public Platform {
WTF_MAKE_NONCOPYABLE(TestingPlatformSupport);
......@@ -142,6 +149,8 @@ class TestingPlatformSupport : public Platform {
std::unique_ptr<TestingInterfaceProvider> m_interfaceProvider;
};
// This class adds mocked scheduler support to TestingPlatformSupport. See also
// ScopedTestingPlatformSupport to use this class correctly.
class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport {
WTF_MAKE_NONCOPYABLE(TestingPlatformSupportWithMockScheduler);
......@@ -188,7 +197,54 @@ class TestingPlatformSupportWithMockScheduler : public TestingPlatformSupport {
std::unique_ptr<WebThread> m_thread;
};
class ScopedUnittestsEnvironmentSetup {
// ScopedTestingPlatformSupport<MyTestingPlatformSupport> can be used to
// override Platform::current() with MyTestingPlatformSupport, like this:
//
// #include "platform/testing/TestingPlatformSupport.h"
//
// TEST_F(SampleTest, sampleTest) {
// ScopedTestingPlatformSupport<MyTestingPlatformSupport> platform;
// ...
// // You can call methods of MyTestingPlatformSupport.
// EXPECT_TRUE(platform->myMethodIsCalled());
//
// // Another instance can be nested.
// {
// // Constructor's arguments can be passed like this.
// Arg arg;
// ScopedTestingPlatformSupport<MyAnotherTestingPlatformSupport, const Arg&>
// another_platform(args);
// ...
// }
//
// // Here the original MyTestingPlatformSupport should be restored.
// }
template <class T, typename... Args>
class ScopedTestingPlatformSupport final {
STACK_ALLOCATED();
public:
explicit ScopedTestingPlatformSupport(Args&&... args) {
m_testingPlatformSupport = WTF::makeUnique<T>(std::forward<Args>(args)...);
m_originalPlatform = Platform::current();
DCHECK(m_originalPlatform);
Platform::setCurrentPlatformForTesting(m_testingPlatformSupport.get());
}
~ScopedTestingPlatformSupport() {
DCHECK_EQ(m_testingPlatformSupport.get(), Platform::current());
m_testingPlatformSupport.reset();
Platform::setCurrentPlatformForTesting(m_originalPlatform);
}
const T* operator->() const { return m_testingPlatformSupport.get(); }
T* operator->() { return m_testingPlatformSupport.get(); }
private:
std::unique_ptr<T> m_testingPlatformSupport;
Platform* m_originalPlatform;
};
class ScopedUnittestsEnvironmentSetup final {
WTF_MAKE_NONCOPYABLE(ScopedUnittestsEnvironmentSetup);
public:
......@@ -199,7 +255,7 @@ class ScopedUnittestsEnvironmentSetup {
class DummyPlatform;
std::unique_ptr<base::TestDiscardableMemoryAllocator>
m_discardableMemoryAllocator;
std::unique_ptr<DummyPlatform> m_platform;
std::unique_ptr<DummyPlatform> m_dummyPlatform;
std::unique_ptr<cc_blink::WebCompositorSupportImpl> m_compositorSupport;
TestingPlatformSupport::Config m_testingPlatformConfig;
std::unique_ptr<TestingPlatformSupport> m_testingPlatformSupport;
......
......@@ -189,7 +189,7 @@ class LocaleMacTest : public ::testing::Test {
};
TEST_F(LocaleMacTest, formatWeek) {
LocalePlatformSupport support;
ScopedTestingPlatformSupport<LocalePlatformSupport> support;
EXPECT_STREQ("Week 04, 2005", formatWeek("en_US", "2005-W04").utf8().data());
EXPECT_STREQ("Week 52, 2005", formatWeek("en_US", "2005-W52").utf8().data());
}
......
......@@ -68,8 +68,8 @@ class OriginAccessEntryTestPlatform : public TestingPlatformSupport {
};
TEST(OriginAccessEntryTest, PublicSuffixListTest) {
OriginAccessEntryTestPlatform platform;
platform.setPublicSuffix("com");
ScopedTestingPlatformSupport<OriginAccessEntryTestPlatform> platform;
platform->setPublicSuffix("com");
RefPtr<SecurityOrigin> origin =
SecurityOrigin::createFromString("http://www.google.com");
......@@ -135,8 +135,8 @@ TEST(OriginAccessEntryTest, AllowSubdomainsTest) {
OriginAccessEntry::DoesNotMatchOrigin, OriginAccessEntry::MatchesOrigin},
};
OriginAccessEntryTestPlatform platform;
platform.setPublicSuffix("com");
ScopedTestingPlatformSupport<OriginAccessEntryTestPlatform> platform;
platform->setPublicSuffix("com");
for (const auto& test : inputs) {
SCOPED_TRACE(testing::Message() << "Host: " << test.host
......@@ -189,8 +189,8 @@ TEST(OriginAccessEntryTest, AllowRegisterableDomainsTest) {
OriginAccessEntry::DoesNotMatchOrigin},
};
OriginAccessEntryTestPlatform platform;
platform.setPublicSuffix("com");
ScopedTestingPlatformSupport<OriginAccessEntryTestPlatform> platform;
platform->setPublicSuffix("com");
for (const auto& test : inputs) {
RefPtr<SecurityOrigin> originToTest =
......@@ -245,8 +245,8 @@ TEST(OriginAccessEntryTest, AllowRegisterableDomainsTestWithDottedSuffix) {
OriginAccessEntry::DoesNotMatchOrigin},
};
OriginAccessEntryTestPlatform platform;
platform.setPublicSuffix("appspot.com");
ScopedTestingPlatformSupport<OriginAccessEntryTestPlatform> platform;
platform->setPublicSuffix("appspot.com");
for (const auto& test : inputs) {
RefPtr<SecurityOrigin> originToTest =
......@@ -296,8 +296,8 @@ TEST(OriginAccessEntryTest, DisallowSubdomainsTest) {
OriginAccessEntry::DoesNotMatchOrigin},
};
OriginAccessEntryTestPlatform platform;
platform.setPublicSuffix("com");
ScopedTestingPlatformSupport<OriginAccessEntryTestPlatform> platform;
platform->setPublicSuffix("com");
for (const auto& test : inputs) {
SCOPED_TRACE(testing::Message() << "Host: " << test.host
......@@ -327,8 +327,8 @@ TEST(OriginAccessEntryTest, IPAddressTest) {
{"http", "", false},
};
OriginAccessEntryTestPlatform platform;
platform.setPublicSuffix("com");
ScopedTestingPlatformSupport<OriginAccessEntryTestPlatform> platform;
platform->setPublicSuffix("com");
for (const auto& test : inputs) {
SCOPED_TRACE(testing::Message() << "Host: " << test.host);
......@@ -355,8 +355,8 @@ TEST(OriginAccessEntryTest, IPAddressMatchingTest) {
OriginAccessEntry::DoesNotMatchOrigin},
};
OriginAccessEntryTestPlatform platform;
platform.setPublicSuffix("com");
ScopedTestingPlatformSupport<OriginAccessEntryTestPlatform> platform;
platform->setPublicSuffix("com");
for (const auto& test : inputs) {
SCOPED_TRACE(testing::Message() << "Host: " << test.host
......
......@@ -140,6 +140,7 @@ class BLINK_PLATFORM_EXPORT Platform {
static Platform* current();
// Used to switch the current platform only for testing.
// You should not pass in a Platform object that is not fully instantiated.
static void setCurrentPlatformForTesting(Platform*);
// May return null.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment