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/system/message_pipe.h" | 10 #include "mojo/public/cpp/system/message_pipe.h" |
11 | 11 |
12 namespace mojo { | 12 namespace mojo { |
13 namespace test { | 13 namespace test { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // This shell handle is shared by multiple test application instances. | 17 // This shell handle is shared by multiple test application instances. |
18 MessagePipeHandle g_shell_handle; | 18 MessagePipeHandle g_shell_handle; |
19 // Share the application command-line arguments with multiple application tests. | 19 // Share the application command-line arguments with multiple application tests. |
20 Array<String> g_args; | 20 Array<String> g_args; |
21 | 21 |
22 } // namespace | |
23 | |
24 ScopedMessagePipeHandle PassShellHandle() { | 22 ScopedMessagePipeHandle PassShellHandle() { |
25 MOJO_CHECK(g_shell_handle.is_valid()); | 23 MOJO_CHECK(g_shell_handle.is_valid()); |
26 ScopedMessagePipeHandle scoped_handle(g_shell_handle); | 24 ScopedMessagePipeHandle scoped_handle(g_shell_handle); |
27 g_shell_handle = MessagePipeHandle(); | 25 g_shell_handle = MessagePipeHandle(); |
28 return scoped_handle.Pass(); | 26 return scoped_handle.Pass(); |
29 } | 27 } |
30 | 28 |
31 void SetShellHandle(ScopedMessagePipeHandle handle) { | 29 void SetShellHandle(ScopedMessagePipeHandle handle) { |
32 MOJO_CHECK(handle.is_valid()); | 30 MOJO_CHECK(handle.is_valid()); |
33 MOJO_CHECK(!g_shell_handle.is_valid()); | 31 MOJO_CHECK(!g_shell_handle.is_valid()); |
34 g_shell_handle = handle.release(); | 32 g_shell_handle = handle.release(); |
35 } | 33 } |
36 | 34 |
37 const Array<String>& Args() { | |
38 return g_args; | |
39 } | |
40 | |
41 void InitializeArgs(int argc, std::vector<const char*> argv) { | 35 void InitializeArgs(int argc, std::vector<const char*> argv) { |
42 MOJO_CHECK(g_args.is_null()); | 36 MOJO_CHECK(g_args.is_null()); |
43 for (const char* arg : argv) { | 37 for (const char* arg : argv) { |
44 if (arg) | 38 if (arg) |
45 g_args.push_back(arg); | 39 g_args.push_back(arg); |
46 } | 40 } |
47 } | 41 } |
48 | 42 |
| 43 } // namespace |
| 44 |
| 45 const Array<String>& Args() { |
| 46 return g_args; |
| 47 } |
| 48 |
| 49 MojoResult RunAllTests(MojoHandle shell_handle) { |
| 50 { |
| 51 // This loop is used for init, and then destroyed before running tests. |
| 52 Environment::InstantiateDefaultRunLoop(); |
| 53 |
| 54 // Construct an ApplicationImpl just for the GTEST commandline arguments. |
| 55 // GTEST command line arguments are supported amid application arguments: |
| 56 // $ mojo_shell mojo:example_apptests |
| 57 // --args-for='mojo:example_apptests arg1 --gtest_filter=foo arg2' |
| 58 mojo::ApplicationDelegate dummy_application_delegate; |
| 59 mojo::ApplicationImpl app(&dummy_application_delegate, shell_handle); |
| 60 MOJO_CHECK(app.WaitForInitialize()); |
| 61 |
| 62 // InitGoogleTest expects (argc + 1) elements, including a terminating null. |
| 63 // It also removes GTEST arguments from |argv| and updates the |argc| count. |
| 64 const std::vector<std::string>& args = app.args(); |
| 65 MOJO_CHECK(args.size() < |
| 66 static_cast<size_t>(std::numeric_limits<int>::max())); |
| 67 int argc = static_cast<int>(args.size()); |
| 68 std::vector<const char*> argv(argc + 1); |
| 69 for (int i = 0; i < argc; ++i) |
| 70 argv[i] = args[i].c_str(); |
| 71 argv[argc] = nullptr; |
| 72 |
| 73 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); |
| 74 SetShellHandle(app.UnbindShell()); |
| 75 InitializeArgs(argc, argv); |
| 76 |
| 77 Environment::DestroyDefaultRunLoop(); |
| 78 } |
| 79 |
| 80 int result = RUN_ALL_TESTS(); |
| 81 |
| 82 shell_handle = mojo::test::PassShellHandle().release().value(); |
| 83 MojoResult close_result = MojoClose(shell_handle); |
| 84 MOJO_CHECK(close_result == MOJO_RESULT_OK); |
| 85 |
| 86 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN; |
| 87 } |
| 88 |
49 ApplicationTestBase::ApplicationTestBase() : application_impl_(nullptr) { | 89 ApplicationTestBase::ApplicationTestBase() : application_impl_(nullptr) { |
50 } | 90 } |
51 | 91 |
52 ApplicationTestBase::~ApplicationTestBase() { | 92 ApplicationTestBase::~ApplicationTestBase() { |
53 } | 93 } |
54 | 94 |
55 ApplicationDelegate* ApplicationTestBase::GetApplicationDelegate() { | 95 ApplicationDelegate* ApplicationTestBase::GetApplicationDelegate() { |
56 return &default_application_delegate_; | 96 return &default_application_delegate_; |
57 } | 97 } |
58 | 98 |
(...skipping 21 matching lines...) Expand all Loading... |
80 if (ShouldCreateDefaultRunLoop()) | 120 if (ShouldCreateDefaultRunLoop()) |
81 Environment::DestroyDefaultRunLoop(); | 121 Environment::DestroyDefaultRunLoop(); |
82 } | 122 } |
83 | 123 |
84 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { | 124 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { |
85 return true; | 125 return true; |
86 } | 126 } |
87 | 127 |
88 } // namespace test | 128 } // namespace test |
89 } // namespace mojo | 129 } // namespace mojo |
OLD | NEW |