| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/edk/base_edk/platform_message_loop_impl.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <thread> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/message_loop/message_pump_default.h" |
| 16 #include "mojo/edk/platform/task_runner.h" |
| 17 #include "mojo/edk/platform/test_message_loop.h" |
| 18 #include "mojo/edk/util/ref_ptr.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 using mojo::platform::TaskRunner; |
| 22 using mojo::util::RefPtr; |
| 23 |
| 24 namespace base_edk { |
| 25 namespace { |
| 26 |
| 27 template <typename Runnable> |
| 28 void RunnableRunner(Runnable runnable) { |
| 29 runnable(); |
| 30 } |
| 31 |
| 32 template <typename Runnable> |
| 33 base::Closure ToClosure(Runnable runnable) { |
| 34 return base::Bind(&RunnableRunner<Runnable>, runnable); |
| 35 } |
| 36 |
| 37 TEST(PlatformMessageLoopImpl, Basic) { |
| 38 PlatformMessageLoopImpl message_loop; |
| 39 |
| 40 EXPECT_EQ(base::MessageLoop::TYPE_DEFAULT, |
| 41 message_loop.base_message_loop().type()); |
| 42 |
| 43 RefPtr<TaskRunner> task_runner = message_loop.GetTaskRunner(); |
| 44 EXPECT_TRUE(task_runner); |
| 45 |
| 46 // |GetTaskRunner()| should always return the same |TaskRunner()|. |
| 47 EXPECT_EQ(task_runner, message_loop.GetTaskRunner()); |
| 48 |
| 49 std::vector<int> stuff; |
| 50 task_runner->PostTask(ToClosure([&stuff, &message_loop, &task_runner]() { |
| 51 EXPECT_TRUE(message_loop.IsRunningOnCurrentThread()); |
| 52 stuff.push_back(1); |
| 53 task_runner->PostTask(ToClosure([&stuff]() { stuff.push_back(3); })); |
| 54 message_loop.QuitWhenIdle(); |
| 55 })); |
| 56 task_runner->PostTask(ToClosure([&stuff]() { stuff.push_back(2); })); |
| 57 EXPECT_TRUE(stuff.empty()); |
| 58 message_loop.Run(); |
| 59 EXPECT_EQ(std::vector<int>({1, 2, 3}), stuff); |
| 60 |
| 61 stuff.clear(); |
| 62 task_runner->PostTask(ToClosure([&stuff, &message_loop, &task_runner]() { |
| 63 EXPECT_TRUE(message_loop.IsRunningOnCurrentThread()); |
| 64 stuff.push_back(4); |
| 65 task_runner->PostTask(ToClosure([&stuff]() { stuff.push_back(6); })); |
| 66 })); |
| 67 task_runner->PostTask(ToClosure([&stuff]() { stuff.push_back(5); })); |
| 68 message_loop.RunUntilIdle(); |
| 69 EXPECT_EQ(std::vector<int>({4, 5, 6}), stuff); |
| 70 |
| 71 stuff.clear(); |
| 72 message_loop.RunUntilIdle(); |
| 73 EXPECT_TRUE(stuff.empty()); |
| 74 |
| 75 { |
| 76 std::thread other_thread([&message_loop]() { |
| 77 // |IsRunningOnCurrentThread()| may be called on any thread. |
| 78 EXPECT_FALSE(message_loop.IsRunningOnCurrentThread()); |
| 79 }); |
| 80 other_thread.join(); |
| 81 } |
| 82 |
| 83 stuff.clear(); |
| 84 task_runner->PostTask(ToClosure([&stuff, &message_loop, &task_runner]() { |
| 85 EXPECT_TRUE(message_loop.IsRunningOnCurrentThread()); |
| 86 std::thread other_thread([&stuff, &message_loop, task_runner]() { |
| 87 EXPECT_FALSE(message_loop.IsRunningOnCurrentThread()); |
| 88 EXPECT_EQ(task_runner, message_loop.GetTaskRunner()); |
| 89 stuff.push_back(7); |
| 90 task_runner->PostTask(ToClosure([&stuff, &message_loop]() { |
| 91 EXPECT_TRUE(message_loop.IsRunningOnCurrentThread()); |
| 92 stuff.push_back(8); |
| 93 message_loop.QuitWhenIdle(); |
| 94 })); |
| 95 }); |
| 96 other_thread.join(); |
| 97 EXPECT_EQ(std::vector<int>({7}), stuff); |
| 98 stuff.clear(); |
| 99 })); |
| 100 message_loop.Run(); |
| 101 EXPECT_EQ(std::vector<int>({8}), stuff); |
| 102 } |
| 103 |
| 104 TEST(PlatformMessageLoopImpl, TypeIO) { |
| 105 PlatformMessageLoopImpl message_loop(base::MessageLoop::TYPE_IO); |
| 106 |
| 107 EXPECT_EQ(base::MessageLoop::TYPE_IO, |
| 108 message_loop.base_message_loop().type()); |
| 109 |
| 110 RefPtr<TaskRunner> task_runner = message_loop.GetTaskRunner(); |
| 111 EXPECT_TRUE(task_runner); |
| 112 |
| 113 bool was_run = false; |
| 114 task_runner->PostTask(ToClosure([&was_run]() { was_run = true; })); |
| 115 EXPECT_FALSE(was_run); |
| 116 message_loop.RunUntilIdle(); |
| 117 EXPECT_TRUE(was_run); |
| 118 } |
| 119 |
| 120 TEST(PlatformMessageLoopImpl, TypeCustom) { |
| 121 PlatformMessageLoopImpl message_loop( |
| 122 make_scoped_ptr(new base::MessagePumpDefault())); |
| 123 |
| 124 EXPECT_EQ(base::MessageLoop::TYPE_CUSTOM, |
| 125 message_loop.base_message_loop().type()); |
| 126 |
| 127 RefPtr<TaskRunner> task_runner = message_loop.GetTaskRunner(); |
| 128 EXPECT_TRUE(task_runner); |
| 129 |
| 130 bool was_run = false; |
| 131 task_runner->PostTask(ToClosure([&was_run]() { was_run = true; })); |
| 132 EXPECT_FALSE(was_run); |
| 133 message_loop.RunUntilIdle(); |
| 134 EXPECT_TRUE(was_run); |
| 135 } |
| 136 |
| 137 // TODO(vtl): This test doesn't really belong here, but it's convenient. (Maybe |
| 138 // it should actually be in a test in //mojo/edk/platform?) |
| 139 TEST(PlatformMessageLoopImpl, CreateTestMessageLoop) { |
| 140 std::unique_ptr<mojo::platform::MessageLoop> message_loop = |
| 141 mojo::platform::test::CreateTestMessageLoop(); |
| 142 |
| 143 bool was_run = false; |
| 144 message_loop->GetTaskRunner()->PostTask( |
| 145 ToClosure([&was_run]() { was_run = true; })); |
| 146 EXPECT_FALSE(was_run); |
| 147 message_loop->RunUntilIdle(); |
| 148 EXPECT_TRUE(was_run); |
| 149 } |
| 150 |
| 151 } // namespace |
| 152 } // namespace base_edk |
| OLD | NEW |