OLD | NEW |
(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_loop.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace media { |
| 12 |
| 13 void BoundBoolSet(bool* var, bool val) { |
| 14 *var = val; |
| 15 } |
| 16 |
| 17 void BoundBoolSetFromScopedPtr(bool* var, scoped_ptr<bool> val) { |
| 18 *var = *val; |
| 19 } |
| 20 |
| 21 void BoundBoolSetFromConstRef(bool* var, const bool& val) { |
| 22 *var = val; |
| 23 } |
| 24 |
| 25 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { |
| 26 *a_var = a_val; |
| 27 *b_var = b_val; |
| 28 } |
| 29 |
| 30 // Various tests that check that the bound function is only actually executed |
| 31 // on the message loop, not during the original Run. |
| 32 class BindToLoopTest : public ::testing::Test { |
| 33 public: |
| 34 BindToLoopTest() : proxy_(loop_.message_loop_proxy()) {} |
| 35 |
| 36 protected: |
| 37 MessageLoop loop_; |
| 38 scoped_refptr<base::MessageLoopProxy> proxy_; |
| 39 }; |
| 40 |
| 41 TEST_F(BindToLoopTest, Closure) { |
| 42 // Test the closure is run inside the loop, not outside it. |
| 43 base::WaitableEvent waiter(false, false); |
| 44 base::Closure cb = BindToLoop(proxy_, base::Bind( |
| 45 &base::WaitableEvent::Signal, base::Unretained(&waiter))); |
| 46 cb.Run(); |
| 47 EXPECT_FALSE(waiter.IsSignaled()); |
| 48 loop_.RunUntilIdle(); |
| 49 EXPECT_TRUE(waiter.IsSignaled()); |
| 50 } |
| 51 |
| 52 TEST_F(BindToLoopTest, Bool) { |
| 53 bool bool_var = false; |
| 54 base::Callback<void(bool)> cb = BindToLoop(proxy_, base::Bind( |
| 55 &BoundBoolSet, &bool_var)); |
| 56 cb.Run(true); |
| 57 EXPECT_FALSE(bool_var); |
| 58 loop_.RunUntilIdle(); |
| 59 EXPECT_TRUE(bool_var); |
| 60 } |
| 61 |
| 62 TEST_F(BindToLoopTest, ScopedPtrBool) { |
| 63 bool bool_val = false; |
| 64 scoped_ptr<bool> scoped_ptr_bool(new bool(true)); |
| 65 base::Closure cb = BindToLoop(proxy_, base::Bind( |
| 66 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); |
| 67 cb.Run(); |
| 68 EXPECT_FALSE(bool_val); |
| 69 loop_.RunUntilIdle(); |
| 70 EXPECT_TRUE(bool_val); |
| 71 } |
| 72 |
| 73 TEST_F(BindToLoopTest, BoolConstRef) { |
| 74 bool bool_var = false; |
| 75 bool true_var = true; |
| 76 const bool& true_ref = true_var; |
| 77 base::Closure cb = BindToLoop(proxy_, base::Bind( |
| 78 &BoundBoolSetFromConstRef, &bool_var, true_ref)); |
| 79 cb.Run(); |
| 80 EXPECT_FALSE(bool_var); |
| 81 loop_.RunUntilIdle(); |
| 82 EXPECT_TRUE(bool_var); |
| 83 } |
| 84 |
| 85 TEST_F(BindToLoopTest, Integers) { |
| 86 int a = 0; |
| 87 int b = 0; |
| 88 base::Callback<void(int, int)> cb = BindToLoop(proxy_, base::Bind( |
| 89 &BoundIntegersSet, &a, &b)); |
| 90 cb.Run(1, -1); |
| 91 EXPECT_EQ(a, 0); |
| 92 EXPECT_EQ(b, 0); |
| 93 loop_.RunUntilIdle(); |
| 94 EXPECT_EQ(a, 1); |
| 95 EXPECT_EQ(b, -1); |
| 96 } |
| 97 |
| 98 } // namespace media |
OLD | NEW |