| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 // Note: This file also tests child_process.*. | |
| 6 | |
| 7 #include "services/service_manager/runner/host/child_process_host.h" | |
| 8 | |
| 9 #include <memory> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/callback.h" | |
| 14 #include "base/command_line.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/message_loop/message_loop.h" | |
| 18 #include "base/path_service.h" | |
| 19 #include "base/run_loop.h" | |
| 20 #include "base/threading/thread.h" | |
| 21 #include "mojo/edk/embedder/embedder.h" | |
| 22 #include "mojo/edk/embedder/process_delegate.h" | |
| 23 #include "services/service_manager/native_runner_delegate.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 | |
| 26 namespace service_manager { | |
| 27 namespace { | |
| 28 | |
| 29 const char kTestServiceName[] = "host_test_service"; | |
| 30 | |
| 31 const base::FilePath::CharType kPackagesPath[] = FILE_PATH_LITERAL("Packages"); | |
| 32 | |
| 33 #if defined(OS_WIN) | |
| 34 const base::FilePath::CharType kServiceExtension[] = | |
| 35 FILE_PATH_LITERAL(".service.exe"); | |
| 36 #else | |
| 37 const base::FilePath::CharType kServiceExtension[] = | |
| 38 FILE_PATH_LITERAL(".service"); | |
| 39 #endif | |
| 40 | |
| 41 void ProcessReadyCallbackAdapater(const base::Closure& callback, | |
| 42 base::ProcessId process_id) { | |
| 43 callback.Run(); | |
| 44 } | |
| 45 | |
| 46 class ProcessDelegate : public mojo::edk::ProcessDelegate { | |
| 47 public: | |
| 48 ProcessDelegate() {} | |
| 49 ~ProcessDelegate() override {} | |
| 50 | |
| 51 private: | |
| 52 void OnShutdownComplete() override {} | |
| 53 DISALLOW_COPY_AND_ASSIGN(ProcessDelegate); | |
| 54 }; | |
| 55 | |
| 56 class NativeRunnerDelegateImpl : public NativeRunnerDelegate { | |
| 57 public: | |
| 58 NativeRunnerDelegateImpl() {} | |
| 59 ~NativeRunnerDelegateImpl() override {} | |
| 60 | |
| 61 size_t get_and_clear_adjust_count() { | |
| 62 size_t count = 0; | |
| 63 std::swap(count, adjust_count_); | |
| 64 return count; | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 // NativeRunnerDelegate: | |
| 69 void AdjustCommandLineArgumentsForTarget( | |
| 70 const Identity& target, | |
| 71 base::CommandLine* command_line) override { | |
| 72 adjust_count_++; | |
| 73 } | |
| 74 | |
| 75 size_t adjust_count_ = 0; | |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(NativeRunnerDelegateImpl); | |
| 78 }; | |
| 79 | |
| 80 #if defined(OS_ANDROID) | |
| 81 // TODO(qsr): Multiprocess service manager tests are not supported on android. | |
| 82 #define MAYBE_StartJoin DISABLED_StartJoin | |
| 83 #else | |
| 84 #define MAYBE_StartJoin StartJoin | |
| 85 #endif // defined(OS_ANDROID) | |
| 86 TEST(ChildProcessHostTest, MAYBE_StartJoin) { | |
| 87 base::FilePath service_manager_dir; | |
| 88 PathService::Get(base::DIR_MODULE, &service_manager_dir); | |
| 89 base::MessageLoop message_loop; | |
| 90 scoped_refptr<base::SequencedWorkerPool> blocking_pool( | |
| 91 new base::SequencedWorkerPool(3, "blocking_pool", | |
| 92 base::TaskPriority::USER_VISIBLE)); | |
| 93 | |
| 94 base::Thread io_thread("io_thread"); | |
| 95 base::Thread::Options options; | |
| 96 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
| 97 io_thread.StartWithOptions(options); | |
| 98 | |
| 99 ProcessDelegate delegate; | |
| 100 mojo::edk::InitIPCSupport(&delegate, io_thread.task_runner()); | |
| 101 | |
| 102 base::FilePath test_service_path = | |
| 103 base::FilePath(kPackagesPath).AppendASCII(kTestServiceName) | |
| 104 .AppendASCII(kTestServiceName) .AddExtension(kServiceExtension); | |
| 105 | |
| 106 NativeRunnerDelegateImpl native_runner_delegate; | |
| 107 ChildProcessHost child_process_host(blocking_pool.get(), | |
| 108 &native_runner_delegate, false, | |
| 109 Identity(), test_service_path); | |
| 110 base::RunLoop run_loop; | |
| 111 child_process_host.Start( | |
| 112 Identity(), | |
| 113 base::Bind(&ProcessReadyCallbackAdapater, run_loop.QuitClosure()), | |
| 114 base::Bind(&base::DoNothing)); | |
| 115 run_loop.Run(); | |
| 116 | |
| 117 child_process_host.Join(); | |
| 118 blocking_pool->Shutdown(); | |
| 119 mojo::edk::ShutdownIPCSupport(); | |
| 120 EXPECT_EQ(1u, native_runner_delegate.get_and_clear_adjust_count()); | |
| 121 } | |
| 122 | |
| 123 } // namespace | |
| 124 } // namespace service_manager | |
| OLD | NEW |