diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-18 04:05:14 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-18 04:05:14 +0000 |
commit | 15fcb6590c658858007a61dd24055dcefd0b699d (patch) | |
tree | 817040b4ccd84a59dcd778696ff328016083d95b /base/callback_unittest.cc | |
parent | 3d049916edeb079db28283fb230b89def0721547 (diff) | |
download | chromium_src-15fcb6590c658858007a61dd24055dcefd0b699d.zip chromium_src-15fcb6590c658858007a61dd24055dcefd0b699d.tar.gz chromium_src-15fcb6590c658858007a61dd24055dcefd0b699d.tar.bz2 |
Emptiness, Reset, and Comparison API for Callbacks.
Since Callback<> is essentially a smartpointer, some introspective APIs are
required for sensible usage.
BUG=35223
TEST=new unittests
Review URL: http://codereview.chromium.org/6507029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75360 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/callback_unittest.cc')
-rw-r--r-- | base/callback_unittest.cc | 96 |
1 files changed, 93 insertions, 3 deletions
diff --git a/base/callback_unittest.cc b/base/callback_unittest.cc index bc15927..bf2b606 100644 --- a/base/callback_unittest.cc +++ b/base/callback_unittest.cc @@ -3,10 +3,12 @@ // found in the LICENSE file. #include "base/callback.h" +#include "base/callback_helpers.h" #include "base/scoped_ptr.h" #include "testing/gtest/include/gtest/gtest.h" +namespace base { namespace { class HelperObject { @@ -19,9 +21,24 @@ class HelperObject { int next_number_; }; -} // namespace +struct FakeTraits { + static void DoInvoke(internal::InvokerStorageBase*) { + } +}; + +// White-box testpoints to inject into a Callback<> object for checking +// comparators and emptiness APIs. +class FakeInvokerStorage1 : public internal::InvokerStorageBase { + public: + typedef FakeTraits FunctionTraits; +}; + +class FakeInvokerStorage2 : public internal::InvokerStorageBase { + public: + typedef FakeTraits FunctionTraits; +}; -TEST(Callback, OneArg) { +TEST(CallbackOld, OneArg) { HelperObject obj; scoped_ptr<Callback1<int*>::Type> callback( NewCallback(&obj, &HelperObject::GetNextNumberArg)); @@ -31,10 +48,83 @@ TEST(Callback, OneArg) { EXPECT_EQ(number, 1); } -TEST(Callback, ReturnValue) { +TEST(CallbackOld, ReturnValue) { HelperObject obj; scoped_ptr<CallbackWithReturnValue<int>::Type> callback( NewCallbackWithReturnValue(&obj, &HelperObject::GetNextNumber)); EXPECT_EQ(callback->Run(), 1); } + +class CallbackTest : public ::testing::Test { + public: + CallbackTest() + : callback_a_(MakeInvokerStorageHolder(new FakeInvokerStorage1())), + callback_b_(MakeInvokerStorageHolder(new FakeInvokerStorage2())) { + } + + virtual ~CallbackTest() { + } + + protected: + Callback<void(void)> callback_a_; + const Callback<void(void)> callback_b_; // Ensure APIs work with const. + Callback<void(void)> null_callback_; +}; + +// Ensure we can create unbound callbacks. We need this to be able to store +// them in class members that can be initialized later. +TEST_F(CallbackTest, DefaultConstruction) { + Callback<void(void)> c0; + Callback<void(int)> c1; + Callback<void(int,int)> c2; + Callback<void(int,int,int)> c3; + Callback<void(int,int,int,int)> c4; + Callback<void(int,int,int,int,int)> c5; + Callback<void(int,int,int,int,int,int)> c6; + + EXPECT_TRUE(c0.is_null()); + EXPECT_TRUE(c1.is_null()); + EXPECT_TRUE(c2.is_null()); + EXPECT_TRUE(c3.is_null()); + EXPECT_TRUE(c4.is_null()); + EXPECT_TRUE(c5.is_null()); + EXPECT_TRUE(c6.is_null()); +} + +TEST_F(CallbackTest, IsNull) { + EXPECT_TRUE(null_callback_.is_null()); + EXPECT_FALSE(callback_a_.is_null()); + EXPECT_FALSE(callback_b_.is_null()); +} + +TEST_F(CallbackTest, Equals) { + EXPECT_TRUE(callback_a_.Equals(callback_a_)); + EXPECT_FALSE(callback_a_.Equals(callback_b_)); + EXPECT_FALSE(callback_b_.Equals(callback_a_)); + + // We should compare based on instance, not type. + Callback<void(void)> callback_c( + MakeInvokerStorageHolder(new FakeInvokerStorage1())); + Callback<void(void)> callback_a2 = callback_a_; + EXPECT_TRUE(callback_a_.Equals(callback_a2)); + EXPECT_FALSE(callback_a_.Equals(callback_c)); + + // Empty, however, is always equal to empty. + Callback<void(void)> empty2; + EXPECT_TRUE(null_callback_.Equals(empty2)); +} + +TEST_F(CallbackTest, Reset) { + // Resetting should bring us back to empty. + ASSERT_FALSE(callback_a_.is_null()); + ASSERT_FALSE(callback_a_.Equals(null_callback_)); + + callback_a_.Reset(); + + EXPECT_TRUE(callback_a_.is_null()); + EXPECT_TRUE(callback_a_.Equals(null_callback_)); +} + +} // namespace +} // namespace base |