Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/c/system/main.h" | |
| 8 #include "mojo/public/cpp/application/application_delegate.h" | 7 #include "mojo/public/cpp/application/application_delegate.h" |
| 9 #include "mojo/public/cpp/application/application_impl.h" | 8 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "mojo/public/cpp/environment/logging.h" | 9 #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 #include "mojo/public/cpp/utility/lib/thread_local.h" | 11 #include "mojo/public/cpp/utility/lib/thread_local.h" |
| 13 #include "mojo/public/cpp/utility/run_loop.h" | 12 #include "mojo/public/cpp/utility/run_loop.h" |
| 14 | 13 |
| 15 namespace mojo { | 14 namespace mojo { |
| 16 namespace test { | 15 namespace test { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 // This global shell handle is needed for repeated use by test applications. | 19 // This shell handle is shared by multiple test application instances. |
| 21 MessagePipeHandle test_shell_handle; | 20 MessagePipeHandle test_shell_handle; |
| 22 | 21 |
| 23 // TODO(msw): Support base::MessageLoop environments. | 22 // TODO(msw): Support base::MessageLoop environments. |
| 24 internal::ThreadLocalPointer<RunLoop> test_run_loop; | 23 internal::ThreadLocalPointer<RunLoop> test_run_loop; |
| 25 | 24 |
| 26 } // namespace | 25 } // namespace |
| 27 | 26 |
| 27 ScopedMessagePipeHandle GetShellHandleForTest() { | |
|
viettrungluu
2014/10/28 23:40:19
Maybe call this "PassShellHandle()", to indicate t
msw
2014/10/29 00:12:38
Done.
| |
| 28 MOJO_CHECK(test_shell_handle.is_valid()); | |
| 29 return MakeScopedHandle(test_shell_handle); | |
|
viettrungluu
2014/10/28 23:40:20
This is wrong: Either you should return a (non-own
msw
2014/10/29 00:12:38
Done; went with the latter.
| |
| 30 } | |
| 31 | |
| 32 void SetShellHandleForTest(ScopedMessagePipeHandle handle) { | |
|
viettrungluu
2014/10/28 23:40:20
...
msw
2014/10/29 00:12:38
Renamed to SetShellHandle, hope that's what you me
| |
| 33 MOJO_CHECK(handle.is_valid()); | |
| 34 test_shell_handle = handle.release(); | |
| 35 } | |
| 36 | |
| 28 ApplicationTestBase::ApplicationTestBase(Array<String> args) | 37 ApplicationTestBase::ApplicationTestBase(Array<String> args) |
| 29 : args_(args.Pass()), application_impl_(nullptr) { | 38 : args_(args.Pass()), application_impl_(nullptr) { |
| 30 } | 39 } |
| 31 | 40 |
| 32 ApplicationTestBase::~ApplicationTestBase() { | 41 ApplicationTestBase::~ApplicationTestBase() { |
| 33 } | 42 } |
| 34 | 43 |
| 35 void ApplicationTestBase::SetUp() { | 44 void ApplicationTestBase::SetUp() { |
| 36 // A run loop is needed for ApplicationImpl initialization and communication. | 45 // A run loop is needed for ApplicationImpl initialization and communication. |
| 37 test_run_loop.Set(new RunLoop()); | 46 test_run_loop.Set(new RunLoop()); |
| 38 | 47 |
| 39 // New applications are constructed for each test to avoid persisting state. | 48 // New applications are constructed for each test to avoid persisting state. |
| 40 MOJO_CHECK(test_shell_handle.is_valid()); | |
| 41 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), | 49 application_impl_ = new ApplicationImpl(GetApplicationDelegate(), |
| 42 MakeScopedHandle(test_shell_handle)); | 50 GetShellHandleForTest()); |
| 43 | 51 |
| 44 // Fake application initialization with the given command line arguments. | 52 // Fake application initialization with the given command line arguments. |
| 45 application_impl_->Initialize(args_.Clone()); | 53 application_impl_->Initialize(args_.Clone()); |
| 46 } | 54 } |
| 47 | 55 |
| 48 void ApplicationTestBase::TearDown() { | 56 void ApplicationTestBase::TearDown() { |
| 49 test_shell_handle = application_impl_->UnbindShell().release(); | 57 SetShellHandleForTest(application_impl_->UnbindShell()); |
| 50 delete application_impl_; | 58 delete application_impl_; |
| 51 delete test_run_loop.Get(); | 59 delete test_run_loop.Get(); |
| 52 test_run_loop.Set(nullptr); | 60 test_run_loop.Set(nullptr); |
| 53 } | 61 } |
| 54 | 62 |
| 55 } // namespace test | 63 } // namespace test |
| 56 } // namespace mojo | 64 } // namespace mojo |
| 57 | |
| 58 // TODO(msw): Split this into an application_test_main.cc. | |
| 59 MojoResult MojoMain(MojoHandle shell_handle) { | |
| 60 mojo::Environment environment; | |
| 61 | |
| 62 { | |
| 63 // This RunLoop is used for init, and then destroyed before running tests. | |
| 64 mojo::RunLoop run_loop; | |
| 65 | |
| 66 // Construct an ApplicationImpl just for the GTEST commandline arguments. | |
| 67 // GTEST command line arguments are supported amid application arguments: | |
| 68 // $ mojo_shell 'mojo:example_apptest arg1 --gtest_filter=foo arg2' | |
| 69 mojo::ApplicationDelegate dummy_application_delegate; | |
| 70 mojo::ApplicationImpl app(&dummy_application_delegate, shell_handle); | |
| 71 MOJO_CHECK(app.WaitForInitialize()); | |
| 72 | |
| 73 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL. | |
| 74 // It also removes GTEST arguments from |argv| and updates the |argc| count. | |
| 75 // TODO(msw): Provide tests access to these actual command line arguments. | |
| 76 const std::vector<std::string>& args = app.args(); | |
| 77 MOJO_CHECK(args.size() < | |
| 78 static_cast<size_t>(std::numeric_limits<int>::max())); | |
| 79 int argc = static_cast<int>(args.size()); | |
| 80 std::vector<const char*> argv(argc + 1); | |
| 81 for (int i = 0; i < argc; ++i) | |
| 82 argv[i] = args[i].c_str(); | |
| 83 argv[argc] = nullptr; | |
| 84 testing::InitGoogleTest(&argc, const_cast<char**>(&(argv[0]))); | |
| 85 mojo::test::test_shell_handle = app.UnbindShell().release(); | |
| 86 } | |
| 87 | |
| 88 int result = RUN_ALL_TESTS(); | |
| 89 | |
| 90 MojoResult close_result = MojoClose(mojo::test::test_shell_handle.value()); | |
| 91 MOJO_CHECK(close_result == MOJO_RESULT_OK); | |
| 92 | |
| 93 return (result == 0) ? MOJO_RESULT_OK : MOJO_RESULT_UNKNOWN; | |
| 94 } | |
| OLD | NEW |