Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | |
| 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 Loading... | |
| 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; | |
| 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); | |
|
akalin
2011/02/01 11:29:27
you may already know this, but you could overload
awong
2011/02/01 23:20:45
Oh....I actually forgot about that. Good point. I
| |
| 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 | |
| 147 TEST(Callback, Prebind_one_arg_nocompile) { | |
|
akalin
2011/02/01 11:29:27
Just to be clear, are these two functions supposed
awong
2011/02/01 23:20:45
No, these are funcitons that I want explicitly to
| |
| 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 | |
| OLD | NEW |