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

Side by Side Diff: base/bind_to_task_runner_unittest.cc

Issue 1553593002: Add base::BindToTaskRunner and BindToCurrentThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Created 4 years, 11 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 unified diff | Download patch
« no previous file with comments | « base/bind_to_task_runner.h ('k') | media/base/bind_to_current_loop.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_current_loop.h" 5 #include "base/bind_to_task_runner.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/synchronization/waitable_event.h" 10 #include "base/synchronization/waitable_event.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace media { 13 namespace base {
14 14
15 void BoundBoolSet(bool* var, bool val) { 15 void BoundBoolSet(bool* var, bool val) {
16 *var = val; 16 *var = val;
17 } 17 }
18 18
19 void BoundBoolSetFromScopedPtr(bool* var, scoped_ptr<bool> val) { 19 void BoundBoolSetFromScopedPtr(bool* var, scoped_ptr<bool> val) {
20 *var = *val; 20 *var = *val;
21 } 21 }
22 22
23 void BoundBoolSetFromScopedPtrFreeDeleter( 23 void BoundBoolSetFromScopedPtrFreeDeleter(bool* var,
24 bool* var, 24 scoped_ptr<bool, FreeDeleter> val) {
25 scoped_ptr<bool, base::FreeDeleter> val) {
26 *var = val; 25 *var = val;
27 } 26 }
28 27
29 void BoundBoolSetFromScopedArray(bool* var, scoped_ptr<bool[]> val) { 28 void BoundBoolSetFromScopedArray(bool* var, scoped_ptr<bool[]> val) {
30 *var = val[0]; 29 *var = val[0];
31 } 30 }
32 31
33 void BoundBoolSetFromConstRef(bool* var, const bool& val) { 32 void BoundBoolSetFromConstRef(bool* var, const bool& val) {
34 *var = val; 33 *var = val;
35 } 34 }
36 35
37 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { 36 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) {
38 *a_var = a_val; 37 *a_var = a_val;
39 *b_var = b_val; 38 *b_var = b_val;
40 } 39 }
41 40
42 // Various tests that check that the bound function is only actually executed 41 // Various tests that check that the bound function is only actually executed
43 // on the message loop, not during the original Run. 42 // on the message loop, not during the original Run.
44 class BindToCurrentLoopTest : public ::testing::Test { 43 class BindToTaskRunnerTest : public ::testing::Test {
45 protected: 44 protected:
46 base::MessageLoop loop_; 45 MessageLoop loop_;
47 }; 46 };
48 47
49 TEST_F(BindToCurrentLoopTest, Closure) { 48 TEST_F(BindToTaskRunnerTest, Closure) {
50 // Test the closure is run inside the loop, not outside it. 49 // Test the closure is run inside the loop, not outside it.
51 base::WaitableEvent waiter(false, false); 50 WaitableEvent waiter(false, false);
52 base::Closure cb = BindToCurrentLoop(base::Bind( 51 Closure cb =
53 &base::WaitableEvent::Signal, base::Unretained(&waiter))); 52 BindToCurrentThread(Bind(&WaitableEvent::Signal, Unretained(&waiter)));
54 cb.Run(); 53 cb.Run();
55 EXPECT_FALSE(waiter.IsSignaled()); 54 EXPECT_FALSE(waiter.IsSignaled());
56 loop_.RunUntilIdle(); 55 loop_.RunUntilIdle();
57 EXPECT_TRUE(waiter.IsSignaled()); 56 EXPECT_TRUE(waiter.IsSignaled());
58 } 57 }
59 58
60 TEST_F(BindToCurrentLoopTest, Bool) { 59 TEST_F(BindToTaskRunnerTest, Bool) {
61 bool bool_var = false; 60 bool bool_var = false;
62 base::Callback<void(bool)> cb = BindToCurrentLoop(base::Bind( 61 Callback<void(bool)> cb = BindToCurrentThread(Bind(&BoundBoolSet, &bool_var));
63 &BoundBoolSet, &bool_var));
64 cb.Run(true); 62 cb.Run(true);
65 EXPECT_FALSE(bool_var); 63 EXPECT_FALSE(bool_var);
66 loop_.RunUntilIdle(); 64 loop_.RunUntilIdle();
67 EXPECT_TRUE(bool_var); 65 EXPECT_TRUE(bool_var);
68 } 66 }
69 67
70 TEST_F(BindToCurrentLoopTest, BoundScopedPtrBool) { 68 TEST_F(BindToTaskRunnerTest, BoundScopedPtrBool) {
71 bool bool_val = false; 69 bool bool_val = false;
72 scoped_ptr<bool> scoped_ptr_bool(new bool(true)); 70 scoped_ptr<bool> scoped_ptr_bool(new bool(true));
73 base::Closure cb = BindToCurrentLoop(base::Bind( 71 Closure cb = BindToCurrentThread(
74 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); 72 Bind(&BoundBoolSetFromScopedPtr, &bool_val, Passed(&scoped_ptr_bool)));
75 cb.Run(); 73 cb.Run();
76 EXPECT_FALSE(bool_val); 74 EXPECT_FALSE(bool_val);
77 loop_.RunUntilIdle(); 75 loop_.RunUntilIdle();
78 EXPECT_TRUE(bool_val); 76 EXPECT_TRUE(bool_val);
79 } 77 }
80 78
81 TEST_F(BindToCurrentLoopTest, PassedScopedPtrBool) { 79 TEST_F(BindToTaskRunnerTest, PassedScopedPtrBool) {
82 bool bool_val = false; 80 bool bool_val = false;
83 scoped_ptr<bool> scoped_ptr_bool(new bool(true)); 81 scoped_ptr<bool> scoped_ptr_bool(new bool(true));
84 base::Callback<void(scoped_ptr<bool>)> cb = BindToCurrentLoop(base::Bind( 82 Callback<void(scoped_ptr<bool>)> cb =
85 &BoundBoolSetFromScopedPtr, &bool_val)); 83 BindToCurrentThread(Bind(&BoundBoolSetFromScopedPtr, &bool_val));
86 cb.Run(std::move(scoped_ptr_bool)); 84 cb.Run(std::move(scoped_ptr_bool));
87 EXPECT_FALSE(bool_val); 85 EXPECT_FALSE(bool_val);
88 loop_.RunUntilIdle(); 86 loop_.RunUntilIdle();
89 EXPECT_TRUE(bool_val); 87 EXPECT_TRUE(bool_val);
90 } 88 }
91 89
92 TEST_F(BindToCurrentLoopTest, BoundScopedArrayBool) { 90 TEST_F(BindToTaskRunnerTest, BoundScopedArrayBool) {
93 bool bool_val = false; 91 bool bool_val = false;
94 scoped_ptr<bool[]> scoped_array_bool(new bool[1]); 92 scoped_ptr<bool[]> scoped_array_bool(new bool[1]);
95 scoped_array_bool[0] = true; 93 scoped_array_bool[0] = true;
96 base::Closure cb = BindToCurrentLoop(base::Bind( 94 Closure cb = BindToCurrentThread(Bind(&BoundBoolSetFromScopedArray, &bool_val,
97 &BoundBoolSetFromScopedArray, &bool_val, 95 Passed(&scoped_array_bool)));
98 base::Passed(&scoped_array_bool)));
99 cb.Run(); 96 cb.Run();
100 EXPECT_FALSE(bool_val); 97 EXPECT_FALSE(bool_val);
101 loop_.RunUntilIdle(); 98 loop_.RunUntilIdle();
102 EXPECT_TRUE(bool_val); 99 EXPECT_TRUE(bool_val);
103 } 100 }
104 101
105 TEST_F(BindToCurrentLoopTest, PassedScopedArrayBool) { 102 TEST_F(BindToTaskRunnerTest, PassedScopedArrayBool) {
106 bool bool_val = false; 103 bool bool_val = false;
107 scoped_ptr<bool[]> scoped_array_bool(new bool[1]); 104 scoped_ptr<bool[]> scoped_array_bool(new bool[1]);
108 scoped_array_bool[0] = true; 105 scoped_array_bool[0] = true;
109 base::Callback<void(scoped_ptr<bool[]>)> cb = BindToCurrentLoop(base::Bind( 106 Callback<void(scoped_ptr<bool[]>)> cb =
110 &BoundBoolSetFromScopedArray, &bool_val)); 107 BindToCurrentThread(Bind(&BoundBoolSetFromScopedArray, &bool_val));
111 cb.Run(std::move(scoped_array_bool)); 108 cb.Run(std::move(scoped_array_bool));
112 EXPECT_FALSE(bool_val); 109 EXPECT_FALSE(bool_val);
113 loop_.RunUntilIdle(); 110 loop_.RunUntilIdle();
114 EXPECT_TRUE(bool_val); 111 EXPECT_TRUE(bool_val);
115 } 112 }
116 113
117 TEST_F(BindToCurrentLoopTest, BoundScopedPtrFreeDeleterBool) { 114 TEST_F(BindToTaskRunnerTest, BoundScopedPtrFreeDeleterBool) {
118 bool bool_val = false; 115 bool bool_val = false;
119 scoped_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( 116 scoped_ptr<bool, FreeDeleter> scoped_ptr_free_deleter_bool(
120 static_cast<bool*>(malloc(sizeof(bool)))); 117 static_cast<bool*>(malloc(sizeof(bool))));
121 *scoped_ptr_free_deleter_bool = true; 118 *scoped_ptr_free_deleter_bool = true;
122 base::Closure cb = BindToCurrentLoop(base::Bind( 119 Closure cb =
123 &BoundBoolSetFromScopedPtrFreeDeleter, &bool_val, 120 BindToCurrentThread(Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val,
124 base::Passed(&scoped_ptr_free_deleter_bool))); 121 Passed(&scoped_ptr_free_deleter_bool)));
125 cb.Run(); 122 cb.Run();
126 EXPECT_FALSE(bool_val); 123 EXPECT_FALSE(bool_val);
127 loop_.RunUntilIdle(); 124 loop_.RunUntilIdle();
128 EXPECT_TRUE(bool_val); 125 EXPECT_TRUE(bool_val);
129 } 126 }
130 127
131 TEST_F(BindToCurrentLoopTest, PassedScopedPtrFreeDeleterBool) { 128 TEST_F(BindToTaskRunnerTest, PassedScopedPtrFreeDeleterBool) {
132 bool bool_val = false; 129 bool bool_val = false;
133 scoped_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( 130 scoped_ptr<bool, FreeDeleter> scoped_ptr_free_deleter_bool(
134 static_cast<bool*>(malloc(sizeof(bool)))); 131 static_cast<bool*>(malloc(sizeof(bool))));
135 *scoped_ptr_free_deleter_bool = true; 132 *scoped_ptr_free_deleter_bool = true;
136 base::Callback<void(scoped_ptr<bool, base::FreeDeleter>)> cb = 133 Callback<void(scoped_ptr<bool, FreeDeleter>)> cb = BindToCurrentThread(
137 BindToCurrentLoop(base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter, 134 Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val));
138 &bool_val));
139 cb.Run(std::move(scoped_ptr_free_deleter_bool)); 135 cb.Run(std::move(scoped_ptr_free_deleter_bool));
140 EXPECT_FALSE(bool_val); 136 EXPECT_FALSE(bool_val);
141 loop_.RunUntilIdle(); 137 loop_.RunUntilIdle();
142 EXPECT_TRUE(bool_val); 138 EXPECT_TRUE(bool_val);
143 } 139 }
144 140
145 TEST_F(BindToCurrentLoopTest, BoolConstRef) { 141 TEST_F(BindToTaskRunnerTest, BoolConstRef) {
146 bool bool_var = false; 142 bool bool_var = false;
147 bool true_var = true; 143 bool true_var = true;
148 const bool& true_ref = true_var; 144 const bool& true_ref = true_var;
149 base::Closure cb = BindToCurrentLoop(base::Bind( 145 Closure cb =
150 &BoundBoolSetFromConstRef, &bool_var, true_ref)); 146 BindToCurrentThread(Bind(&BoundBoolSetFromConstRef, &bool_var, true_ref));
151 cb.Run(); 147 cb.Run();
152 EXPECT_FALSE(bool_var); 148 EXPECT_FALSE(bool_var);
153 loop_.RunUntilIdle(); 149 loop_.RunUntilIdle();
154 EXPECT_TRUE(bool_var); 150 EXPECT_TRUE(bool_var);
155 } 151 }
156 152
157 TEST_F(BindToCurrentLoopTest, Integers) { 153 TEST_F(BindToTaskRunnerTest, Integers) {
158 int a = 0; 154 int a = 0;
159 int b = 0; 155 int b = 0;
160 base::Callback<void(int, int)> cb = BindToCurrentLoop(base::Bind( 156 Callback<void(int, int)> cb =
161 &BoundIntegersSet, &a, &b)); 157 BindToCurrentThread(Bind(&BoundIntegersSet, &a, &b));
162 cb.Run(1, -1); 158 cb.Run(1, -1);
163 EXPECT_EQ(a, 0); 159 EXPECT_EQ(a, 0);
164 EXPECT_EQ(b, 0); 160 EXPECT_EQ(b, 0);
165 loop_.RunUntilIdle(); 161 loop_.RunUntilIdle();
166 EXPECT_EQ(a, 1); 162 EXPECT_EQ(a, 1);
167 EXPECT_EQ(b, -1); 163 EXPECT_EQ(b, -1);
168 } 164 }
169 165
170 } // namespace media 166 } // namespace base
OLDNEW
« no previous file with comments | « base/bind_to_task_runner.h ('k') | media/base/bind_to_current_loop.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698