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/public/cpp/application/application_test_base.h" | 5 #include "mojo/public/cpp/application/application_test_base.h" |
6 | 6 |
7 #include "mojo/public/cpp/application/application_delegate.h" | 7 #include "mojo/public/cpp/application/application_delegate.h" |
8 #include "mojo/public/cpp/application/application_impl.h" | 8 #include "mojo/public/cpp/application/application_impl.h" |
9 #include "mojo/public/cpp/environment/environment.h" | 9 #include "mojo/public/cpp/environment/environment.h" |
10 #include "mojo/public/cpp/environment/logging.h" | |
11 #include "mojo/public/cpp/system/message_pipe.h" | 10 #include "mojo/public/cpp/system/message_pipe.h" |
12 | 11 |
13 namespace mojo { | 12 namespace mojo { |
14 namespace test { | 13 namespace test { |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 // This shell handle is shared by multiple test application instances. | 17 // This shell handle is shared by multiple test application instances. |
19 MessagePipeHandle g_shell_handle; | 18 MessagePipeHandle g_shell_handle; |
19 // Share the application command-line arguments with multiple application tests. | |
20 Array<String> g_args; | |
20 | 21 |
21 } // namespace | 22 } // namespace |
22 | 23 |
23 ScopedMessagePipeHandle PassShellHandle() { | 24 ScopedMessagePipeHandle PassShellHandle() { |
24 MOJO_CHECK(g_shell_handle.is_valid()); | 25 MOJO_CHECK(g_shell_handle.is_valid()); |
25 ScopedMessagePipeHandle scoped_handle(g_shell_handle); | 26 ScopedMessagePipeHandle scoped_handle(g_shell_handle); |
26 g_shell_handle = MessagePipeHandle(); | 27 g_shell_handle = MessagePipeHandle(); |
27 return scoped_handle.Pass(); | 28 return scoped_handle.Pass(); |
28 } | 29 } |
29 | 30 |
30 void SetShellHandle(ScopedMessagePipeHandle handle) { | 31 void SetShellHandle(ScopedMessagePipeHandle handle) { |
31 MOJO_CHECK(handle.is_valid()); | 32 MOJO_CHECK(handle.is_valid()); |
32 MOJO_CHECK(!g_shell_handle.is_valid()); | 33 MOJO_CHECK(!g_shell_handle.is_valid()); |
33 g_shell_handle = handle.release(); | 34 g_shell_handle = handle.release(); |
34 } | 35 } |
35 | 36 |
36 ApplicationTestBase::ApplicationTestBase(Array<String> args) | 37 const Array<String>& Args() { |
37 : args_(args.Pass()), application_impl_(nullptr) { | 38 return g_args; |
39 } | |
40 | |
41 void InitializeArgs(int argc, std::vector<const char*> argv) { | |
42 MOJO_CHECK(g_args.is_null()); | |
43 for (const char* arg : argv) { | |
44 if (arg) | |
45 g_args.push_back(arg); | |
46 } | |
47 } | |
48 | |
49 ApplicationTestBase::ApplicationTestBase() | |
50 : args_(mojo::test::Args().Clone()), application_impl_(nullptr) { | |
msw
2014/11/22 18:23:38
Namespace not needed.
Chris Masone
2014/11/22 20:14:44
Done.
| |
38 } | 51 } |
39 | 52 |
40 ApplicationTestBase::~ApplicationTestBase() { | 53 ApplicationTestBase::~ApplicationTestBase() { |
41 } | 54 } |
42 | 55 |
56 void ApplicationTestBase::SetUpWithArgs(Array<String> args) { | |
57 args_.Swap(&args); | |
58 SetUp(); | |
msw
2014/11/22 18:23:38
Flip this around, have SetUp call SetUpWithArgs(Ar
Chris Masone
2014/11/22 20:14:44
Done.
| |
59 } | |
60 | |
43 void ApplicationTestBase::SetUp() { | 61 void ApplicationTestBase::SetUp() { |
44 // A run loop is needed for ApplicationImpl initialization and communication. | 62 // A run loop is needed for ApplicationImpl initialization and communication. |
45 Environment::InstantiateDefaultRunLoop(); | 63 Environment::InstantiateDefaultRunLoop(); |
46 | 64 |
47 // New applications are constructed for each test to avoid persisting state. | 65 // New applications are constructed for each test to avoid persisting state. |
48 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), | 66 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), |
49 PassShellHandle()); | 67 PassShellHandle()); |
50 | 68 |
51 // Fake application initialization with the given command line arguments. | 69 // Fake application initialization with the given command line arguments. |
52 application_impl_->Initialize(args_.Clone()); | 70 application_impl_->Initialize(args_.Clone()); |
53 } | 71 } |
54 | 72 |
55 void ApplicationTestBase::TearDown() { | 73 void ApplicationTestBase::TearDown() { |
56 SetShellHandle(application_impl_->UnbindShell()); | 74 SetShellHandle(application_impl_->UnbindShell()); |
57 delete application_impl_; | 75 delete application_impl_; |
58 Environment::DestroyDefaultRunLoop(); | 76 Environment::DestroyDefaultRunLoop(); |
59 } | 77 } |
60 | 78 |
61 } // namespace test | 79 } // namespace test |
62 } // namespace mojo | 80 } // namespace mojo |
OLD | NEW |