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.
5 #include "media/base/bind_to_current_loop.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 void BoundBoolSet(bool* var
, bool val
) {
17 void BoundBoolSetFromScopedPtr(bool* var
, scoped_ptr
<bool> val
) {
21 void BoundBoolSetFromScopedPtrFreeDeleter(
23 scoped_ptr
<bool, base::FreeDeleter
> val
) {
27 void BoundBoolSetFromScopedArray(bool* var
, scoped_ptr
<bool[]> val
) {
31 void BoundBoolSetFromConstRef(bool* var
, const bool& val
) {
35 void BoundIntegersSet(int* a_var
, int* b_var
, int a_val
, int b_val
) {
40 // Various tests that check that the bound function is only actually executed
41 // on the message loop, not during the original Run.
42 class BindToCurrentLoopTest
: public ::testing::Test
{
44 base::MessageLoop loop_
;
47 TEST_F(BindToCurrentLoopTest
, Closure
) {
48 // Test the closure is run inside the loop, not outside it.
49 base::WaitableEvent
waiter(false, false);
50 base::Closure cb
= BindToCurrentLoop(base::Bind(
51 &base::WaitableEvent::Signal
, base::Unretained(&waiter
)));
53 EXPECT_FALSE(waiter
.IsSignaled());
55 EXPECT_TRUE(waiter
.IsSignaled());
58 TEST_F(BindToCurrentLoopTest
, Bool
) {
59 bool bool_var
= false;
60 base::Callback
<void(bool)> cb
= BindToCurrentLoop(base::Bind(
61 &BoundBoolSet
, &bool_var
));
63 EXPECT_FALSE(bool_var
);
65 EXPECT_TRUE(bool_var
);
68 TEST_F(BindToCurrentLoopTest
, BoundScopedPtrBool
) {
69 bool bool_val
= false;
70 scoped_ptr
<bool> scoped_ptr_bool(new bool(true));
71 base::Closure cb
= BindToCurrentLoop(base::Bind(
72 &BoundBoolSetFromScopedPtr
, &bool_val
, base::Passed(&scoped_ptr_bool
)));
74 EXPECT_FALSE(bool_val
);
76 EXPECT_TRUE(bool_val
);
79 TEST_F(BindToCurrentLoopTest
, PassedScopedPtrBool
) {
80 bool bool_val
= false;
81 scoped_ptr
<bool> scoped_ptr_bool(new bool(true));
82 base::Callback
<void(scoped_ptr
<bool>)> cb
= BindToCurrentLoop(base::Bind(
83 &BoundBoolSetFromScopedPtr
, &bool_val
));
84 cb
.Run(scoped_ptr_bool
.Pass());
85 EXPECT_FALSE(bool_val
);
87 EXPECT_TRUE(bool_val
);
90 TEST_F(BindToCurrentLoopTest
, BoundScopedArrayBool
) {
91 bool bool_val
= false;
92 scoped_ptr
<bool[]> scoped_array_bool(new bool[1]);
93 scoped_array_bool
[0] = true;
94 base::Closure cb
= BindToCurrentLoop(base::Bind(
95 &BoundBoolSetFromScopedArray
, &bool_val
,
96 base::Passed(&scoped_array_bool
)));
98 EXPECT_FALSE(bool_val
);
100 EXPECT_TRUE(bool_val
);
103 TEST_F(BindToCurrentLoopTest
, PassedScopedArrayBool
) {
104 bool bool_val
= false;
105 scoped_ptr
<bool[]> scoped_array_bool(new bool[1]);
106 scoped_array_bool
[0] = true;
107 base::Callback
<void(scoped_ptr
<bool[]>)> cb
= BindToCurrentLoop(base::Bind(
108 &BoundBoolSetFromScopedArray
, &bool_val
));
109 cb
.Run(scoped_array_bool
.Pass());
110 EXPECT_FALSE(bool_val
);
111 loop_
.RunUntilIdle();
112 EXPECT_TRUE(bool_val
);
115 TEST_F(BindToCurrentLoopTest
, BoundScopedPtrFreeDeleterBool
) {
116 bool bool_val
= false;
117 scoped_ptr
<bool, base::FreeDeleter
> scoped_ptr_free_deleter_bool(
118 static_cast<bool*>(malloc(sizeof(bool))));
119 *scoped_ptr_free_deleter_bool
= true;
120 base::Closure cb
= BindToCurrentLoop(base::Bind(
121 &BoundBoolSetFromScopedPtrFreeDeleter
, &bool_val
,
122 base::Passed(&scoped_ptr_free_deleter_bool
)));
124 EXPECT_FALSE(bool_val
);
125 loop_
.RunUntilIdle();
126 EXPECT_TRUE(bool_val
);
129 TEST_F(BindToCurrentLoopTest
, PassedScopedPtrFreeDeleterBool
) {
130 bool bool_val
= false;
131 scoped_ptr
<bool, base::FreeDeleter
> scoped_ptr_free_deleter_bool(
132 static_cast<bool*>(malloc(sizeof(bool))));
133 *scoped_ptr_free_deleter_bool
= true;
134 base::Callback
<void(scoped_ptr
<bool, base::FreeDeleter
>)> cb
=
135 BindToCurrentLoop(base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter
,
137 cb
.Run(scoped_ptr_free_deleter_bool
.Pass());
138 EXPECT_FALSE(bool_val
);
139 loop_
.RunUntilIdle();
140 EXPECT_TRUE(bool_val
);
143 TEST_F(BindToCurrentLoopTest
, BoolConstRef
) {
144 bool bool_var
= false;
145 bool true_var
= true;
146 const bool& true_ref
= true_var
;
147 base::Closure cb
= BindToCurrentLoop(base::Bind(
148 &BoundBoolSetFromConstRef
, &bool_var
, true_ref
));
150 EXPECT_FALSE(bool_var
);
151 loop_
.RunUntilIdle();
152 EXPECT_TRUE(bool_var
);
155 TEST_F(BindToCurrentLoopTest
, Integers
) {
158 base::Callback
<void(int, int)> cb
= BindToCurrentLoop(base::Bind(
159 &BoundIntegersSet
, &a
, &b
));
163 loop_
.RunUntilIdle();