OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/callback_internal.h" | 8 #include "base/callback_internal.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 struct FakeInvoker { | 17 struct FakeInvoker { |
18 typedef void(RunType)(internal::BindStateBase*); | 18 // MSVC 2013 doesn't support Type Alias of function types. |
| 19 // Revisit this after we update it to newer version. |
| 20 typedef void RunType(internal::BindStateBase*); |
19 static void Run(internal::BindStateBase*) { | 21 static void Run(internal::BindStateBase*) { |
20 } | 22 } |
21 }; | 23 }; |
22 | 24 |
23 } // namespace | 25 } // namespace |
24 | 26 |
25 namespace internal { | 27 namespace internal { |
26 | 28 |
27 // White-box testpoints to inject into a Callback<> object for checking | 29 // White-box testpoints to inject into a Callback<> object for checking |
28 // comparators and emptiness APIs. Use a BindState that is specialized | 30 // comparators and emptiness APIs. Use a BindState that is specialized |
29 // based on a type we declared in the anonymous namespace above to remove any | 31 // based on a type we declared in the anonymous namespace above to remove any |
30 // chance of colliding with another instantiation and breaking the | 32 // chance of colliding with another instantiation and breaking the |
31 // one-definition-rule. | 33 // one-definition-rule. |
32 template <> | 34 template <> |
33 struct BindState<void(void), void(void), FakeInvoker> | 35 struct BindState<void(), void(), FakeInvoker> |
34 : public BindStateBase { | 36 : public BindStateBase { |
35 public: | 37 public: |
36 BindState() : BindStateBase(&Destroy) {} | 38 BindState() : BindStateBase(&Destroy) {} |
37 typedef FakeInvoker InvokerType; | 39 using InvokerType = FakeInvoker; |
38 private: | 40 private: |
39 ~BindState() {} | 41 ~BindState() {} |
40 static void Destroy(BindStateBase* self) { | 42 static void Destroy(BindStateBase* self) { |
41 delete static_cast<BindState*>(self); | 43 delete static_cast<BindState*>(self); |
42 } | 44 } |
43 }; | 45 }; |
44 | 46 |
45 template <> | 47 template <> |
46 struct BindState<void(void), void(void), FakeInvoker, FakeInvoker> | 48 struct BindState<void(), void(), FakeInvoker, FakeInvoker> |
47 : public BindStateBase { | 49 : public BindStateBase { |
48 public: | 50 public: |
49 BindState() : BindStateBase(&Destroy) {} | 51 BindState() : BindStateBase(&Destroy) {} |
50 typedef FakeInvoker InvokerType; | 52 using InvokerType = FakeInvoker; |
51 private: | 53 private: |
52 ~BindState() {} | 54 ~BindState() {} |
53 static void Destroy(BindStateBase* self) { | 55 static void Destroy(BindStateBase* self) { |
54 delete static_cast<BindState*>(self); | 56 delete static_cast<BindState*>(self); |
55 } | 57 } |
56 }; | 58 }; |
57 } // namespace internal | 59 } // namespace internal |
58 | 60 |
59 namespace { | 61 namespace { |
60 | 62 |
61 typedef internal::BindState<void(void), void(void), FakeInvoker> | 63 using FakeBindState1 = internal::BindState<void(), void(), FakeInvoker>; |
62 FakeBindState1; | 64 using FakeBindState2 = |
63 typedef internal::BindState<void(void), void(void), FakeInvoker, FakeInvoker> | 65 internal::BindState<void(), void(), FakeInvoker, FakeInvoker>; |
64 FakeBindState2; | |
65 | 66 |
66 class CallbackTest : public ::testing::Test { | 67 class CallbackTest : public ::testing::Test { |
67 public: | 68 public: |
68 CallbackTest() | 69 CallbackTest() |
69 : callback_a_(new FakeBindState1()), | 70 : callback_a_(new FakeBindState1()), |
70 callback_b_(new FakeBindState2()) { | 71 callback_b_(new FakeBindState2()) { |
71 } | 72 } |
72 | 73 |
73 ~CallbackTest() override {} | 74 ~CallbackTest() override {} |
74 | 75 |
75 protected: | 76 protected: |
76 Callback<void(void)> callback_a_; | 77 Callback<void()> callback_a_; |
77 const Callback<void(void)> callback_b_; // Ensure APIs work with const. | 78 const Callback<void()> callback_b_; // Ensure APIs work with const. |
78 Callback<void(void)> null_callback_; | 79 Callback<void()> null_callback_; |
79 }; | 80 }; |
80 | 81 |
81 // Ensure we can create unbound callbacks. We need this to be able to store | 82 // Ensure we can create unbound callbacks. We need this to be able to store |
82 // them in class members that can be initialized later. | 83 // them in class members that can be initialized later. |
83 TEST_F(CallbackTest, DefaultConstruction) { | 84 TEST_F(CallbackTest, DefaultConstruction) { |
84 Callback<void(void)> c0; | 85 Callback<void()> c0; |
85 Callback<void(int)> c1; | 86 Callback<void(int)> c1; |
86 Callback<void(int,int)> c2; | 87 Callback<void(int,int)> c2; |
87 Callback<void(int,int,int)> c3; | 88 Callback<void(int,int,int)> c3; |
88 Callback<void(int,int,int,int)> c4; | 89 Callback<void(int,int,int,int)> c4; |
89 Callback<void(int,int,int,int,int)> c5; | 90 Callback<void(int,int,int,int,int)> c5; |
90 Callback<void(int,int,int,int,int,int)> c6; | 91 Callback<void(int,int,int,int,int,int)> c6; |
91 | 92 |
92 EXPECT_TRUE(c0.is_null()); | 93 EXPECT_TRUE(c0.is_null()); |
93 EXPECT_TRUE(c1.is_null()); | 94 EXPECT_TRUE(c1.is_null()); |
94 EXPECT_TRUE(c2.is_null()); | 95 EXPECT_TRUE(c2.is_null()); |
95 EXPECT_TRUE(c3.is_null()); | 96 EXPECT_TRUE(c3.is_null()); |
96 EXPECT_TRUE(c4.is_null()); | 97 EXPECT_TRUE(c4.is_null()); |
97 EXPECT_TRUE(c5.is_null()); | 98 EXPECT_TRUE(c5.is_null()); |
98 EXPECT_TRUE(c6.is_null()); | 99 EXPECT_TRUE(c6.is_null()); |
99 } | 100 } |
100 | 101 |
101 TEST_F(CallbackTest, IsNull) { | 102 TEST_F(CallbackTest, IsNull) { |
102 EXPECT_TRUE(null_callback_.is_null()); | 103 EXPECT_TRUE(null_callback_.is_null()); |
103 EXPECT_FALSE(callback_a_.is_null()); | 104 EXPECT_FALSE(callback_a_.is_null()); |
104 EXPECT_FALSE(callback_b_.is_null()); | 105 EXPECT_FALSE(callback_b_.is_null()); |
105 } | 106 } |
106 | 107 |
107 TEST_F(CallbackTest, Equals) { | 108 TEST_F(CallbackTest, Equals) { |
108 EXPECT_TRUE(callback_a_.Equals(callback_a_)); | 109 EXPECT_TRUE(callback_a_.Equals(callback_a_)); |
109 EXPECT_FALSE(callback_a_.Equals(callback_b_)); | 110 EXPECT_FALSE(callback_a_.Equals(callback_b_)); |
110 EXPECT_FALSE(callback_b_.Equals(callback_a_)); | 111 EXPECT_FALSE(callback_b_.Equals(callback_a_)); |
111 | 112 |
112 // We should compare based on instance, not type. | 113 // We should compare based on instance, not type. |
113 Callback<void(void)> callback_c(new FakeBindState1()); | 114 Callback<void()> callback_c(new FakeBindState1()); |
114 Callback<void(void)> callback_a2 = callback_a_; | 115 Callback<void()> callback_a2 = callback_a_; |
115 EXPECT_TRUE(callback_a_.Equals(callback_a2)); | 116 EXPECT_TRUE(callback_a_.Equals(callback_a2)); |
116 EXPECT_FALSE(callback_a_.Equals(callback_c)); | 117 EXPECT_FALSE(callback_a_.Equals(callback_c)); |
117 | 118 |
118 // Empty, however, is always equal to empty. | 119 // Empty, however, is always equal to empty. |
119 Callback<void(void)> empty2; | 120 Callback<void()> empty2; |
120 EXPECT_TRUE(null_callback_.Equals(empty2)); | 121 EXPECT_TRUE(null_callback_.Equals(empty2)); |
121 } | 122 } |
122 | 123 |
123 TEST_F(CallbackTest, Reset) { | 124 TEST_F(CallbackTest, Reset) { |
124 // Resetting should bring us back to empty. | 125 // Resetting should bring us back to empty. |
125 ASSERT_FALSE(callback_a_.is_null()); | 126 ASSERT_FALSE(callback_a_.is_null()); |
126 ASSERT_FALSE(callback_a_.Equals(null_callback_)); | 127 ASSERT_FALSE(callback_a_.Equals(null_callback_)); |
127 | 128 |
128 callback_a_.Reset(); | 129 callback_a_.Reset(); |
129 | 130 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 | 180 |
180 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { | 181 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) { |
181 bool deleted = false; | 182 bool deleted = false; |
182 CallbackOwner* owner = new CallbackOwner(&deleted); | 183 CallbackOwner* owner = new CallbackOwner(&deleted); |
183 owner->Reset(); | 184 owner->Reset(); |
184 ASSERT_TRUE(deleted); | 185 ASSERT_TRUE(deleted); |
185 } | 186 } |
186 | 187 |
187 } // namespace | 188 } // namespace |
188 } // namespace base | 189 } // namespace base |
OLD | NEW |