OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/bind_to_loop.h" | 5 #include "media/base/bind_to_current_loop.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace media { | 11 namespace media { |
12 | 12 |
13 void BoundBoolSet(bool* var, bool val) { | 13 void BoundBoolSet(bool* var, bool val) { |
14 *var = val; | 14 *var = val; |
15 } | 15 } |
(...skipping 14 matching lines...) Expand all Loading... | |
30 *var = val; | 30 *var = val; |
31 } | 31 } |
32 | 32 |
33 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { | 33 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { |
34 *a_var = a_val; | 34 *a_var = a_val; |
35 *b_var = b_val; | 35 *b_var = b_val; |
36 } | 36 } |
37 | 37 |
38 // Various tests that check that the bound function is only actually executed | 38 // Various tests that check that the bound function is only actually executed |
39 // on the message loop, not during the original Run. | 39 // on the message loop, not during the original Run. |
40 class BindToLoopTest : public ::testing::Test { | 40 class BindToCurrentLoopTest : public ::testing::Test { |
41 protected: | 41 protected: |
42 base::MessageLoop loop_; | 42 base::MessageLoop loop_; |
43 }; | 43 }; |
44 | 44 |
45 TEST_F(BindToLoopTest, Closure) { | 45 TEST_F(BindToCurrentLoopTest, Closure) { |
46 // Test the closure is run inside the loop, not outside it. | 46 // Test the closure is run inside the loop, not outside it. |
47 base::WaitableEvent waiter(false, false); | 47 base::WaitableEvent waiter(false, false); |
48 base::Closure cb = BindToCurrentLoop(base::Bind( | 48 base::Closure cb = BindToCurrentLoop(base::Bind( |
49 &base::WaitableEvent::Signal, base::Unretained(&waiter))); | 49 &base::WaitableEvent::Signal, base::Unretained(&waiter))); |
xhwang
2014/01/10 00:30:29
nit: How do we make sure it's "signal"ed on the "c
scherkus (not reviewing)
2014/01/10 01:09:19
I don't believe we're testing for the current-ness
| |
50 cb.Run(); | 50 cb.Run(); |
51 EXPECT_FALSE(waiter.IsSignaled()); | 51 EXPECT_FALSE(waiter.IsSignaled()); |
52 loop_.RunUntilIdle(); | 52 loop_.RunUntilIdle(); |
53 EXPECT_TRUE(waiter.IsSignaled()); | 53 EXPECT_TRUE(waiter.IsSignaled()); |
54 } | 54 } |
55 | 55 |
56 TEST_F(BindToLoopTest, Bool) { | 56 TEST_F(BindToCurrentLoopTest, Bool) { |
57 bool bool_var = false; | 57 bool bool_var = false; |
58 base::Callback<void(bool)> cb = BindToCurrentLoop(base::Bind( | 58 base::Callback<void(bool)> cb = BindToCurrentLoop(base::Bind( |
59 &BoundBoolSet, &bool_var)); | 59 &BoundBoolSet, &bool_var)); |
60 cb.Run(true); | 60 cb.Run(true); |
61 EXPECT_FALSE(bool_var); | 61 EXPECT_FALSE(bool_var); |
62 loop_.RunUntilIdle(); | 62 loop_.RunUntilIdle(); |
63 EXPECT_TRUE(bool_var); | 63 EXPECT_TRUE(bool_var); |
64 } | 64 } |
65 | 65 |
66 TEST_F(BindToLoopTest, BoundScopedPtrBool) { | 66 TEST_F(BindToCurrentLoopTest, BoundScopedPtrBool) { |
67 bool bool_val = false; | 67 bool bool_val = false; |
68 scoped_ptr<bool> scoped_ptr_bool(new bool(true)); | 68 scoped_ptr<bool> scoped_ptr_bool(new bool(true)); |
69 base::Closure cb = BindToCurrentLoop(base::Bind( | 69 base::Closure cb = BindToCurrentLoop(base::Bind( |
70 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); | 70 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); |
71 cb.Run(); | 71 cb.Run(); |
72 EXPECT_FALSE(bool_val); | 72 EXPECT_FALSE(bool_val); |
73 loop_.RunUntilIdle(); | 73 loop_.RunUntilIdle(); |
74 EXPECT_TRUE(bool_val); | 74 EXPECT_TRUE(bool_val); |
75 } | 75 } |
76 | 76 |
77 TEST_F(BindToLoopTest, PassedScopedPtrBool) { | 77 TEST_F(BindToCurrentLoopTest, PassedScopedPtrBool) { |
78 bool bool_val = false; | 78 bool bool_val = false; |
79 scoped_ptr<bool> scoped_ptr_bool(new bool(true)); | 79 scoped_ptr<bool> scoped_ptr_bool(new bool(true)); |
80 base::Callback<void(scoped_ptr<bool>)> cb = BindToCurrentLoop(base::Bind( | 80 base::Callback<void(scoped_ptr<bool>)> cb = BindToCurrentLoop(base::Bind( |
81 &BoundBoolSetFromScopedPtr, &bool_val)); | 81 &BoundBoolSetFromScopedPtr, &bool_val)); |
82 cb.Run(scoped_ptr_bool.Pass()); | 82 cb.Run(scoped_ptr_bool.Pass()); |
83 EXPECT_FALSE(bool_val); | 83 EXPECT_FALSE(bool_val); |
84 loop_.RunUntilIdle(); | 84 loop_.RunUntilIdle(); |
85 EXPECT_TRUE(bool_val); | 85 EXPECT_TRUE(bool_val); |
86 } | 86 } |
87 | 87 |
88 TEST_F(BindToLoopTest, BoundScopedArrayBool) { | 88 TEST_F(BindToCurrentLoopTest, BoundScopedArrayBool) { |
89 bool bool_val = false; | 89 bool bool_val = false; |
90 scoped_ptr<bool[]> scoped_array_bool(new bool[1]); | 90 scoped_ptr<bool[]> scoped_array_bool(new bool[1]); |
91 scoped_array_bool[0] = true; | 91 scoped_array_bool[0] = true; |
92 base::Closure cb = BindToCurrentLoop(base::Bind( | 92 base::Closure cb = BindToCurrentLoop(base::Bind( |
93 &BoundBoolSetFromScopedArray, &bool_val, | 93 &BoundBoolSetFromScopedArray, &bool_val, |
94 base::Passed(&scoped_array_bool))); | 94 base::Passed(&scoped_array_bool))); |
95 cb.Run(); | 95 cb.Run(); |
96 EXPECT_FALSE(bool_val); | 96 EXPECT_FALSE(bool_val); |
97 loop_.RunUntilIdle(); | 97 loop_.RunUntilIdle(); |
98 EXPECT_TRUE(bool_val); | 98 EXPECT_TRUE(bool_val); |
99 } | 99 } |
100 | 100 |
101 TEST_F(BindToLoopTest, PassedScopedArrayBool) { | 101 TEST_F(BindToCurrentLoopTest, PassedScopedArrayBool) { |
102 bool bool_val = false; | 102 bool bool_val = false; |
103 scoped_ptr<bool[]> scoped_array_bool(new bool[1]); | 103 scoped_ptr<bool[]> scoped_array_bool(new bool[1]); |
104 scoped_array_bool[0] = true; | 104 scoped_array_bool[0] = true; |
105 base::Callback<void(scoped_ptr<bool[]>)> cb = BindToCurrentLoop(base::Bind( | 105 base::Callback<void(scoped_ptr<bool[]>)> cb = BindToCurrentLoop(base::Bind( |
106 &BoundBoolSetFromScopedArray, &bool_val)); | 106 &BoundBoolSetFromScopedArray, &bool_val)); |
107 cb.Run(scoped_array_bool.Pass()); | 107 cb.Run(scoped_array_bool.Pass()); |
108 EXPECT_FALSE(bool_val); | 108 EXPECT_FALSE(bool_val); |
109 loop_.RunUntilIdle(); | 109 loop_.RunUntilIdle(); |
110 EXPECT_TRUE(bool_val); | 110 EXPECT_TRUE(bool_val); |
111 } | 111 } |
112 | 112 |
113 TEST_F(BindToLoopTest, BoundScopedPtrMallocBool) { | 113 TEST_F(BindToCurrentLoopTest, BoundScopedPtrMallocBool) { |
114 bool bool_val = false; | 114 bool bool_val = false; |
115 scoped_ptr_malloc<bool> scoped_ptr_malloc_bool( | 115 scoped_ptr_malloc<bool> scoped_ptr_malloc_bool( |
116 static_cast<bool*>(malloc(sizeof(bool)))); | 116 static_cast<bool*>(malloc(sizeof(bool)))); |
117 *scoped_ptr_malloc_bool = true; | 117 *scoped_ptr_malloc_bool = true; |
118 base::Closure cb = BindToCurrentLoop(base::Bind( | 118 base::Closure cb = BindToCurrentLoop(base::Bind( |
119 &BoundBoolSetFromScopedPtrMalloc, &bool_val, | 119 &BoundBoolSetFromScopedPtrMalloc, &bool_val, |
120 base::Passed(&scoped_ptr_malloc_bool))); | 120 base::Passed(&scoped_ptr_malloc_bool))); |
121 cb.Run(); | 121 cb.Run(); |
122 EXPECT_FALSE(bool_val); | 122 EXPECT_FALSE(bool_val); |
123 loop_.RunUntilIdle(); | 123 loop_.RunUntilIdle(); |
124 EXPECT_TRUE(bool_val); | 124 EXPECT_TRUE(bool_val); |
125 } | 125 } |
126 | 126 |
127 TEST_F(BindToLoopTest, PassedScopedPtrMallocBool) { | 127 TEST_F(BindToCurrentLoopTest, PassedScopedPtrMallocBool) { |
128 bool bool_val = false; | 128 bool bool_val = false; |
129 scoped_ptr_malloc<bool> scoped_ptr_malloc_bool( | 129 scoped_ptr_malloc<bool> scoped_ptr_malloc_bool( |
130 static_cast<bool*>(malloc(sizeof(bool)))); | 130 static_cast<bool*>(malloc(sizeof(bool)))); |
131 *scoped_ptr_malloc_bool = true; | 131 *scoped_ptr_malloc_bool = true; |
132 base::Callback<void(scoped_ptr_malloc<bool>)> cb = BindToCurrentLoop( | 132 base::Callback<void(scoped_ptr_malloc<bool>)> cb = BindToCurrentLoop( |
133 base::Bind(&BoundBoolSetFromScopedPtrMalloc, &bool_val)); | 133 base::Bind(&BoundBoolSetFromScopedPtrMalloc, &bool_val)); |
134 cb.Run(scoped_ptr_malloc_bool.Pass()); | 134 cb.Run(scoped_ptr_malloc_bool.Pass()); |
135 EXPECT_FALSE(bool_val); | 135 EXPECT_FALSE(bool_val); |
136 loop_.RunUntilIdle(); | 136 loop_.RunUntilIdle(); |
137 EXPECT_TRUE(bool_val); | 137 EXPECT_TRUE(bool_val); |
138 } | 138 } |
139 | 139 |
140 TEST_F(BindToLoopTest, BoolConstRef) { | 140 TEST_F(BindToCurrentLoopTest, BoolConstRef) { |
141 bool bool_var = false; | 141 bool bool_var = false; |
142 bool true_var = true; | 142 bool true_var = true; |
143 const bool& true_ref = true_var; | 143 const bool& true_ref = true_var; |
144 base::Closure cb = BindToCurrentLoop(base::Bind( | 144 base::Closure cb = BindToCurrentLoop(base::Bind( |
145 &BoundBoolSetFromConstRef, &bool_var, true_ref)); | 145 &BoundBoolSetFromConstRef, &bool_var, true_ref)); |
146 cb.Run(); | 146 cb.Run(); |
147 EXPECT_FALSE(bool_var); | 147 EXPECT_FALSE(bool_var); |
148 loop_.RunUntilIdle(); | 148 loop_.RunUntilIdle(); |
149 EXPECT_TRUE(bool_var); | 149 EXPECT_TRUE(bool_var); |
150 } | 150 } |
151 | 151 |
152 TEST_F(BindToLoopTest, Integers) { | 152 TEST_F(BindToCurrentLoopTest, Integers) { |
153 int a = 0; | 153 int a = 0; |
154 int b = 0; | 154 int b = 0; |
155 base::Callback<void(int, int)> cb = BindToCurrentLoop(base::Bind( | 155 base::Callback<void(int, int)> cb = BindToCurrentLoop(base::Bind( |
156 &BoundIntegersSet, &a, &b)); | 156 &BoundIntegersSet, &a, &b)); |
157 cb.Run(1, -1); | 157 cb.Run(1, -1); |
158 EXPECT_EQ(a, 0); | 158 EXPECT_EQ(a, 0); |
159 EXPECT_EQ(b, 0); | 159 EXPECT_EQ(b, 0); |
160 loop_.RunUntilIdle(); | 160 loop_.RunUntilIdle(); |
161 EXPECT_EQ(a, 1); | 161 EXPECT_EQ(a, 1); |
162 EXPECT_EQ(b, -1); | 162 EXPECT_EQ(b, -1); |
163 } | 163 } |
164 | 164 |
165 } // namespace media | 165 } // namespace media |
OLD | NEW |