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

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

Powered by Google App Engine
This is Rietveld 408576698