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.
5 #include "mojo/public/cpp/application/application_test_base.h"
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/environment/logging.h"
11 #include "mojo/public/cpp/system/message_pipe.h"
18 // This shell handle is shared by multiple test application instances.
19 MessagePipeHandle g_shell_handle;
23 ScopedMessagePipeHandle PassShellHandle() {
24 MOJO_CHECK(g_shell_handle.is_valid());
25 ScopedMessagePipeHandle scoped_handle(g_shell_handle);
26 g_shell_handle = MessagePipeHandle();
27 return scoped_handle.Pass();
30 void SetShellHandle(ScopedMessagePipeHandle handle) {
31 MOJO_CHECK(handle.is_valid());
32 MOJO_CHECK(!g_shell_handle.is_valid());
33 g_shell_handle = handle.release();
36 ApplicationTestBase::ApplicationTestBase(Array<String> args)
37 : args_(args.Pass()), application_impl_(nullptr) {
40 ApplicationTestBase::~ApplicationTestBase() {
43 void ApplicationTestBase::SetUp() {
44 // A run loop is needed for ApplicationImpl initialization and communication.
45 Environment::InstantiateDefaultRunLoop();
47 // New applications are constructed for each test to avoid persisting state.
48 application_impl_ = new ApplicationImpl(GetApplicationDelegate(),
51 // Fake application initialization with the given command line arguments.
52 application_impl_->Initialize(args_.Clone());
55 void ApplicationTestBase::TearDown() {
56 SetShellHandle(application_impl_->UnbindShell());
57 delete application_impl_;
58 Environment::DestroyDefaultRunLoop();