| 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/application/application_test_base_chromium.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "mojo/application/public/cpp/application_impl.h" | |
| 10 #include "mojo/application/public/interfaces/application.mojom.h" | |
| 11 #include "mojo/public/cpp/bindings/binding.h" | |
| 12 #include "mojo/public/cpp/environment/environment.h" | |
| 13 #include "mojo/public/cpp/system/message_pipe.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 namespace test { | |
| 17 | |
| 18 namespace { | |
| 19 // Share the application URL with multiple application tests. | |
| 20 String g_url; | |
| 21 | |
| 22 // Application request handle passed from the shell in MojoMain, stored in | |
| 23 // between SetUp()/TearDown() so we can (re-)intialize new ApplicationImpls. | |
| 24 InterfaceRequest<Application> g_application_request; | |
| 25 | |
| 26 // Shell pointer passed in the initial mojo.Application.Initialize() call, | |
| 27 // stored in between initial setup and the first test and between SetUp/TearDown | |
| 28 // calls so we can (re-)initialize new ApplicationImpls. | |
| 29 ShellPtr g_shell; | |
| 30 | |
| 31 class ShellGrabber : public Application { | |
| 32 public: | |
| 33 explicit ShellGrabber(InterfaceRequest<Application> application_request) | |
| 34 : binding_(this, application_request.Pass()) {} | |
| 35 | |
| 36 void WaitForInitialize() { | |
| 37 // Initialize is always the first call made on Application. | |
| 38 MOJO_CHECK(binding_.WaitForIncomingMethodCall()); | |
| 39 } | |
| 40 | |
| 41 private: | |
| 42 // Application implementation. | |
| 43 void Initialize(ShellPtr shell, const mojo::String& url) override { | |
| 44 g_url = url; | |
| 45 g_application_request = binding_.Unbind(); | |
| 46 g_shell = shell.Pass(); | |
| 47 } | |
| 48 | |
| 49 void AcceptConnection(const String& requestor_url, | |
| 50 InterfaceRequest<ServiceProvider> services, | |
| 51 ServiceProviderPtr exposed_services, | |
| 52 const String& url) override { | |
| 53 MOJO_CHECK(false); | |
| 54 } | |
| 55 | |
| 56 void RequestQuit() override { MOJO_CHECK(false); } | |
| 57 | |
| 58 Binding<Application> binding_; | |
| 59 }; | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 MojoResult RunAllTests(MojoHandle application_request_handle) { | |
| 64 { | |
| 65 // This loop is used for init, and then destroyed before running tests. | |
| 66 Environment::InstantiateDefaultRunLoop(); | |
| 67 | |
| 68 // Grab the shell handle. | |
| 69 ShellGrabber grabber( | |
| 70 MakeRequest<Application>(MakeScopedHandle( | |
| 71 MessagePipeHandle(application_request_handle)))); | |
| 72 grabber.WaitForInitialize(); | |
| 73 MOJO_CHECK(g_shell); | |
| 74 MOJO_CHECK(g_application_request.is_pending()); | |
| 75 | |
| 76 int argc = 0; | |
| 77 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
| 78 const char** argv = new const char* [cmd_line->argv().size()]; | |
| 79 #if defined(OS_WIN) | |
| 80 std::vector<std::string> local_strings; | |
| 81 #endif | |
| 82 for (auto& arg : cmd_line->argv()) { | |
| 83 #if defined(OS_WIN) | |
| 84 local_strings.push_back(base::WideToUTF8(arg)); | |
| 85 argv[argc++] = local_strings.back().c_str(); | |
| 86 #else | |
| 87 argv[argc++] = arg.c_str(); | |
| 88 #endif | |
| 89 } | |
| 90 | |
| 91 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); | |
| 92 | |
| 93 Environment::DestroyDefaultRunLoop(); | |
| 94 } | |
| 95 | |
| 96 int result = RUN_ALL_TESTS(); | |
| 97 | |
| 98 // Shut down our message pipes before exiting. | |
| 99 (void)g_application_request.PassMessagePipe(); | |
| 100 (void)g_shell.PassInterface(); | |
| 101 | |
| 102 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN; | |
| 103 } | |
| 104 | |
| 105 ApplicationTestBase::ApplicationTestBase() : application_impl_(nullptr) { | |
| 106 } | |
| 107 | |
| 108 ApplicationTestBase::~ApplicationTestBase() { | |
| 109 } | |
| 110 | |
| 111 ApplicationDelegate* ApplicationTestBase::GetApplicationDelegate() { | |
| 112 return &default_application_delegate_; | |
| 113 } | |
| 114 | |
| 115 void ApplicationTestBase::SetUp() { | |
| 116 // A run loop is recommended for ApplicationImpl initialization and | |
| 117 // communication. | |
| 118 if (ShouldCreateDefaultRunLoop()) | |
| 119 Environment::InstantiateDefaultRunLoop(); | |
| 120 | |
| 121 MOJO_CHECK(g_application_request.is_pending()); | |
| 122 MOJO_CHECK(g_shell); | |
| 123 | |
| 124 // New applications are constructed for each test to avoid persisting state. | |
| 125 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), | |
| 126 g_application_request.Pass()); | |
| 127 | |
| 128 // Fake application initialization. | |
| 129 application_impl_->Initialize(g_shell.Pass(), g_url); | |
| 130 } | |
| 131 | |
| 132 void ApplicationTestBase::TearDown() { | |
| 133 MOJO_CHECK(!g_application_request.is_pending()); | |
| 134 MOJO_CHECK(!g_shell); | |
| 135 | |
| 136 application_impl_->UnbindConnections(&g_application_request, &g_shell); | |
| 137 delete application_impl_; | |
| 138 if (ShouldCreateDefaultRunLoop()) | |
| 139 Environment::DestroyDefaultRunLoop(); | |
| 140 } | |
| 141 | |
| 142 bool ApplicationTestBase::ShouldCreateDefaultRunLoop() { | |
| 143 return true; | |
| 144 } | |
| 145 | |
| 146 } // namespace test | |
| 147 } // namespace mojo | |
| OLD | NEW |