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