Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / application / lib / application_test_base.cc
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/environment/logging.h"
11 #include "mojo/public/cpp/system/message_pipe.h"
12
13 namespace mojo {
14 namespace test {
15
16 namespace {
17
18 // This shell handle is shared by multiple test application instances.
19 MessagePipeHandle g_shell_handle;
20
21 }  // namespace
22
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();
28 }
29
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();
34 }
35
36 ApplicationTestBase::ApplicationTestBase(Array<String> args)
37     : args_(args.Pass()), application_impl_(nullptr) {
38 }
39
40 ApplicationTestBase::~ApplicationTestBase() {
41 }
42
43 void ApplicationTestBase::SetUp() {
44   // A run loop is needed for ApplicationImpl initialization and communication.
45   Environment::InstantiateDefaultRunLoop();
46
47   // New applications are constructed for each test to avoid persisting state.
48   application_impl_ = new ApplicationImpl(GetApplicationDelegate(),
49                                           PassShellHandle());
50
51   // Fake application initialization with the given command line arguments.
52   application_impl_->Initialize(args_.Clone());
53 }
54
55 void ApplicationTestBase::TearDown() {
56   SetShellHandle(application_impl_->UnbindShell());
57   delete application_impl_;
58   Environment::DestroyDefaultRunLoop();
59 }
60
61 }  // namespace test
62 }  // namespace mojo