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

Unified Diff: base/callback_list_unittest.cc

Issue 22877038: Add a CallbackRegistry class to base/ to manage callbacks (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
« base/callback_list.h ('K') | « base/callback_list.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« base/callback_list.h ('K') | « base/callback_list.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698