Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: mojo/public/cpp/application/lib/application_test_base.cc

Issue 744973002: Pass command line args to apptests (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebase and address comments Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/environment/logging.h"
11 #include "mojo/public/cpp/system/message_pipe.h" 10 #include "mojo/public/cpp/system/message_pipe.h"
12 11
13 namespace mojo { 12 namespace mojo {
14 namespace test { 13 namespace test {
15 14
16 namespace { 15 namespace {
17 16
18 // This shell handle is shared by multiple test application instances. 17 // This shell handle is shared by multiple test application instances.
19 MessagePipeHandle g_shell_handle; 18 MessagePipeHandle g_shell_handle;
19 // Share the application command-line arguments with multiple application tests.
20 Array<String> g_args;
20 21
21 } // namespace 22 } // namespace
22 23
23 ScopedMessagePipeHandle PassShellHandle() { 24 ScopedMessagePipeHandle PassShellHandle() {
24 MOJO_CHECK(g_shell_handle.is_valid()); 25 MOJO_CHECK(g_shell_handle.is_valid());
25 ScopedMessagePipeHandle scoped_handle(g_shell_handle); 26 ScopedMessagePipeHandle scoped_handle(g_shell_handle);
26 g_shell_handle = MessagePipeHandle(); 27 g_shell_handle = MessagePipeHandle();
27 return scoped_handle.Pass(); 28 return scoped_handle.Pass();
28 } 29 }
29 30
30 void SetShellHandle(ScopedMessagePipeHandle handle) { 31 void SetShellHandle(ScopedMessagePipeHandle handle) {
31 MOJO_CHECK(handle.is_valid()); 32 MOJO_CHECK(handle.is_valid());
32 MOJO_CHECK(!g_shell_handle.is_valid()); 33 MOJO_CHECK(!g_shell_handle.is_valid());
33 g_shell_handle = handle.release(); 34 g_shell_handle = handle.release();
34 } 35 }
35 36
36 ApplicationTestBase::ApplicationTestBase(Array<String> args) 37 const Array<String>& Args() {
37 : args_(args.Pass()), application_impl_(nullptr) { 38 return g_args;
39 }
40
41 void InitializeArgs(int argc, std::vector<const char*> argv) {
42 MOJO_CHECK(g_args.is_null());
43 for (const char* arg : argv) {
44 if (arg)
45 g_args.push_back(arg);
46 }
47 }
48
49 ApplicationTestBase::ApplicationTestBase() : application_impl_(nullptr) {
38 } 50 }
39 51
40 ApplicationTestBase::~ApplicationTestBase() { 52 ApplicationTestBase::~ApplicationTestBase() {
41 } 53 }
42 54
43 void ApplicationTestBase::SetUp() { 55 void ApplicationTestBase::SetUpWithArgs(const Array<String>& args) {
44 // A run loop is needed for ApplicationImpl initialization and communication. 56 // A run loop is needed for ApplicationImpl initialization and communication.
45 Environment::InstantiateDefaultRunLoop(); 57 Environment::InstantiateDefaultRunLoop();
46 58
47 // New applications are constructed for each test to avoid persisting state. 59 // New applications are constructed for each test to avoid persisting state.
48 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), 60 application_impl_ = new ApplicationImpl(GetApplicationDelegate(),
49 PassShellHandle()); 61 PassShellHandle());
50 62
51 // Fake application initialization with the given command line arguments. 63 // Fake application initialization with the given command line arguments.
52 application_impl_->Initialize(args_.Clone()); 64 application_impl_->Initialize(args.Clone());
65 }
66
67 void ApplicationTestBase::SetUp() {
68 SetUpWithArgs(Args());
53 } 69 }
54 70
55 void ApplicationTestBase::TearDown() { 71 void ApplicationTestBase::TearDown() {
56 SetShellHandle(application_impl_->UnbindShell()); 72 SetShellHandle(application_impl_->UnbindShell());
57 delete application_impl_; 73 delete application_impl_;
58 Environment::DestroyDefaultRunLoop(); 74 Environment::DestroyDefaultRunLoop();
59 } 75 }
60 76
61 } // namespace test 77 } // namespace test
62 } // namespace mojo 78 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698