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 <limits.h> | 5 #include <limits.h> |
| 6 | 6 |
| 7 #include "mojo/examples/apptest/example_client_application.h" | 7 #include "mojo/examples/apptest/example_client_application.h" |
| 8 #include "mojo/examples/apptest/example_client_impl.h" | 8 #include "mojo/examples/apptest/example_client_impl.h" |
| 9 #include "mojo/examples/apptest/example_service.mojom.h" | 9 #include "mojo/examples/apptest/example_service.mojom.h" |
| 10 #include "mojo/public/c/system/main.h" | 10 #include "mojo/public/c/system/main.h" |
| 11 #include "mojo/public/cpp/application/application_impl.h" | 11 #include "mojo/public/cpp/application/application_impl.h" |
| 12 #include "mojo/public/cpp/bindings/array.h" | 12 #include "mojo/public/cpp/bindings/array.h" |
| 13 #include "mojo/public/cpp/bindings/callback.h" | 13 #include "mojo/public/cpp/bindings/callback.h" |
| 14 #include "mojo/public/cpp/bindings/string.h" | 14 #include "mojo/public/cpp/bindings/string.h" |
| 15 #include "mojo/public/cpp/environment/environment.h" | 15 #include "mojo/public/cpp/environment/environment.h" |
| 16 #include "mojo/public/cpp/environment/logging.h" | 16 #include "mojo/public/cpp/environment/logging.h" |
| 17 #include "mojo/public/cpp/system/macros.h" | 17 #include "mojo/public/cpp/system/macros.h" |
| 18 #include "mojo/public/cpp/utility/run_loop.h" | 18 #include "mojo/public/cpp/utility/run_loop.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // TODO(msw): Remove this once we can get ApplicationImpl from TLS. | 23 // A shell handle and command line args used for numerous application instances. |
| 24 mojo::ApplicationImpl* g_application_impl_hack = NULL; | 24 mojo::ScopedMessagePipeHandle g_shell_scoped_message_pipe_handle_hack; |
| 25 mojo::Array<mojo::String> g_args_hack; | |
|
viettrungluu
2014/10/15 20:15:50
I don't think the individual tests actually need t
msw
2014/10/16 00:34:09
Okay, I've removed this for now and added TODOs to
| |
| 25 | 26 |
| 26 } // namespace | 27 } // namespace |
| 27 | 28 |
| 28 namespace mojo { | 29 namespace mojo { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 33 // These GTESTs exemplify application testing executed within mojo_shell. | |
| 34 // GTEST command line arguments are supported amid application arguments: | |
| 35 // mojo_shell 'mojo:mojo_example_apptest arg1 --gtest_filter=foo arg2' | |
| 32 class ExampleApptest : public testing::Test { | 36 class ExampleApptest : public testing::Test { |
|
viettrungluu
2014/10/15 20:15:50
Maybe provide this as a test fixture base class. T
msw
2014/10/16 00:34:09
Done.
| |
| 33 public: | 37 public: |
| 34 ExampleApptest() { | 38 ExampleApptest() : application_impl_(NULL) { |
| 35 g_application_impl_hack->ConnectToService("mojo:mojo_example_service", | 39 // New applications are constructed for each test to avoid persisting state. |
| 36 &example_service_); | 40 application_impl_ = new mojo::ApplicationImpl( |
|
viettrungluu
2014/10/15 20:15:50
You may as well initialize this in the initializer
msw
2014/10/16 00:34:09
I'm not sure that's easily possible. ApplicationIm
| |
| 41 &example_client_application, | |
| 42 g_shell_scoped_message_pipe_handle_hack.Pass()); | |
| 43 // Fake the application initialize call with actual command line arguments. | |
| 44 application_impl_->Initialize(g_args_hack.Clone()); | |
|
viettrungluu
2014/10/15 20:15:50
Arguably, since this is nontrivial work, this shou
msw
2014/10/16 00:34:09
Done.
| |
| 45 application_impl_->ConnectToService("mojo:mojo_example_service", | |
|
viettrungluu
2014/10/15 20:15:50
(And this wouldn't be in the base class, of course
msw
2014/10/16 00:34:09
Acknowledged.
| |
| 46 &example_service_); | |
| 37 example_service_.set_client(&example_client_); | 47 example_service_.set_client(&example_client_); |
| 38 } | 48 } |
| 39 | 49 |
| 40 virtual ~ExampleApptest() override {} | 50 virtual ~ExampleApptest() override { |
| 51 g_shell_scoped_message_pipe_handle_hack = application_impl_->UnbindShell(); | |
| 52 delete application_impl_; | |
| 53 } | |
| 41 | 54 |
| 42 protected: | 55 protected: |
| 43 ExampleServicePtr example_service_; | 56 ExampleServicePtr example_service_; |
| 44 ExampleClientImpl example_client_; | 57 ExampleClientImpl example_client_; |
| 45 | 58 |
| 46 private: | 59 private: |
| 60 mojo::RunLoop loop_; | |
| 61 mojo::ExampleClientApplication example_client_application; | |
| 62 mojo::ApplicationImpl* application_impl_; | |
| 63 | |
| 47 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleApptest); | 64 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleApptest); |
| 48 }; | 65 }; |
| 49 | 66 |
| 50 TEST_F(ExampleApptest, PongClientDirectly) { | 67 TEST_F(ExampleApptest, PongClientDirectly) { |
| 51 // Test very basic standalone ExampleClient functionality. | 68 // Test very basic standalone ExampleClient functionality. |
| 52 ExampleClientImpl example_client; | 69 ExampleClientImpl example_client; |
| 53 EXPECT_EQ(0, example_client.last_pong_value()); | 70 EXPECT_EQ(0, example_client.last_pong_value()); |
| 54 example_client.Pong(1); | 71 example_client.Pong(1); |
| 55 EXPECT_EQ(1, example_client.last_pong_value()); | 72 EXPECT_EQ(1, example_client.last_pong_value()); |
| 56 } | 73 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 79 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); | 96 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); |
| 80 EXPECT_TRUE(was_run); | 97 EXPECT_TRUE(was_run); |
| 81 } | 98 } |
| 82 | 99 |
| 83 } // namespace | 100 } // namespace |
| 84 | 101 |
| 85 } // namespace mojo | 102 } // namespace mojo |
| 86 | 103 |
| 87 MojoResult MojoMain(MojoHandle shell_handle) { | 104 MojoResult MojoMain(MojoHandle shell_handle) { |
| 88 mojo::Environment env; | 105 mojo::Environment env; |
| 89 // TODO(msw): Destroy this ambient RunLoop before running tests. | |
| 90 // Need to CancelWait() / PassMessagePipe() from the ShellPtr? | |
| 91 mojo::RunLoop loop; | |
| 92 mojo::ApplicationDelegate* delegate = new mojo::ExampleClientApplication(); | |
| 93 mojo::ApplicationImpl app(delegate, shell_handle); | |
| 94 g_application_impl_hack = &app; | |
| 95 MOJO_CHECK(app.WaitForInitialize()); | |
| 96 | 106 |
| 97 { | 107 { |
| 108 // This RunLoop is used for init, and then destroyed before running tests. | |
| 109 mojo::RunLoop loop; | |
| 110 | |
| 111 // Construct an ApplicationImpl just for the GTEST commandline arguments. | |
| 112 mojo::ApplicationDelegate dummy_application_delegate; | |
| 113 mojo::ApplicationImpl app(&dummy_application_delegate, shell_handle); | |
| 114 MOJO_CHECK(app.WaitForInitialize()); | |
| 115 | |
| 98 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL. | 116 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL. |
| 99 // It also removes GTEST arguments from |argv| and updates the |argc| count. | 117 // It also removes GTEST arguments from |argv| and updates the |argc| count. |
| 100 const mojo::Array<mojo::String>& args = app.args(); | 118 g_args_hack = app.args().Clone(); |
| 101 MOJO_CHECK(args.size() < INT_MAX); | 119 MOJO_CHECK(g_args_hack.size() < INT_MAX); |
| 102 int argc = static_cast<int>(args.size()); | 120 int argc = static_cast<int>(g_args_hack.size()); |
| 103 std::vector<char*> argv(argc + 1); | 121 std::vector<char*> argv(argc + 1); |
| 104 for (int i = 0; i < argc; ++i) | 122 for (int i = 0; i < argc; ++i) |
| 105 argv[i] = const_cast<char*>(args[i].data()); | 123 argv[i] = const_cast<char*>(g_args_hack[i].data()); |
| 106 argv[argc] = NULL; | 124 argv[argc] = NULL; |
| 107 testing::InitGoogleTest(&argc, &argv[0]); | 125 testing::InitGoogleTest(&argc, &argv[0]); |
| 126 g_shell_scoped_message_pipe_handle_hack = app.UnbindShell(); | |
| 108 } | 127 } |
| 109 | 128 |
| 110 mojo_ignore_result(RUN_ALL_TESTS()); | 129 mojo_ignore_result(RUN_ALL_TESTS()); |
| 111 | |
| 112 delete delegate; | |
| 113 return MOJO_RESULT_OK; | 130 return MOJO_RESULT_OK; |
| 114 } | 131 } |
| OLD | NEW |