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), 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.
| |
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 | |
Avi (use Gerrit)
2013/08/21 19:56:18
drop blank line
Cait (Slow)
2013/08/22 15:30:25
Done.
| |
66 } | |
67 | |
68 | |
69 } // namespace | |
70 } // namespace base | |
OLD | NEW |