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

Unified Diff: third_party/WebKit/Source/wtf/MakeCancellableTest.cpp

Issue 2177283005: Add WTF::makeCancellable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: +comment Created 4 years, 5 months 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
« no previous file with comments | « third_party/WebKit/Source/wtf/MakeCancellable.cpp ('k') | third_party/WebKit/Source/wtf/wtf.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..9202d443a30bdf396d0d4bc13e0dc44f9e2e5443
--- /dev/null
+++ b/third_party/WebKit/Source/wtf/MakeCancellableTest.cpp
@@ -0,0 +1,165 @@
+// 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"
+#include "wtf/Compiler.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));
+ auto result = makeCancellable(std::move(f));
+
+ EXPECT_EQ(0, v);
+ (*result.function)(3);
+ EXPECT_EQ(3, v);
+}
+
+TEST(MakeCancellableTest, ExplicitCancel)
+{
+ int v = 0;
+ auto f = bind(&add, unretained(&v));
+ auto result = makeCancellable(std::move(f));
+
+ EXPECT_TRUE(result.canceller.isActive());
+ result.canceller.cancel();
+ EXPECT_FALSE(result.canceller.isActive());
+
+ EXPECT_EQ(0, v);
+ (*result.function)(3);
+ EXPECT_EQ(0, v);
+}
+
+TEST(MakeCancellableTest, ScopeOutCancel)
+{
+ int v = 0;
+ auto f = bind(&add, unretained(&v));
+ {
+ auto result = makeCancellable(std::move(f));
+ f = std::move(result.function);
+
+ ScopedFunctionCanceller scopedCanceller = std::move(result.canceller);
+ EXPECT_TRUE(scopedCanceller.isActive());
+ }
+
+ EXPECT_EQ(0, v);
+ (*f)(3);
+ EXPECT_EQ(0, v);
+}
+
+TEST(MakeCancellableTest, Detach)
+{
+ int v = 0;
+ auto f = bind(&add, unretained(&v));
+ {
+ auto result = makeCancellable(std::move(f));
+ f = std::move(result.function);
+
+ ScopedFunctionCanceller scopedCanceller = std::move(result.canceller);
+ EXPECT_TRUE(scopedCanceller.isActive());
+ scopedCanceller.detach();
+ EXPECT_FALSE(scopedCanceller.isActive());
+ }
+
+ EXPECT_EQ(0, v);
+ (*f)(3);
+ EXPECT_EQ(3, v);
+}
+
+TEST(MakeCancellableTest, MultiCall)
+{
+ int v = 0;
+ auto f = bind(&add, unretained(&v));
+ auto result = makeCancellable(std::move(f));
+
+ EXPECT_EQ(0, v);
+ (*result.function)(2);
+ EXPECT_EQ(2, v);
+ (*result.function)(3);
+ EXPECT_EQ(5, v);
+}
+
+TEST(MakeCancellableTest, DestroyOnCancel)
+{
+ int counter = 0;
+ auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter));
+ auto result = makeCancellable(std::move(f));
+
+ EXPECT_EQ(0, counter);
+ result.canceller.cancel();
+ EXPECT_EQ(1, counter);
+}
+
+TEST(MakeCancellableTest, DestroyOnWrapperDestruction)
+{
+ int counter = 0;
+ auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter));
+ auto result = makeCancellable(std::move(f));
+
+ EXPECT_TRUE(result.canceller.isActive());
+
+ EXPECT_EQ(0, counter);
+ result.function = nullptr;
+ EXPECT_EQ(1, counter);
+
+ EXPECT_FALSE(result.canceller.isActive());
+}
+
+TEST(MakeCancellableTest, SelfAssignment)
+{
+ int v = 0;
+ auto f = bind(&add, unretained(&v));
+ auto result = makeCancellable(std::move(f));
+
+ ScopedFunctionCanceller scopedCanceller = std::move(result.canceller);
+ EXPECT_TRUE(scopedCanceller.isActive());
+
+#if COMPILER(CLANG)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wself-move"
+ scopedCanceller = std::move(scopedCanceller);
+#pragma clang diagnostic pop
+#else
+ scopedCanceller = std::move(scopedCanceller);
+#endif
+
+ EXPECT_TRUE(scopedCanceller.isActive());
+ EXPECT_EQ(0, v);
+ (*result.function)(1);
+ EXPECT_EQ(1, v);
+}
+
+} // namespace WTF
« no previous file with comments | « third_party/WebKit/Source/wtf/MakeCancellable.cpp ('k') | third_party/WebKit/Source/wtf/wtf.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698