Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 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 "base/at_exit.h" |
Michael Wasserman | 6d1f41d | 2014-11-12 09:44:50 -0800 | [diff] [blame] | 6 | #include "base/logging.h" |
| 7 | #include "base/message_loop/message_loop.h" |
Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 8 | #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 Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 12 | #include "mojo/public/cpp/system/message_pipe.h" |
| 13 | |
| 14 | MojoResult MojoMain(MojoHandle shell_handle) { |
Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 15 | // An AtExitManager instance is needed to construct message loops. |
| 16 | base::AtExitManager at_exit; |
Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 17 | |
| 18 | { |
Michael Wasserman | 6d1f41d | 2014-11-12 09:44:50 -0800 | [diff] [blame] | 19 | // This loop is used for init, and then destroyed before running tests. |
| 20 | base::MessageLoop message_loop; |
Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 21 | |
| 22 | // Construct an ApplicationImpl just for the GTEST commandline arguments. |
| 23 | // GTEST command line arguments are supported amid application arguments: |
Chris Masone | f5d7806 | 2014-11-24 13:25:55 -0800 | [diff] [blame] | 24 | // $ mojo_shell mojo:example_apptests |
| 25 | // --args-for='mojo:example_apptests arg1 --gtest_filter=foo arg2' |
Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 26 | mojo::ApplicationDelegate dummy_application_delegate; |
| 27 | mojo::ApplicationImpl app(&dummy_application_delegate, shell_handle); |
Michael Wasserman | 6d1f41d | 2014-11-12 09:44:50 -0800 | [diff] [blame] | 28 | CHECK(app.WaitForInitialize()); |
Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 29 | |
| 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 Wasserman | 6d1f41d | 2014-11-12 09:44:50 -0800 | [diff] [blame] | 34 | CHECK_LT(args.size(), static_cast<size_t>(std::numeric_limits<int>::max())); |
Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 35 | 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 Masone | f5d7806 | 2014-11-24 13:25:55 -0800 | [diff] [blame] | 43 | mojo::test::InitializeArgs(argc, argv); |
Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | int result = RUN_ALL_TESTS(); |
| 47 | |
| 48 | shell_handle = mojo::test::PassShellHandle().release().value(); |
| 49 | MojoResult close_result = MojoClose(shell_handle); |
Michael Wasserman | 6d1f41d | 2014-11-12 09:44:50 -0800 | [diff] [blame] | 50 | CHECK_EQ(close_result, MOJO_RESULT_OK); |
Michael Wasserman | 808cd0c | 2014-10-31 13:42:56 -0700 | [diff] [blame] | 51 | |
| 52 | return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN; |
| 53 | } |