| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "wtf/MakeCancellable.h" | 5 #include "wtf/MakeCancellable.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "wtf/Compiler.h" | 8 #include "wtf/Compiler.h" |
| 9 | 9 |
| 10 namespace WTF { | 10 namespace WTF { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 TEST(MakeCancellableTest, NotCancelled) | 40 TEST(MakeCancellableTest, NotCancelled) |
| 41 { | 41 { |
| 42 int v = 0; | 42 int v = 0; |
| 43 auto f = bind(&add, unretained(&v)); | 43 auto f = bind(&add, unretained(&v)); |
| 44 auto result = makeCancellable(std::move(f)); | 44 auto result = makeCancellable(std::move(f)); |
| 45 | 45 |
| 46 EXPECT_FALSE(result.function->isCancelled()); |
| 46 EXPECT_EQ(0, v); | 47 EXPECT_EQ(0, v); |
| 47 (*result.function)(3); | 48 (*result.function)(3); |
| 48 EXPECT_EQ(3, v); | 49 EXPECT_EQ(3, v); |
| 49 } | 50 } |
| 50 | 51 |
| 51 TEST(MakeCancellableTest, ExplicitCancel) | 52 TEST(MakeCancellableTest, ExplicitCancel) |
| 52 { | 53 { |
| 53 int v = 0; | 54 int v = 0; |
| 54 auto f = bind(&add, unretained(&v)); | 55 auto f = bind(&add, unretained(&v)); |
| 55 auto result = makeCancellable(std::move(f)); | 56 auto result = makeCancellable(std::move(f)); |
| 56 | 57 |
| 58 EXPECT_FALSE(result.function->isCancelled()); |
| 57 EXPECT_TRUE(result.canceller.isActive()); | 59 EXPECT_TRUE(result.canceller.isActive()); |
| 58 result.canceller.cancel(); | 60 result.canceller.cancel(); |
| 61 EXPECT_TRUE(result.function->isCancelled()); |
| 59 EXPECT_FALSE(result.canceller.isActive()); | 62 EXPECT_FALSE(result.canceller.isActive()); |
| 60 | 63 |
| 61 EXPECT_EQ(0, v); | 64 EXPECT_EQ(0, v); |
| 62 (*result.function)(3); | 65 (*result.function)(3); |
| 63 EXPECT_EQ(0, v); | 66 EXPECT_EQ(0, v); |
| 64 } | 67 } |
| 65 | 68 |
| 66 TEST(MakeCancellableTest, ScopeOutCancel) | 69 TEST(MakeCancellableTest, ScopeOutCancel) |
| 67 { | 70 { |
| 68 int v = 0; | 71 int v = 0; |
| 69 auto f = bind(&add, unretained(&v)); | 72 auto f = bind(&add, unretained(&v)); |
| 70 { | 73 { |
| 71 auto result = makeCancellable(std::move(f)); | 74 auto result = makeCancellable(std::move(f)); |
| 72 f = std::move(result.function); | 75 f = std::move(result.function); |
| 73 | 76 |
| 74 ScopedFunctionCanceller scopedCanceller = std::move(result.canceller); | 77 ScopedFunctionCanceller scopedCanceller = std::move(result.canceller); |
| 75 EXPECT_TRUE(scopedCanceller.isActive()); | 78 EXPECT_TRUE(scopedCanceller.isActive()); |
| 79 EXPECT_FALSE(f->isCancelled()); |
| 76 } | 80 } |
| 77 | 81 |
| 82 EXPECT_TRUE(f->isCancelled()); |
| 78 EXPECT_EQ(0, v); | 83 EXPECT_EQ(0, v); |
| 79 (*f)(3); | 84 (*f)(3); |
| 80 EXPECT_EQ(0, v); | 85 EXPECT_EQ(0, v); |
| 81 } | 86 } |
| 82 | 87 |
| 83 TEST(MakeCancellableTest, Detach) | 88 TEST(MakeCancellableTest, Detach) |
| 84 { | 89 { |
| 85 int v = 0; | 90 int v = 0; |
| 86 auto f = bind(&add, unretained(&v)); | 91 auto f = bind(&add, unretained(&v)); |
| 87 { | 92 { |
| 88 auto result = makeCancellable(std::move(f)); | 93 auto result = makeCancellable(std::move(f)); |
| 89 f = std::move(result.function); | 94 f = std::move(result.function); |
| 90 | 95 |
| 91 ScopedFunctionCanceller scopedCanceller = std::move(result.canceller); | 96 ScopedFunctionCanceller scopedCanceller = std::move(result.canceller); |
| 92 EXPECT_TRUE(scopedCanceller.isActive()); | 97 EXPECT_TRUE(scopedCanceller.isActive()); |
| 93 scopedCanceller.detach(); | 98 scopedCanceller.detach(); |
| 94 EXPECT_FALSE(scopedCanceller.isActive()); | 99 EXPECT_FALSE(scopedCanceller.isActive()); |
| 100 EXPECT_FALSE(f->isCancelled()); |
| 95 } | 101 } |
| 96 | 102 |
| 103 EXPECT_FALSE(f->isCancelled()); |
| 97 EXPECT_EQ(0, v); | 104 EXPECT_EQ(0, v); |
| 98 (*f)(3); | 105 (*f)(3); |
| 99 EXPECT_EQ(3, v); | 106 EXPECT_EQ(3, v); |
| 100 } | 107 } |
| 101 | 108 |
| 102 TEST(MakeCancellableTest, MultiCall) | 109 TEST(MakeCancellableTest, MultiCall) |
| 103 { | 110 { |
| 104 int v = 0; | 111 int v = 0; |
| 105 auto f = bind(&add, unretained(&v)); | 112 auto f = bind(&add, unretained(&v)); |
| 106 auto result = makeCancellable(std::move(f)); | 113 auto result = makeCancellable(std::move(f)); |
| 107 | 114 |
| 115 EXPECT_FALSE(result.function->isCancelled()); |
| 108 EXPECT_EQ(0, v); | 116 EXPECT_EQ(0, v); |
| 109 (*result.function)(2); | 117 (*result.function)(2); |
| 110 EXPECT_EQ(2, v); | 118 EXPECT_EQ(2, v); |
| 111 (*result.function)(3); | 119 (*result.function)(3); |
| 112 EXPECT_EQ(5, v); | 120 EXPECT_EQ(5, v); |
| 121 EXPECT_FALSE(result.function->isCancelled()); |
| 113 } | 122 } |
| 114 | 123 |
| 115 TEST(MakeCancellableTest, DestroyOnCancel) | 124 TEST(MakeCancellableTest, DestroyOnCancel) |
| 116 { | 125 { |
| 117 int counter = 0; | 126 int counter = 0; |
| 118 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter)
); | 127 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter)
); |
| 119 auto result = makeCancellable(std::move(f)); | 128 auto result = makeCancellable(std::move(f)); |
| 120 | 129 |
| 130 EXPECT_FALSE(result.function->isCancelled()); |
| 121 EXPECT_EQ(0, counter); | 131 EXPECT_EQ(0, counter); |
| 122 result.canceller.cancel(); | 132 result.canceller.cancel(); |
| 133 EXPECT_TRUE(result.function->isCancelled()); |
| 123 EXPECT_EQ(1, counter); | 134 EXPECT_EQ(1, counter); |
| 124 } | 135 } |
| 125 | 136 |
| 126 TEST(MakeCancellableTest, DestroyOnWrapperDestruction) | 137 TEST(MakeCancellableTest, DestroyOnWrapperDestruction) |
| 127 { | 138 { |
| 128 int counter = 0; | 139 int counter = 0; |
| 129 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter)
); | 140 auto f = bind([](const DestructionCounter&) {}, DestructionCounter(&counter)
); |
| 130 auto result = makeCancellable(std::move(f)); | 141 auto result = makeCancellable(std::move(f)); |
| 131 | 142 |
| 132 EXPECT_TRUE(result.canceller.isActive()); | 143 EXPECT_TRUE(result.canceller.isActive()); |
| 144 EXPECT_FALSE(result.function->isCancelled()); |
| 133 | 145 |
| 134 EXPECT_EQ(0, counter); | 146 EXPECT_EQ(0, counter); |
| 135 result.function = nullptr; | 147 result.function = nullptr; |
| 136 EXPECT_EQ(1, counter); | 148 EXPECT_EQ(1, counter); |
| 137 | 149 |
| 138 EXPECT_FALSE(result.canceller.isActive()); | 150 EXPECT_FALSE(result.canceller.isActive()); |
| 139 } | 151 } |
| 140 | 152 |
| 141 TEST(MakeCancellableTest, SelfAssignment) | 153 TEST(MakeCancellableTest, SelfAssignment) |
| 142 { | 154 { |
| 143 int v = 0; | 155 int v = 0; |
| 144 auto f = bind(&add, unretained(&v)); | 156 auto f = bind(&add, unretained(&v)); |
| 145 auto result = makeCancellable(std::move(f)); | 157 auto result = makeCancellable(std::move(f)); |
| 146 | 158 |
| 147 ScopedFunctionCanceller scopedCanceller = std::move(result.canceller); | 159 ScopedFunctionCanceller scopedCanceller = std::move(result.canceller); |
| 148 EXPECT_TRUE(scopedCanceller.isActive()); | 160 EXPECT_TRUE(scopedCanceller.isActive()); |
| 149 | 161 |
| 150 #if COMPILER(CLANG) | 162 #if COMPILER(CLANG) |
| 151 #pragma clang diagnostic push | 163 #pragma clang diagnostic push |
| 152 #pragma clang diagnostic ignored "-Wself-move" | 164 #pragma clang diagnostic ignored "-Wself-move" |
| 153 scopedCanceller = std::move(scopedCanceller); | 165 scopedCanceller = std::move(scopedCanceller); |
| 154 #pragma clang diagnostic pop | 166 #pragma clang diagnostic pop |
| 155 #else | 167 #else |
| 156 scopedCanceller = std::move(scopedCanceller); | 168 scopedCanceller = std::move(scopedCanceller); |
| 157 #endif | 169 #endif |
| 158 | 170 |
| 159 EXPECT_TRUE(scopedCanceller.isActive()); | 171 EXPECT_TRUE(scopedCanceller.isActive()); |
| 172 EXPECT_FALSE(result.function->isCancelled()); |
| 160 EXPECT_EQ(0, v); | 173 EXPECT_EQ(0, v); |
| 161 (*result.function)(1); | 174 (*result.function)(1); |
| 162 EXPECT_EQ(1, v); | 175 EXPECT_EQ(1, v); |
| 163 } | 176 } |
| 164 | 177 |
| 165 } // namespace WTF | 178 } // namespace WTF |
| OLD | NEW |