Commit 092a8aa6 authored by Yuta Kitamura's avatar Yuta Kitamura Committed by Commit Bot

Add Platform test helper allowing custom scheduler.

This patch adds a new test utility for Blink platform. It's called
TestingPlatformSupportWithCustomScheduler, and is a natural extension
of TestingPlatformSupport.

The new utility removes the need of writing custom Platform and
WebThread in tests. Essentially, the objective of this CL is to shift
the responsibility of writing custom WebThread from each test to
platform/testing. Two affected tests are rewritten to use it.

Bug: 826203
Change-Id: I41160fe50286adc3acb09dbd128d654c356d4355
Reviewed-on: https://chromium-review.googlesource.com/1170702
Commit-Queue: Yuta Kitamura <yutak@chromium.org>
Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582883}
parent 338703fb
......@@ -8,7 +8,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support_with_mock_scheduler.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support_with_custom_scheduler.h"
#include "third_party/blink/renderer/platform/testing/wtf/scoped_mock_clock.h"
#include "third_party/blink/renderer/platform/wtf/time.h"
......@@ -61,29 +61,6 @@ class MockIdleDeadlineScheduler final : public ThreadScheduler {
DISALLOW_COPY_AND_ASSIGN(MockIdleDeadlineScheduler);
};
class MockIdleDeadlineThread final : public WebThread {
public:
MockIdleDeadlineThread() = default;
~MockIdleDeadlineThread() override = default;
bool IsCurrentThread() const override { return true; }
ThreadScheduler* Scheduler() const override { return &scheduler_; }
private:
mutable MockIdleDeadlineScheduler scheduler_;
DISALLOW_COPY_AND_ASSIGN(MockIdleDeadlineThread);
};
class MockIdleDeadlinePlatform : public TestingPlatformSupport {
public:
MockIdleDeadlinePlatform() = default;
~MockIdleDeadlinePlatform() override = default;
WebThread* CurrentThread() override { return &thread_; }
private:
MockIdleDeadlineThread thread_;
DISALLOW_COPY_AND_ASSIGN(MockIdleDeadlinePlatform);
};
} // namespace
class IdleDeadlineTest : public testing::Test {
......@@ -110,7 +87,10 @@ TEST_F(IdleDeadlineTest, deadlineInPast) {
}
TEST_F(IdleDeadlineTest, yieldForHighPriorityWork) {
ScopedTestingPlatformSupport<MockIdleDeadlinePlatform> platform;
MockIdleDeadlineScheduler scheduler;
ScopedTestingPlatformSupport<TestingPlatformSupportWithCustomScheduler,
ThreadScheduler*>
platform(&scheduler);
IdleDeadline* deadline =
IdleDeadline::Create(TimeTicks() + TimeDelta::FromSecondsD(1.25),
......
......@@ -11,16 +11,18 @@
#include "third_party/blink/renderer/core/dom/idle_request_options.h"
#include "third_party/blink/renderer/core/testing/null_execution_context.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support_with_custom_scheduler.h"
#include "third_party/blink/renderer/platform/wtf/time.h"
namespace blink {
namespace {
enum class ShouldYield { YIELD, DONT_YIELD };
class MockScriptedIdleTaskControllerScheduler final : public ThreadScheduler {
public:
MockScriptedIdleTaskControllerScheduler(bool should_yield)
: should_yield_(should_yield) {}
explicit MockScriptedIdleTaskControllerScheduler(ShouldYield should_yield)
: should_yield_(should_yield == ShouldYield::YIELD) {}
~MockScriptedIdleTaskControllerScheduler() override = default;
// ThreadScheduler implementation:
......@@ -73,37 +75,6 @@ class MockScriptedIdleTaskControllerScheduler final : public ThreadScheduler {
DISALLOW_COPY_AND_ASSIGN(MockScriptedIdleTaskControllerScheduler);
};
class MockScriptedIdleTaskControllerThread final : public WebThread {
public:
MockScriptedIdleTaskControllerThread(bool should_yield)
: scheduler_(should_yield) {}
~MockScriptedIdleTaskControllerThread() override = default;
bool IsCurrentThread() const override { return true; }
ThreadScheduler* Scheduler() const override { return &scheduler_; }
void RunIdleTask() { scheduler_.RunIdleTask(); }
bool HasIdleTask() const { return scheduler_.HasIdleTask(); }
private:
mutable MockScriptedIdleTaskControllerScheduler scheduler_;
DISALLOW_COPY_AND_ASSIGN(MockScriptedIdleTaskControllerThread);
};
class MockScriptedIdleTaskControllerPlatform : public TestingPlatformSupport {
public:
MockScriptedIdleTaskControllerPlatform(bool should_yield)
: thread_(should_yield) {}
~MockScriptedIdleTaskControllerPlatform() override = default;
WebThread* CurrentThread() override { return &thread_; }
void RunIdleTask() { thread_.RunIdleTask(); }
bool HasIdleTask() const { return thread_.HasIdleTask(); }
private:
MockScriptedIdleTaskControllerThread thread_;
DISALLOW_COPY_AND_ASSIGN(MockScriptedIdleTaskControllerPlatform);
};
class MockIdleTask : public ScriptedIdleTaskController::IdleTask {
public:
MOCK_METHOD1(invoke, void(IdleDeadline*));
......@@ -119,28 +90,34 @@ class ScriptedIdleTaskControllerTest : public testing::Test {
};
TEST_F(ScriptedIdleTaskControllerTest, RunCallback) {
ScopedTestingPlatformSupport<MockScriptedIdleTaskControllerPlatform, bool>
platform(false);
MockScriptedIdleTaskControllerScheduler scheduler(ShouldYield::DONT_YIELD);
ScopedTestingPlatformSupport<TestingPlatformSupportWithCustomScheduler,
ThreadScheduler*>
platform(&scheduler);
NullExecutionContext execution_context;
ScriptedIdleTaskController* controller =
ScriptedIdleTaskController::Create(execution_context_);
Persistent<MockIdleTask> idle_task(new MockIdleTask());
IdleRequestOptions options;
EXPECT_FALSE(platform->HasIdleTask());
EXPECT_FALSE(scheduler.HasIdleTask());
int id = controller->RegisterCallback(idle_task, options);
EXPECT_TRUE(platform->HasIdleTask());
EXPECT_TRUE(scheduler.HasIdleTask());
EXPECT_NE(0, id);
EXPECT_CALL(*idle_task, invoke(testing::_));
platform->RunIdleTask();
scheduler.RunIdleTask();
testing::Mock::VerifyAndClearExpectations(idle_task);
EXPECT_FALSE(platform->HasIdleTask());
EXPECT_FALSE(scheduler.HasIdleTask());
}
TEST_F(ScriptedIdleTaskControllerTest, DontRunCallbackWhenAskedToYield) {
ScopedTestingPlatformSupport<MockScriptedIdleTaskControllerPlatform, bool>
platform(true);
MockScriptedIdleTaskControllerScheduler scheduler(ShouldYield::YIELD);
ScopedTestingPlatformSupport<TestingPlatformSupportWithCustomScheduler,
ThreadScheduler*>
platform(&scheduler);
NullExecutionContext execution_context;
ScriptedIdleTaskController* controller =
ScriptedIdleTaskController::Create(execution_context_);
......@@ -151,11 +128,11 @@ TEST_F(ScriptedIdleTaskControllerTest, DontRunCallbackWhenAskedToYield) {
EXPECT_NE(0, id);
EXPECT_CALL(*idle_task, invoke(testing::_)).Times(0);
platform->RunIdleTask();
scheduler.RunIdleTask();
testing::Mock::VerifyAndClearExpectations(idle_task);
// The idle task should have been reposted.
EXPECT_TRUE(platform->HasIdleTask());
EXPECT_TRUE(scheduler.HasIdleTask());
}
} // namespace blink
......@@ -1596,6 +1596,8 @@ jumbo_static_library("test_support") {
"testing/test_paint_artifact.h",
"testing/testing_platform_support.cc",
"testing/testing_platform_support.h",
"testing/testing_platform_support_with_custom_scheduler.cc",
"testing/testing_platform_support_with_custom_scheduler.h",
"testing/testing_platform_support_with_mock_scheduler.cc",
"testing/testing_platform_support_with_mock_scheduler.h",
"testing/testing_platform_support_with_web_rtc.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/testing/testing_platform_support_with_custom_scheduler.h"
#include "third_party/blink/public/platform/web_thread.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"
namespace blink {
namespace {
class ThreadWithCustomScheduler : public WebThread {
public:
explicit ThreadWithCustomScheduler(ThreadScheduler* scheduler)
: scheduler_(scheduler) {}
~ThreadWithCustomScheduler() override {}
bool IsCurrentThread() const override {
DCHECK(WTF::IsMainThread());
return true;
}
ThreadScheduler* Scheduler() const override { return scheduler_; }
private:
ThreadScheduler* scheduler_;
};
} // namespace
TestingPlatformSupportWithCustomScheduler ::
TestingPlatformSupportWithCustomScheduler(ThreadScheduler* scheduler)
: thread_(std::make_unique<ThreadWithCustomScheduler>(scheduler)) {}
TestingPlatformSupportWithCustomScheduler ::
~TestingPlatformSupportWithCustomScheduler() {}
WebThread* TestingPlatformSupportWithCustomScheduler::CurrentThread() {
DCHECK(WTF::IsMainThread());
return thread_.get();
}
} // namespace blink
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_TESTING_TESTING_PLATFORM_SUPPORT_WITH_CUSTOM_SCHEDULER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_TESTING_TESTING_PLATFORM_SUPPORT_WITH_CUSTOM_SCHEDULER_H_
#include <memory>
#include "base/macros.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
// Test environment where you can inject your custom implementation of
// ThreadScheduler on the main thread. Multi-thread is not supported.
//
// You would probably like to use this with ScopedTestingPlatformSupport
// class template. See testing_platform_support.h for details.
namespace blink {
class TestingPlatformSupportWithCustomScheduler
: public TestingPlatformSupport {
public:
// |scheduler| must be owned by the caller.
explicit TestingPlatformSupportWithCustomScheduler(
ThreadScheduler* scheduler);
~TestingPlatformSupportWithCustomScheduler() override;
WebThread* CurrentThread() override;
private:
std::unique_ptr<WebThread> thread_;
DISALLOW_COPY_AND_ASSIGN(TestingPlatformSupportWithCustomScheduler);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_TESTING_TESTING_PLATFORM_SUPPORT_WITH_CUSTOM_SCHEDULER_H_
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