Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(814)

Unified Diff: third_party/WebKit/Source/platform/WebTaskRunnerTest.cpp

Issue 2487493004: Support external task cancellation mechanisms in base::Callback::IsCancelled (Closed)
Patch Set: . Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
index 022a649a53e2ac5b9ca23fb21a2ef320fc8faba3..bcff0c113ae43105fedd67c95a1ce46c39108309 100644
--- a/third_party/WebKit/Source/platform/WebTaskRunnerTest.cpp
+++ b/third_party/WebKit/Source/platform/WebTaskRunnerTest.cpp
@@ -18,6 +18,23 @@ void getIsActive(bool* isActive, TaskHandle* handle) {
*isActive = handle->isActive();
}
+class CancellationTestHelper {
+ public:
+ CancellationTestHelper() : m_weakPtrFactory(this) {}
+
+ WeakPtr<CancellationTestHelper> createWeakPtr() {
+ return m_weakPtrFactory.createWeakPtr();
+ }
+
+ void revokeWeakPtrs() { m_weakPtrFactory.revokeAll(); }
+ void incrementCounter() { ++m_counter; }
+ int counter() const { return m_counter; }
+
+ private:
+ int m_counter = 0;
+ WeakPtrFactory<CancellationTestHelper> m_weakPtrFactory;
+};
+
} // namespace
TEST(WebTaskRunnerTest, PostCancellableTaskTest) {
@@ -99,4 +116,38 @@ TEST(WebTaskRunnerTest, PostCancellableTaskTest) {
EXPECT_FALSE(handle.isActive());
}
+TEST(WebTaskRunnerTest, CancellationCheckerTest) {
+ scheduler::FakeWebTaskRunner taskRunner;
+
+ int count = 0;
+ TaskHandle handle = taskRunner.postCancellableTask(
+ BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)));
+ EXPECT_EQ(0, count);
+
+ // TaskHandle::isActive should detect the deletion of posted task.
+ auto queue = taskRunner.takePendingTasksForTesting();
+ ASSERT_EQ(1u, queue.size());
+ EXPECT_FALSE(queue[0].IsCancelled());
+ EXPECT_TRUE(handle.isActive());
+ queue.clear();
+ EXPECT_FALSE(handle.isActive());
+ EXPECT_EQ(0, count);
+
+ count = 0;
+ CancellationTestHelper helper;
+ handle = taskRunner.postCancellableTask(
+ BLINK_FROM_HERE, WTF::bind(&CancellationTestHelper::incrementCounter,
+ helper.createWeakPtr()));
+ EXPECT_EQ(0, helper.counter());
+
+ // The cancellation of the posted task should be propagated to TaskHandle.
+ queue = taskRunner.takePendingTasksForTesting();
+ ASSERT_EQ(1u, queue.size());
+ EXPECT_FALSE(queue[0].IsCancelled());
+ EXPECT_TRUE(handle.isActive());
+ helper.revokeWeakPtrs();
+ EXPECT_TRUE(queue[0].IsCancelled());
+ EXPECT_FALSE(handle.isActive());
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698