Index: third_party/WebKit/Source/wtf/MakeCancellableTest.cpp |
diff --git a/third_party/WebKit/Source/wtf/MakeCancellableTest.cpp b/third_party/WebKit/Source/wtf/MakeCancellableTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ab8f514f52b57ba564a7c147a279e4880447bf18 |
--- /dev/null |
+++ b/third_party/WebKit/Source/wtf/MakeCancellableTest.cpp |
@@ -0,0 +1,138 @@ |
+// Copyright 2016 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 "wtf/MakeCancellable.h" |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace WTF { |
+namespace { |
+ |
+void add(int* x, int y) { *x += y; } |
+ |
+class DestructionCounter { |
+public: |
+ explicit DestructionCounter(int* counter) : m_counter(counter) { } |
+ ~DestructionCounter() |
+ { |
+ if (m_counter) |
+ ++*m_counter; |
+ } |
+ |
+ DestructionCounter(DestructionCounter&& other) : m_counter(other.m_counter) |
+ { |
+ other.m_counter = nullptr; |
+ } |
+ |
+private: |
+ int* m_counter; |
+}; |
+ |
+} // namespace |
+ |
+TEST(MakeCancellableTest, NotCancelled) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ FunctionCanceller canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ EXPECT_EQ(0, v); |
+ (*f)(3); |
+ EXPECT_EQ(3, v); |
+} |
+ |
+TEST(MakeCancellableTest, ExplicitCancel) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ FunctionCanceller canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ canceller.cancel(); |
+ EXPECT_EQ(0, v); |
+ (*f)(3); |
+ EXPECT_EQ(0, v); |
+} |
+ |
+TEST(MakeCancellableTest, ScopeOutCancel) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ { |
+ FunctionCanceller canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ } |
+ |
+ EXPECT_EQ(0, v); |
+ (*f)(3); |
+ EXPECT_EQ(0, v); |
+} |
+ |
+TEST(MakeCancellableTest, Detach) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ { |
+ FunctionCanceller canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ canceller.detach(); |
+ } |
+ |
+ EXPECT_EQ(0, v); |
+ (*f)(3); |
+ EXPECT_EQ(3, v); |
+} |
+ |
+TEST(MakeCancellableTest, CancellerMove) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ FunctionCanceller canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ FunctionCanceller moved = std::move(canceller); |
+ |
+ canceller.cancel(); // This should take no effect. |
+ |
+ EXPECT_EQ(0, v); |
+ (*f)(3); |
+ EXPECT_EQ(3, v); |
+ |
+ std::tie(f, canceller) = makeCancellable(bind(&add, unretained(&v))); |
+ moved = std::move(canceller); |
+ moved.cancel(); |
+ |
+ EXPECT_EQ(3, v); |
+ (*f)(4); |
+ EXPECT_EQ(3, v); |
+} |
+ |
+TEST(MakeCancellableTest, MultiCall) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ FunctionCanceller canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ EXPECT_EQ(0, v); |
+ (*f)(2); |
+ EXPECT_EQ(2, v); |
+ (*f)(3); |
+ EXPECT_EQ(5, v); |
+} |
+ |
+TEST(MakeCancellableTest, DestroyOnCancel) |
+{ |
+ int counter = 0; |
+ auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter)); |
+ FunctionCanceller canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ EXPECT_EQ(0, counter); |
+ canceller.cancel(); |
+ EXPECT_EQ(1, counter); |
+} |
+ |
+} // namespace WTF |