Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / mojo / application / application_test_main_chromium.cc
blobf5b8bfb17661fb618d34e8c3f354fca8afe64228
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 "base/at_exit.h"
6 #include "base/logging.h"
7 #include "base/message_loop/message_loop.h"
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"
12 #include "mojo/public/cpp/system/message_pipe.h"
14 MojoResult MojoMain(MojoHandle shell_handle) {
15 // An AtExitManager instance is needed to construct message loops.
16 base::AtExitManager at_exit;
19 // This loop is used for init, and then destroyed before running tests.
20 base::MessageLoop message_loop;
22 // Construct an ApplicationImpl just for the GTEST commandline arguments.
23 // GTEST command line arguments are supported amid application arguments:
24 // $ mojo_shell 'mojo:example_apptests arg1 --gtest_filter=foo arg2'
25 mojo::ApplicationDelegate dummy_application_delegate;
26 mojo::ApplicationImpl app(&dummy_application_delegate, shell_handle);
27 CHECK(app.WaitForInitialize());
29 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL.
30 // It also removes GTEST arguments from |argv| and updates the |argc| count.
31 // TODO(msw): Provide tests access to these actual command line arguments.
32 const std::vector<std::string>& args = app.args();
33 CHECK_LT(args.size(), static_cast<size_t>(std::numeric_limits<int>::max()));
34 int argc = static_cast<int>(args.size());
35 std::vector<const char*> argv(argc + 1);
36 for (int i = 0; i < argc; ++i)
37 argv[i] = args[i].c_str();
38 argv[argc] = nullptr;
40 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0])));
41 mojo::test::SetShellHandle(app.UnbindShell());
44 int result = RUN_ALL_TESTS();
46 shell_handle = mojo::test::PassShellHandle().release().value();
47 MojoResult close_result = MojoClose(shell_handle);
48 CHECK_EQ(close_result, MOJO_RESULT_OK);
50 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN;