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

Side by Side Diff: base/callback_unittest.cc

Issue 8483003: Callback API Change: Reimplement Bind(); support IgnoreResult, full currying, and use less types (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 9 years, 1 month 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_internal.cc ('k') | base/file_util_proxy.cc » ('j') | 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_internal.h" 6 #include "base/callback_internal.h"
7 #include "base/callback_old.h" 7 #include "base/callback_old.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 9
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 namespace base { 12 namespace base {
13 namespace { 13 namespace {
14 14
15 class HelperObject { 15 class HelperObject {
16 public: 16 public:
17 HelperObject() : next_number_(0) { } 17 HelperObject() : next_number_(0) { }
18 int GetNextNumber() { return ++next_number_; } 18 int GetNextNumber() { return ++next_number_; }
19 void GetNextNumberArg(int* number) { *number = GetNextNumber(); } 19 void GetNextNumberArg(int* number) { *number = GetNextNumber(); }
20 20
21 private: 21 private:
22 int next_number_; 22 int next_number_;
23 }; 23 };
24 24
25 struct FakeInvoker { 25 struct FakeInvoker {
26 typedef void(*DoInvokeType)(internal::InvokerStorageBase*); 26 typedef void(RunType)(internal::BindStateBase*);
27 static void DoInvoke(internal::InvokerStorageBase*) { 27 static void Run(internal::BindStateBase*) {
28 } 28 }
29 }; 29 };
30 30
31 // White-box testpoints to inject into a Callback<> object for checking 31 // White-box testpoints to inject into a Callback<> object for checking
32 // comparators and emptiness APIs. 32 // comparators and emptiness APIs.
33 class FakeInvokerStorage1 : public internal::InvokerStorageBase { 33 class FakeBindState1 : public internal::BindStateBase {
34 public: 34 public:
35 typedef FakeInvoker Invoker; 35 typedef FakeInvoker InvokerType;
36 }; 36 };
37 37
38 class FakeInvokerStorage2 : public internal::InvokerStorageBase { 38 class FakeBindState2 : public internal::BindStateBase {
39 public: 39 public:
40 typedef FakeInvoker Invoker; 40 typedef FakeInvoker InvokerType;
41 }; 41 };
42 42
43 TEST(CallbackOld, OneArg) { 43 TEST(CallbackOld, OneArg) {
44 HelperObject obj; 44 HelperObject obj;
45 scoped_ptr<Callback1<int*>::Type> callback( 45 scoped_ptr<Callback1<int*>::Type> callback(
46 NewCallback(&obj, &HelperObject::GetNextNumberArg)); 46 NewCallback(&obj, &HelperObject::GetNextNumberArg));
47 47
48 int number = 0; 48 int number = 0;
49 callback->Run(&number); 49 callback->Run(&number);
50 EXPECT_EQ(number, 1); 50 EXPECT_EQ(number, 1);
51 } 51 }
52 52
53 TEST(CallbackOld, ReturnValue) { 53 TEST(CallbackOld, ReturnValue) {
54 HelperObject obj; 54 HelperObject obj;
55 scoped_ptr<CallbackWithReturnValue<int>::Type> callback( 55 scoped_ptr<CallbackWithReturnValue<int>::Type> callback(
56 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber)); 56 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber));
57 57
58 EXPECT_EQ(callback->Run(), 1); 58 EXPECT_EQ(callback->Run(), 1);
59 } 59 }
60 60
61 class CallbackTest : public ::testing::Test { 61 class CallbackTest : public ::testing::Test {
62 public: 62 public:
63 CallbackTest() 63 CallbackTest()
64 : callback_a_(MakeInvokerStorageHolder(new FakeInvokerStorage1())), 64 : callback_a_(MakeBindStateHolder(new FakeBindState1())),
65 callback_b_(MakeInvokerStorageHolder(new FakeInvokerStorage2())) { 65 callback_b_(MakeBindStateHolder(new FakeBindState2())) {
66 } 66 }
67 67
68 virtual ~CallbackTest() { 68 virtual ~CallbackTest() {
69 } 69 }
70 70
71 protected: 71 protected:
72 Callback<void(void)> callback_a_; 72 Callback<void(void)> callback_a_;
73 const Callback<void(void)> callback_b_; // Ensure APIs work with const. 73 const Callback<void(void)> callback_b_; // Ensure APIs work with const.
74 Callback<void(void)> null_callback_; 74 Callback<void(void)> null_callback_;
75 }; 75 };
(...skipping 24 matching lines...) Expand all
100 EXPECT_FALSE(callback_b_.is_null()); 100 EXPECT_FALSE(callback_b_.is_null());
101 } 101 }
102 102
103 TEST_F(CallbackTest, Equals) { 103 TEST_F(CallbackTest, Equals) {
104 EXPECT_TRUE(callback_a_.Equals(callback_a_)); 104 EXPECT_TRUE(callback_a_.Equals(callback_a_));
105 EXPECT_FALSE(callback_a_.Equals(callback_b_)); 105 EXPECT_FALSE(callback_a_.Equals(callback_b_));
106 EXPECT_FALSE(callback_b_.Equals(callback_a_)); 106 EXPECT_FALSE(callback_b_.Equals(callback_a_));
107 107
108 // We should compare based on instance, not type. 108 // We should compare based on instance, not type.
109 Callback<void(void)> callback_c( 109 Callback<void(void)> callback_c(
110 MakeInvokerStorageHolder(new FakeInvokerStorage1())); 110 MakeBindStateHolder(new FakeBindState1()));
111 Callback<void(void)> callback_a2 = callback_a_; 111 Callback<void(void)> callback_a2 = callback_a_;
112 EXPECT_TRUE(callback_a_.Equals(callback_a2)); 112 EXPECT_TRUE(callback_a_.Equals(callback_a2));
113 EXPECT_FALSE(callback_a_.Equals(callback_c)); 113 EXPECT_FALSE(callback_a_.Equals(callback_c));
114 114
115 // Empty, however, is always equal to empty. 115 // Empty, however, is always equal to empty.
116 Callback<void(void)> empty2; 116 Callback<void(void)> empty2;
117 EXPECT_TRUE(null_callback_.Equals(empty2)); 117 EXPECT_TRUE(null_callback_.Equals(empty2));
118 } 118 }
119 119
120 TEST_F(CallbackTest, Reset) { 120 TEST_F(CallbackTest, Reset) {
121 // Resetting should bring us back to empty. 121 // Resetting should bring us back to empty.
122 ASSERT_FALSE(callback_a_.is_null()); 122 ASSERT_FALSE(callback_a_.is_null());
123 ASSERT_FALSE(callback_a_.Equals(null_callback_)); 123 ASSERT_FALSE(callback_a_.Equals(null_callback_));
124 124
125 callback_a_.Reset(); 125 callback_a_.Reset();
126 126
127 EXPECT_TRUE(callback_a_.is_null()); 127 EXPECT_TRUE(callback_a_.is_null());
128 EXPECT_TRUE(callback_a_.Equals(null_callback_)); 128 EXPECT_TRUE(callback_a_.Equals(null_callback_));
129 } 129 }
130 130
131 } // namespace 131 } // namespace
132 } // namespace base 132 } // namespace base
OLDNEW
« no previous file with comments | « base/callback_internal.cc ('k') | base/file_util_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698