[email protected] | 4c03b2e9 | 2012-01-03 19:36:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5b6c1a80 | 2009-07-17 18:14:47 | [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 | |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 6 | #include "base/callback.h" |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 7 | #include "base/callback_helpers.h" |
[email protected] | 59eff91 | 2011-02-18 23:29:31 | [diff] [blame] | 8 | #include "base/callback_internal.h" |
[email protected] | 31d92665 | 2012-06-13 23:15:16 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 5b6c1a80 | 2009-07-17 18:14:47 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 13 | namespace base { |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 14 | |
[email protected] | 5b6c1a80 | 2009-07-17 18:14:47 | [diff] [blame] | 15 | namespace { |
| 16 | |
[email protected] | 4346ef91 | 2011-02-19 00:52:15 | [diff] [blame] | 17 | struct FakeInvoker { |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 18 | typedef void(RunType)(internal::BindStateBase*); |
| 19 | static void Run(internal::BindStateBase*) { |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 20 | } |
| 21 | }; |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 22 | |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 23 | } // namespace |
| 24 | |
| 25 | namespace internal { |
| 26 | template <typename Runnable, typename RunType, typename BoundArgsType> |
| 27 | struct BindState; |
[email protected] | 5b6c1a80 | 2009-07-17 18:14:47 | [diff] [blame] | 28 | |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 29 | // White-box testpoints to inject into a Callback<> object for checking |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [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 { |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 37 | public: |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 38 | typedef FakeInvoker InvokerType; |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 39 | private: |
| 40 | ~BindState() override {} |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 41 | }; |
| 42 | |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 43 | template <> |
| 44 | struct BindState<void(void), void(void), |
| 45 | void(FakeInvoker, FakeInvoker)> |
| 46 | : public BindStateBase { |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 47 | public: |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 48 | typedef FakeInvoker InvokerType; |
dmichael | 7d09007e | 2014-12-18 22:30:11 | [diff] [blame] | 49 | private: |
| 50 | ~BindState() override {} |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 51 | }; |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 52 | } // namespace internal |
| 53 | |
| 54 | namespace { |
| 55 | |
| 56 | typedef internal::BindState<void(void), void(void), void(FakeInvoker)> |
| 57 | FakeBindState1; |
| 58 | typedef internal::BindState<void(void), void(void), |
| 59 | void(FakeInvoker, FakeInvoker)> |
| 60 | FakeBindState2; |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 61 | |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 62 | class CallbackTest : public ::testing::Test { |
| 63 | public: |
| 64 | CallbackTest() |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 65 | : callback_a_(new FakeBindState1()), |
| 66 | callback_b_(new FakeBindState2()) { |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 67 | } |
| 68 | |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 69 | ~CallbackTest() override {} |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 70 | |
| 71 | protected: |
| 72 | Callback<void(void)> callback_a_; |
| 73 | const Callback<void(void)> callback_b_; // Ensure APIs work with const. |
| 74 | Callback<void(void)> null_callback_; |
| 75 | }; |
| 76 | |
| 77 | // Ensure we can create unbound callbacks. We need this to be able to store |
| 78 | // them in class members that can be initialized later. |
| 79 | TEST_F(CallbackTest, DefaultConstruction) { |
| 80 | Callback<void(void)> c0; |
| 81 | Callback<void(int)> c1; |
| 82 | Callback<void(int,int)> c2; |
| 83 | Callback<void(int,int,int)> c3; |
| 84 | Callback<void(int,int,int,int)> c4; |
| 85 | Callback<void(int,int,int,int,int)> c5; |
| 86 | Callback<void(int,int,int,int,int,int)> c6; |
| 87 | |
| 88 | EXPECT_TRUE(c0.is_null()); |
| 89 | EXPECT_TRUE(c1.is_null()); |
| 90 | EXPECT_TRUE(c2.is_null()); |
| 91 | EXPECT_TRUE(c3.is_null()); |
| 92 | EXPECT_TRUE(c4.is_null()); |
| 93 | EXPECT_TRUE(c5.is_null()); |
| 94 | EXPECT_TRUE(c6.is_null()); |
| 95 | } |
| 96 | |
| 97 | TEST_F(CallbackTest, IsNull) { |
| 98 | EXPECT_TRUE(null_callback_.is_null()); |
| 99 | EXPECT_FALSE(callback_a_.is_null()); |
| 100 | EXPECT_FALSE(callback_b_.is_null()); |
| 101 | } |
| 102 | |
| 103 | TEST_F(CallbackTest, Equals) { |
| 104 | EXPECT_TRUE(callback_a_.Equals(callback_a_)); |
| 105 | EXPECT_FALSE(callback_a_.Equals(callback_b_)); |
| 106 | EXPECT_FALSE(callback_b_.Equals(callback_a_)); |
| 107 | |
| 108 | // We should compare based on instance, not type. |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 109 | Callback<void(void)> callback_c(new FakeBindState1()); |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 110 | Callback<void(void)> callback_a2 = callback_a_; |
| 111 | EXPECT_TRUE(callback_a_.Equals(callback_a2)); |
| 112 | EXPECT_FALSE(callback_a_.Equals(callback_c)); |
| 113 | |
| 114 | // Empty, however, is always equal to empty. |
| 115 | Callback<void(void)> empty2; |
| 116 | EXPECT_TRUE(null_callback_.Equals(empty2)); |
| 117 | } |
| 118 | |
| 119 | TEST_F(CallbackTest, Reset) { |
| 120 | // Resetting should bring us back to empty. |
| 121 | ASSERT_FALSE(callback_a_.is_null()); |
| 122 | ASSERT_FALSE(callback_a_.Equals(null_callback_)); |
| 123 | |
| 124 | callback_a_.Reset(); |
| 125 | |
| 126 | EXPECT_TRUE(callback_a_.is_null()); |
| 127 | EXPECT_TRUE(callback_a_.Equals(null_callback_)); |
| 128 | } |
| 129 | |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 130 | struct TestForReentrancy { |
| 131 | TestForReentrancy() |
| 132 | : cb_already_run(false), |
| 133 | cb(Bind(&TestForReentrancy::AssertCBIsNull, Unretained(this))) { |
| 134 | } |
| 135 | void AssertCBIsNull() { |
| 136 | ASSERT_TRUE(cb.is_null()); |
| 137 | cb_already_run = true; |
| 138 | } |
| 139 | bool cb_already_run; |
| 140 | Closure cb; |
| 141 | }; |
| 142 | |
| 143 | TEST_F(CallbackTest, ResetAndReturn) { |
| 144 | TestForReentrancy tfr; |
| 145 | ASSERT_FALSE(tfr.cb.is_null()); |
| 146 | ASSERT_FALSE(tfr.cb_already_run); |
| 147 | ResetAndReturn(&tfr.cb).Run(); |
| 148 | ASSERT_TRUE(tfr.cb.is_null()); |
| 149 | ASSERT_TRUE(tfr.cb_already_run); |
| 150 | } |
| 151 | |
[email protected] | 31d92665 | 2012-06-13 23:15:16 | [diff] [blame] | 152 | class CallbackOwner : public base::RefCounted<CallbackOwner> { |
| 153 | public: |
[email protected] | f3c697c5 | 2013-01-15 10:52:11 | [diff] [blame] | 154 | explicit CallbackOwner(bool* deleted) { |
[email protected] | 31d92665 | 2012-06-13 23:15:16 | [diff] [blame] | 155 | callback_ = Bind(&CallbackOwner::Unused, this); |
| 156 | deleted_ = deleted; |
| 157 | } |
| 158 | void Reset() { |
| 159 | callback_.Reset(); |
| 160 | // We are deleted here if no-one else had a ref to us. |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | friend class base::RefCounted<CallbackOwner>; |
| 165 | virtual ~CallbackOwner() { |
| 166 | *deleted_ = true; |
| 167 | } |
| 168 | void Unused() { |
| 169 | FAIL() << "Should never be called"; |
| 170 | } |
| 171 | |
| 172 | Closure callback_; |
| 173 | bool* deleted_; |
| 174 | }; |
| 175 | |
| 176 | TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { |
| 177 | bool deleted = false; |
| 178 | CallbackOwner* owner = new CallbackOwner(&deleted); |
| 179 | owner->Reset(); |
| 180 | ASSERT_TRUE(deleted); |
| 181 | } |
| 182 | |
[email protected] | 15fcb659 | 2011-02-18 04:05:14 | [diff] [blame] | 183 | } // namespace |
| 184 | } // namespace base |