Index: media/base/bind_to_loop_unittest.cc |
diff --git a/media/base/bind_to_loop_unittest.cc b/media/base/bind_to_loop_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fd9c93c201ce02e3922b3bd961167625fd2597e6 |
--- /dev/null |
+++ b/media/base/bind_to_loop_unittest.cc |
@@ -0,0 +1,98 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/base/bind_to_loop.h" |
+ |
+#include "base/message_loop.h" |
+#include "base/synchronization/waitable_event.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace media { |
+ |
+void BoundBoolSet(bool* var, bool val) { |
+ *var = val; |
+} |
+ |
+void BoundBoolSetFromScopedPtr(bool* var, scoped_ptr<bool> val) { |
+ *var = *val; |
+} |
+ |
+void BoundBoolSetFromConstRef(bool* var, const bool& val) { |
+ *var = val; |
+} |
+ |
+void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { |
+ *a_var = a_val; |
+ *b_var = b_val; |
+} |
+ |
+// Various tests that check that the bound function is only actually executed |
+// on the message loop, not during the original Run. |
+class BindToLoopTest : public ::testing::Test { |
+ public: |
+ BindToLoopTest() : proxy_(loop_.message_loop_proxy()) {} |
+ |
+ protected: |
+ MessageLoop loop_; |
+ scoped_refptr<base::MessageLoopProxy> proxy_; |
+}; |
+ |
+TEST_F(BindToLoopTest, Closure) { |
+ // Test the closure is run inside the loop, not outside it. |
+ base::WaitableEvent waiter(false, false); |
+ base::Closure cb = BindToLoop(proxy_, base::Bind( |
+ &base::WaitableEvent::Signal, base::Unretained(&waiter))); |
+ cb.Run(); |
+ EXPECT_FALSE(waiter.IsSignaled()); |
+ loop_.RunUntilIdle(); |
+ EXPECT_TRUE(waiter.IsSignaled()); |
+} |
+ |
+TEST_F(BindToLoopTest, Bool) { |
+ bool bool_var = false; |
+ base::Callback<void(bool)> cb = BindToLoop(proxy_, base::Bind( |
+ &BoundBoolSet, &bool_var)); |
+ cb.Run(true); |
+ EXPECT_FALSE(bool_var); |
+ loop_.RunUntilIdle(); |
+ EXPECT_TRUE(bool_var); |
+} |
+ |
+TEST_F(BindToLoopTest, ScopedPtrBool) { |
+ bool bool_val = false; |
+ scoped_ptr<bool> scoped_ptr_bool(new bool(true)); |
+ base::Closure cb = BindToLoop(proxy_, base::Bind( |
+ &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); |
+ cb.Run(); |
+ EXPECT_FALSE(bool_val); |
+ loop_.RunUntilIdle(); |
+ EXPECT_TRUE(bool_val); |
+} |
+ |
+TEST_F(BindToLoopTest, BoolConstRef) { |
+ bool bool_var = false; |
+ bool true_var = true; |
+ const bool& true_ref = true_var; |
+ base::Closure cb = BindToLoop(proxy_, base::Bind( |
+ &BoundBoolSetFromConstRef, &bool_var, true_ref)); |
+ cb.Run(); |
+ EXPECT_FALSE(bool_var); |
+ loop_.RunUntilIdle(); |
+ EXPECT_TRUE(bool_var); |
+} |
+ |
+TEST_F(BindToLoopTest, Integers) { |
+ int a = 0; |
+ int b = 0; |
+ base::Callback<void(int, int)> cb = BindToLoop(proxy_, base::Bind( |
+ &BoundIntegersSet, &a, &b)); |
+ cb.Run(1, -1); |
+ EXPECT_EQ(a, 0); |
+ EXPECT_EQ(b, 0); |
+ loop_.RunUntilIdle(); |
+ EXPECT_EQ(a, 1); |
+ EXPECT_EQ(b, -1); |
+} |
+ |
+} // namespace media |