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