Index: base/callback_list_unittest.cc |
diff --git a/base/callback_list_unittest.cc b/base/callback_list_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fd4b142cd004e661ff398f124a2b09a2130c9494 |
--- /dev/null |
+++ b/base/callback_list_unittest.cc |
@@ -0,0 +1,80 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/callback_list.h" |
+ |
+#include <vector> |
+ |
+#include "base/compiler_specific.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "base/threading/platform_thread.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace base { |
+namespace { |
+ |
+class Listener { |
+ public: |
+ explicit Listener(int scaler) : total(0), scaler_(scaler) {} |
+ virtual void OnEvent(int x) { |
+ total += x * scaler_; |
+ } |
+ virtual ~Listener() {} |
+ int total; |
+ |
+ private: |
+ int scaler_; |
+}; |
+ |
+TEST(CallbackListTest, BasicTest) { |
+ CallbackList<base::Callback<void(int)> > cb_list; |
+ Listener a(1), b(-1), c(1); |
+ |
+ base::Closure remove_a = cb_list.RegisterCallback( |
+ base::Bind(&Listener::OnEvent, base::Unretained(&a))); |
+ base::Closure remove_b = cb_list.RegisterCallback( |
+ base::Bind(&Listener::OnEvent, base::Unretained(&b))); |
+ |
+ EXPECT_FALSE(remove_a.is_null()); |
+ EXPECT_FALSE(remove_b.is_null()); |
+ |
+ FOR_EACH_CALLBACK(base::Callback<void(int)>, cb_list, 10); |
+ |
+ EXPECT_EQ(10, a.total); |
+ EXPECT_EQ(-10, b.total); |
+ |
+ remove_b.Run(); |
+ |
+ base::Closure remove_c = cb_list.RegisterCallback( |
+ base::Bind(&Listener::OnEvent, base::Unretained(&c))); |
+ |
+ FOR_EACH_CALLBACK(base::Callback<void(int)>, cb_list, 10); |
+ |
+ EXPECT_EQ(20, a.total); |
+ EXPECT_EQ(-10, b.total); |
+ EXPECT_EQ(10, c.total); |
+ |
+ remove_a.Run(); |
+ remove_b.Run(); |
+ remove_c.Run(); |
+ |
+ EXPECT_FALSE(cb_list.might_have_callbacks()); |
+} |
+ |
+TEST(CallbackListTest, AddACallbackTwice) { |
+ // TODO(caitkp): Write this test. |
+} |
+ |
+TEST(CallbackListTest, CallbackOutlivesList) { |
+ // TODO(caitkp): and this one. |
+} |
+ |
+TEST(CallbackListTest, RemoveDuringCallbacks) { |
+ // TODO(caitkp): and also this one. |
+} |
+ |
Avi (use Gerrit)
2013/08/22 16:25:42
Tests for base::Closures (Callback<void(void)>s) w
Cait (Slow)
2013/08/23 16:33:44
Converted the existing tests to use base::Closures
|
+} // namespace |
+} // namespace base |