OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/arc/standalone/service_helper.h" | 5 #include "components/arc/standalone/service_helper.h" |
6 | 6 |
7 #include <signal.h> | 7 #include <signal.h> |
8 #include <unistd.h> | 8 #include <unistd.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/files/file_descriptor_watcher_posix.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" |
14 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
16 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
17 #include "base/test/launcher/unit_test_launcher.h" | 19 #include "base/test/launcher/unit_test_launcher.h" |
18 #include "base/test/test_suite.h" | 20 #include "base/test/test_suite.h" |
19 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
20 #include "base/time/time.h" | 22 #include "base/time/time.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 | 24 |
23 namespace arc { | 25 namespace arc { |
24 | 26 |
25 class ServiceHelperTest : public ::testing::Test { | 27 class ServiceHelperTest : public ::testing::Test { |
26 public: | 28 public: |
27 ServiceHelperTest() {} | 29 ServiceHelperTest() {} |
28 ~ServiceHelperTest() override {} | 30 ~ServiceHelperTest() override {} |
29 | 31 |
30 void Quit() { | 32 void Quit() { |
31 CHECK(base_loop_); | 33 CHECK(base_loop_); |
32 CHECK(run_loop_); | 34 CHECK(run_loop_); |
33 base_loop_->task_runner()->PostTask(FROM_HERE, run_loop_->QuitClosure()); | 35 base_loop_->task_runner()->PostTask(FROM_HERE, run_loop_->QuitClosure()); |
34 } | 36 } |
35 | 37 |
36 void Init() { | 38 void Init() { |
37 // Dynamically create the message loop to avoid thread check failure of task | 39 // Dynamically create the message loop to avoid thread check failure of task |
38 // runner in Debug mode. | 40 // runner in Debug mode. |
39 base_loop_.reset(new base::MessageLoopForIO()); | 41 base_loop_.reset(new base::MessageLoopForIO()); |
40 run_loop_.reset(new base::RunLoop()); | 42 run_loop_.reset(new base::RunLoop()); |
41 | 43 |
| 44 // Required to watch a file descriptor from ServiceHelper. |
| 45 file_descriptor_watcher_ = |
| 46 base::MakeUnique<FileDescriptorWatcher>(base_loop_.get()); |
| 47 |
42 // This cannot be put inside SetUp() because we need to run it after fork(). | 48 // This cannot be put inside SetUp() because we need to run it after fork(). |
43 helper_.reset(new ServiceHelper()); | 49 helper_.reset(new ServiceHelper()); |
44 helper_->Init(base::Bind(&ServiceHelperTest::Quit, | 50 helper_->Init(base::Bind(&ServiceHelperTest::Quit, |
45 base::Unretained(this))); | 51 base::Unretained(this))); |
46 } | 52 } |
47 | 53 |
48 protected: | 54 protected: |
49 std::unique_ptr<base::MessageLoopForIO> base_loop_; | 55 std::unique_ptr<base::MessageLoopForIO> base_loop_; |
| 56 std::unique_ptr<base::MessageLoopForIO> file_descriptor_watcher_; |
50 std::unique_ptr<base::RunLoop> run_loop_; | 57 std::unique_ptr<base::RunLoop> run_loop_; |
51 std::unique_ptr<ServiceHelper> helper_; | 58 std::unique_ptr<ServiceHelper> helper_; |
52 | 59 |
53 private: | 60 private: |
54 DISALLOW_COPY_AND_ASSIGN(ServiceHelperTest); | 61 DISALLOW_COPY_AND_ASSIGN(ServiceHelperTest); |
55 }; | 62 }; |
56 | 63 |
57 TEST_F(ServiceHelperTest, CheckExternalTerm) { | 64 TEST_F(ServiceHelperTest, CheckExternalTerm) { |
58 // Fork ourselves and run it. | 65 // Fork ourselves and run it. |
59 pid_t child_pid = fork(); | 66 pid_t child_pid = fork(); |
(...skipping 18 matching lines...) Expand all Loading... |
78 } | 85 } |
79 | 86 |
80 } // namespace arc | 87 } // namespace arc |
81 | 88 |
82 int main(int argc, char** argv) { | 89 int main(int argc, char** argv) { |
83 base::TestSuite test_suite(argc, argv); | 90 base::TestSuite test_suite(argc, argv); |
84 return base::LaunchUnitTestsSerially( | 91 return base::LaunchUnitTestsSerially( |
85 argc, argv, | 92 argc, argv, |
86 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); | 93 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); |
87 } | 94 } |
OLD | NEW |