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

Side by Side Diff: chromecast/base/bind_to_task_runner_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698