Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/child/scheduler/webthread_impl_for_worker_scheduler.h" | |
| 6 | |
| 7 #include "base/synchronization/waitable_event.h" | |
| 8 #include "content/child/scheduler/worker_scheduler_impl.h" | |
| 9 #include "testing/gmock/include/gmock/gmock.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "third_party/WebKit/public/platform/WebTraceLocation.h" | |
| 12 | |
| 13 using testing::AnyOf; | |
| 14 using testing::ElementsAre; | |
| 15 using testing::Invoke; | |
| 16 | |
| 17 namespace content { | |
| 18 namespace { | |
| 19 | |
| 20 class MockTask : public blink::WebThread::Task { | |
| 21 public: | |
| 22 MOCK_METHOD0(run, void()); | |
| 23 }; | |
| 24 | |
| 25 class TestObserver : public blink::WebThread::TaskObserver { | |
| 26 public: | |
| 27 explicit TestObserver(std::string* calls) : calls_(calls) {} | |
| 28 | |
| 29 ~TestObserver() override {} | |
| 30 | |
| 31 void willProcessTask() override { calls_->append(" willProcessTask"); } | |
| 32 | |
| 33 void didProcessTask() override { calls_->append(" didProcessTask"); } | |
| 34 | |
| 35 private: | |
| 36 std::string* calls_; // NOT OWNED | |
| 37 }; | |
| 38 | |
| 39 class TestTask : public blink::WebThread::Task { | |
| 40 public: | |
| 41 explicit TestTask(std::string* calls) : calls_(calls) {} | |
| 42 | |
| 43 ~TestTask() override {} | |
| 44 | |
| 45 void run() override { calls_->append(" run"); } | |
| 46 | |
| 47 private: | |
| 48 std::string* calls_; // NOT OWNED | |
| 49 }; | |
| 50 | |
| 51 void addTaskObserver(WebThreadImplForWorkerScheduler* thread, | |
| 52 TestObserver* observer) { | |
| 53 thread->addTaskObserver(observer); | |
| 54 } | |
| 55 | |
| 56 void removeTaskObserver(WebThreadImplForWorkerScheduler* thread, | |
| 57 TestObserver* observer) { | |
| 58 thread->removeTaskObserver(observer); | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 class WebThreadImplForWorkerSchedulerTest : public testing::Test { | |
| 64 public: | |
| 65 WebThreadImplForWorkerSchedulerTest() : thread_("test thread") {} | |
| 66 | |
| 67 ~WebThreadImplForWorkerSchedulerTest() override {} | |
| 68 | |
| 69 void RunOnWorkerThread(const tracked_objects::Location& from_here, | |
| 70 const base::Closure& task) { | |
| 71 base::WaitableEvent completion(false, false); | |
| 72 thread_.TaskRunner()->PostTask( | |
| 73 from_here, | |
| 74 base::Bind(&WebThreadImplForWorkerSchedulerTest::RunOnWorkerThreadTask, | |
| 75 base::Unretained(this), task, &completion)); | |
| 76 completion.Wait(); | |
| 77 } | |
| 78 | |
| 79 protected: | |
| 80 void RunOnWorkerThreadTask(const base::Closure& task, | |
| 81 base::WaitableEvent* completion) { | |
| 82 task.Run(); | |
| 83 completion->Signal(); | |
| 84 } | |
| 85 | |
| 86 WebThreadImplForWorkerScheduler thread_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(WebThreadImplForWorkerSchedulerTest); | |
| 89 }; | |
| 90 | |
| 91 TEST_F(WebThreadImplForWorkerSchedulerTest, TestDefaultTask) { | |
| 92 scoped_ptr<MockTask> task(new MockTask()); | |
| 93 base::WaitableEvent completion(false, false); | |
| 94 | |
| 95 EXPECT_CALL(*task, run()); | |
| 96 ON_CALL(*task, run()) | |
| 97 .WillByDefault(Invoke([&completion]() { completion.Signal(); })); | |
| 98 | |
| 99 thread_.postTask(blink::WebTraceLocation(), task.release()); | |
| 100 completion.Wait(); | |
| 101 } | |
| 102 | |
| 103 TEST_F(WebThreadImplForWorkerSchedulerTest, TestTaskObserver) { | |
| 104 std::string calls; | |
| 105 TestObserver observer(&calls); | |
| 106 | |
| 107 RunOnWorkerThread(FROM_HERE, | |
| 108 base::Bind(&addTaskObserver, &thread_, &observer)); | |
| 109 thread_.postTask(blink::WebTraceLocation(), new TestTask(&calls)); | |
| 110 RunOnWorkerThread(FROM_HERE, | |
| 111 base::Bind(&removeTaskObserver, &thread_, &observer)); | |
| 112 | |
| 113 // We need to be careful what we test here. We want to make sure the | |
| 114 // observers are un in the expected order before and after the task. | |
| 115 // Sometimes we get an internal scheduler task running before or after | |
| 116 // TestTask as well. This is not a bug, and we need to make sure the test | |
| 117 // doesn't fail when that happens. | |
| 118 EXPECT_THAT(calls, testing::HasSubstr("willProcessTask run didProcessTask")); | |
| 119 } | |
| 120 | |
|
rmcilroy
2015/04/08 18:34:24
How about some idle task tests too?
alex clarke (OOO till 29th)
2015/04/09 10:02:37
So I agree we should have a test to confirm that i
| |
| 121 } // namespace content | |
| OLD | NEW |