1 // Copyright 2018 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_TEST_TEST_WITH_TASK_ENVIRONMENT_H_ 6 #define NET_TEST_TEST_WITH_TASK_ENVIRONMENT_H_ 7 8 #include <memory> 9 10 #include "base/test/task_environment.h" 11 #include "base/time/time.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace base { 15 class TickClock; 16 } // namespace base 17 18 namespace net { 19 20 class TestNetLogManager; 21 22 // Inherit from this class if a TaskEnvironment is needed in a test. 23 // Use in class hierachies where inheritance from ::testing::Test at the same 24 // time is not desirable or possible (for example, when inheriting from 25 // PlatformTest at the same time). 26 class WithTaskEnvironment { 27 public: 28 WithTaskEnvironment(const WithTaskEnvironment&) = delete; 29 WithTaskEnvironment& operator=(const WithTaskEnvironment&) = delete; 30 31 protected: 32 // Always uses MainThreadType::IO, |time_source| may optionally be provided 33 // to mock time. 34 explicit WithTaskEnvironment( 35 base::test::TaskEnvironment::TimeSource time_source = 36 base::test::TaskEnvironment::TimeSource::DEFAULT); 37 38 ~WithTaskEnvironment(); 39 MainThreadIsIdle()40 [[nodiscard]] bool MainThreadIsIdle() const { 41 return task_environment_.MainThreadIsIdle(); 42 } 43 QuitClosure()44 [[nodiscard]] base::RepeatingClosure QuitClosure() { 45 return task_environment_.QuitClosure(); 46 } 47 RunUntilQuit()48 void RunUntilQuit() { task_environment_.RunUntilQuit(); } 49 RunUntilIdle()50 void RunUntilIdle() { task_environment_.RunUntilIdle(); } 51 FastForwardBy(base::TimeDelta delta)52 void FastForwardBy(base::TimeDelta delta) { 53 task_environment_.FastForwardBy(delta); 54 } 55 FastForwardUntilNoTasksRemain()56 void FastForwardUntilNoTasksRemain() { 57 task_environment_.FastForwardUntilNoTasksRemain(); 58 } 59 60 // Only valid for instances using TimeSource::MOCK_TIME. AdvanceClock(base::TimeDelta delta)61 void AdvanceClock(base::TimeDelta delta) { 62 task_environment_.AdvanceClock(delta); 63 } 64 GetMockTickClock()65 [[nodiscard]] const base::TickClock* GetMockTickClock() { 66 return task_environment_.GetMockTickClock(); 67 } 68 GetPendingMainThreadTaskCount()69 [[nodiscard]] size_t GetPendingMainThreadTaskCount() const { 70 return task_environment_.GetPendingMainThreadTaskCount(); 71 } 72 NextMainThreadPendingTaskDelay()73 [[nodiscard]] base::TimeDelta NextMainThreadPendingTaskDelay() const { 74 return task_environment_.NextMainThreadPendingTaskDelay(); 75 } 76 77 private: 78 void MaybeStartNetLog(); 79 80 base::test::TaskEnvironment task_environment_; 81 std::unique_ptr<TestNetLogManager> net_log_manager_; 82 }; 83 84 // Inherit from this class instead of ::testing::Test directly if a 85 // TaskEnvironment is needed in a test. 86 class TestWithTaskEnvironment : public ::testing::Test, 87 public WithTaskEnvironment { 88 public: 89 TestWithTaskEnvironment(const TestWithTaskEnvironment&) = delete; 90 TestWithTaskEnvironment& operator=(const TestWithTaskEnvironment&) = delete; 91 92 protected: 93 using WithTaskEnvironment::WithTaskEnvironment; 94 }; 95 96 } // namespace net 97 98 #endif // NET_TEST_TEST_WITH_TASK_ENVIRONMENT_H_ 99