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