jhawkins@chromium.org | 4a31021 | 2012-01-04 04:36:57 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
jam@chromium.org | 2750bf8 | 2009-07-18 03:14:47 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
fischman@chromium.org | dc221a7 | 2012-03-25 05:37:27 +0900 | [diff] [blame] | 5 | #include "base/bind.h" |
akalin@chromium.org | 6ed4f88 | 2010-02-19 12:15:59 +0900 | [diff] [blame] | 6 | #include "base/callback.h" |
fischman@chromium.org | dc221a7 | 2012-03-25 05:37:27 +0900 | [diff] [blame] | 7 | #include "base/callback_helpers.h" |
ajwong@chromium.org | fa0ff43 | 2011-02-19 08:29:31 +0900 | [diff] [blame] | 8 | #include "base/callback_internal.h" |
mattm@chromium.org | bf27cb7 | 2012-06-14 08:15:16 +0900 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
jam@chromium.org | 2750bf8 | 2009-07-18 03:14:47 +0900 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 13 | namespace base { |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 14 | |
jam@chromium.org | 2750bf8 | 2009-07-18 03:14:47 +0900 | [diff] [blame] | 15 | namespace { |
| 16 | |
ajwong@chromium.org | e064823 | 2011-02-19 09:52:15 +0900 | [diff] [blame] | 17 | struct FakeInvoker { |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 18 | typedef void(RunType)(internal::BindStateBase*); |
| 19 | static void Run(internal::BindStateBase*) { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 20 | } |
| 21 | }; |
fischman@chromium.org | dc221a7 | 2012-03-25 05:37:27 +0900 | [diff] [blame] | 22 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 23 | } // namespace |
| 24 | |
| 25 | namespace internal { |
| 26 | template <typename Runnable, typename RunType, typename BoundArgsType> |
| 27 | struct BindState; |
jam@chromium.org | 2750bf8 | 2009-07-18 03:14:47 +0900 | [diff] [blame] | 28 | |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 29 | // White-box testpoints to inject into a Callback<> object for checking |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 30 | // comparators and emptiness APIs. Use a BindState that is specialized |
| 31 | // based on a type we declared in the anonymous namespace above to remove any |
| 32 | // chance of colliding with another instantiation and breaking the |
| 33 | // one-definition-rule. |
| 34 | template <> |
| 35 | struct BindState<void(void), void(void), void(FakeInvoker)> |
| 36 | : public BindStateBase { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 37 | public: |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 38 | BindState() : BindStateBase(&Destroy) {} |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 39 | typedef FakeInvoker InvokerType; |
dmichael | 2f7bc20 | 2014-12-19 07:30:11 +0900 | [diff] [blame] | 40 | private: |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 41 | ~BindState() {} |
| 42 | static void Destroy(BindStateBase* self) { |
| 43 | delete static_cast<BindState*>(self); |
| 44 | } |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 45 | }; |
| 46 | |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 47 | template <> |
| 48 | struct BindState<void(void), void(void), |
| 49 | void(FakeInvoker, FakeInvoker)> |
| 50 | : public BindStateBase { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 51 | public: |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 52 | BindState() : BindStateBase(&Destroy) {} |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 53 | typedef FakeInvoker InvokerType; |
dmichael | 2f7bc20 | 2014-12-19 07:30:11 +0900 | [diff] [blame] | 54 | private: |
tapted | 9cef421 | 2015-05-14 17:03:32 +0900 | [diff] [blame] | 55 | ~BindState() {} |
| 56 | static void Destroy(BindStateBase* self) { |
| 57 | delete static_cast<BindState*>(self); |
| 58 | } |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 59 | }; |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 60 | } // namespace internal |
| 61 | |
| 62 | namespace { |
| 63 | |
| 64 | typedef internal::BindState<void(void), void(void), void(FakeInvoker)> |
| 65 | FakeBindState1; |
| 66 | typedef internal::BindState<void(void), void(void), |
| 67 | void(FakeInvoker, FakeInvoker)> |
| 68 | FakeBindState2; |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 69 | |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 70 | class CallbackTest : public ::testing::Test { |
| 71 | public: |
| 72 | CallbackTest() |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 73 | : callback_a_(new FakeBindState1()), |
| 74 | callback_b_(new FakeBindState2()) { |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 75 | } |
| 76 | |
dcheng | ca87abb | 2014-12-23 11:56:47 +0900 | [diff] [blame] | 77 | ~CallbackTest() override {} |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 78 | |
| 79 | protected: |
| 80 | Callback<void(void)> callback_a_; |
| 81 | const Callback<void(void)> callback_b_; // Ensure APIs work with const. |
| 82 | Callback<void(void)> null_callback_; |
| 83 | }; |
| 84 | |
| 85 | // Ensure we can create unbound callbacks. We need this to be able to store |
| 86 | // them in class members that can be initialized later. |
| 87 | TEST_F(CallbackTest, DefaultConstruction) { |
| 88 | Callback<void(void)> c0; |
| 89 | Callback<void(int)> c1; |
| 90 | Callback<void(int,int)> c2; |
| 91 | Callback<void(int,int,int)> c3; |
| 92 | Callback<void(int,int,int,int)> c4; |
| 93 | Callback<void(int,int,int,int,int)> c5; |
| 94 | Callback<void(int,int,int,int,int,int)> c6; |
| 95 | |
| 96 | EXPECT_TRUE(c0.is_null()); |
| 97 | EXPECT_TRUE(c1.is_null()); |
| 98 | EXPECT_TRUE(c2.is_null()); |
| 99 | EXPECT_TRUE(c3.is_null()); |
| 100 | EXPECT_TRUE(c4.is_null()); |
| 101 | EXPECT_TRUE(c5.is_null()); |
| 102 | EXPECT_TRUE(c6.is_null()); |
| 103 | } |
| 104 | |
| 105 | TEST_F(CallbackTest, IsNull) { |
| 106 | EXPECT_TRUE(null_callback_.is_null()); |
| 107 | EXPECT_FALSE(callback_a_.is_null()); |
| 108 | EXPECT_FALSE(callback_b_.is_null()); |
| 109 | } |
| 110 | |
| 111 | TEST_F(CallbackTest, Equals) { |
| 112 | EXPECT_TRUE(callback_a_.Equals(callback_a_)); |
| 113 | EXPECT_FALSE(callback_a_.Equals(callback_b_)); |
| 114 | EXPECT_FALSE(callback_b_.Equals(callback_a_)); |
| 115 | |
| 116 | // We should compare based on instance, not type. |
ajwong@chromium.org | abd7000 | 2011-12-20 09:10:04 +0900 | [diff] [blame] | 117 | Callback<void(void)> callback_c(new FakeBindState1()); |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 118 | Callback<void(void)> callback_a2 = callback_a_; |
| 119 | EXPECT_TRUE(callback_a_.Equals(callback_a2)); |
| 120 | EXPECT_FALSE(callback_a_.Equals(callback_c)); |
| 121 | |
| 122 | // Empty, however, is always equal to empty. |
| 123 | Callback<void(void)> empty2; |
| 124 | EXPECT_TRUE(null_callback_.Equals(empty2)); |
| 125 | } |
| 126 | |
| 127 | TEST_F(CallbackTest, Reset) { |
| 128 | // Resetting should bring us back to empty. |
| 129 | ASSERT_FALSE(callback_a_.is_null()); |
| 130 | ASSERT_FALSE(callback_a_.Equals(null_callback_)); |
| 131 | |
| 132 | callback_a_.Reset(); |
| 133 | |
| 134 | EXPECT_TRUE(callback_a_.is_null()); |
| 135 | EXPECT_TRUE(callback_a_.Equals(null_callback_)); |
| 136 | } |
| 137 | |
fischman@chromium.org | dc221a7 | 2012-03-25 05:37:27 +0900 | [diff] [blame] | 138 | struct TestForReentrancy { |
| 139 | TestForReentrancy() |
| 140 | : cb_already_run(false), |
| 141 | cb(Bind(&TestForReentrancy::AssertCBIsNull, Unretained(this))) { |
| 142 | } |
| 143 | void AssertCBIsNull() { |
| 144 | ASSERT_TRUE(cb.is_null()); |
| 145 | cb_already_run = true; |
| 146 | } |
| 147 | bool cb_already_run; |
| 148 | Closure cb; |
| 149 | }; |
| 150 | |
| 151 | TEST_F(CallbackTest, ResetAndReturn) { |
| 152 | TestForReentrancy tfr; |
| 153 | ASSERT_FALSE(tfr.cb.is_null()); |
| 154 | ASSERT_FALSE(tfr.cb_already_run); |
| 155 | ResetAndReturn(&tfr.cb).Run(); |
| 156 | ASSERT_TRUE(tfr.cb.is_null()); |
| 157 | ASSERT_TRUE(tfr.cb_already_run); |
| 158 | } |
| 159 | |
mattm@chromium.org | bf27cb7 | 2012-06-14 08:15:16 +0900 | [diff] [blame] | 160 | class CallbackOwner : public base::RefCounted<CallbackOwner> { |
| 161 | public: |
hans@chromium.org | 07615b4 | 2013-01-15 19:52:11 +0900 | [diff] [blame] | 162 | explicit CallbackOwner(bool* deleted) { |
mattm@chromium.org | bf27cb7 | 2012-06-14 08:15:16 +0900 | [diff] [blame] | 163 | callback_ = Bind(&CallbackOwner::Unused, this); |
| 164 | deleted_ = deleted; |
| 165 | } |
| 166 | void Reset() { |
| 167 | callback_.Reset(); |
| 168 | // We are deleted here if no-one else had a ref to us. |
| 169 | } |
| 170 | |
| 171 | private: |
| 172 | friend class base::RefCounted<CallbackOwner>; |
| 173 | virtual ~CallbackOwner() { |
| 174 | *deleted_ = true; |
| 175 | } |
| 176 | void Unused() { |
| 177 | FAIL() << "Should never be called"; |
| 178 | } |
| 179 | |
| 180 | Closure callback_; |
| 181 | bool* deleted_; |
| 182 | }; |
| 183 | |
| 184 | TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { |
| 185 | bool deleted = false; |
| 186 | CallbackOwner* owner = new CallbackOwner(&deleted); |
| 187 | owner->Reset(); |
| 188 | ASSERT_TRUE(deleted); |
| 189 | } |
| 190 | |
ajwong@chromium.org | 62fb0a0 | 2011-02-18 13:05:14 +0900 | [diff] [blame] | 191 | } // namespace |
| 192 | } // namespace base |