blob: d9f6ca672a18c5f3b4efa8ea093513425c0d8a45 [file] [log] [blame]
Michael Wasserman808cd0c2014-10-31 13:42:56 -07001// 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 "base/at_exit.h"
Michael Wasserman6d1f41d2014-11-12 09:44:50 -08006#include "base/logging.h"
7#include "base/message_loop/message_loop.h"
Michael Wasserman808cd0c2014-10-31 13:42:56 -07008#include "mojo/public/c/system/main.h"
9#include "mojo/public/cpp/application/application_delegate.h"
10#include "mojo/public/cpp/application/application_impl.h"
11#include "mojo/public/cpp/application/application_test_base.h"
Michael Wasserman808cd0c2014-10-31 13:42:56 -070012#include "mojo/public/cpp/system/message_pipe.h"
13
14MojoResult MojoMain(MojoHandle shell_handle) {
Michael Wasserman808cd0c2014-10-31 13:42:56 -070015 // An AtExitManager instance is needed to construct message loops.
16 base::AtExitManager at_exit;
Michael Wasserman808cd0c2014-10-31 13:42:56 -070017
18 {
Michael Wasserman6d1f41d2014-11-12 09:44:50 -080019 // This loop is used for init, and then destroyed before running tests.
20 base::MessageLoop message_loop;
Michael Wasserman808cd0c2014-10-31 13:42:56 -070021
22 // Construct an ApplicationImpl just for the GTEST commandline arguments.
23 // GTEST command line arguments are supported amid application arguments:
Chris Masonef5d78062014-11-24 13:25:55 -080024 // $ mojo_shell mojo:example_apptests
25 // --args-for='mojo:example_apptests arg1 --gtest_filter=foo arg2'
Michael Wasserman808cd0c2014-10-31 13:42:56 -070026 mojo::ApplicationDelegate dummy_application_delegate;
27 mojo::ApplicationImpl app(&dummy_application_delegate, shell_handle);
Michael Wasserman6d1f41d2014-11-12 09:44:50 -080028 CHECK(app.WaitForInitialize());
Michael Wasserman808cd0c2014-10-31 13:42:56 -070029
30 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL.
31 // It also removes GTEST arguments from |argv| and updates the |argc| count.
32 // TODO(msw): Provide tests access to these actual command line arguments.
33 const std::vector<std::string>& args = app.args();
Michael Wasserman6d1f41d2014-11-12 09:44:50 -080034 CHECK_LT(args.size(), static_cast<size_t>(std::numeric_limits<int>::max()));
Michael Wasserman808cd0c2014-10-31 13:42:56 -070035 int argc = static_cast<int>(args.size());
36 std::vector<const char*> argv(argc + 1);
37 for (int i = 0; i < argc; ++i)
38 argv[i] = args[i].c_str();
39 argv[argc] = nullptr;
40
41 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0])));
42 mojo::test::SetShellHandle(app.UnbindShell());
Chris Masonef5d78062014-11-24 13:25:55 -080043 mojo::test::InitializeArgs(argc, argv);
Michael Wasserman808cd0c2014-10-31 13:42:56 -070044 }
45
46 int result = RUN_ALL_TESTS();
47
48 shell_handle = mojo::test::PassShellHandle().release().value();
49 MojoResult close_result = MojoClose(shell_handle);
Michael Wasserman6d1f41d2014-11-12 09:44:50 -080050 CHECK_EQ(close_result, MOJO_RESULT_OK);
Michael Wasserman808cd0c2014-10-31 13:42:56 -070051
52 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN;
53}