OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "public/platform/WebTaskRunner.h" |
| 6 |
| 7 #include "platform/scheduler/test/fake_web_task_runner.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace blink { |
| 11 namespace { |
| 12 |
| 13 void increment(int* x) { |
| 14 ++*x; |
| 15 } |
| 16 |
| 17 void getIsActive(bool* isActive, RefPtr<TaskHandle>* handle) { |
| 18 *isActive = (*handle)->isActive(); |
| 19 } |
| 20 |
| 21 } // namespace |
| 22 |
| 23 TEST(WebTaskRunnerTest, PostCancellableTaskTest) { |
| 24 scheduler::FakeWebTaskRunner taskRunner; |
| 25 |
| 26 // Run without cancellation. |
| 27 int count = 0; |
| 28 RefPtr<TaskHandle> handle = taskRunner.postCancellableTask( |
| 29 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); |
| 30 EXPECT_EQ(0, count); |
| 31 EXPECT_TRUE(handle->isActive()); |
| 32 taskRunner.runUntilIdle(); |
| 33 EXPECT_EQ(1, count); |
| 34 EXPECT_FALSE(handle->isActive()); |
| 35 |
| 36 count = 0; |
| 37 handle = taskRunner.postDelayedCancellableTask( |
| 38 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)), 1); |
| 39 EXPECT_EQ(0, count); |
| 40 EXPECT_TRUE(handle->isActive()); |
| 41 taskRunner.runUntilIdle(); |
| 42 EXPECT_EQ(1, count); |
| 43 EXPECT_FALSE(handle->isActive()); |
| 44 |
| 45 // Cancel a task. |
| 46 count = 0; |
| 47 handle = taskRunner.postCancellableTask( |
| 48 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); |
| 49 handle->cancel(); |
| 50 EXPECT_EQ(0, count); |
| 51 EXPECT_FALSE(handle->isActive()); |
| 52 taskRunner.runUntilIdle(); |
| 53 EXPECT_EQ(0, count); |
| 54 |
| 55 // The task should be valid even when the handle is dropped. |
| 56 count = 0; |
| 57 handle = taskRunner.postCancellableTask( |
| 58 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); |
| 59 EXPECT_TRUE(handle->isActive()); |
| 60 handle = nullptr; |
| 61 EXPECT_EQ(0, count); |
| 62 taskRunner.runUntilIdle(); |
| 63 EXPECT_EQ(1, count); |
| 64 |
| 65 // handle->isActive() should switch to false before the task starts running. |
| 66 bool isActive = false; |
| 67 handle = taskRunner.postCancellableTask( |
| 68 BLINK_FROM_HERE, WTF::bind(&getIsActive, WTF::unretained(&isActive), |
| 69 WTF::unretained(&handle))); |
| 70 EXPECT_TRUE(handle->isActive()); |
| 71 taskRunner.runUntilIdle(); |
| 72 EXPECT_FALSE(isActive); |
| 73 EXPECT_FALSE(handle->isActive()); |
| 74 } |
| 75 |
| 76 } // namespace blink |
OLD | NEW |