OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/callback_list.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/run_loop.h" | |
13 #include "base/threading/platform_thread.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace base { | |
17 namespace { | |
18 | |
19 class Listener { | |
20 public: | |
21 explicit Listener(int scaler) : total(0), scaler_(scaler) {} | |
22 virtual void OnEvent(int x) { | |
23 total += x * scaler_; | |
24 } | |
25 virtual ~Listener() {} | |
26 int total; | |
27 | |
28 private: | |
29 int scaler_; | |
30 }; | |
31 | |
32 TEST(CallbackListTest, BasicTest) { | |
33 CallbackList<base::Callback<void(int)> > cb_list; | |
34 Listener a(1), b(-1), c(1); | |
35 | |
36 base::Closure remove_a = cb_list.RegisterCallback( | |
37 base::Bind(&Listener::OnEvent, base::Unretained(&a))); | |
38 base::Closure remove_b = cb_list.RegisterCallback( | |
39 base::Bind(&Listener::OnEvent, base::Unretained(&b))); | |
40 | |
41 EXPECT_FALSE(remove_a.is_null()); | |
42 EXPECT_FALSE(remove_b.is_null()); | |
43 | |
44 FOR_EACH_CALLBACK(base::Callback<void(int)>, cb_list, 10); | |
45 | |
46 EXPECT_EQ(10, a.total); | |
47 EXPECT_EQ(-10, b.total); | |
48 | |
49 remove_b.Run(); | |
50 | |
51 base::Closure remove_c = cb_list.RegisterCallback( | |
52 base::Bind(&Listener::OnEvent, base::Unretained(&c))); | |
53 | |
54 FOR_EACH_CALLBACK(base::Callback<void(int)>, cb_list, 10); | |
55 | |
56 EXPECT_EQ(20, a.total); | |
57 EXPECT_EQ(-10, b.total); | |
58 EXPECT_EQ(10, c.total); | |
59 | |
60 remove_a.Run(); | |
61 remove_b.Run(); | |
62 remove_c.Run(); | |
63 | |
64 EXPECT_FALSE(cb_list.might_have_callbacks()); | |
65 } | |
66 | |
67 TEST(CallbackListTest, AddACallbackTwice) { | |
68 // TODO(caitkp): Write this test. | |
69 } | |
70 | |
71 TEST(CallbackListTest, CallbackOutlivesList) { | |
72 // TODO(caitkp): and this one. | |
73 } | |
74 | |
75 TEST(CallbackListTest, RemoveDuringCallbacks) { | |
76 // TODO(caitkp): and also this one. | |
77 } | |
78 | |
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
| |
79 } // namespace | |
80 } // namespace base | |
OLD | NEW |