Index: third_party/WebKit/Source/platform/scheduler/child/worker_global_scope_scheduler_unittest.cc |
diff --git a/third_party/WebKit/Source/platform/scheduler/child/worker_global_scope_scheduler_unittest.cc b/third_party/WebKit/Source/platform/scheduler/child/worker_global_scope_scheduler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..460749a24a7527a7834ce4e3eed7d9189095f8ef |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/scheduler/child/worker_global_scope_scheduler_unittest.cc |
@@ -0,0 +1,91 @@ |
+// Copyright 2017 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 "platform/scheduler/child/worker_global_scope_scheduler.h" |
+ |
+#include "base/macros.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/test/simple_test_tick_clock.h" |
+#include "cc/test/ordered_simple_task_runner.h" |
+#include "platform/scheduler/base/test_time_source.h" |
+#include "platform/scheduler/child/scheduler_tqm_delegate_for_test.h" |
+#include "platform/scheduler/child/worker_scheduler_impl.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using testing::ElementsAreArray; |
+ |
+namespace blink { |
+namespace scheduler { |
+ |
+namespace { |
+ |
+void AppendToVectorTestTask(std::vector<std::string>* vector, |
+ std::string value) { |
+ vector->push_back(value); |
+} |
+ |
+} // namespace |
+ |
+class WorkerGlobalScopeSchedulerTest : public testing::Test { |
+ public: |
+ WorkerGlobalScopeSchedulerTest() |
+ : clock_(new base::SimpleTestTickClock()), |
+ mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_.get(), true)), |
kinuko
2017/04/17 05:01:11
We don't have a handy one outside cc class? (Just
nhiroki
2017/04/17 11:03:46
Apparently, base::TestSimpleTaskRunner would be av
|
+ main_task_runner_(SchedulerTqmDelegateForTest::Create( |
+ mock_task_runner_, |
+ base::WrapUnique(new TestTimeSource(clock_.get())))), |
+ scheduler_(new WorkerSchedulerImpl(main_task_runner_)) { |
+ clock_->Advance(base::TimeDelta::FromMicroseconds(5000)); |
+ } |
+ |
+ ~WorkerGlobalScopeSchedulerTest() override {} |
+ |
+ void SetUp() override { |
+ scheduler_->Init(); |
+ global_scope_scheduler_ = |
+ base::MakeUnique<WorkerGlobalScopeScheduler>(scheduler_.get()); |
+ } |
+ |
+ void RunUntilIdle() { mock_task_runner_->RunUntilIdle(); } |
+ |
+ // Helper for posting a task. |
+ void PostTestTask(std::vector<std::string>* run_order, |
+ const std::string& task_descriptor) { |
+ global_scope_scheduler_->UnthrottledTaskRunner()->PostTask( |
+ FROM_HERE, WTF::Bind(&AppendToVectorTestTask, |
+ WTF::Unretained(run_order), task_descriptor)); |
+ } |
+ |
+ protected: |
+ std::unique_ptr<base::SimpleTestTickClock> clock_; |
+ scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; |
+ |
+ scoped_refptr<SchedulerTqmDelegate> main_task_runner_; |
+ std::unique_ptr<WorkerSchedulerImpl> scheduler_; |
+ std::unique_ptr<WorkerGlobalScopeScheduler> global_scope_scheduler_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WorkerGlobalScopeSchedulerTest); |
+}; |
+ |
+TEST_F(WorkerGlobalScopeSchedulerTest, TestPostTasks) { |
+ std::vector<std::string> run_order; |
+ PostTestTask(&run_order, "T1"); |
+ PostTestTask(&run_order, "T2"); |
+ RunUntilIdle(); |
+ PostTestTask(&run_order, "T3"); |
+ RunUntilIdle(); |
+ EXPECT_THAT(run_order, testing::ElementsAre("T1", "T2", "T3")); |
+ |
+ // Tasks should not run after the scheduler is disposed of. |
+ global_scope_scheduler_->Dispose(); |
+ run_order.clear(); |
+ PostTestTask(&run_order, "T4"); |
+ PostTestTask(&run_order, "T5"); |
+ RunUntilIdle(); |
+ EXPECT_TRUE(run_order.empty()); |
+} |
+ |
+} // namespace scheduler |
+} // namespace blink |