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..3f89b7f92d7cd3e902112298ad86c432e2402f49 |
--- /dev/null |
+++ b/third_party/WebKit/Source/wtf/MakeCancellableTest.cpp |
@@ -0,0 +1,152 @@ |
+// 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; |
+}; |
+ |
+class RefCountedDestructionCounter : public RefCounted<RefCountedDestructionCounter>, public DestructionCounter { |
+public: |
+ explicit RefCountedDestructionCounter(int* counter) : DestructionCounter(counter) {} |
+}; |
+ |
+} // namespace |
+ |
+TEST(MakeCancellableTest, NotCancelled) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ RefPtr<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)); |
+ RefPtr<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)); |
+ { |
+ RefPtr<FunctionCanceller> canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ ScopedFunctionCanceller scopedCanceller(canceller); |
+ } |
+ |
+ EXPECT_EQ(0, v); |
+ (*f)(3); |
+ EXPECT_EQ(0, v); |
+} |
+ |
+TEST(MakeCancellableTest, Detach) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ { |
+ RefPtr<FunctionCanceller> canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ ScopedFunctionCanceller scopedCanceller(canceller); |
+ scopedCanceller.detach(); |
+ } |
+ |
+ EXPECT_EQ(0, v); |
+ (*f)(3); |
+ EXPECT_EQ(3, v); |
+} |
+ |
+TEST(MakeCancellableTest, MultiCall) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ std::tie(f, std::ignore) = 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)); |
+ RefPtr<FunctionCanceller> canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ EXPECT_EQ(0, counter); |
+ canceller->cancel(); |
+ EXPECT_EQ(1, counter); |
+} |
+ |
+TEST(MakeCancellableTest, DestroyOnWrapperDestruction) |
+{ |
+ int counter = 0; |
+ auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter)); |
+ RefPtr<FunctionCanceller> canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ EXPECT_EQ(0, counter); |
+ f = nullptr; |
+ EXPECT_EQ(1, counter); |
+} |
+ |
+TEST(MakeCancellableTest, SelfAssignment) |
+{ |
+ int v = 0; |
+ auto f = bind(&add, unretained(&v)); |
+ RefPtr<FunctionCanceller> canceller; |
+ std::tie(f, canceller) = makeCancellable(std::move(f)); |
+ |
+ ScopedFunctionCanceller scopedCanceller(std::move(canceller)); |
+#pragma clang diagnostic push |
+#pragma clang diagnostic ignored "-Wself-move" |
+ scopedCanceller = std::move(scopedCanceller); |
+#pragma clang diagnostic pop |
+ |
+ EXPECT_EQ(0, v); |
+ (*f)(1); |
+ EXPECT_EQ(1, v); |
+} |
+ |
+} // namespace WTF |