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

Side by Side Diff: base/callback_unittest.cc

Issue 6109007: Unified callback system. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/base
Patch Set: Ready for another structural review. (not optimized) 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
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/prebind.h"
6 #include "base/scoped_ptr.h" 7 #include "base/scoped_ptr.h"
8 #include "base/logging.h"
scherkus (not reviewing) 2011/02/01 01:00:10 include order
awong 2011/02/01 01:21:38 Will fix in next pass...I'm about to regenerate th
7 9
8 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
9 11
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(); }
(...skipping 14 matching lines...) Expand all
31 EXPECT_EQ(number, 1); 33 EXPECT_EQ(number, 1);
32 } 34 }
33 35
34 TEST(Callback, ReturnValue) { 36 TEST(Callback, ReturnValue) {
35 HelperObject obj; 37 HelperObject obj;
36 scoped_ptr<CallbackWithReturnValue<int>::Type> callback( 38 scoped_ptr<CallbackWithReturnValue<int>::Type> callback(
37 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber)); 39 NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber));
38 40
39 EXPECT_EQ(callback->Run(), 1); 41 EXPECT_EQ(callback->Run(), 1);
40 } 42 }
43
44 void one_arg(int n) {
45 LOG(ERROR) << __func__ << " " << n;
46 }
47
48 void two_arg(int n, int m) {
49 LOG(ERROR) << __func__ << " " << n << " " << m;
50 }
51
52 class Foo {
53 public:
54 void no_arg(void) {
55 LOG(ERROR) << __func__;
56 }
57
58 void no_arg_const() const {
59 LOG(ERROR) << __func__;
60 }
61
62 void one_arg(int n) {
63 LOG(ERROR) << __func__ << n;
64 }
65
66 void one_arg_const(int n) const {
67 LOG(ERROR) << __func__ << n;
68 }
69 };
70
71 class RefFoo {
72 int n;
scherkus (not reviewing) 2011/02/01 01:00:10 move this to private section?
awong 2011/02/01 01:21:38 Yep. This will get recreated by pump in a bit.
73 public:
74 RefFoo() : n(0) {}
75 ~RefFoo() { if (n != 0) ADD_FAILURE() << "imbalanced ref: " << n; }
76 void AddRef() {
77 n++;
78 }
79 void Release() {
80 n--;
81 }
82
83 void no_arg(void) {
84 LOG(ERROR) << __func__;
85 }
86
87 void no_arg_const() const {
88 LOG(ERROR) << __func__;
89 }
90
91 void one_arg(int n) {
92 LOG(ERROR) << __func__ << n;
93 }
94
95 void one_arg_const(int n) const {
96 LOG(ERROR) << __func__ << n;
97 }
98 };
99
100 class Bar : public Foo {};
101
102 TEST(Callback, Prebind_AllBound) {
103 RefFoo f; // Must be declared before func for proper destruction order.
104 base::Callback<void(void)> func = base::Prebind(&one_arg, 1);
105 func.Run();
106
107 func = base::Prebind(&RefFoo::no_arg, &f);
108 func.Run();
109
110 func = base::Prebind(&RefFoo::no_arg_const, &f);
111 func.Run();
112 }
113
114 TEST(Callback, Prebind_OneUnbound) {
115 RefFoo f; // Must be declared before func for proper destruction order.
116 base::Callback<void(int)> func = base::Prebind(&two_arg, 1);
117 func.Run(2);
118
119 func = base::Prebind(&RefFoo::one_arg, &f);
120 func.Run(4);
121
122 func = base::Prebind(&RefFoo::one_arg_const, &f);
123 func.Run(6);
124 }
125
126 TEST(Callback, Prebind_AllBoundNoref) {
127 Foo f;
128 base::Callback<void(void)> func =
129 base::Prebind(&Foo::no_arg, base::Unretained(&f));
130 func.Run();
131
132 func = base::Prebind(&Foo::no_arg_const, base::Unretained(&f));
133 func.Run();
134 }
135
136 TEST(Callback, Prebind_OneUnboundNoRef) {
137 Foo f;
138 base::Callback<void(int)> func =
139 base::Prebind(&Foo::one_arg, base::Unretained(&f));
140 func.Run(4);
141
142 func = base::Prebind(&Foo::one_arg_const, base::Unretained(&f));
143 func.Run(6);
144 }
145
146 #if 0
scherkus (not reviewing) 2011/02/01 01:00:10 :)
awong 2011/02/01 01:21:38 *sigh* Yeah, I wish I had a better method for thi
147 TEST(Callback, Prebind_one_arg_nocompile) {
148 Bar b;
149 func = base::Prebind(&Foo::no_arg_const, &b);
150 func.Run();
151 }
152
153 TEST(Callback, Prebind_ref_with_unretained_nocompile) {
154 RefFoo f;
155 func = base::Prebind(&Foo::one_arg, base::Unretained(&f));
156 func.Run(4);
157
158 func = base::Prebind(&Foo::one_arg_const, base::Unretained(&f));
159 func.Run(6);
160 }
161 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698