| // Copyright 2013 The Flutter 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 <atomic> |
| #include <thread> |
| |
| #include "flutter/fml/synchronization/waitable_event.h" |
| #include "flutter/fml/task_runner_util.h" |
| #include "flutter/fml/thread.h" |
| #include "gtest/gtest.h" |
| |
| namespace fml { |
| namespace testing { |
| |
| TEST(TaskRunnerUtilTests, WrapperBasicTaskRunnerPostTask) { |
| fml::Thread thread; |
| |
| WrapperBasicTaskRunner wrapper(thread.GetTaskRunner()); |
| |
| std::thread::id wrapper_thread_id; |
| wrapper.PostTask([&]() { wrapper_thread_id = std::this_thread::get_id(); }); |
| |
| thread.Join(); |
| |
| EXPECT_NE(wrapper_thread_id, std::this_thread::get_id()); |
| } |
| |
| TEST(TaskRunnerUtilTests, ConditionalBasicTaskRunnerPostTask) { |
| fml::Thread thread; |
| std::atomic_bool active = true; |
| ConditionalBasicTaskRunner runner(thread.GetTaskRunner(), |
| [&active]() -> bool { return active; }); |
| |
| fml::AutoResetWaitableEvent latch; |
| std::atomic_bool task1_called = false; |
| runner.PostTask([&]() { |
| task1_called.store(true); |
| latch.Signal(); |
| }); |
| latch.Wait(); |
| |
| active.store(false); |
| |
| std::atomic_bool task2_called = false; |
| runner.PostTask([&]() { task2_called.store(true); }); |
| |
| thread.GetTaskRunner()->PostTask([&]() { latch.Signal(); }); |
| latch.Wait(); |
| |
| thread.Join(); |
| |
| EXPECT_TRUE(task1_called.load()); |
| EXPECT_FALSE(task2_called.load()); |
| } |
| |
| } // namespace testing |
| } // namespace fml |