| 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/public/cpp/application/application_test_base.h" | |
| 6 | |
| 7 #include "mojo/public/cpp/application/application_delegate.h" | |
| 8 #include "mojo/public/cpp/application/application_impl.h" | |
| 9 #include "mojo/public/cpp/environment/environment.h" | |
| 10 #include "mojo/public/cpp/system/message_pipe.h" | |
| 11 | |
| 12 namespace mojo { | |
| 13 namespace test { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // This shell handle is shared by multiple test application instances. | |
| 18 MessagePipeHandle g_shell_handle; | |
| 19 // Share the application command-line arguments with multiple application tests. | |
| 20 Array<String> g_args; | |
| 21 | |
| 22 ScopedMessagePipeHandle PassShellHandle() { | |
| 23 MOJO_CHECK(g_shell_handle.is_valid()); | |
| 24 ScopedMessagePipeHandle scoped_handle(g_shell_handle); | |
| 25 g_shell_handle = MessagePipeHandle(); | |
| 26 return scoped_handle.Pass(); | |
| 27 } | |
| 28 | |
| 29 void SetShellHandle(ScopedMessagePipeHandle handle) { | |
| 30 MOJO_CHECK(handle.is_valid()); | |
| 31 MOJO_CHECK(!g_shell_handle.is_valid()); | |
| 32 g_shell_handle = handle.release(); | |
| 33 } | |
| 34 | |
| 35 void InitializeArgs(int argc, std::vector<const char*> argv) { | |
| 36 MOJO_CHECK(g_args.is_null()); | |
| 37 for (const char* arg : argv) { | |
| 38 if (arg) | |
| 39 g_args.push_back(arg); | |
| 40 } | |
| 41 } | |
| 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 | |
| 89 ApplicationTestBase::ApplicationTestBase() : application_impl_(nullptr) { | |
| 90 } | |
| 91 | |
| 92 ApplicationTestBase::~ApplicationTestBase() { | |
| 93 } | |
| 94 | |
| 95 ApplicationDelegate* ApplicationTestBase::GetApplicationDelegate() { | |
| 96 return &default_application_delegate_; | |
| 97 } | |
| 98 | |
| 99 void ApplicationTestBase::SetUpWithArgs(const Array<String>& args) { | |
| 100 // A run loop is recommended for ApplicationImpl initialization and | |
| 101 // communication. | |
| 102 if (ShouldCreateDefaultRunLoop()) | |
| 103 Environment::InstantiateDefaultRunLoop(); | |
| 104 | |
| 105 // New applications are constructed for each test to avoid persisting state. | |
| 106 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), | |
| 107 PassShellHandle()); | |
| 108 | |
| 109 // Fake application initialization with the given command line arguments. | |
| 110 application_impl_->Initialize(args.Clone()); | |
| 111 } | |
| 112 | |
| 113 void ApplicationTestBase::SetUp() { | |
| 114 SetUpWithArgs(Args()); | |
| 115 } | |
| 116 | |
| 117 void ApplicationTestBase::TearDown() { | |
| 118 SetShellHandle(application_impl_->UnbindShell()); | |
| 119 delete application_impl_; | |
| 120 if (ShouldCreateDefaultRunLoop()) | |
| 121 Environment::DestroyDefaultRunLoop(); | |
| 122 } | |
| 123 | |
| 124 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 128 } // namespace test | |
| 129 } // namespace mojo | |
| OLD | NEW |