| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromecast/base/bind_to_task_runner.h" | 5 #include "components/sync/base/bind_to_task_runner.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/free_deleter.h" | 9 #include "base/memory/free_deleter.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 11 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace chromecast { | 15 namespace syncer { |
| 15 | 16 |
| 16 void BoundBoolSet(bool* var, bool val) { | 17 void BoundBoolSet(bool* var, bool val) { |
| 17 *var = val; | 18 *var = val; |
| 18 } | 19 } |
| 19 | 20 |
| 20 void BoundBoolSetFromScopedPtr(bool* var, std::unique_ptr<bool> val) { | 21 void BoundBoolSetFromScopedPtr(bool* var, std::unique_ptr<bool> val) { |
| 21 *var = *val; | 22 *var = *val; |
| 22 } | 23 } |
| 23 | 24 |
| 24 void BoundBoolSetFromScopedPtrFreeDeleter( | 25 void BoundBoolSetFromScopedPtrFreeDeleter( |
| (...skipping 23 matching lines...) Expand all Loading... |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 TEST_F(BindToTaskRunnerTest, Closure) { | 51 TEST_F(BindToTaskRunnerTest, Closure) { |
| 51 // Test the closure is run inside the loop, not outside it. | 52 // Test the closure is run inside the loop, not outside it. |
| 52 base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 53 base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 53 base::WaitableEvent::InitialState::NOT_SIGNALED); | 54 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 54 base::Closure cb = BindToCurrentThread( | 55 base::Closure cb = BindToCurrentThread( |
| 55 base::Bind(&base::WaitableEvent::Signal, Unretained(&waiter))); | 56 base::Bind(&base::WaitableEvent::Signal, Unretained(&waiter))); |
| 56 cb.Run(); | 57 cb.Run(); |
| 57 EXPECT_FALSE(waiter.IsSignaled()); | 58 EXPECT_FALSE(waiter.IsSignaled()); |
| 58 loop_.RunUntilIdle(); | 59 base::RunLoop().RunUntilIdle(); |
| 59 EXPECT_TRUE(waiter.IsSignaled()); | 60 EXPECT_TRUE(waiter.IsSignaled()); |
| 60 } | 61 } |
| 61 | 62 |
| 62 TEST_F(BindToTaskRunnerTest, Bool) { | 63 TEST_F(BindToTaskRunnerTest, Bool) { |
| 63 bool bool_var = false; | 64 bool bool_var = false; |
| 64 base::Callback<void(bool)> cb = | 65 base::Callback<void(bool)> cb = |
| 65 BindToCurrentThread(base::Bind(&BoundBoolSet, &bool_var)); | 66 BindToCurrentThread(base::Bind(&BoundBoolSet, &bool_var)); |
| 66 cb.Run(true); | 67 cb.Run(true); |
| 67 EXPECT_FALSE(bool_var); | 68 EXPECT_FALSE(bool_var); |
| 68 loop_.RunUntilIdle(); | 69 base::RunLoop().RunUntilIdle(); |
| 69 EXPECT_TRUE(bool_var); | 70 EXPECT_TRUE(bool_var); |
| 70 } | 71 } |
| 71 | 72 |
| 72 TEST_F(BindToTaskRunnerTest, BoundScopedPtrBool) { | 73 TEST_F(BindToTaskRunnerTest, BoundScopedPtrBool) { |
| 73 bool bool_val = false; | 74 bool bool_val = false; |
| 74 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); | 75 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); |
| 75 base::Closure cb = BindToCurrentThread(base::Bind( | 76 base::Closure cb = BindToCurrentThread(base::Bind( |
| 76 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); | 77 &BoundBoolSetFromScopedPtr, &bool_val, base::Passed(&scoped_ptr_bool))); |
| 77 cb.Run(); | 78 cb.Run(); |
| 78 EXPECT_FALSE(bool_val); | 79 EXPECT_FALSE(bool_val); |
| 79 loop_.RunUntilIdle(); | 80 base::RunLoop().RunUntilIdle(); |
| 80 EXPECT_TRUE(bool_val); | 81 EXPECT_TRUE(bool_val); |
| 81 } | 82 } |
| 82 | 83 |
| 83 TEST_F(BindToTaskRunnerTest, PassedScopedPtrBool) { | 84 TEST_F(BindToTaskRunnerTest, PassedScopedPtrBool) { |
| 84 bool bool_val = false; | 85 bool bool_val = false; |
| 85 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); | 86 std::unique_ptr<bool> scoped_ptr_bool(new bool(true)); |
| 86 base::Callback<void(std::unique_ptr<bool>)> cb = | 87 base::Callback<void(std::unique_ptr<bool>)> cb = |
| 87 BindToCurrentThread(base::Bind(&BoundBoolSetFromScopedPtr, &bool_val)); | 88 BindToCurrentThread(base::Bind(&BoundBoolSetFromScopedPtr, &bool_val)); |
| 88 cb.Run(std::move(scoped_ptr_bool)); | 89 cb.Run(std::move(scoped_ptr_bool)); |
| 89 EXPECT_FALSE(bool_val); | 90 EXPECT_FALSE(bool_val); |
| 90 loop_.RunUntilIdle(); | 91 base::RunLoop().RunUntilIdle(); |
| 91 EXPECT_TRUE(bool_val); | 92 EXPECT_TRUE(bool_val); |
| 92 } | 93 } |
| 93 | 94 |
| 94 TEST_F(BindToTaskRunnerTest, BoundScopedArrayBool) { | 95 TEST_F(BindToTaskRunnerTest, BoundScopedArrayBool) { |
| 95 bool bool_val = false; | 96 bool bool_val = false; |
| 96 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); | 97 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); |
| 97 scoped_array_bool[0] = true; | 98 scoped_array_bool[0] = true; |
| 98 base::Closure cb = | 99 base::Closure cb = |
| 99 BindToCurrentThread(base::Bind(&BoundBoolSetFromScopedArray, &bool_val, | 100 BindToCurrentThread(base::Bind(&BoundBoolSetFromScopedArray, &bool_val, |
| 100 base::Passed(&scoped_array_bool))); | 101 base::Passed(&scoped_array_bool))); |
| 101 cb.Run(); | 102 cb.Run(); |
| 102 EXPECT_FALSE(bool_val); | 103 EXPECT_FALSE(bool_val); |
| 103 loop_.RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
| 104 EXPECT_TRUE(bool_val); | 105 EXPECT_TRUE(bool_val); |
| 105 } | 106 } |
| 106 | 107 |
| 107 TEST_F(BindToTaskRunnerTest, PassedScopedArrayBool) { | 108 TEST_F(BindToTaskRunnerTest, PassedScopedArrayBool) { |
| 108 bool bool_val = false; | 109 bool bool_val = false; |
| 109 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); | 110 std::unique_ptr<bool[]> scoped_array_bool(new bool[1]); |
| 110 scoped_array_bool[0] = true; | 111 scoped_array_bool[0] = true; |
| 111 base::Callback<void(std::unique_ptr<bool[]>)> cb = | 112 base::Callback<void(std::unique_ptr<bool[]>)> cb = |
| 112 BindToCurrentThread(base::Bind(&BoundBoolSetFromScopedArray, &bool_val)); | 113 BindToCurrentThread(base::Bind(&BoundBoolSetFromScopedArray, &bool_val)); |
| 113 cb.Run(std::move(scoped_array_bool)); | 114 cb.Run(std::move(scoped_array_bool)); |
| 114 EXPECT_FALSE(bool_val); | 115 EXPECT_FALSE(bool_val); |
| 115 loop_.RunUntilIdle(); | 116 base::RunLoop().RunUntilIdle(); |
| 116 EXPECT_TRUE(bool_val); | 117 EXPECT_TRUE(bool_val); |
| 117 } | 118 } |
| 118 | 119 |
| 119 TEST_F(BindToTaskRunnerTest, BoundScopedPtrFreeDeleterBool) { | 120 TEST_F(BindToTaskRunnerTest, BoundScopedPtrFreeDeleterBool) { |
| 120 bool bool_val = false; | 121 bool bool_val = false; |
| 121 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( | 122 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( |
| 122 static_cast<bool*>(malloc(sizeof(bool)))); | 123 static_cast<bool*>(malloc(sizeof(bool)))); |
| 123 *scoped_ptr_free_deleter_bool = true; | 124 *scoped_ptr_free_deleter_bool = true; |
| 124 base::Closure cb = BindToCurrentThread( | 125 base::Closure cb = BindToCurrentThread( |
| 125 base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val, | 126 base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val, |
| 126 base::Passed(&scoped_ptr_free_deleter_bool))); | 127 base::Passed(&scoped_ptr_free_deleter_bool))); |
| 127 cb.Run(); | 128 cb.Run(); |
| 128 EXPECT_FALSE(bool_val); | 129 EXPECT_FALSE(bool_val); |
| 129 loop_.RunUntilIdle(); | 130 base::RunLoop().RunUntilIdle(); |
| 130 EXPECT_TRUE(bool_val); | 131 EXPECT_TRUE(bool_val); |
| 131 } | 132 } |
| 132 | 133 |
| 133 TEST_F(BindToTaskRunnerTest, PassedScopedPtrFreeDeleterBool) { | 134 TEST_F(BindToTaskRunnerTest, PassedScopedPtrFreeDeleterBool) { |
| 134 bool bool_val = false; | 135 bool bool_val = false; |
| 135 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( | 136 std::unique_ptr<bool, base::FreeDeleter> scoped_ptr_free_deleter_bool( |
| 136 static_cast<bool*>(malloc(sizeof(bool)))); | 137 static_cast<bool*>(malloc(sizeof(bool)))); |
| 137 *scoped_ptr_free_deleter_bool = true; | 138 *scoped_ptr_free_deleter_bool = true; |
| 138 base::Callback<void(std::unique_ptr<bool, base::FreeDeleter>)> cb = | 139 base::Callback<void(std::unique_ptr<bool, base::FreeDeleter>)> cb = |
| 139 BindToCurrentThread( | 140 BindToCurrentThread( |
| 140 base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val)); | 141 base::Bind(&BoundBoolSetFromScopedPtrFreeDeleter, &bool_val)); |
| 141 cb.Run(std::move(scoped_ptr_free_deleter_bool)); | 142 cb.Run(std::move(scoped_ptr_free_deleter_bool)); |
| 142 EXPECT_FALSE(bool_val); | 143 EXPECT_FALSE(bool_val); |
| 143 loop_.RunUntilIdle(); | 144 base::RunLoop().RunUntilIdle(); |
| 144 EXPECT_TRUE(bool_val); | 145 EXPECT_TRUE(bool_val); |
| 145 } | 146 } |
| 146 | 147 |
| 147 TEST_F(BindToTaskRunnerTest, BoolConstRef) { | 148 TEST_F(BindToTaskRunnerTest, BoolConstRef) { |
| 148 bool bool_var = false; | 149 bool bool_var = false; |
| 149 bool true_var = true; | 150 bool true_var = true; |
| 150 const bool& true_ref = true_var; | 151 const bool& true_ref = true_var; |
| 151 base::Closure cb = BindToCurrentThread( | 152 base::Closure cb = BindToCurrentThread( |
| 152 base::Bind(&BoundBoolSetFromConstRef, &bool_var, true_ref)); | 153 base::Bind(&BoundBoolSetFromConstRef, &bool_var, true_ref)); |
| 153 cb.Run(); | 154 cb.Run(); |
| 154 EXPECT_FALSE(bool_var); | 155 EXPECT_FALSE(bool_var); |
| 155 loop_.RunUntilIdle(); | 156 base::RunLoop().RunUntilIdle(); |
| 156 EXPECT_TRUE(bool_var); | 157 EXPECT_TRUE(bool_var); |
| 157 } | 158 } |
| 158 | 159 |
| 159 TEST_F(BindToTaskRunnerTest, Integers) { | 160 TEST_F(BindToTaskRunnerTest, Integers) { |
| 160 int a = 0; | 161 int a = 0; |
| 161 int b = 0; | 162 int b = 0; |
| 162 base::Callback<void(int, int)> cb = | 163 base::Callback<void(int, int)> cb = |
| 163 BindToCurrentThread(base::Bind(&BoundIntegersSet, &a, &b)); | 164 BindToCurrentThread(base::Bind(&BoundIntegersSet, &a, &b)); |
| 164 cb.Run(1, -1); | 165 cb.Run(1, -1); |
| 165 EXPECT_EQ(a, 0); | 166 EXPECT_EQ(a, 0); |
| 166 EXPECT_EQ(b, 0); | 167 EXPECT_EQ(b, 0); |
| 167 loop_.RunUntilIdle(); | 168 base::RunLoop().RunUntilIdle(); |
| 168 EXPECT_EQ(a, 1); | 169 EXPECT_EQ(a, 1); |
| 169 EXPECT_EQ(b, -1); | 170 EXPECT_EQ(b, -1); |
| 170 } | 171 } |
| 171 | 172 |
| 172 } // namespace chromecast | 173 } // namespace syncer |
| OLD | NEW |