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..a9c9caf0711d0b0429a4c51c45e10e37e31d2904 |
--- /dev/null |
+++ b/base/callback_list_unittest.cc |
@@ -0,0 +1,70 @@ |
+// 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), d(-1), e(-1); |
Avi (use Gerrit)
2013/08/21 19:56:18
d and e aren't used
Cait (Slow)
2013/08/22 15:30:25
Done.
|
+ |
+ 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()); |
+ |
Avi (use Gerrit)
2013/08/21 19:56:18
drop blank line
Cait (Slow)
2013/08/22 15:30:25
Done.
|
+} |
+ |
+ |
+} // namespace |
+} // namespace base |