1 // Copyright 2018 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 "base/task/sequence_manager/test/sequence_manager_for_test.h"
6
7 #include "base/task/sequence_manager/thread_controller_impl.h"
8
9 namespace base {
10 namespace sequence_manager {
11
12 namespace {
13
14 class ThreadControllerForTest : public internal::ThreadControllerImpl {
15 public:
ThreadControllerForTest(MessageLoop * message_loop,scoped_refptr<SingleThreadTaskRunner> task_runner,const TickClock * time_source)16 ThreadControllerForTest(MessageLoop* message_loop,
17 scoped_refptr<SingleThreadTaskRunner> task_runner,
18 const TickClock* time_source)
19 : ThreadControllerImpl(message_loop,
20 std::move(task_runner),
21 time_source) {}
22
AddNestingObserver(RunLoop::NestingObserver * observer)23 void AddNestingObserver(RunLoop::NestingObserver* observer) override {
24 if (!message_loop_)
25 return;
26 ThreadControllerImpl::AddNestingObserver(observer);
27 }
28
RemoveNestingObserver(RunLoop::NestingObserver * observer)29 void RemoveNestingObserver(RunLoop::NestingObserver* observer) override {
30 if (!message_loop_)
31 return;
32 ThreadControllerImpl::RemoveNestingObserver(observer);
33 }
34
35 ~ThreadControllerForTest() override = default;
36 };
37
38 } // namespace
39
SequenceManagerForTest(std::unique_ptr<internal::ThreadController> thread_controller)40 SequenceManagerForTest::SequenceManagerForTest(
41 std::unique_ptr<internal::ThreadController> thread_controller)
42 : SequenceManagerImpl(std::move(thread_controller)) {}
43
44 // static
Create(MessageLoop * message_loop,scoped_refptr<SingleThreadTaskRunner> task_runner,const TickClock * clock)45 std::unique_ptr<SequenceManagerForTest> SequenceManagerForTest::Create(
46 MessageLoop* message_loop,
47 scoped_refptr<SingleThreadTaskRunner> task_runner,
48 const TickClock* clock) {
49 return std::make_unique<SequenceManagerForTest>(
50 std::make_unique<ThreadControllerForTest>(message_loop,
51 std::move(task_runner), clock));
52 }
53
ActiveQueuesCount() const54 size_t SequenceManagerForTest::ActiveQueuesCount() const {
55 return main_thread_only().active_queues.size();
56 }
57
HasImmediateWork() const58 bool SequenceManagerForTest::HasImmediateWork() const {
59 return !main_thread_only().selector.AllEnabledWorkQueuesAreEmpty();
60 }
61
PendingTasksCount() const62 size_t SequenceManagerForTest::PendingTasksCount() const {
63 size_t task_count = 0;
64 for (auto* const queue : main_thread_only().active_queues)
65 task_count += queue->GetNumberOfPendingTasks();
66 return task_count;
67 }
68
QueuesToDeleteCount() const69 size_t SequenceManagerForTest::QueuesToDeleteCount() const {
70 return main_thread_only().queues_to_delete.size();
71 }
72
QueuesToShutdownCount()73 size_t SequenceManagerForTest::QueuesToShutdownCount() {
74 TakeQueuesToGracefullyShutdownFromHelper();
75 return main_thread_only().queues_to_gracefully_shutdown.size();
76 }
77
78 } // namespace sequence_manager
79 } // namespace base
80