Index: third_party/WebKit/Source/platform/WebTaskRunnerTest.cpp |
diff --git a/third_party/WebKit/Source/platform/WebTaskRunnerTest.cpp b/third_party/WebKit/Source/platform/WebTaskRunnerTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fd899acf51b096a25f96ef5098cb0b7913baefce |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/WebTaskRunnerTest.cpp |
@@ -0,0 +1,71 @@ |
+ |
haraken
2016/10/19 15:20:39
Add a copyright.
tzik
2016/10/20 16:15:12
Done.
|
+#include "public/platform/WebTaskRunner.h" |
+ |
+#include "platform/scheduler/test/fake_web_task_runner.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace blink { |
+namespace { |
+ |
+void increment(int* x) { |
+ ++*x; |
+} |
+ |
+void getIsActive(bool* isActive, RefPtr<TaskHandle>* handle) { |
+ *isActive = (*handle)->isActive(); |
+} |
+ |
+} // namespace |
+ |
+TEST(WebTaskRunnerTest, PostCancellableTaskTest) { |
+ scheduler::FakeWebTaskRunner taskRunner; |
+ |
+ int count = 0; |
+ |
+ // Run without cancellation. |
+ RefPtr<TaskHandle> handle = taskRunner.postCancellableTask( |
+ BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); |
+ EXPECT_EQ(0, count); |
+ EXPECT_TRUE(handle->isActive()); |
+ taskRunner.runUntilIdle(); |
+ EXPECT_EQ(1, count); |
+ EXPECT_FALSE(handle->isActive()); |
+ |
+ handle = taskRunner.postDelayedCancellableTask( |
+ BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)), 1); |
+ EXPECT_EQ(0, count); |
+ EXPECT_TRUE(handle->isActive()); |
+ taskRunner.runUntilIdle(); |
+ EXPECT_EQ(1, count); |
+ EXPECT_FALSE(handle->isActive()); |
+ |
+ // Cancel a task. |
+ handle = taskRunner.postCancellableTask( |
+ BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); |
+ handle->cancel(); |
+ EXPECT_EQ(1, count); |
+ EXPECT_FALSE(handle->isActive()); |
+ taskRunner.runUntilIdle(); |
+ EXPECT_EQ(1, count); |
+ |
+ // The task should be valid even when the handle is dropped. |
+ handle = taskRunner.postCancellableTask( |
+ BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); |
+ EXPECT_TRUE(handle->isActive()); |
+ handle = nullptr; |
+ EXPECT_EQ(1, count); |
+ taskRunner.runUntilIdle(); |
+ EXPECT_EQ(2, count); |
+ |
+ // handle->isActive() should switch to false bofer the task start running. |
+ bool isActive = false; |
+ handle = taskRunner.postCancellableTask( |
+ BLINK_FROM_HERE, WTF::bind(&getIsActive, WTF::unretained(&isActive), |
+ WTF::unretained(&handle))); |
+ EXPECT_TRUE(handle->isActive()); |
+ taskRunner.runUntilIdle(); |
+ EXPECT_FALSE(isActive); |
+ EXPECT_FALSE(handle->isActive()); |
+} |
+ |
+} // namespace blink |