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