OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/common/service_process_util.h" | 5 #include "chrome/common/service_process_util.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/process/kill.h" | 13 #include "base/process/kill.h" |
14 #include "base/process/launch.h" | 14 #include "base/process/launch.h" |
| 15 #include "base/run_loop.h" |
15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
16 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
17 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 | 20 |
20 #if !defined(OS_MACOSX) | 21 #if !defined(OS_MACOSX) |
21 #include "base/at_exit.h" | 22 #include "base/at_exit.h" |
22 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
23 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
24 #include "base/test/multiprocess_test.h" | 25 #include "base/test/multiprocess_test.h" |
(...skipping 21 matching lines...) Expand all Loading... |
46 #define MAYBE_ForceShutdown DISABLED_ForceShutdown | 47 #define MAYBE_ForceShutdown DISABLED_ForceShutdown |
47 #else | 48 #else |
48 #define MAYBE_ForceShutdown ForceShutdown | 49 #define MAYBE_ForceShutdown ForceShutdown |
49 #endif | 50 #endif |
50 #endif | 51 #endif |
51 | 52 |
52 namespace { | 53 namespace { |
53 | 54 |
54 bool g_good_shutdown = false; | 55 bool g_good_shutdown = false; |
55 | 56 |
56 void ShutdownTask(base::MessageLoop* loop) { | 57 void ShutdownTask(base::RunLoop* loop) { |
57 // Quit the main message loop. | 58 // Quit the main message loop. |
58 ASSERT_FALSE(g_good_shutdown); | 59 ASSERT_FALSE(g_good_shutdown); |
59 g_good_shutdown = true; | 60 g_good_shutdown = true; |
60 loop->task_runner()->PostTask(FROM_HERE, | 61 loop->QuitWhenIdle(); |
61 base::MessageLoop::QuitWhenIdleClosure()); | |
62 } | 62 } |
63 | 63 |
64 } // namespace | 64 } // namespace |
65 | 65 |
66 TEST(ServiceProcessUtilTest, ScopedVersionedName) { | 66 TEST(ServiceProcessUtilTest, ScopedVersionedName) { |
67 std::string test_str = "test"; | 67 std::string test_str = "test"; |
68 std::string scoped_name = GetServiceProcessScopedVersionedName(test_str); | 68 std::string scoped_name = GetServiceProcessScopedVersionedName(test_str); |
69 EXPECT_TRUE(base::EndsWith(scoped_name, test_str, | 69 EXPECT_TRUE(base::EndsWith(scoped_name, test_str, |
70 base::CompareCase::SENSITIVE)); | 70 base::CompareCase::SENSITIVE)); |
71 EXPECT_NE(std::string::npos, | 71 EXPECT_NE(std::string::npos, |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 } | 223 } |
224 | 224 |
225 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestReadyFalse) { | 225 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestReadyFalse) { |
226 EXPECT_FALSE(CheckServiceProcessReady()); | 226 EXPECT_FALSE(CheckServiceProcessReady()); |
227 return 0; | 227 return 0; |
228 } | 228 } |
229 | 229 |
230 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestShutdown) { | 230 MULTIPROCESS_TEST_MAIN(ServiceProcessStateTestShutdown) { |
231 base::PlatformThread::SetName("ServiceProcessStateTestShutdownMainThread"); | 231 base::PlatformThread::SetName("ServiceProcessStateTestShutdownMainThread"); |
232 base::MessageLoop message_loop; | 232 base::MessageLoop message_loop; |
| 233 base::RunLoop run_loop; |
233 base::Thread io_thread_("ServiceProcessStateTestShutdownIOThread"); | 234 base::Thread io_thread_("ServiceProcessStateTestShutdownIOThread"); |
234 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); | 235 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
235 EXPECT_TRUE(io_thread_.StartWithOptions(options)); | 236 EXPECT_TRUE(io_thread_.StartWithOptions(options)); |
236 ServiceProcessState state; | 237 ServiceProcessState state; |
237 EXPECT_TRUE(state.Initialize()); | 238 EXPECT_TRUE(state.Initialize()); |
238 EXPECT_TRUE(state.SignalReady( | 239 EXPECT_TRUE(state.SignalReady(io_thread_.task_runner().get(), |
239 io_thread_.task_runner().get(), | 240 base::Bind(&ShutdownTask, &run_loop))); |
240 base::Bind(&ShutdownTask, base::MessageLoop::current()))); | |
241 message_loop.task_runner()->PostDelayedTask( | 241 message_loop.task_runner()->PostDelayedTask( |
242 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | 242 FROM_HERE, run_loop.QuitWhenIdleClosure(), |
243 TestTimeouts::action_max_timeout()); | 243 TestTimeouts::action_max_timeout()); |
244 EXPECT_FALSE(g_good_shutdown); | 244 EXPECT_FALSE(g_good_shutdown); |
245 message_loop.Run(); | 245 run_loop.Run(); |
246 EXPECT_TRUE(g_good_shutdown); | 246 EXPECT_TRUE(g_good_shutdown); |
247 return 0; | 247 return 0; |
248 } | 248 } |
249 | 249 |
250 #else // !OS_MACOSX | 250 #else // !OS_MACOSX |
251 | 251 |
252 #include <CoreFoundation/CoreFoundation.h> | 252 #include <CoreFoundation/CoreFoundation.h> |
253 | 253 |
254 #include "base/files/file_path.h" | 254 #include "base/files/file_path.h" |
255 #include "base/files/file_util.h" | 255 #include "base/files/file_util.h" |
(...skipping 20 matching lines...) Expand all Loading... |
276 "Test", | 276 "Test", |
277 &bundle_path_, | 277 &bundle_path_, |
278 &executable_path_)); | 278 &executable_path_)); |
279 mock_launchd_.reset(new MockLaunchd(executable_path_, &loop_, | 279 mock_launchd_.reset(new MockLaunchd(executable_path_, &loop_, |
280 false, false)); | 280 false, false)); |
281 scoped_launchd_instance_.reset( | 281 scoped_launchd_instance_.reset( |
282 new Launchd::ScopedInstance(mock_launchd_.get())); | 282 new Launchd::ScopedInstance(mock_launchd_.get())); |
283 ASSERT_TRUE(service_process_state_.Initialize()); | 283 ASSERT_TRUE(service_process_state_.Initialize()); |
284 ASSERT_TRUE(service_process_state_.SignalReady( | 284 ASSERT_TRUE(service_process_state_.SignalReady( |
285 io_thread_.task_runner().get(), base::Closure())); | 285 io_thread_.task_runner().get(), base::Closure())); |
286 loop_.task_runner()->PostDelayedTask( | 286 loop_.task_runner()->PostDelayedTask(FROM_HERE, |
287 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | 287 run_loop_.QuitWhenIdleClosure(), |
288 TestTimeouts::action_max_timeout()); | 288 TestTimeouts::action_max_timeout()); |
289 } | 289 } |
290 | 290 |
291 const MockLaunchd* mock_launchd() const { return mock_launchd_.get(); } | 291 const MockLaunchd* mock_launchd() const { return mock_launchd_.get(); } |
292 const base::FilePath& executable_path() const { return executable_path_; } | 292 const base::FilePath& executable_path() const { return executable_path_; } |
293 const base::FilePath& bundle_path() const { return bundle_path_; } | 293 const base::FilePath& bundle_path() const { return bundle_path_; } |
294 const base::FilePath& GetTempDirPath() const { return temp_dir_.path(); } | 294 const base::FilePath& GetTempDirPath() const { return temp_dir_.path(); } |
295 | 295 |
296 base::SingleThreadTaskRunner* GetIOTaskRunner() { | 296 base::SingleThreadTaskRunner* GetIOTaskRunner() { |
297 return io_thread_.task_runner().get(); | 297 return io_thread_.task_runner().get(); |
298 } | 298 } |
299 void Run() { loop_.Run(); } | 299 void Run() { run_loop_.Run(); } |
300 | 300 |
301 private: | 301 private: |
302 base::ScopedTempDir temp_dir_; | 302 base::ScopedTempDir temp_dir_; |
303 base::MessageLoopForUI loop_; | 303 base::MessageLoopForUI loop_; |
| 304 base::RunLoop run_loop_; |
304 base::Thread io_thread_; | 305 base::Thread io_thread_; |
305 base::FilePath executable_path_, bundle_path_; | 306 base::FilePath executable_path_, bundle_path_; |
306 std::unique_ptr<MockLaunchd> mock_launchd_; | 307 std::unique_ptr<MockLaunchd> mock_launchd_; |
307 std::unique_ptr<Launchd::ScopedInstance> scoped_launchd_instance_; | 308 std::unique_ptr<Launchd::ScopedInstance> scoped_launchd_instance_; |
308 ServiceProcessState service_process_state_; | 309 ServiceProcessState service_process_state_; |
309 }; | 310 }; |
310 | 311 |
311 void DeleteFunc(const base::FilePath& file) { | 312 void DeleteFunc(const base::FilePath& file) { |
312 EXPECT_TRUE(base::DeleteFile(file, true)); | 313 EXPECT_TRUE(base::DeleteFile(file, true)); |
313 } | 314 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 TEST_F(ServiceProcessStateFileManipulationTest, ChangeAttr) { | 416 TEST_F(ServiceProcessStateFileManipulationTest, ChangeAttr) { |
416 ScopedAttributesRestorer restorer(bundle_path(), 0777); | 417 ScopedAttributesRestorer restorer(bundle_path(), 0777); |
417 GetIOTaskRunner()->PostTask(FROM_HERE, | 418 GetIOTaskRunner()->PostTask(FROM_HERE, |
418 base::Bind(&ChangeAttr, bundle_path(), 0222)); | 419 base::Bind(&ChangeAttr, bundle_path(), 0222)); |
419 Run(); | 420 Run(); |
420 ASSERT_TRUE(mock_launchd()->remove_called()); | 421 ASSERT_TRUE(mock_launchd()->remove_called()); |
421 ASSERT_TRUE(mock_launchd()->delete_called()); | 422 ASSERT_TRUE(mock_launchd()->delete_called()); |
422 } | 423 } |
423 | 424 |
424 #endif // !OS_MACOSX | 425 #endif // !OS_MACOSX |
OLD | NEW |