rsleevi@chromium.org | de3a6cf | 2012-04-06 12:53:02 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 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 "base/bind.h" |
| 6 | |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <utility> |
dcheng | b61e1f9 | 2016-02-02 13:09:33 +0900 | [diff] [blame] | 9 | #include <vector> |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 10 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 11 | #include "base/callback.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 12 | #include "base/macros.h" |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 13 | #include "base/memory/ptr_util.h" |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
gab | bcf9c76 | 2016-08-02 01:39:56 +0900 | [diff] [blame] | 16 | #include "base/test/gtest_util.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 17 | #include "build/build_config.h" |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 18 | #include "testing/gmock/include/gmock/gmock.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
dcheng | b78a84f | 2016-09-24 14:05:57 +0900 | [diff] [blame] | 21 | using ::testing::_; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 22 | using ::testing::Mock; |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 23 | using ::testing::ByMove; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 24 | using ::testing::Return; |
| 25 | using ::testing::StrictMock; |
| 26 | |
| 27 | namespace base { |
| 28 | namespace { |
| 29 | |
ajwong@chromium.org | 27e5685 | 2011-10-01 15:31:41 +0900 | [diff] [blame] | 30 | class IncompleteType; |
| 31 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 32 | class NoRef { |
| 33 | public: |
| 34 | NoRef() {} |
| 35 | |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 36 | MOCK_METHOD0(VoidMethod0, void()); |
| 37 | MOCK_CONST_METHOD0(VoidConstMethod0, void()); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 38 | |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 39 | MOCK_METHOD0(IntMethod0, int()); |
| 40 | MOCK_CONST_METHOD0(IntConstMethod0, int()); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 41 | |
dcheng | b78a84f | 2016-09-24 14:05:57 +0900 | [diff] [blame] | 42 | MOCK_METHOD1(VoidMethodWithIntArg, void(int)); |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 43 | MOCK_METHOD0(UniquePtrMethod0, std::unique_ptr<int>()); |
dcheng | b78a84f | 2016-09-24 14:05:57 +0900 | [diff] [blame] | 44 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 45 | private: |
| 46 | // Particularly important in this test to ensure no copies are made. |
| 47 | DISALLOW_COPY_AND_ASSIGN(NoRef); |
| 48 | }; |
| 49 | |
| 50 | class HasRef : public NoRef { |
| 51 | public: |
| 52 | HasRef() {} |
| 53 | |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 54 | MOCK_CONST_METHOD0(AddRef, void()); |
| 55 | MOCK_CONST_METHOD0(Release, bool()); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | // Particularly important in this test to ensure no copies are made. |
| 59 | DISALLOW_COPY_AND_ASSIGN(HasRef); |
| 60 | }; |
| 61 | |
ajwong@chromium.org | 6299740 | 2011-04-14 07:40:46 +0900 | [diff] [blame] | 62 | class HasRefPrivateDtor : public HasRef { |
| 63 | private: |
| 64 | ~HasRefPrivateDtor() {} |
| 65 | }; |
| 66 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 67 | static const int kParentValue = 1; |
| 68 | static const int kChildValue = 2; |
| 69 | |
| 70 | class Parent { |
| 71 | public: |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 72 | void AddRef() const {} |
| 73 | void Release() const {} |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 74 | virtual void VirtualSet() { value = kParentValue; } |
| 75 | void NonVirtualSet() { value = kParentValue; } |
| 76 | int value; |
| 77 | }; |
| 78 | |
| 79 | class Child : public Parent { |
| 80 | public: |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 81 | void VirtualSet() override { value = kChildValue; } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 82 | void NonVirtualSet() { value = kChildValue; } |
| 83 | }; |
| 84 | |
| 85 | class NoRefParent { |
| 86 | public: |
| 87 | virtual void VirtualSet() { value = kParentValue; } |
| 88 | void NonVirtualSet() { value = kParentValue; } |
| 89 | int value; |
| 90 | }; |
| 91 | |
| 92 | class NoRefChild : public NoRefParent { |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 93 | void VirtualSet() override { value = kChildValue; } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 94 | void NonVirtualSet() { value = kChildValue; } |
| 95 | }; |
| 96 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 97 | // Used for probing the number of copies and moves that occur if a type must be |
| 98 | // coerced during argument forwarding in the Run() methods. |
| 99 | struct DerivedCopyMoveCounter { |
| 100 | DerivedCopyMoveCounter(int* copies, |
| 101 | int* assigns, |
| 102 | int* move_constructs, |
| 103 | int* move_assigns) |
| 104 | : copies_(copies), |
| 105 | assigns_(assigns), |
| 106 | move_constructs_(move_constructs), |
| 107 | move_assigns_(move_assigns) {} |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 108 | int* copies_; |
| 109 | int* assigns_; |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 110 | int* move_constructs_; |
| 111 | int* move_assigns_; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 112 | }; |
| 113 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 114 | // Used for probing the number of copies and moves in an argument. |
| 115 | class CopyMoveCounter { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 116 | public: |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 117 | CopyMoveCounter(int* copies, |
| 118 | int* assigns, |
| 119 | int* move_constructs, |
| 120 | int* move_assigns) |
| 121 | : copies_(copies), |
| 122 | assigns_(assigns), |
| 123 | move_constructs_(move_constructs), |
| 124 | move_assigns_(move_assigns) {} |
| 125 | |
| 126 | CopyMoveCounter(const CopyMoveCounter& other) |
| 127 | : copies_(other.copies_), |
| 128 | assigns_(other.assigns_), |
| 129 | move_constructs_(other.move_constructs_), |
| 130 | move_assigns_(other.move_assigns_) { |
| 131 | (*copies_)++; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 132 | } |
| 133 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 134 | CopyMoveCounter(CopyMoveCounter&& other) |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 135 | : copies_(other.copies_), |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 136 | assigns_(other.assigns_), |
| 137 | move_constructs_(other.move_constructs_), |
| 138 | move_assigns_(other.move_assigns_) { |
| 139 | (*move_constructs_)++; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 140 | } |
| 141 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 142 | // Probing for copies from coercion. |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 143 | explicit CopyMoveCounter(const DerivedCopyMoveCounter& other) |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 144 | : copies_(other.copies_), |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 145 | assigns_(other.assigns_), |
| 146 | move_constructs_(other.move_constructs_), |
| 147 | move_assigns_(other.move_assigns_) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 148 | (*copies_)++; |
| 149 | } |
| 150 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 151 | // Probing for moves from coercion. |
| 152 | explicit CopyMoveCounter(DerivedCopyMoveCounter&& other) |
| 153 | : copies_(other.copies_), |
| 154 | assigns_(other.assigns_), |
| 155 | move_constructs_(other.move_constructs_), |
| 156 | move_assigns_(other.move_assigns_) { |
| 157 | (*move_constructs_)++; |
| 158 | } |
| 159 | |
| 160 | const CopyMoveCounter& operator=(const CopyMoveCounter& rhs) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 161 | copies_ = rhs.copies_; |
| 162 | assigns_ = rhs.assigns_; |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 163 | move_constructs_ = rhs.move_constructs_; |
| 164 | move_assigns_ = rhs.move_assigns_; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 165 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 166 | (*assigns_)++; |
| 167 | |
| 168 | return *this; |
| 169 | } |
| 170 | |
| 171 | const CopyMoveCounter& operator=(CopyMoveCounter&& rhs) { |
| 172 | copies_ = rhs.copies_; |
| 173 | assigns_ = rhs.assigns_; |
| 174 | move_constructs_ = rhs.move_constructs_; |
| 175 | move_assigns_ = rhs.move_assigns_; |
| 176 | |
| 177 | (*move_assigns_)++; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 178 | |
| 179 | return *this; |
| 180 | } |
| 181 | |
| 182 | int copies() const { |
| 183 | return *copies_; |
| 184 | } |
| 185 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 186 | private: |
| 187 | int* copies_; |
| 188 | int* assigns_; |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 189 | int* move_constructs_; |
| 190 | int* move_assigns_; |
| 191 | }; |
| 192 | |
| 193 | // Used for probing the number of copies in an argument. The instance is a |
| 194 | // copyable and non-movable type. |
| 195 | class CopyCounter { |
| 196 | public: |
| 197 | CopyCounter(int* copies, int* assigns) |
| 198 | : counter_(copies, assigns, nullptr, nullptr) {} |
| 199 | CopyCounter(const CopyCounter& other) : counter_(other.counter_) {} |
| 200 | CopyCounter& operator=(const CopyCounter& other) { |
| 201 | counter_ = other.counter_; |
| 202 | return *this; |
| 203 | } |
| 204 | |
| 205 | explicit CopyCounter(const DerivedCopyMoveCounter& other) : counter_(other) {} |
| 206 | |
| 207 | int copies() const { return counter_.copies(); } |
| 208 | |
| 209 | private: |
| 210 | CopyMoveCounter counter_; |
| 211 | }; |
| 212 | |
| 213 | // Used for probing the number of moves in an argument. The instance is a |
| 214 | // non-copyable and movable type. |
| 215 | class MoveCounter { |
| 216 | public: |
| 217 | MoveCounter(int* move_constructs, int* move_assigns) |
| 218 | : counter_(nullptr, nullptr, move_constructs, move_assigns) {} |
| 219 | MoveCounter(MoveCounter&& other) : counter_(std::move(other.counter_)) {} |
| 220 | MoveCounter& operator=(MoveCounter&& other) { |
| 221 | counter_ = std::move(other.counter_); |
| 222 | return *this; |
| 223 | } |
| 224 | |
| 225 | explicit MoveCounter(DerivedCopyMoveCounter&& other) |
| 226 | : counter_(std::move(other)) {} |
| 227 | |
| 228 | private: |
| 229 | CopyMoveCounter counter_; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 230 | }; |
| 231 | |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 232 | class DeleteCounter { |
| 233 | public: |
| 234 | explicit DeleteCounter(int* deletes) |
| 235 | : deletes_(deletes) { |
| 236 | } |
| 237 | |
| 238 | ~DeleteCounter() { |
| 239 | (*deletes_)++; |
| 240 | } |
| 241 | |
| 242 | void VoidMethod0() {} |
| 243 | |
| 244 | private: |
| 245 | int* deletes_; |
| 246 | }; |
| 247 | |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 248 | template <typename T> |
| 249 | T PassThru(T scoper) { |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 250 | return scoper; |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 251 | } |
| 252 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 253 | // Some test functions that we can Bind to. |
| 254 | template <typename T> |
| 255 | T PolymorphicIdentity(T t) { |
| 256 | return t; |
| 257 | } |
| 258 | |
tzik | 5b8bf3f | 2015-12-18 11:23:26 +0900 | [diff] [blame] | 259 | template <typename... Ts> |
| 260 | struct VoidPolymorphic { |
| 261 | static void Run(Ts... t) {} |
| 262 | }; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 263 | |
| 264 | int Identity(int n) { |
| 265 | return n; |
| 266 | } |
| 267 | |
| 268 | int ArrayGet(const int array[], int n) { |
| 269 | return array[n]; |
| 270 | } |
| 271 | |
| 272 | int Sum(int a, int b, int c, int d, int e, int f) { |
| 273 | return a + b + c + d + e + f; |
| 274 | } |
| 275 | |
| 276 | const char* CStringIdentity(const char* s) { |
| 277 | return s; |
| 278 | } |
| 279 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 280 | int GetCopies(const CopyMoveCounter& counter) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 281 | return counter.copies(); |
| 282 | } |
| 283 | |
| 284 | int UnwrapNoRefParent(NoRefParent p) { |
| 285 | return p.value; |
| 286 | } |
| 287 | |
| 288 | int UnwrapNoRefParentPtr(NoRefParent* p) { |
| 289 | return p->value; |
| 290 | } |
| 291 | |
| 292 | int UnwrapNoRefParentConstRef(const NoRefParent& p) { |
| 293 | return p.value; |
| 294 | } |
| 295 | |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 296 | void RefArgSet(int &n) { |
| 297 | n = 2; |
| 298 | } |
| 299 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 300 | void PtrArgSet(int *n) { |
| 301 | *n = 2; |
| 302 | } |
| 303 | |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 304 | int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { |
| 305 | return n; |
| 306 | } |
| 307 | |
willchan@chromium.org | 6141a82 | 2013-06-23 05:32:50 +0900 | [diff] [blame] | 308 | int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) { |
| 309 | return n; |
| 310 | } |
| 311 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 312 | void TakesACallback(const Closure& callback) { |
| 313 | callback.Run(); |
| 314 | } |
| 315 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 316 | class BindTest : public ::testing::Test { |
| 317 | public: |
| 318 | BindTest() { |
| 319 | const_has_ref_ptr_ = &has_ref_; |
| 320 | const_no_ref_ptr_ = &no_ref_; |
| 321 | static_func_mock_ptr = &static_func_mock_; |
| 322 | } |
| 323 | |
| 324 | virtual ~BindTest() { |
| 325 | } |
| 326 | |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 327 | static void VoidFunc0() { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 328 | static_func_mock_ptr->VoidMethod0(); |
| 329 | } |
| 330 | |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 331 | static int IntFunc0() { return static_func_mock_ptr->IntMethod0(); } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 332 | |
| 333 | protected: |
| 334 | StrictMock<NoRef> no_ref_; |
| 335 | StrictMock<HasRef> has_ref_; |
| 336 | const HasRef* const_has_ref_ptr_; |
| 337 | const NoRef* const_no_ref_ptr_; |
| 338 | StrictMock<NoRef> static_func_mock_; |
| 339 | |
| 340 | // Used by the static functions to perform expectations. |
| 341 | static StrictMock<NoRef>* static_func_mock_ptr; |
| 342 | |
| 343 | private: |
| 344 | DISALLOW_COPY_AND_ASSIGN(BindTest); |
| 345 | }; |
| 346 | |
| 347 | StrictMock<NoRef>* BindTest::static_func_mock_ptr; |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 348 | StrictMock<NoRef>* g_func_mock_ptr; |
| 349 | |
| 350 | void VoidFunc0() { |
| 351 | g_func_mock_ptr->VoidMethod0(); |
| 352 | } |
| 353 | |
| 354 | int IntFunc0() { |
| 355 | return g_func_mock_ptr->IntMethod0(); |
| 356 | } |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 357 | |
tzik | e7cf901 | 2016-08-23 13:23:49 +0900 | [diff] [blame] | 358 | TEST_F(BindTest, BasicTest) { |
| 359 | Callback<int(int, int, int)> cb = Bind(&Sum, 32, 16, 8); |
| 360 | EXPECT_EQ(92, cb.Run(13, 12, 11)); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 361 | |
tzik | e7cf901 | 2016-08-23 13:23:49 +0900 | [diff] [blame] | 362 | Callback<int(int, int, int, int, int, int)> c1 = Bind(&Sum); |
| 363 | EXPECT_EQ(69, c1.Run(14, 13, 12, 11, 10, 9)); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 364 | |
tzik | e7cf901 | 2016-08-23 13:23:49 +0900 | [diff] [blame] | 365 | Callback<int(int, int, int)> c2 = Bind(c1, 32, 16, 8); |
| 366 | EXPECT_EQ(86, c2.Run(11, 10, 9)); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 367 | |
tzik | e7cf901 | 2016-08-23 13:23:49 +0900 | [diff] [blame] | 368 | Callback<int()> c3 = Bind(c2, 4, 2, 1); |
| 369 | EXPECT_EQ(63, c3.Run()); |
ajwong@chromium.org | d483ffe | 2011-09-30 18:09:34 +0900 | [diff] [blame] | 370 | } |
| 371 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 372 | // Test that currying the rvalue result of another Bind() works correctly. |
| 373 | // - rvalue should be usable as argument to Bind(). |
| 374 | // - multiple runs of resulting Callback remain valid. |
| 375 | TEST_F(BindTest, CurryingRvalueResultOfBind) { |
| 376 | int n = 0; |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 377 | RepeatingClosure cb = BindRepeating(&TakesACallback, |
| 378 | BindRepeating(&PtrArgSet, &n)); |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 379 | |
| 380 | // If we implement Bind() such that the return value has auto_ptr-like |
| 381 | // semantics, the second call here will fail because ownership of |
| 382 | // the internal BindState<> would have been transfered to a *temporary* |
| 383 | // constructon of a Callback object on the first call. |
| 384 | cb.Run(); |
| 385 | EXPECT_EQ(2, n); |
| 386 | |
| 387 | n = 0; |
| 388 | cb.Run(); |
| 389 | EXPECT_EQ(2, n); |
| 390 | } |
| 391 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 392 | TEST_F(BindTest, RepeatingCallbackBasicTest) { |
| 393 | RepeatingCallback<int(int)> c0 = BindRepeating(&Sum, 1, 2, 4, 8, 16); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 394 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 395 | // RepeatingCallback can run via a lvalue-reference. |
| 396 | EXPECT_EQ(63, c0.Run(32)); |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 397 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 398 | // It is valid to call a RepeatingCallback more than once. |
| 399 | EXPECT_EQ(54, c0.Run(23)); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 400 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 401 | // BindRepeating can handle a RepeatingCallback as the target functor. |
| 402 | RepeatingCallback<int()> c1 = BindRepeating(c0, 11); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 403 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 404 | // RepeatingCallback can run via a rvalue-reference. |
| 405 | EXPECT_EQ(42, std::move(c1).Run()); |
| 406 | |
| 407 | // BindRepeating can handle a rvalue-reference of RepeatingCallback. |
| 408 | EXPECT_EQ(32, BindRepeating(std::move(c0), 1).Run()); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 409 | } |
| 410 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 411 | TEST_F(BindTest, OnceCallbackBasicTest) { |
| 412 | OnceCallback<int(int)> c0 = BindOnce(&Sum, 1, 2, 4, 8, 16); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 413 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 414 | // OnceCallback can run via a rvalue-reference. |
| 415 | EXPECT_EQ(63, std::move(c0).Run(32)); |
| 416 | |
| 417 | // After running via the rvalue-reference, the value of the OnceCallback |
| 418 | // is undefined. The implementation simply clears the instance after the |
| 419 | // invocation. |
| 420 | EXPECT_TRUE(c0.is_null()); |
| 421 | |
| 422 | c0 = BindOnce(&Sum, 2, 3, 5, 7, 11); |
| 423 | |
| 424 | // BindOnce can handle a rvalue-reference of OnceCallback as the target |
| 425 | // functor. |
| 426 | OnceCallback<int()> c1 = BindOnce(std::move(c0), 13); |
| 427 | EXPECT_EQ(41, std::move(c1).Run()); |
| 428 | |
| 429 | RepeatingCallback<int(int)> c2 = BindRepeating(&Sum, 2, 3, 5, 7, 11); |
| 430 | EXPECT_EQ(41, BindOnce(c2, 13).Run()); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 431 | } |
| 432 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 433 | // IgnoreResult adapter test. |
| 434 | // - Function with return value. |
| 435 | // - Method with return value. |
| 436 | // - Const Method with return. |
| 437 | // - Method with return value bound to WeakPtr<>. |
| 438 | // - Const Method with return bound to WeakPtr<>. |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 439 | TEST_F(BindTest, IgnoreResultForRepeating) { |
ajwong@chromium.org | 718dddf | 2011-09-28 09:26:37 +0900 | [diff] [blame] | 440 | EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 441 | EXPECT_CALL(has_ref_, AddRef()).Times(2); |
| 442 | EXPECT_CALL(has_ref_, Release()).Times(2); |
| 443 | EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10)); |
| 444 | EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11)); |
| 445 | EXPECT_CALL(no_ref_, IntMethod0()).WillOnce(Return(12)); |
| 446 | EXPECT_CALL(no_ref_, IntConstMethod0()).WillOnce(Return(13)); |
| 447 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 448 | RepeatingClosure normal_func_cb = BindRepeating(IgnoreResult(&IntFunc0)); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 449 | normal_func_cb.Run(); |
| 450 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 451 | RepeatingClosure non_void_method_cb = |
| 452 | BindRepeating(IgnoreResult(&HasRef::IntMethod0), &has_ref_); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 453 | non_void_method_cb.Run(); |
| 454 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 455 | RepeatingClosure non_void_const_method_cb = |
| 456 | BindRepeating(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 457 | non_void_const_method_cb.Run(); |
| 458 | |
| 459 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 460 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); |
| 461 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 462 | RepeatingClosure non_void_weak_method_cb = |
| 463 | BindRepeating(IgnoreResult(&NoRef::IntMethod0), |
| 464 | weak_factory.GetWeakPtr()); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 465 | non_void_weak_method_cb.Run(); |
| 466 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 467 | RepeatingClosure non_void_weak_const_method_cb = |
| 468 | BindRepeating(IgnoreResult(&NoRef::IntConstMethod0), |
| 469 | weak_factory.GetWeakPtr()); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 470 | non_void_weak_const_method_cb.Run(); |
| 471 | |
| 472 | weak_factory.InvalidateWeakPtrs(); |
| 473 | non_void_weak_const_method_cb.Run(); |
| 474 | non_void_weak_method_cb.Run(); |
ajwong@chromium.org | 718dddf | 2011-09-28 09:26:37 +0900 | [diff] [blame] | 475 | } |
| 476 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 477 | TEST_F(BindTest, IgnoreResultForOnce) { |
| 478 | EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); |
| 479 | EXPECT_CALL(has_ref_, AddRef()).Times(2); |
| 480 | EXPECT_CALL(has_ref_, Release()).Times(2); |
| 481 | EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10)); |
| 482 | EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11)); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 483 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 484 | OnceClosure normal_func_cb = BindOnce(IgnoreResult(&IntFunc0)); |
| 485 | std::move(normal_func_cb).Run(); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 486 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 487 | OnceClosure non_void_method_cb = |
| 488 | BindOnce(IgnoreResult(&HasRef::IntMethod0), &has_ref_); |
| 489 | std::move(non_void_method_cb).Run(); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 490 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 491 | OnceClosure non_void_const_method_cb = |
| 492 | BindOnce(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_); |
| 493 | std::move(non_void_const_method_cb).Run(); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 494 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 495 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 496 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 497 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 498 | OnceClosure non_void_weak_method_cb = |
| 499 | BindOnce(IgnoreResult(&NoRef::IntMethod0), |
| 500 | weak_factory.GetWeakPtr()); |
| 501 | OnceClosure non_void_weak_const_method_cb = |
| 502 | BindOnce(IgnoreResult(&NoRef::IntConstMethod0), |
| 503 | weak_factory.GetWeakPtr()); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 504 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 505 | weak_factory.InvalidateWeakPtrs(); |
| 506 | std::move(non_void_weak_const_method_cb).Run(); |
| 507 | std::move(non_void_weak_method_cb).Run(); |
ajwong@chromium.org | a7e7482 | 2011-03-24 11:02:17 +0900 | [diff] [blame] | 508 | } |
| 509 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 510 | // Functions that take reference parameters. |
| 511 | // - Forced reference parameter type still stores a copy. |
| 512 | // - Forced const reference parameter type still stores a copy. |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 513 | TEST_F(BindTest, ReferenceArgumentBindingForRepeating) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 514 | int n = 1; |
| 515 | int& ref_n = n; |
| 516 | const int& const_ref_n = n; |
| 517 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 518 | RepeatingCallback<int()> ref_copies_cb = BindRepeating(&Identity, ref_n); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 519 | EXPECT_EQ(n, ref_copies_cb.Run()); |
| 520 | n++; |
| 521 | EXPECT_EQ(n - 1, ref_copies_cb.Run()); |
| 522 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 523 | RepeatingCallback<int()> const_ref_copies_cb = |
| 524 | BindRepeating(&Identity, const_ref_n); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 525 | EXPECT_EQ(n, const_ref_copies_cb.Run()); |
| 526 | n++; |
| 527 | EXPECT_EQ(n - 1, const_ref_copies_cb.Run()); |
| 528 | } |
| 529 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 530 | TEST_F(BindTest, ReferenceArgumentBindingForOnce) { |
| 531 | int n = 1; |
| 532 | int& ref_n = n; |
| 533 | const int& const_ref_n = n; |
| 534 | |
| 535 | OnceCallback<int()> ref_copies_cb = BindOnce(&Identity, ref_n); |
| 536 | n++; |
| 537 | EXPECT_EQ(n - 1, std::move(ref_copies_cb).Run()); |
| 538 | |
| 539 | OnceCallback<int()> const_ref_copies_cb = |
| 540 | BindOnce(&Identity, const_ref_n); |
| 541 | n++; |
| 542 | EXPECT_EQ(n - 1, std::move(const_ref_copies_cb).Run()); |
| 543 | } |
| 544 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 545 | // Check that we can pass in arrays and have them be stored as a pointer. |
| 546 | // - Array of values stores a pointer. |
| 547 | // - Array of const values stores a pointer. |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 548 | TEST_F(BindTest, ArrayArgumentBindingForRepeating) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 549 | int array[4] = {1, 1, 1, 1}; |
| 550 | const int (*const_array_ptr)[4] = &array; |
| 551 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 552 | RepeatingCallback<int()> array_cb = BindRepeating(&ArrayGet, array, 1); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 553 | EXPECT_EQ(1, array_cb.Run()); |
| 554 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 555 | RepeatingCallback<int()> const_array_cb = |
| 556 | BindRepeating(&ArrayGet, *const_array_ptr, 1); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 557 | EXPECT_EQ(1, const_array_cb.Run()); |
| 558 | |
| 559 | array[1] = 3; |
| 560 | EXPECT_EQ(3, array_cb.Run()); |
| 561 | EXPECT_EQ(3, const_array_cb.Run()); |
| 562 | } |
| 563 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 564 | TEST_F(BindTest, ArrayArgumentBindingForOnce) { |
| 565 | int array[4] = {1, 1, 1, 1}; |
| 566 | const int (*const_array_ptr)[4] = &array; |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 567 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 568 | OnceCallback<int()> array_cb = BindOnce(&ArrayGet, array, 1); |
| 569 | OnceCallback<int()> const_array_cb = |
| 570 | BindOnce(&ArrayGet, *const_array_ptr, 1); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 571 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 572 | array[1] = 3; |
| 573 | EXPECT_EQ(3, std::move(array_cb).Run()); |
| 574 | EXPECT_EQ(3, std::move(const_array_cb).Run()); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 575 | } |
| 576 | |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 577 | // WeakPtr() support. |
| 578 | // - Method bound to WeakPtr<> to non-const object. |
| 579 | // - Const method bound to WeakPtr<> to non-const object. |
| 580 | // - Const method bound to WeakPtr<> to const object. |
| 581 | // - Normal Function with WeakPtr<> as P1 can have return type and is |
| 582 | // not canceled. |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 583 | TEST_F(BindTest, WeakPtrForRepeating) { |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 584 | EXPECT_CALL(no_ref_, VoidMethod0()); |
| 585 | EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); |
| 586 | |
| 587 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 588 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); |
| 589 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 590 | RepeatingClosure method_cb = |
| 591 | BindRepeating(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 592 | method_cb.Run(); |
| 593 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 594 | RepeatingClosure const_method_cb = |
| 595 | BindRepeating(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 596 | const_method_cb.Run(); |
| 597 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 598 | RepeatingClosure const_method_const_ptr_cb = |
| 599 | BindRepeating(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 600 | const_method_const_ptr_cb.Run(); |
| 601 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 602 | RepeatingCallback<int(int)> normal_func_cb = |
| 603 | BindRepeating(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr()); |
ajwong@chromium.org | c711b82 | 2011-05-17 07:35:14 +0900 | [diff] [blame] | 604 | EXPECT_EQ(1, normal_func_cb.Run(1)); |
| 605 | |
| 606 | weak_factory.InvalidateWeakPtrs(); |
| 607 | const_weak_factory.InvalidateWeakPtrs(); |
| 608 | |
| 609 | method_cb.Run(); |
| 610 | const_method_cb.Run(); |
| 611 | const_method_const_ptr_cb.Run(); |
| 612 | |
| 613 | // Still runs even after the pointers are invalidated. |
| 614 | EXPECT_EQ(2, normal_func_cb.Run(2)); |
| 615 | } |
| 616 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 617 | TEST_F(BindTest, WeakPtrForOnce) { |
| 618 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 619 | WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); |
| 620 | |
| 621 | OnceClosure method_cb = |
| 622 | BindOnce(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); |
| 623 | OnceClosure const_method_cb = |
| 624 | BindOnce(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| 625 | OnceClosure const_method_const_ptr_cb = |
| 626 | BindOnce(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| 627 | Callback<int(int)> normal_func_cb = |
| 628 | Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr()); |
| 629 | |
| 630 | weak_factory.InvalidateWeakPtrs(); |
| 631 | const_weak_factory.InvalidateWeakPtrs(); |
| 632 | |
| 633 | std::move(method_cb).Run(); |
| 634 | std::move(const_method_cb).Run(); |
| 635 | std::move(const_method_const_ptr_cb).Run(); |
| 636 | |
| 637 | // Still runs even after the pointers are invalidated. |
| 638 | EXPECT_EQ(2, std::move(normal_func_cb).Run(2)); |
| 639 | } |
| 640 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 641 | // ConstRef() wrapper support. |
| 642 | // - Binding w/o ConstRef takes a copy. |
| 643 | // - Binding a ConstRef takes a reference. |
| 644 | // - Binding ConstRef to a function ConstRef does not copy on invoke. |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 645 | TEST_F(BindTest, ConstRefForRepeating) { |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 646 | int n = 1; |
| 647 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 648 | RepeatingCallback<int()> copy_cb = BindRepeating(&Identity, n); |
| 649 | RepeatingCallback<int()> const_ref_cb = BindRepeating(&Identity, ConstRef(n)); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 650 | EXPECT_EQ(n, copy_cb.Run()); |
| 651 | EXPECT_EQ(n, const_ref_cb.Run()); |
| 652 | n++; |
| 653 | EXPECT_EQ(n - 1, copy_cb.Run()); |
| 654 | EXPECT_EQ(n, const_ref_cb.Run()); |
| 655 | |
| 656 | int copies = 0; |
| 657 | int assigns = 0; |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 658 | int move_constructs = 0; |
| 659 | int move_assigns = 0; |
| 660 | CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns); |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 661 | RepeatingCallback<int()> all_const_ref_cb = |
| 662 | BindRepeating(&GetCopies, ConstRef(counter)); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 663 | EXPECT_EQ(0, all_const_ref_cb.Run()); |
| 664 | EXPECT_EQ(0, copies); |
| 665 | EXPECT_EQ(0, assigns); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 666 | EXPECT_EQ(0, move_constructs); |
| 667 | EXPECT_EQ(0, move_assigns); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 668 | } |
| 669 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 670 | TEST_F(BindTest, ConstRefForOnce) { |
| 671 | int n = 1; |
willchan@chromium.org | 6141a82 | 2013-06-23 05:32:50 +0900 | [diff] [blame] | 672 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 673 | OnceCallback<int()> copy_cb = BindOnce(&Identity, n); |
| 674 | OnceCallback<int()> const_ref_cb = BindOnce(&Identity, ConstRef(n)); |
| 675 | n++; |
| 676 | EXPECT_EQ(n - 1, std::move(copy_cb).Run()); |
| 677 | EXPECT_EQ(n, std::move(const_ref_cb).Run()); |
| 678 | |
| 679 | int copies = 0; |
| 680 | int assigns = 0; |
| 681 | int move_constructs = 0; |
| 682 | int move_assigns = 0; |
| 683 | CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns); |
| 684 | OnceCallback<int()> all_const_ref_cb = |
| 685 | BindOnce(&GetCopies, ConstRef(counter)); |
| 686 | EXPECT_EQ(0, std::move(all_const_ref_cb).Run()); |
| 687 | EXPECT_EQ(0, copies); |
| 688 | EXPECT_EQ(0, assigns); |
| 689 | EXPECT_EQ(0, move_constructs); |
| 690 | EXPECT_EQ(0, move_assigns); |
willchan@chromium.org | 6141a82 | 2013-06-23 05:32:50 +0900 | [diff] [blame] | 691 | } |
| 692 | |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 693 | // Test Owned() support. |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 694 | TEST_F(BindTest, OwnedForRepeating) { |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 695 | int deletes = 0; |
| 696 | DeleteCounter* counter = new DeleteCounter(&deletes); |
| 697 | |
| 698 | // If we don't capture, delete happens on Callback destruction/reset. |
| 699 | // return the same value. |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 700 | RepeatingCallback<DeleteCounter*()> no_capture_cb = |
| 701 | BindRepeating(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 702 | ASSERT_EQ(counter, no_capture_cb.Run()); |
| 703 | ASSERT_EQ(counter, no_capture_cb.Run()); |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 704 | EXPECT_EQ(0, deletes); |
| 705 | no_capture_cb.Reset(); // This should trigger a delete. |
| 706 | EXPECT_EQ(1, deletes); |
| 707 | |
| 708 | deletes = 0; |
| 709 | counter = new DeleteCounter(&deletes); |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 710 | RepeatingClosure own_object_cb = |
| 711 | BindRepeating(&DeleteCounter::VoidMethod0, Owned(counter)); |
ajwong@chromium.org | 4133914 | 2011-10-15 09:34:42 +0900 | [diff] [blame] | 712 | own_object_cb.Run(); |
| 713 | EXPECT_EQ(0, deletes); |
| 714 | own_object_cb.Reset(); |
| 715 | EXPECT_EQ(1, deletes); |
| 716 | } |
| 717 | |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 718 | TEST_F(BindTest, OwnedForOnce) { |
| 719 | int deletes = 0; |
| 720 | DeleteCounter* counter = new DeleteCounter(&deletes); |
| 721 | |
| 722 | // If we don't capture, delete happens on Callback destruction/reset. |
| 723 | // return the same value. |
| 724 | OnceCallback<DeleteCounter*()> no_capture_cb = |
| 725 | BindOnce(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); |
| 726 | EXPECT_EQ(0, deletes); |
| 727 | no_capture_cb.Reset(); // This should trigger a delete. |
| 728 | EXPECT_EQ(1, deletes); |
| 729 | |
| 730 | deletes = 0; |
| 731 | counter = new DeleteCounter(&deletes); |
| 732 | OnceClosure own_object_cb = |
| 733 | BindOnce(&DeleteCounter::VoidMethod0, Owned(counter)); |
| 734 | EXPECT_EQ(0, deletes); |
| 735 | own_object_cb.Reset(); |
| 736 | EXPECT_EQ(1, deletes); |
| 737 | } |
| 738 | |
| 739 | template <typename T> |
| 740 | class BindVariantsTest : public ::testing::Test { |
| 741 | }; |
| 742 | |
| 743 | struct RepeatingTestConfig { |
| 744 | template <typename Signature> |
| 745 | using CallbackType = RepeatingCallback<Signature>; |
| 746 | using ClosureType = RepeatingClosure; |
| 747 | |
| 748 | template <typename F, typename... Args> |
| 749 | static CallbackType<MakeUnboundRunType<F, Args...>> |
| 750 | Bind(F&& f, Args&&... args) { |
| 751 | return BindRepeating(std::forward<F>(f), std::forward<Args>(args)...); |
| 752 | } |
| 753 | }; |
| 754 | |
| 755 | struct OnceTestConfig { |
| 756 | template <typename Signature> |
| 757 | using CallbackType = OnceCallback<Signature>; |
| 758 | using ClosureType = OnceClosure; |
| 759 | |
| 760 | template <typename F, typename... Args> |
| 761 | static CallbackType<MakeUnboundRunType<F, Args...>> |
| 762 | Bind(F&& f, Args&&... args) { |
| 763 | return BindOnce(std::forward<F>(f), std::forward<Args>(args)...); |
| 764 | } |
| 765 | }; |
| 766 | |
| 767 | using BindVariantsTestConfig = ::testing::Types< |
| 768 | RepeatingTestConfig, OnceTestConfig>; |
| 769 | TYPED_TEST_CASE(BindVariantsTest, BindVariantsTestConfig); |
| 770 | |
| 771 | template <typename TypeParam, typename Signature> |
| 772 | using CallbackType = typename TypeParam::template CallbackType<Signature>; |
| 773 | |
| 774 | // Function type support. |
| 775 | // - Normal function. |
| 776 | // - Normal function bound with non-refcounted first argument. |
| 777 | // - Method bound to non-const object. |
| 778 | // - Method bound to scoped_refptr. |
| 779 | // - Const method bound to non-const object. |
| 780 | // - Const method bound to const object. |
| 781 | // - Derived classes can be used with pointers to non-virtual base functions. |
| 782 | // - Derived classes can be used with pointers to virtual base functions (and |
| 783 | // preserve virtual dispatch). |
| 784 | TYPED_TEST(BindVariantsTest, FunctionTypeSupport) { |
| 785 | using ClosureType = typename TypeParam::ClosureType; |
| 786 | |
| 787 | StrictMock<HasRef> has_ref; |
| 788 | StrictMock<NoRef> no_ref; |
| 789 | StrictMock<NoRef> static_func_mock; |
| 790 | const HasRef* const_has_ref_ptr = &has_ref; |
| 791 | g_func_mock_ptr = &static_func_mock; |
| 792 | |
| 793 | EXPECT_CALL(static_func_mock, VoidMethod0()); |
| 794 | EXPECT_CALL(has_ref, AddRef()).Times(4); |
| 795 | EXPECT_CALL(has_ref, Release()).Times(4); |
| 796 | EXPECT_CALL(has_ref, VoidMethod0()).Times(2); |
| 797 | EXPECT_CALL(has_ref, VoidConstMethod0()).Times(2); |
| 798 | |
| 799 | ClosureType normal_cb = TypeParam::Bind(&VoidFunc0); |
| 800 | CallbackType<TypeParam, NoRef*()> normal_non_refcounted_cb = |
| 801 | TypeParam::Bind(&PolymorphicIdentity<NoRef*>, &no_ref); |
| 802 | std::move(normal_cb).Run(); |
| 803 | EXPECT_EQ(&no_ref, std::move(normal_non_refcounted_cb).Run()); |
| 804 | |
| 805 | ClosureType method_cb = TypeParam::Bind(&HasRef::VoidMethod0, &has_ref); |
| 806 | ClosureType method_refptr_cb = TypeParam::Bind(&HasRef::VoidMethod0, |
| 807 | make_scoped_refptr(&has_ref)); |
| 808 | ClosureType const_method_nonconst_obj_cb = |
| 809 | TypeParam::Bind(&HasRef::VoidConstMethod0, &has_ref); |
| 810 | ClosureType const_method_const_obj_cb = |
| 811 | TypeParam::Bind(&HasRef::VoidConstMethod0, const_has_ref_ptr); |
| 812 | std::move(method_cb).Run(); |
| 813 | std::move(method_refptr_cb).Run(); |
| 814 | std::move(const_method_nonconst_obj_cb).Run(); |
| 815 | std::move(const_method_const_obj_cb).Run(); |
| 816 | |
| 817 | Child child; |
| 818 | child.value = 0; |
| 819 | ClosureType virtual_set_cb = TypeParam::Bind(&Parent::VirtualSet, &child); |
| 820 | std::move(virtual_set_cb).Run(); |
| 821 | EXPECT_EQ(kChildValue, child.value); |
| 822 | |
| 823 | child.value = 0; |
| 824 | ClosureType non_virtual_set_cb = |
| 825 | TypeParam::Bind(&Parent::NonVirtualSet, &child); |
| 826 | std::move(non_virtual_set_cb).Run(); |
| 827 | EXPECT_EQ(kParentValue, child.value); |
| 828 | } |
| 829 | |
| 830 | // Return value support. |
| 831 | // - Function with return value. |
| 832 | // - Method with return value. |
| 833 | // - Const method with return value. |
| 834 | // - Move-only return value. |
| 835 | TYPED_TEST(BindVariantsTest, ReturnValues) { |
| 836 | StrictMock<NoRef> static_func_mock; |
| 837 | StrictMock<HasRef> has_ref; |
| 838 | g_func_mock_ptr = &static_func_mock; |
| 839 | const HasRef* const_has_ref_ptr = &has_ref; |
| 840 | |
| 841 | EXPECT_CALL(static_func_mock, IntMethod0()).WillOnce(Return(1337)); |
| 842 | EXPECT_CALL(has_ref, AddRef()).Times(4); |
| 843 | EXPECT_CALL(has_ref, Release()).Times(4); |
| 844 | EXPECT_CALL(has_ref, IntMethod0()).WillOnce(Return(31337)); |
| 845 | EXPECT_CALL(has_ref, IntConstMethod0()) |
| 846 | .WillOnce(Return(41337)) |
| 847 | .WillOnce(Return(51337)); |
| 848 | EXPECT_CALL(has_ref, UniquePtrMethod0()) |
| 849 | .WillOnce(Return(ByMove(MakeUnique<int>(42)))); |
| 850 | |
| 851 | CallbackType<TypeParam, int()> normal_cb = TypeParam::Bind(&IntFunc0); |
| 852 | CallbackType<TypeParam, int()> method_cb = |
| 853 | TypeParam::Bind(&HasRef::IntMethod0, &has_ref); |
| 854 | CallbackType<TypeParam, int()> const_method_nonconst_obj_cb = |
| 855 | TypeParam::Bind(&HasRef::IntConstMethod0, &has_ref); |
| 856 | CallbackType<TypeParam, int()> const_method_const_obj_cb = |
| 857 | TypeParam::Bind(&HasRef::IntConstMethod0, const_has_ref_ptr); |
| 858 | CallbackType<TypeParam, std::unique_ptr<int>()> move_only_rv_cb = |
| 859 | TypeParam::Bind(&HasRef::UniquePtrMethod0, &has_ref); |
| 860 | EXPECT_EQ(1337, std::move(normal_cb).Run()); |
| 861 | EXPECT_EQ(31337, std::move(method_cb).Run()); |
| 862 | EXPECT_EQ(41337, std::move(const_method_nonconst_obj_cb).Run()); |
| 863 | EXPECT_EQ(51337, std::move(const_method_const_obj_cb).Run()); |
| 864 | EXPECT_EQ(42, *std::move(move_only_rv_cb).Run()); |
| 865 | } |
| 866 | |
| 867 | // Argument binding tests. |
| 868 | // - Argument binding to primitive. |
| 869 | // - Argument binding to primitive pointer. |
| 870 | // - Argument binding to a literal integer. |
| 871 | // - Argument binding to a literal string. |
| 872 | // - Argument binding with template function. |
| 873 | // - Argument binding to an object. |
| 874 | // - Argument binding to pointer to incomplete type. |
| 875 | // - Argument gets type converted. |
| 876 | // - Pointer argument gets converted. |
| 877 | // - Const Reference forces conversion. |
| 878 | TYPED_TEST(BindVariantsTest, ArgumentBinding) { |
| 879 | int n = 2; |
| 880 | |
| 881 | EXPECT_EQ(n, TypeParam::Bind(&Identity, n).Run()); |
| 882 | EXPECT_EQ(&n, TypeParam::Bind(&PolymorphicIdentity<int*>, &n).Run()); |
| 883 | EXPECT_EQ(3, TypeParam::Bind(&Identity, 3).Run()); |
| 884 | EXPECT_STREQ("hi", TypeParam::Bind(&CStringIdentity, "hi").Run()); |
| 885 | EXPECT_EQ(4, TypeParam::Bind(&PolymorphicIdentity<int>, 4).Run()); |
| 886 | |
| 887 | NoRefParent p; |
| 888 | p.value = 5; |
| 889 | EXPECT_EQ(5, TypeParam::Bind(&UnwrapNoRefParent, p).Run()); |
| 890 | |
| 891 | IncompleteType* incomplete_ptr = reinterpret_cast<IncompleteType*>(123); |
| 892 | EXPECT_EQ(incomplete_ptr, |
| 893 | TypeParam::Bind(&PolymorphicIdentity<IncompleteType*>, |
| 894 | incomplete_ptr).Run()); |
| 895 | |
| 896 | NoRefChild c; |
| 897 | c.value = 6; |
| 898 | EXPECT_EQ(6, TypeParam::Bind(&UnwrapNoRefParent, c).Run()); |
| 899 | |
| 900 | c.value = 7; |
| 901 | EXPECT_EQ(7, TypeParam::Bind(&UnwrapNoRefParentPtr, &c).Run()); |
| 902 | |
| 903 | c.value = 8; |
| 904 | EXPECT_EQ(8, TypeParam::Bind(&UnwrapNoRefParentConstRef, c).Run()); |
| 905 | } |
| 906 | |
| 907 | // Unbound argument type support tests. |
| 908 | // - Unbound value. |
| 909 | // - Unbound pointer. |
| 910 | // - Unbound reference. |
| 911 | // - Unbound const reference. |
| 912 | // - Unbound unsized array. |
| 913 | // - Unbound sized array. |
| 914 | // - Unbound array-of-arrays. |
| 915 | TYPED_TEST(BindVariantsTest, UnboundArgumentTypeSupport) { |
| 916 | CallbackType<TypeParam, void(int)> unbound_value_cb = |
| 917 | TypeParam::Bind(&VoidPolymorphic<int>::Run); |
| 918 | CallbackType<TypeParam, void(int*)> unbound_pointer_cb = |
| 919 | TypeParam::Bind(&VoidPolymorphic<int*>::Run); |
| 920 | CallbackType<TypeParam, void(int&)> unbound_ref_cb = |
| 921 | TypeParam::Bind(&VoidPolymorphic<int&>::Run); |
| 922 | CallbackType<TypeParam, void(const int&)> unbound_const_ref_cb = |
| 923 | TypeParam::Bind(&VoidPolymorphic<const int&>::Run); |
| 924 | CallbackType<TypeParam, void(int[])> unbound_unsized_array_cb = |
| 925 | TypeParam::Bind(&VoidPolymorphic<int[]>::Run); |
| 926 | CallbackType<TypeParam, void(int[2])> unbound_sized_array_cb = |
| 927 | TypeParam::Bind(&VoidPolymorphic<int[2]>::Run); |
| 928 | CallbackType<TypeParam, void(int[][2])> unbound_array_of_arrays_cb = |
| 929 | TypeParam::Bind(&VoidPolymorphic<int[][2]>::Run); |
| 930 | CallbackType<TypeParam, void(int&)> unbound_ref_with_bound_arg = |
| 931 | TypeParam::Bind(&VoidPolymorphic<int, int&>::Run, 1); |
| 932 | } |
| 933 | |
| 934 | // Function with unbound reference parameter. |
| 935 | // - Original parameter is modified by callback. |
| 936 | TYPED_TEST(BindVariantsTest, UnboundReferenceSupport) { |
| 937 | int n = 0; |
| 938 | CallbackType<TypeParam, void(int&)> unbound_ref_cb = |
| 939 | TypeParam::Bind(&RefArgSet); |
| 940 | std::move(unbound_ref_cb).Run(n); |
| 941 | EXPECT_EQ(2, n); |
| 942 | } |
| 943 | |
| 944 | // Unretained() wrapper support. |
| 945 | // - Method bound to Unretained() non-const object. |
| 946 | // - Const method bound to Unretained() non-const object. |
| 947 | // - Const method bound to Unretained() const object. |
| 948 | TYPED_TEST(BindVariantsTest, Unretained) { |
| 949 | StrictMock<NoRef> no_ref; |
| 950 | const NoRef* const_no_ref_ptr = &no_ref; |
| 951 | |
| 952 | EXPECT_CALL(no_ref, VoidMethod0()); |
| 953 | EXPECT_CALL(no_ref, VoidConstMethod0()).Times(2); |
| 954 | |
| 955 | TypeParam::Bind(&NoRef::VoidMethod0, Unretained(&no_ref)).Run(); |
| 956 | TypeParam::Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref)).Run(); |
| 957 | TypeParam::Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr)).Run(); |
| 958 | } |
| 959 | |
| 960 | TYPED_TEST(BindVariantsTest, ScopedRefptr) { |
| 961 | StrictMock<HasRef> has_ref; |
| 962 | EXPECT_CALL(has_ref, AddRef()).Times(1); |
| 963 | EXPECT_CALL(has_ref, Release()).Times(1); |
| 964 | |
| 965 | const scoped_refptr<HasRef> refptr(&has_ref); |
| 966 | CallbackType<TypeParam, int()> scoped_refptr_const_ref_cb = |
| 967 | TypeParam::Bind(&FunctionWithScopedRefptrFirstParam, |
| 968 | base::ConstRef(refptr), 1); |
| 969 | EXPECT_EQ(1, std::move(scoped_refptr_const_ref_cb).Run()); |
| 970 | } |
| 971 | |
| 972 | TYPED_TEST(BindVariantsTest, UniquePtrReceiver) { |
tzik | fa7381d | 2016-05-12 08:05:05 +0900 | [diff] [blame] | 973 | std::unique_ptr<StrictMock<NoRef>> no_ref(new StrictMock<NoRef>); |
| 974 | EXPECT_CALL(*no_ref, VoidMethod0()).Times(1); |
tzik | d14dea2 | 2016-10-12 13:55:02 +0900 | [diff] [blame] | 975 | TypeParam::Bind(&NoRef::VoidMethod0, std::move(no_ref)).Run(); |
tzik | fa7381d | 2016-05-12 08:05:05 +0900 | [diff] [blame] | 976 | } |
| 977 | |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 978 | // Tests for Passed() wrapper support: |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 979 | // - Passed() can be constructed from a pointer to scoper. |
| 980 | // - Passed() can be constructed from a scoper rvalue. |
| 981 | // - Using Passed() gives Callback Ownership. |
| 982 | // - Ownership is transferred from Callback to callee on the first Run(). |
| 983 | // - Callback supports unbound arguments. |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 984 | template <typename T> |
| 985 | class BindMoveOnlyTypeTest : public ::testing::Test { |
| 986 | }; |
| 987 | |
| 988 | struct CustomDeleter { |
| 989 | void operator()(DeleteCounter* c) { delete c; } |
| 990 | }; |
| 991 | |
| 992 | using MoveOnlyTypesToTest = |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 993 | ::testing::Types<std::unique_ptr<DeleteCounter>, |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 994 | std::unique_ptr<DeleteCounter, CustomDeleter>>; |
| 995 | TYPED_TEST_CASE(BindMoveOnlyTypeTest, MoveOnlyTypesToTest); |
| 996 | |
| 997 | TYPED_TEST(BindMoveOnlyTypeTest, PassedToBoundCallback) { |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 998 | int deletes = 0; |
| 999 | |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1000 | TypeParam ptr(new DeleteCounter(&deletes)); |
| 1001 | Callback<TypeParam()> callback = Bind(&PassThru<TypeParam>, Passed(&ptr)); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 1002 | EXPECT_FALSE(ptr.get()); |
| 1003 | EXPECT_EQ(0, deletes); |
| 1004 | |
| 1005 | // If we never invoke the Callback, it retains ownership and deletes. |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 1006 | callback.Reset(); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 1007 | EXPECT_EQ(1, deletes); |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 1008 | } |
| 1009 | |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1010 | TYPED_TEST(BindMoveOnlyTypeTest, PassedWithRvalue) { |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1011 | int deletes = 0; |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1012 | Callback<TypeParam()> callback = Bind( |
| 1013 | &PassThru<TypeParam>, Passed(TypeParam(new DeleteCounter(&deletes)))); |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1014 | EXPECT_EQ(0, deletes); |
| 1015 | |
| 1016 | // If we never invoke the Callback, it retains ownership and deletes. |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1017 | callback.Reset(); |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1018 | EXPECT_EQ(1, deletes); |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1019 | } |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1020 | |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1021 | // Check that ownership can be transferred back out. |
| 1022 | TYPED_TEST(BindMoveOnlyTypeTest, ReturnMoveOnlyType) { |
| 1023 | int deletes = 0; |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1024 | DeleteCounter* counter = new DeleteCounter(&deletes); |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1025 | Callback<TypeParam()> callback = |
| 1026 | Bind(&PassThru<TypeParam>, Passed(TypeParam(counter))); |
| 1027 | TypeParam result = callback.Run(); |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1028 | ASSERT_EQ(counter, result.get()); |
| 1029 | EXPECT_EQ(0, deletes); |
| 1030 | |
| 1031 | // Resetting does not delete since ownership was transferred. |
| 1032 | callback.Reset(); |
| 1033 | EXPECT_EQ(0, deletes); |
| 1034 | |
| 1035 | // Ensure that we actually did get ownership. |
| 1036 | result.reset(); |
| 1037 | EXPECT_EQ(1, deletes); |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1038 | } |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1039 | |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1040 | TYPED_TEST(BindMoveOnlyTypeTest, UnboundForwarding) { |
| 1041 | int deletes = 0; |
| 1042 | TypeParam ptr(new DeleteCounter(&deletes)); |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1043 | // Test unbound argument forwarding. |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1044 | Callback<TypeParam(TypeParam)> cb_unbound = Bind(&PassThru<TypeParam>); |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1045 | cb_unbound.Run(std::move(ptr)); |
dcheng | 11c9cca | 2016-01-22 04:37:55 +0900 | [diff] [blame] | 1046 | EXPECT_EQ(1, deletes); |
dcheng | 3d580cf | 2015-12-15 05:31:52 +0900 | [diff] [blame] | 1047 | } |
| 1048 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 1049 | void VerifyVector(const std::vector<std::unique_ptr<int>>& v) { |
dcheng | b61e1f9 | 2016-02-02 13:09:33 +0900 | [diff] [blame] | 1050 | ASSERT_EQ(1u, v.size()); |
| 1051 | EXPECT_EQ(12345, *v[0]); |
| 1052 | } |
| 1053 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 1054 | std::vector<std::unique_ptr<int>> AcceptAndReturnMoveOnlyVector( |
| 1055 | std::vector<std::unique_ptr<int>> v) { |
dcheng | b61e1f9 | 2016-02-02 13:09:33 +0900 | [diff] [blame] | 1056 | VerifyVector(v); |
| 1057 | return v; |
| 1058 | } |
| 1059 | |
| 1060 | // Test that a vector containing move-only types can be used with Callback. |
| 1061 | TEST_F(BindTest, BindMoveOnlyVector) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 1062 | using MoveOnlyVector = std::vector<std::unique_ptr<int>>; |
dcheng | b61e1f9 | 2016-02-02 13:09:33 +0900 | [diff] [blame] | 1063 | |
| 1064 | MoveOnlyVector v; |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 1065 | v.push_back(WrapUnique(new int(12345))); |
dcheng | b61e1f9 | 2016-02-02 13:09:33 +0900 | [diff] [blame] | 1066 | |
| 1067 | // Early binding should work: |
| 1068 | base::Callback<MoveOnlyVector()> bound_cb = |
| 1069 | base::Bind(&AcceptAndReturnMoveOnlyVector, Passed(&v)); |
| 1070 | MoveOnlyVector intermediate_result = bound_cb.Run(); |
| 1071 | VerifyVector(intermediate_result); |
| 1072 | |
| 1073 | // As should passing it as an argument to Run(): |
| 1074 | base::Callback<MoveOnlyVector(MoveOnlyVector)> unbound_cb = |
| 1075 | base::Bind(&AcceptAndReturnMoveOnlyVector); |
| 1076 | MoveOnlyVector final_result = unbound_cb.Run(std::move(intermediate_result)); |
| 1077 | VerifyVector(final_result); |
| 1078 | } |
| 1079 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1080 | // Argument copy-constructor usage for non-reference copy-only parameters. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1081 | // - Bound arguments are only copied once. |
| 1082 | // - Forwarded arguments are only copied once. |
ajwong@chromium.org | f66a7db | 2011-12-23 06:12:58 +0900 | [diff] [blame] | 1083 | // - Forwarded arguments with coercions are only copied twice (once for the |
| 1084 | // coercion, and one for the final dispatch). |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1085 | TEST_F(BindTest, ArgumentCopies) { |
| 1086 | int copies = 0; |
| 1087 | int assigns = 0; |
| 1088 | |
| 1089 | CopyCounter counter(&copies, &assigns); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1090 | Bind(&VoidPolymorphic<CopyCounter>::Run, counter); |
| 1091 | EXPECT_EQ(1, copies); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1092 | EXPECT_EQ(0, assigns); |
| 1093 | |
| 1094 | copies = 0; |
| 1095 | assigns = 0; |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1096 | Bind(&VoidPolymorphic<CopyCounter>::Run, CopyCounter(&copies, &assigns)); |
| 1097 | EXPECT_EQ(1, copies); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1098 | EXPECT_EQ(0, assigns); |
| 1099 | |
| 1100 | copies = 0; |
| 1101 | assigns = 0; |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1102 | Bind(&VoidPolymorphic<CopyCounter>::Run).Run(counter); |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 1103 | EXPECT_EQ(2, copies); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1104 | EXPECT_EQ(0, assigns); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1105 | |
| 1106 | copies = 0; |
| 1107 | assigns = 0; |
| 1108 | Bind(&VoidPolymorphic<CopyCounter>::Run).Run(CopyCounter(&copies, &assigns)); |
| 1109 | EXPECT_EQ(1, copies); |
| 1110 | EXPECT_EQ(0, assigns); |
| 1111 | |
| 1112 | copies = 0; |
| 1113 | assigns = 0; |
| 1114 | DerivedCopyMoveCounter derived(&copies, &assigns, nullptr, nullptr); |
| 1115 | Bind(&VoidPolymorphic<CopyCounter>::Run).Run(CopyCounter(derived)); |
| 1116 | EXPECT_EQ(2, copies); |
| 1117 | EXPECT_EQ(0, assigns); |
| 1118 | |
| 1119 | copies = 0; |
| 1120 | assigns = 0; |
| 1121 | Bind(&VoidPolymorphic<CopyCounter>::Run) |
| 1122 | .Run(CopyCounter( |
| 1123 | DerivedCopyMoveCounter(&copies, &assigns, nullptr, nullptr))); |
| 1124 | EXPECT_EQ(2, copies); |
| 1125 | EXPECT_EQ(0, assigns); |
| 1126 | } |
| 1127 | |
| 1128 | // Argument move-constructor usage for move-only parameters. |
| 1129 | // - Bound arguments passed by move are not copied. |
| 1130 | TEST_F(BindTest, ArgumentMoves) { |
| 1131 | int move_constructs = 0; |
| 1132 | int move_assigns = 0; |
| 1133 | |
| 1134 | Bind(&VoidPolymorphic<const MoveCounter&>::Run, |
| 1135 | MoveCounter(&move_constructs, &move_assigns)); |
| 1136 | EXPECT_EQ(1, move_constructs); |
| 1137 | EXPECT_EQ(0, move_assigns); |
| 1138 | |
| 1139 | // TODO(tzik): Support binding move-only type into a non-reference parameter |
| 1140 | // of a variant of Callback. |
| 1141 | |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 1142 | move_constructs = 0; |
| 1143 | move_assigns = 0; |
| 1144 | Bind(&VoidPolymorphic<MoveCounter>::Run) |
| 1145 | .Run(MoveCounter(&move_constructs, &move_assigns)); |
| 1146 | EXPECT_EQ(1, move_constructs); |
| 1147 | EXPECT_EQ(0, move_assigns); |
| 1148 | |
| 1149 | move_constructs = 0; |
| 1150 | move_assigns = 0; |
| 1151 | Bind(&VoidPolymorphic<MoveCounter>::Run) |
| 1152 | .Run(MoveCounter(DerivedCopyMoveCounter( |
| 1153 | nullptr, nullptr, &move_constructs, &move_assigns))); |
| 1154 | EXPECT_EQ(2, move_constructs); |
| 1155 | EXPECT_EQ(0, move_assigns); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | // Argument constructor usage for non-reference movable-copyable |
| 1159 | // parameters. |
| 1160 | // - Bound arguments passed by move are not copied. |
| 1161 | // - Forwarded arguments are only copied once. |
| 1162 | // - Forwarded arguments with coercions are only copied once and moved once. |
| 1163 | TEST_F(BindTest, ArgumentCopiesAndMoves) { |
| 1164 | int copies = 0; |
| 1165 | int assigns = 0; |
| 1166 | int move_constructs = 0; |
| 1167 | int move_assigns = 0; |
| 1168 | |
| 1169 | CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns); |
| 1170 | Bind(&VoidPolymorphic<CopyMoveCounter>::Run, counter); |
| 1171 | EXPECT_EQ(1, copies); |
| 1172 | EXPECT_EQ(0, assigns); |
| 1173 | EXPECT_EQ(0, move_constructs); |
| 1174 | EXPECT_EQ(0, move_assigns); |
| 1175 | |
| 1176 | copies = 0; |
| 1177 | assigns = 0; |
| 1178 | move_constructs = 0; |
| 1179 | move_assigns = 0; |
| 1180 | Bind(&VoidPolymorphic<CopyMoveCounter>::Run, |
| 1181 | CopyMoveCounter(&copies, &assigns, &move_constructs, &move_assigns)); |
| 1182 | EXPECT_EQ(0, copies); |
| 1183 | EXPECT_EQ(0, assigns); |
| 1184 | EXPECT_EQ(1, move_constructs); |
| 1185 | EXPECT_EQ(0, move_assigns); |
| 1186 | |
| 1187 | copies = 0; |
| 1188 | assigns = 0; |
| 1189 | move_constructs = 0; |
| 1190 | move_assigns = 0; |
| 1191 | Bind(&VoidPolymorphic<CopyMoveCounter>::Run).Run(counter); |
| 1192 | EXPECT_EQ(1, copies); |
| 1193 | EXPECT_EQ(0, assigns); |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 1194 | EXPECT_EQ(1, move_constructs); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1195 | EXPECT_EQ(0, move_assigns); |
| 1196 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1197 | copies = 0; |
| 1198 | assigns = 0; |
| 1199 | move_constructs = 0; |
| 1200 | move_assigns = 0; |
| 1201 | Bind(&VoidPolymorphic<CopyMoveCounter>::Run) |
| 1202 | .Run(CopyMoveCounter(&copies, &assigns, &move_constructs, &move_assigns)); |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 1203 | EXPECT_EQ(0, copies); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1204 | EXPECT_EQ(0, assigns); |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 1205 | EXPECT_EQ(1, move_constructs); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1206 | EXPECT_EQ(0, move_assigns); |
| 1207 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1208 | DerivedCopyMoveCounter derived_counter(&copies, &assigns, &move_constructs, |
| 1209 | &move_assigns); |
| 1210 | copies = 0; |
| 1211 | assigns = 0; |
| 1212 | move_constructs = 0; |
| 1213 | move_assigns = 0; |
| 1214 | Bind(&VoidPolymorphic<CopyMoveCounter>::Run) |
| 1215 | .Run(CopyMoveCounter(derived_counter)); |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 1216 | EXPECT_EQ(1, copies); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1217 | EXPECT_EQ(0, assigns); |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 1218 | EXPECT_EQ(1, move_constructs); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1219 | EXPECT_EQ(0, move_assigns); |
| 1220 | |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1221 | copies = 0; |
| 1222 | assigns = 0; |
| 1223 | move_constructs = 0; |
| 1224 | move_assigns = 0; |
| 1225 | Bind(&VoidPolymorphic<CopyMoveCounter>::Run) |
| 1226 | .Run(CopyMoveCounter(DerivedCopyMoveCounter( |
| 1227 | &copies, &assigns, &move_constructs, &move_assigns))); |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 1228 | EXPECT_EQ(0, copies); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1229 | EXPECT_EQ(0, assigns); |
tzik | 0d4bab2 | 2016-03-09 14:46:05 +0900 | [diff] [blame] | 1230 | EXPECT_EQ(2, move_constructs); |
tzik | 14cb77d | 2016-02-15 20:54:30 +0900 | [diff] [blame] | 1231 | EXPECT_EQ(0, move_assigns); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1232 | } |
| 1233 | |
tzik | 31d3fae | 2016-07-08 18:42:38 +0900 | [diff] [blame] | 1234 | TEST_F(BindTest, CapturelessLambda) { |
| 1235 | EXPECT_FALSE(internal::IsConvertibleToRunType<void>::value); |
| 1236 | EXPECT_FALSE(internal::IsConvertibleToRunType<int>::value); |
| 1237 | EXPECT_FALSE(internal::IsConvertibleToRunType<void(*)()>::value); |
| 1238 | EXPECT_FALSE(internal::IsConvertibleToRunType<void(NoRef::*)()>::value); |
| 1239 | |
| 1240 | auto f = []() {}; |
| 1241 | EXPECT_TRUE(internal::IsConvertibleToRunType<decltype(f)>::value); |
| 1242 | |
| 1243 | int i = 0; |
krasin | 44a8a93 | 2017-01-19 04:29:33 +0900 | [diff] [blame] | 1244 | auto g = [i]() { (void)i; }; |
tzik | 31d3fae | 2016-07-08 18:42:38 +0900 | [diff] [blame] | 1245 | EXPECT_FALSE(internal::IsConvertibleToRunType<decltype(g)>::value); |
| 1246 | |
| 1247 | auto h = [](int, double) { return 'k'; }; |
| 1248 | EXPECT_TRUE((std::is_same< |
| 1249 | char(int, double), |
| 1250 | internal::ExtractCallableRunType<decltype(h)>>::value)); |
| 1251 | |
| 1252 | EXPECT_EQ(42, Bind([] { return 42; }).Run()); |
| 1253 | EXPECT_EQ(42, Bind([](int i) { return i * 7; }, 6).Run()); |
| 1254 | |
| 1255 | int x = 1; |
| 1256 | base::Callback<void(int)> cb = |
| 1257 | Bind([](int* x, int i) { *x *= i; }, Unretained(&x)); |
| 1258 | cb.Run(6); |
| 1259 | EXPECT_EQ(6, x); |
| 1260 | cb.Run(7); |
| 1261 | EXPECT_EQ(42, x); |
| 1262 | } |
| 1263 | |
tzik | 058872d | 2016-09-08 19:58:53 +0900 | [diff] [blame] | 1264 | TEST_F(BindTest, Cancellation) { |
dcheng | b78a84f | 2016-09-24 14:05:57 +0900 | [diff] [blame] | 1265 | EXPECT_CALL(no_ref_, VoidMethodWithIntArg(_)).Times(2); |
tzik | 058872d | 2016-09-08 19:58:53 +0900 | [diff] [blame] | 1266 | |
| 1267 | WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
tzik | 9c75652 | 2016-10-07 13:34:54 +0900 | [diff] [blame] | 1268 | RepeatingCallback<void(int)> cb = |
| 1269 | BindRepeating(&NoRef::VoidMethodWithIntArg, weak_factory.GetWeakPtr()); |
| 1270 | RepeatingClosure cb2 = BindRepeating(cb, 8); |
| 1271 | OnceClosure cb3 = BindOnce(cb, 8); |
| 1272 | |
| 1273 | OnceCallback<void(int)> cb4 = |
| 1274 | BindOnce(&NoRef::VoidMethodWithIntArg, weak_factory.GetWeakPtr()); |
| 1275 | EXPECT_FALSE(cb4.IsCancelled()); |
| 1276 | |
| 1277 | OnceClosure cb5 = BindOnce(std::move(cb4), 8); |
tzik | 058872d | 2016-09-08 19:58:53 +0900 | [diff] [blame] | 1278 | |
| 1279 | EXPECT_FALSE(cb.IsCancelled()); |
| 1280 | EXPECT_FALSE(cb2.IsCancelled()); |
tzik | 9c75652 | 2016-10-07 13:34:54 +0900 | [diff] [blame] | 1281 | EXPECT_FALSE(cb3.IsCancelled()); |
| 1282 | EXPECT_FALSE(cb5.IsCancelled()); |
tzik | 058872d | 2016-09-08 19:58:53 +0900 | [diff] [blame] | 1283 | |
dcheng | b78a84f | 2016-09-24 14:05:57 +0900 | [diff] [blame] | 1284 | cb.Run(6); |
tzik | 058872d | 2016-09-08 19:58:53 +0900 | [diff] [blame] | 1285 | cb2.Run(); |
| 1286 | |
| 1287 | weak_factory.InvalidateWeakPtrs(); |
| 1288 | |
| 1289 | EXPECT_TRUE(cb.IsCancelled()); |
| 1290 | EXPECT_TRUE(cb2.IsCancelled()); |
tzik | 9c75652 | 2016-10-07 13:34:54 +0900 | [diff] [blame] | 1291 | EXPECT_TRUE(cb3.IsCancelled()); |
| 1292 | EXPECT_TRUE(cb5.IsCancelled()); |
tzik | 058872d | 2016-09-08 19:58:53 +0900 | [diff] [blame] | 1293 | |
dcheng | b78a84f | 2016-09-24 14:05:57 +0900 | [diff] [blame] | 1294 | cb.Run(6); |
tzik | 058872d | 2016-09-08 19:58:53 +0900 | [diff] [blame] | 1295 | cb2.Run(); |
tzik | 9c75652 | 2016-10-07 13:34:54 +0900 | [diff] [blame] | 1296 | std::move(cb3).Run(); |
| 1297 | std::move(cb5).Run(); |
tzik | 058872d | 2016-09-08 19:58:53 +0900 | [diff] [blame] | 1298 | } |
| 1299 | |
tzik | c60a812 | 2016-09-13 14:28:59 +0900 | [diff] [blame] | 1300 | TEST_F(BindTest, OnceCallback) { |
tzik | c60a812 | 2016-09-13 14:28:59 +0900 | [diff] [blame] | 1301 | // Check if Callback variants have declarations of conversions as expected. |
| 1302 | // Copy constructor and assignment of RepeatingCallback. |
| 1303 | static_assert(std::is_constructible< |
| 1304 | RepeatingClosure, const RepeatingClosure&>::value, |
| 1305 | "RepeatingClosure should be copyable."); |
| 1306 | static_assert(is_assignable< |
| 1307 | RepeatingClosure, const RepeatingClosure&>::value, |
| 1308 | "RepeatingClosure should be copy-assignable."); |
| 1309 | |
| 1310 | // Move constructor and assignment of RepeatingCallback. |
| 1311 | static_assert(std::is_constructible< |
| 1312 | RepeatingClosure, RepeatingClosure&&>::value, |
| 1313 | "RepeatingClosure should be movable."); |
| 1314 | static_assert(is_assignable< |
| 1315 | RepeatingClosure, RepeatingClosure&&>::value, |
| 1316 | "RepeatingClosure should be move-assignable"); |
| 1317 | |
| 1318 | // Conversions from OnceCallback to RepeatingCallback. |
| 1319 | static_assert(!std::is_constructible< |
| 1320 | RepeatingClosure, const OnceClosure&>::value, |
| 1321 | "OnceClosure should not be convertible to RepeatingClosure."); |
| 1322 | static_assert(!is_assignable< |
| 1323 | RepeatingClosure, const OnceClosure&>::value, |
| 1324 | "OnceClosure should not be convertible to RepeatingClosure."); |
| 1325 | |
| 1326 | // Destructive conversions from OnceCallback to RepeatingCallback. |
| 1327 | static_assert(!std::is_constructible< |
| 1328 | RepeatingClosure, OnceClosure&&>::value, |
| 1329 | "OnceClosure should not be convertible to RepeatingClosure."); |
| 1330 | static_assert(!is_assignable< |
| 1331 | RepeatingClosure, OnceClosure&&>::value, |
| 1332 | "OnceClosure should not be convertible to RepeatingClosure."); |
| 1333 | |
| 1334 | // Copy constructor and assignment of OnceCallback. |
| 1335 | static_assert(!std::is_constructible< |
| 1336 | OnceClosure, const OnceClosure&>::value, |
| 1337 | "OnceClosure should not be copyable."); |
| 1338 | static_assert(!is_assignable< |
| 1339 | OnceClosure, const OnceClosure&>::value, |
| 1340 | "OnceClosure should not be copy-assignable"); |
| 1341 | |
| 1342 | // Move constructor and assignment of OnceCallback. |
| 1343 | static_assert(std::is_constructible< |
| 1344 | OnceClosure, OnceClosure&&>::value, |
| 1345 | "OnceClosure should be movable."); |
| 1346 | static_assert(is_assignable< |
| 1347 | OnceClosure, OnceClosure&&>::value, |
| 1348 | "OnceClosure should be move-assignable."); |
| 1349 | |
| 1350 | // Conversions from RepeatingCallback to OnceCallback. |
| 1351 | static_assert(std::is_constructible< |
| 1352 | OnceClosure, const RepeatingClosure&>::value, |
| 1353 | "RepeatingClosure should be convertible to OnceClosure."); |
| 1354 | static_assert(is_assignable< |
| 1355 | OnceClosure, const RepeatingClosure&>::value, |
| 1356 | "RepeatingClosure should be convertible to OnceClosure."); |
| 1357 | |
| 1358 | // Destructive conversions from RepeatingCallback to OnceCallback. |
| 1359 | static_assert(std::is_constructible< |
| 1360 | OnceClosure, RepeatingClosure&&>::value, |
| 1361 | "RepeatingClosure should be convertible to OnceClosure."); |
| 1362 | static_assert(is_assignable< |
| 1363 | OnceClosure, RepeatingClosure&&>::value, |
| 1364 | "RepeatingClosure should be covretible to OnceClosure."); |
| 1365 | |
| 1366 | OnceClosure cb = BindOnce(&VoidPolymorphic<>::Run); |
| 1367 | std::move(cb).Run(); |
| 1368 | |
| 1369 | // RepeatingCallback should be convertible to OnceCallback. |
| 1370 | OnceClosure cb2 = BindRepeating(&VoidPolymorphic<>::Run); |
| 1371 | std::move(cb2).Run(); |
| 1372 | |
| 1373 | RepeatingClosure cb3 = BindRepeating(&VoidPolymorphic<>::Run); |
| 1374 | cb = cb3; |
| 1375 | std::move(cb).Run(); |
| 1376 | |
| 1377 | cb = std::move(cb2); |
| 1378 | |
| 1379 | OnceCallback<void(int)> cb4 = BindOnce( |
| 1380 | &VoidPolymorphic<std::unique_ptr<int>, int>::Run, MakeUnique<int>(0)); |
| 1381 | BindOnce(std::move(cb4), 1).Run(); |
| 1382 | } |
| 1383 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1384 | // Callback construction and assignment tests. |
| 1385 | // - Construction from an InvokerStorageHolder should not cause ref/deref. |
| 1386 | // - Assignment from other callback should only cause one ref |
| 1387 | // |
| 1388 | // TODO(ajwong): Is there actually a way to test this? |
| 1389 | |
ajwong@chromium.org | cb17534 | 2011-02-27 10:25:59 +0900 | [diff] [blame] | 1390 | #if defined(OS_WIN) |
| 1391 | int __fastcall FastCallFunc(int n) { |
| 1392 | return n; |
| 1393 | } |
| 1394 | |
| 1395 | int __stdcall StdCallFunc(int n) { |
| 1396 | return n; |
| 1397 | } |
| 1398 | |
| 1399 | // Windows specific calling convention support. |
| 1400 | // - Can bind a __fastcall function. |
| 1401 | // - Can bind a __stdcall function. |
| 1402 | TEST_F(BindTest, WindowsCallingConventions) { |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 1403 | Callback<int()> fastcall_cb = Bind(&FastCallFunc, 1); |
ajwong@chromium.org | cb17534 | 2011-02-27 10:25:59 +0900 | [diff] [blame] | 1404 | EXPECT_EQ(1, fastcall_cb.Run()); |
| 1405 | |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 1406 | Callback<int()> stdcall_cb = Bind(&StdCallFunc, 2); |
ajwong@chromium.org | cb17534 | 2011-02-27 10:25:59 +0900 | [diff] [blame] | 1407 | EXPECT_EQ(2, stdcall_cb.Run()); |
| 1408 | } |
| 1409 | #endif |
| 1410 | |
hashimoto@chromium.org | bc14c57 | 2012-11-20 17:28:14 +0900 | [diff] [blame] | 1411 | // Test null callbacks cause a DCHECK. |
| 1412 | TEST(BindDeathTest, NullCallback) { |
| 1413 | base::Callback<void(int)> null_cb; |
| 1414 | ASSERT_TRUE(null_cb.is_null()); |
gab | 73fdcc3 | 2016-08-05 12:25:40 +0900 | [diff] [blame] | 1415 | EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42)); |
hashimoto@chromium.org | bc14c57 | 2012-11-20 17:28:14 +0900 | [diff] [blame] | 1416 | } |
| 1417 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 1418 | } // namespace |
| 1419 | } // namespace base |