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