OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "wtf/MakeCancellable.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace WTF { |
| 10 namespace { |
| 11 |
| 12 void add(int* x, int y) { *x += y; } |
| 13 |
| 14 class DestructionCounter { |
| 15 public: |
| 16 explicit DestructionCounter(int* counter) : m_counter(counter) { } |
| 17 ~DestructionCounter() |
| 18 { |
| 19 if (m_counter) |
| 20 ++*m_counter; |
| 21 } |
| 22 |
| 23 DestructionCounter(DestructionCounter&& other) : m_counter(other.m_counter) |
| 24 { |
| 25 other.m_counter = nullptr; |
| 26 } |
| 27 |
| 28 private: |
| 29 int* m_counter; |
| 30 }; |
| 31 |
| 32 class RefCountedDestructionCounter : public RefCounted<RefCountedDestructionCoun
ter>, public DestructionCounter { |
| 33 public: |
| 34 explicit RefCountedDestructionCounter(int* counter) : DestructionCounter(cou
nter) {} |
| 35 }; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 TEST(MakeCancellableTest, NotCancelled) |
| 40 { |
| 41 int v = 0; |
| 42 auto f = bind(&add, unretained(&v)); |
| 43 RefPtr<FunctionCanceller> canceller; |
| 44 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 45 |
| 46 EXPECT_EQ(0, v); |
| 47 (*f)(3); |
| 48 EXPECT_EQ(3, v); |
| 49 } |
| 50 |
| 51 TEST(MakeCancellableTest, ExplicitCancel) |
| 52 { |
| 53 int v = 0; |
| 54 auto f = bind(&add, unretained(&v)); |
| 55 RefPtr<FunctionCanceller> canceller; |
| 56 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 57 |
| 58 canceller->cancel(); |
| 59 EXPECT_EQ(0, v); |
| 60 (*f)(3); |
| 61 EXPECT_EQ(0, v); |
| 62 } |
| 63 |
| 64 TEST(MakeCancellableTest, ScopeOutCancel) |
| 65 { |
| 66 int v = 0; |
| 67 auto f = bind(&add, unretained(&v)); |
| 68 { |
| 69 RefPtr<FunctionCanceller> canceller; |
| 70 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 71 |
| 72 ScopedFunctionCanceller scopedCanceller(canceller); |
| 73 } |
| 74 |
| 75 EXPECT_EQ(0, v); |
| 76 (*f)(3); |
| 77 EXPECT_EQ(0, v); |
| 78 } |
| 79 |
| 80 TEST(MakeCancellableTest, Detach) |
| 81 { |
| 82 int v = 0; |
| 83 auto f = bind(&add, unretained(&v)); |
| 84 { |
| 85 RefPtr<FunctionCanceller> canceller; |
| 86 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 87 |
| 88 ScopedFunctionCanceller scopedCanceller(canceller); |
| 89 scopedCanceller.detach(); |
| 90 } |
| 91 |
| 92 EXPECT_EQ(0, v); |
| 93 (*f)(3); |
| 94 EXPECT_EQ(3, v); |
| 95 } |
| 96 |
| 97 TEST(MakeCancellableTest, MultiCall) |
| 98 { |
| 99 int v = 0; |
| 100 auto f = bind(&add, unretained(&v)); |
| 101 std::tie(f, std::ignore) = makeCancellable(std::move(f)); |
| 102 |
| 103 EXPECT_EQ(0, v); |
| 104 (*f)(2); |
| 105 EXPECT_EQ(2, v); |
| 106 (*f)(3); |
| 107 EXPECT_EQ(5, v); |
| 108 } |
| 109 |
| 110 TEST(MakeCancellableTest, DestroyOnCancel) |
| 111 { |
| 112 int counter = 0; |
| 113 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter)
); |
| 114 RefPtr<FunctionCanceller> canceller; |
| 115 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 116 |
| 117 EXPECT_EQ(0, counter); |
| 118 canceller->cancel(); |
| 119 EXPECT_EQ(1, counter); |
| 120 } |
| 121 |
| 122 TEST(MakeCancellableTest, DestroyOnWrapperDestruction) |
| 123 { |
| 124 int counter = 0; |
| 125 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter)
); |
| 126 RefPtr<FunctionCanceller> canceller; |
| 127 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 128 |
| 129 EXPECT_EQ(0, counter); |
| 130 f = nullptr; |
| 131 EXPECT_EQ(1, counter); |
| 132 } |
| 133 |
| 134 TEST(MakeCancellableTest, SelfAssignment) |
| 135 { |
| 136 int v = 0; |
| 137 auto f = bind(&add, unretained(&v)); |
| 138 RefPtr<FunctionCanceller> canceller; |
| 139 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 140 |
| 141 ScopedFunctionCanceller scopedCanceller(std::move(canceller)); |
| 142 #pragma clang diagnostic push |
| 143 #pragma clang diagnostic ignored "-Wself-move" |
| 144 scopedCanceller = std::move(scopedCanceller); |
| 145 #pragma clang diagnostic pop |
| 146 |
| 147 EXPECT_EQ(0, v); |
| 148 (*f)(1); |
| 149 EXPECT_EQ(1, v); |
| 150 } |
| 151 |
| 152 } // namespace WTF |
OLD | NEW |