diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-07-15 01:26:09 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-07-15 01:26:09 +0000 |
commit | 536b96d958dab9d943af978463fe82fa084d0042 (patch) | |
tree | 0f4d08540c8e5fa16bcd0c99cd413fe815560e4b /base/test/scoped_mock_time_message_loop_task_runner_unittest.cc | |
parent | b4233fd961752b80d42ae68e8b42acfb3e99be57 (diff) | |
parent | 0d51dc717edd4d97116b47bc156e83b8fa193d3d (diff) | |
download | libchrome-536b96d958dab9d943af978463fe82fa084d0042.tar.gz |
Snap for 7550844 from 0d51dc717edd4d97116b47bc156e83b8fa193d3d to mainline-conscrypt-releaseandroid-mainline-12.0.0_r8android-mainline-12.0.0_r25android12-mainline-conscrypt-release
Change-Id: I4679185e58ca2896cc6470a6441557faff4b018e
Diffstat (limited to 'base/test/scoped_mock_time_message_loop_task_runner_unittest.cc')
-rw-r--r-- | base/test/scoped_mock_time_message_loop_task_runner_unittest.cc | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/base/test/scoped_mock_time_message_loop_task_runner_unittest.cc b/base/test/scoped_mock_time_message_loop_task_runner_unittest.cc new file mode 100644 index 0000000000..b08323d380 --- /dev/null +++ b/base/test/scoped_mock_time_message_loop_task_runner_unittest.cc @@ -0,0 +1,120 @@ +// Copyright 2015 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 "base/test/scoped_mock_time_message_loop_task_runner.h" + +#include <memory> + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/callback_forward.h" +#include "base/containers/circular_deque.h" +#include "base/macros.h" +#include "base/memory/ptr_util.h" +#include "base/memory/ref_counted.h" +#include "base/message_loop/message_loop.h" +#include "base/message_loop/message_loop_current.h" +#include "base/test/test_mock_time_task_runner.h" +#include "base/test/test_pending_task.h" +#include "base/time/time.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace base { +namespace { + +TaskRunner* GetCurrentTaskRunner() { + return MessageLoopCurrent::Get()->task_runner().get(); +} + +void AssignTrue(bool* out) { + *out = true; +} + +// Pops a task from the front of |pending_tasks| and returns it. +TestPendingTask PopFront(base::circular_deque<TestPendingTask>* pending_tasks) { + TestPendingTask task = std::move(pending_tasks->front()); + pending_tasks->pop_front(); + return task; +} + +class ScopedMockTimeMessageLoopTaskRunnerTest : public testing::Test { + public: + ScopedMockTimeMessageLoopTaskRunnerTest() + : original_task_runner_(new TestMockTimeTaskRunner()) { + MessageLoopCurrent::Get()->SetTaskRunner(original_task_runner_); + } + + protected: + TestMockTimeTaskRunner* original_task_runner() { + return original_task_runner_.get(); + } + + private: + scoped_refptr<TestMockTimeTaskRunner> original_task_runner_; + + MessageLoop message_loop_; + + DISALLOW_COPY_AND_ASSIGN(ScopedMockTimeMessageLoopTaskRunnerTest); +}; + +// Verifies a new TaskRunner is installed while a +// ScopedMockTimeMessageLoopTaskRunner exists and the previous one is installed +// after destruction. +TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, CurrentTaskRunners) { + auto scoped_task_runner_ = + std::make_unique<ScopedMockTimeMessageLoopTaskRunner>(); + EXPECT_EQ(scoped_task_runner_->task_runner(), GetCurrentTaskRunner()); + scoped_task_runner_.reset(); + EXPECT_EQ(original_task_runner(), GetCurrentTaskRunner()); +} + +TEST_F(ScopedMockTimeMessageLoopTaskRunnerTest, + IncompleteTasksAreCopiedToPreviousTaskRunnerAfterDestruction) { + auto scoped_task_runner_ = + std::make_unique<ScopedMockTimeMessageLoopTaskRunner>(); + + bool task_10_has_run = false; + bool task_11_has_run = false; + + Closure task_1 = DoNothing(); + Closure task_2 = DoNothing(); + Closure task_10 = Bind(&AssignTrue, &task_10_has_run); + Closure task_11 = Bind(&AssignTrue, &task_11_has_run); + + constexpr TimeDelta task_1_delay = TimeDelta::FromSeconds(1); + constexpr TimeDelta task_2_delay = TimeDelta::FromSeconds(2); + constexpr TimeDelta task_10_delay = TimeDelta::FromSeconds(10); + constexpr TimeDelta task_11_delay = TimeDelta::FromSeconds(11); + + constexpr TimeDelta step_time_by = TimeDelta::FromSeconds(5); + + GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_1, task_1_delay); + GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_2, task_2_delay); + GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_10, task_10_delay); + GetCurrentTaskRunner()->PostDelayedTask(FROM_HERE, task_11, task_11_delay); + + scoped_task_runner_->task_runner()->FastForwardBy(step_time_by); + + scoped_task_runner_.reset(); + + base::circular_deque<TestPendingTask> pending_tasks = + original_task_runner()->TakePendingTasks(); + + EXPECT_EQ(2U, pending_tasks.size()); + + TestPendingTask pending_task = PopFront(&pending_tasks); + EXPECT_FALSE(task_10_has_run); + std::move(pending_task.task).Run(); + EXPECT_TRUE(task_10_has_run); + EXPECT_EQ(task_10_delay - step_time_by, pending_task.delay); + + pending_task = PopFront(&pending_tasks); + EXPECT_FALSE(task_11_has_run); + std::move(pending_task.task).Run(); + EXPECT_TRUE(task_11_has_run); + EXPECT_EQ(task_11_delay - step_time_by, pending_task.delay); +} + +} // namespace +} // namespace base |