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