OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/shell/shell_test_helper.h" | 5 #include "mojo/shell/shell_test_helper.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "mojo/shell/context.h" | 10 #include "mojo/shell/context.h" |
11 #include "mojo/shell/init.h" | 11 #include "mojo/shell/init.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 namespace shell { | 14 namespace shell { |
15 | 15 |
16 // State used on the background thread. Be careful, this is created on the main | 16 // State used on the background thread. Be careful, this is created on the main |
17 // thread than passed to the shell thread. Destruction happens on the shell | 17 // thread than passed to the shell thread. Destruction happens on the shell |
18 // thread. | 18 // thread. |
19 struct ShellTestHelper::State { | 19 struct ShellTestHelper::State { |
20 scoped_ptr<Context> context; | 20 scoped_ptr<Context> context; |
21 scoped_ptr<ServiceManager::TestAPI> test_api; | 21 scoped_ptr<ServiceManager::TestAPI> test_api; |
22 ScopedMessagePipeHandle service_provider_handle; | 22 ScopedMessagePipeHandle shell_handle; |
23 }; | 23 }; |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 void StartShellOnShellThread(ShellTestHelper::State* state) { | 27 void StartShellOnShellThread(ShellTestHelper::State* state) { |
28 state->context.reset(new Context); | 28 state->context.reset(new Context); |
29 state->test_api.reset( | 29 state->test_api.reset( |
30 new ServiceManager::TestAPI(state->context->service_manager())); | 30 new ServiceManager::TestAPI(state->context->service_manager())); |
31 state->service_provider_handle = state->test_api->GetServiceProviderHandle(); | 31 state->shell_handle = state->test_api->GetShellHandle(); |
32 } | 32 } |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 class ShellTestHelper::TestServiceProvider : public ServiceProvider { | 36 class ShellTestHelper::TestShellClient : public ShellClient { |
37 public: | 37 public: |
38 TestServiceProvider() {} | 38 TestShellClient() {} |
39 virtual ~TestServiceProvider() {} | 39 virtual ~TestShellClient() {} |
40 | 40 |
41 // ServiceProvider: | 41 // ShellClient: |
42 virtual void ConnectToService( | 42 virtual void AcceptConnection( |
43 const mojo::String& url, | 43 const mojo::String& url, |
44 ScopedMessagePipeHandle client_handle) OVERRIDE { | 44 ScopedMessagePipeHandle client_handle) OVERRIDE { |
45 } | 45 } |
46 | 46 |
47 private: | 47 private: |
48 DISALLOW_COPY_AND_ASSIGN(TestServiceProvider); | 48 DISALLOW_COPY_AND_ASSIGN(TestShellClient); |
49 }; | 49 }; |
50 | 50 |
51 ShellTestHelper::ShellTestHelper() | 51 ShellTestHelper::ShellTestHelper() |
52 : service_provider_thread_("shell_test_helper"), | 52 : shell_thread_("shell_test_helper"), |
53 state_(NULL) { | 53 state_(NULL) { |
54 base::CommandLine::Init(0, NULL); | 54 base::CommandLine::Init(0, NULL); |
55 mojo::shell::InitializeLogging(); | 55 mojo::shell::InitializeLogging(); |
56 } | 56 } |
57 | 57 |
58 ShellTestHelper::~ShellTestHelper() { | 58 ShellTestHelper::~ShellTestHelper() { |
59 if (state_) { | 59 if (state_) { |
60 // |state_| contains data created on the background thread. Destroy it | 60 // |state_| contains data created on the background thread. Destroy it |
61 // there so that there aren't any race conditions. | 61 // there so that there aren't any race conditions. |
62 service_provider_thread_.message_loop()->DeleteSoon(FROM_HERE, state_); | 62 shell_thread_.message_loop()->DeleteSoon(FROM_HERE, state_); |
63 state_ = NULL; | 63 state_ = NULL; |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 void ShellTestHelper::Init() { | 67 void ShellTestHelper::Init() { |
68 DCHECK(!state_); | 68 DCHECK(!state_); |
69 state_ = new State; | 69 state_ = new State; |
70 service_provider_thread_.Start(); | 70 shell_thread_.Start(); |
71 base::MessageLoopProxy* message_loop_proxy = | 71 shell_thread_.message_loop()->message_loop_proxy()->PostTaskAndReply( |
72 service_provider_thread_.message_loop()->message_loop_proxy(); | |
73 message_loop_proxy->PostTaskAndReply( | |
74 FROM_HERE, | 72 FROM_HERE, |
75 base::Bind(&StartShellOnShellThread, state_), | 73 base::Bind(&StartShellOnShellThread, state_), |
76 base::Bind(&ShellTestHelper::OnServiceProviderStarted, | 74 base::Bind(&ShellTestHelper::OnShellStarted, base::Unretained(this))); |
77 base::Unretained(this))); | |
78 run_loop_.reset(new base::RunLoop); | 75 run_loop_.reset(new base::RunLoop); |
79 run_loop_->Run(); | 76 run_loop_->Run(); |
80 } | 77 } |
81 | 78 |
82 void ShellTestHelper::OnServiceProviderStarted() { | 79 void ShellTestHelper::OnShellStarted() { |
83 DCHECK(state_); | 80 DCHECK(state_); |
84 local_service_provider_.reset(new TestServiceProvider); | 81 shell_client_.reset(new TestShellClient); |
85 service_provider_.Bind(state_->service_provider_handle.Pass()); | 82 shell_.Bind(state_->shell_handle.Pass()); |
86 service_provider_.set_client(local_service_provider_.get()); | 83 shell_.set_client(shell_client_.get()); |
87 run_loop_->Quit(); | 84 run_loop_->Quit(); |
88 } | 85 } |
89 | 86 |
90 } // namespace shell | 87 } // namespace shell |
91 } // namespace mojo | 88 } // namespace mojo |
OLD | NEW |