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 } // namespace |
| 33 |
| 34 TEST(MakeCancellableTest, NotCancelled) |
| 35 { |
| 36 int v = 0; |
| 37 auto f = bind(&add, unretained(&v)); |
| 38 RefPtr<FunctionCanceller> canceller; |
| 39 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 40 |
| 41 EXPECT_EQ(0, v); |
| 42 (*f)(3); |
| 43 EXPECT_EQ(3, v); |
| 44 } |
| 45 |
| 46 TEST(MakeCancellableTest, ExplicitCancel) |
| 47 { |
| 48 int v = 0; |
| 49 auto f = bind(&add, unretained(&v)); |
| 50 RefPtr<FunctionCanceller> canceller; |
| 51 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 52 |
| 53 canceller->cancel(); |
| 54 EXPECT_EQ(0, v); |
| 55 (*f)(3); |
| 56 EXPECT_EQ(0, v); |
| 57 } |
| 58 |
| 59 TEST(MakeCancellableTest, ScopeOutCancel) |
| 60 { |
| 61 int v = 0; |
| 62 auto f = bind(&add, unretained(&v)); |
| 63 { |
| 64 RefPtr<FunctionCanceller> canceller; |
| 65 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 66 |
| 67 ScopedFunctionCanceller scopedCanceller(canceller); |
| 68 } |
| 69 |
| 70 EXPECT_EQ(0, v); |
| 71 (*f)(3); |
| 72 EXPECT_EQ(0, v); |
| 73 } |
| 74 |
| 75 TEST(MakeCancellableTest, Detach) |
| 76 { |
| 77 int v = 0; |
| 78 auto f = bind(&add, unretained(&v)); |
| 79 { |
| 80 RefPtr<FunctionCanceller> canceller; |
| 81 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 82 |
| 83 ScopedFunctionCanceller scopedCanceller(canceller); |
| 84 scopedCanceller.detach(); |
| 85 } |
| 86 |
| 87 EXPECT_EQ(0, v); |
| 88 (*f)(3); |
| 89 EXPECT_EQ(3, v); |
| 90 } |
| 91 |
| 92 TEST(MakeCancellableTest, MultiCall) |
| 93 { |
| 94 int v = 0; |
| 95 auto f = bind(&add, unretained(&v)); |
| 96 std::tie(f, std::ignore) = makeCancellable(std::move(f)); |
| 97 |
| 98 EXPECT_EQ(0, v); |
| 99 (*f)(2); |
| 100 EXPECT_EQ(2, v); |
| 101 (*f)(3); |
| 102 EXPECT_EQ(5, v); |
| 103 } |
| 104 |
| 105 TEST(MakeCancellableTest, DestroyOnCancel) |
| 106 { |
| 107 int counter = 0; |
| 108 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter)
); |
| 109 RefPtr<FunctionCanceller> canceller; |
| 110 std::tie(f, canceller) = makeCancellable(std::move(f)); |
| 111 |
| 112 EXPECT_EQ(0, counter); |
| 113 canceller->cancel(); |
| 114 EXPECT_EQ(1, counter); |
| 115 } |
| 116 |
| 117 } // namespace WTF |
OLD | NEW |