Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: base/callback_unittest.cc

Issue 6507029: Callback API Change: is_null, Reset, and Equals (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Can we color it "is_null()"? Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/callback.h.pump ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/callback.h" 5 #include "base/callback.h"
6 #include "base/callback_helpers.h"
6 #include "base/scoped_ptr.h" 7 #include "base/scoped_ptr.h"
7 8
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 10
11 namespace base {
10 namespace { 12 namespace {
11 13
12 class HelperObject { 14 class HelperObject {
13 public: 15 public:
14 HelperObject() : next_number_(0) { } 16 HelperObject() : next_number_(0) { }
15 int GetNextNumber() { return ++next_number_; } 17 int GetNextNumber() { return ++next_number_; }
16 void GetNextNumberArg(int* number) { *number = GetNextNumber(); } 18 void GetNextNumberArg(int* number) { *number = GetNextNumber(); }
17 19
18 private: 20 private:
19 int next_number_; 21 int next_number_;
20 }; 22 };
21 23
22 } // namespace 24 struct FakeTraits {
25 static void DoInvoke(internal::InvokerStorageBase*) {
26 }
27 };
23 28
24 TEST(Callback, OneArg) { 29 // White-box testpoints to inject into a Callback<> object for checking
30 // comparators and emptiness APIs.
31 class FakeInvokerStorage1 : public internal::InvokerStorageBase {
32 public:
33 typedef FakeTraits FunctionTraits;
34 };
35
36 class FakeInvokerStorage2 : public internal::InvokerStorageBase {
37 public:
38 typedef FakeTraits FunctionTraits;
39 };
40
41 TEST(CallbackOld, OneArg) {
25 HelperObject obj; 42 HelperObject obj;
26 scoped_ptr<Callback1<int*>::Type> callback( 43 scoped_ptr<Callback1<int*>::Type> callback(
27 NewCallback(&obj, &HelperObject::GetNextNumberArg)); 44 NewCallback(&obj, &HelperObject::GetNextNumberArg));
28 45
29 int number = 0; 46 int number = 0;
30 callback->Run(&number); 47 callback->Run(&number);
31 EXPECT_EQ(number, 1); 48 EXPECT_EQ(number, 1);
32 } 49 }
33 50
34 TEST(Callback, ReturnValue) { 51 TEST(CallbackOld, ReturnValue) {
35 HelperObject obj; 52 HelperObject obj;
36 scoped_ptr<CallbackWithReturnValue<int>::Type> callback( 53 scoped_ptr<CallbackWithReturnValue<int>::Type> callback(
37 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber)); 54 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber));
38 55
39 EXPECT_EQ(callback->Run(), 1); 56 EXPECT_EQ(callback->Run(), 1);
40 } 57 }
58
59 class CallbackTest : public ::testing::Test {
60 public:
61 CallbackTest()
62 : callback_a_(MakeInvokerStorageHolder(new FakeInvokerStorage1())),
63 callback_b_(MakeInvokerStorageHolder(new FakeInvokerStorage2())) {
64 }
65
66 virtual ~CallbackTest() {
67 }
68
69 protected:
70 Callback<void(void)> callback_a_;
71 const Callback<void(void)> callback_b_; // Ensure APIs work with const.
72 Callback<void(void)> null_callback_;
73 };
74
75 // Ensure we can create unbound callbacks. We need this to be able to store
76 // them in class members that can be initialized later.
77 TEST_F(CallbackTest, DefaultConstruction) {
78 Callback<void(void)> c0;
79 Callback<void(int)> c1;
80 Callback<void(int,int)> c2;
81 Callback<void(int,int,int)> c3;
82 Callback<void(int,int,int,int)> c4;
83 Callback<void(int,int,int,int,int)> c5;
84 Callback<void(int,int,int,int,int,int)> c6;
85
86 EXPECT_TRUE(c0.is_null());
87 EXPECT_TRUE(c1.is_null());
88 EXPECT_TRUE(c2.is_null());
89 EXPECT_TRUE(c3.is_null());
90 EXPECT_TRUE(c4.is_null());
91 EXPECT_TRUE(c5.is_null());
92 EXPECT_TRUE(c6.is_null());
93 }
94
95 TEST_F(CallbackTest, IsNull) {
96 EXPECT_TRUE(null_callback_.is_null());
97 EXPECT_FALSE(callback_a_.is_null());
98 EXPECT_FALSE(callback_b_.is_null());
99 }
100
101 TEST_F(CallbackTest, Equals) {
102 EXPECT_TRUE(callback_a_.Equals(callback_a_));
103 EXPECT_FALSE(callback_a_.Equals(callback_b_));
104 EXPECT_FALSE(callback_b_.Equals(callback_a_));
105
106 // We should compare based on instance, not type.
107 Callback<void(void)> callback_c(
108 MakeInvokerStorageHolder(new FakeInvokerStorage1()));
109 Callback<void(void)> callback_a2 = callback_a_;
110 EXPECT_TRUE(callback_a_.Equals(callback_a2));
111 EXPECT_FALSE(callback_a_.Equals(callback_c));
112
113 // Empty, however, is always equal to empty.
114 Callback<void(void)> empty2;
115 EXPECT_TRUE(null_callback_.Equals(empty2));
116 }
117
118 TEST_F(CallbackTest, Reset) {
119 // Resetting should bring us back to empty.
120 ASSERT_FALSE(callback_a_.is_null());
akalin 2011/02/17 20:29:44 no need for these to be asserts, I think
awong 2011/02/18 00:42:41 The rest of the test is invalid if these assumptio
121 ASSERT_FALSE(callback_a_.Equals(null_callback_));
122
123 callback_a_.Reset();
124
125 EXPECT_TRUE(callback_a_.is_null());
126 EXPECT_TRUE(callback_a_.Equals(null_callback_));
127 }
128
129 } // namespace
130 } // namespace base
OLDNEW
« no previous file with comments | « base/callback.h.pump ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698