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 { | |
22 | |
23 // TODO(msw): Remove this once we can get ApplicationImpl from TLS. | |
24 mojo::ApplicationImpl* g_application_impl_hack = NULL; | |
25 | |
26 } // namespace | |
27 | |
28 namespace mojo { | 21 namespace mojo { |
29 | 22 |
30 namespace { | 23 namespace { |
31 | 24 |
32 class ExampleApptest : public testing::Test { | 25 // TODO(msw): Revise this shell handle hack used for multiple test applications. |
26 ScopedMessagePipeHandle g_shell_scoped_message_pipe_handle_hack; | |
27 | |
28 // A GTEST base class for application testing executed within mojo_shell. | |
29 class Apptest : public testing::Test { | |
33 public: | 30 public: |
34 ExampleApptest() { | 31 explicit Apptest(Array<String> args) |
35 g_application_impl_hack->ConnectToService("mojo:mojo_example_service", | 32 : args_(args.Pass()), |
36 &example_service_); | 33 application_impl_(NULL) { |
viettrungluu
2014/10/16 02:22:23
nit: nullptr instead of NULL
msw
2014/10/16 19:40:59
Done.
| |
34 } | |
35 virtual ~Apptest() override {} | |
36 | |
37 protected: | |
38 ApplicationImpl* application_impl() { return application_impl_; } | |
39 | |
40 // Get the ApplicationDelegate used for application construction. | |
41 virtual ApplicationDelegate* GetApplicationDelegate() = 0; | |
42 | |
43 // testing::Test: | |
44 virtual void SetUp() override { | |
45 // TODO(msw): Base classes shouldn't need to provide a RunLoop for this. | |
46 // New applications are constructed for each test to avoid persisting state. | |
47 MOJO_CHECK(g_shell_scoped_message_pipe_handle_hack.is_valid()); | |
48 application_impl_ = new ApplicationImpl( | |
49 GetApplicationDelegate(), | |
50 g_shell_scoped_message_pipe_handle_hack.Pass()); | |
51 | |
52 // Fake application initialization with the given command line arguments. | |
53 application_impl_->Initialize(args_.Clone()); | |
54 } | |
55 virtual void TearDown() override { | |
56 g_shell_scoped_message_pipe_handle_hack = application_impl_->UnbindShell(); | |
57 delete application_impl_; | |
58 } | |
59 | |
60 private: | |
61 // The command line arguments supplied to each test application instance. | |
62 Array<String> args_; | |
63 | |
64 // The application implementation instance, reconstructed for each test. | |
65 ApplicationImpl* application_impl_; | |
66 | |
67 MOJO_DISALLOW_COPY_AND_ASSIGN(Apptest); | |
68 }; | |
69 | |
70 // A GTEST class exemplifying Apptest's application testing pattern. | |
71 class ExampleApptest : public Apptest { | |
72 public: | |
73 // TODO(msw): Exemplify the use of actual command line arguments. | |
74 ExampleApptest() : Apptest(Array<String>()) {} | |
75 virtual ~ExampleApptest() override {} | |
76 | |
77 protected: | |
78 // Apptest: | |
79 virtual ApplicationDelegate* GetApplicationDelegate() override { | |
80 return &example_client_application_; | |
81 } | |
82 virtual void SetUp() override { | |
83 Apptest::SetUp(); | |
84 application_impl()->ConnectToService("mojo:mojo_example_service", | |
85 &example_service_); | |
37 example_service_.set_client(&example_client_); | 86 example_service_.set_client(&example_client_); |
38 } | 87 } |
39 | 88 |
40 virtual ~ExampleApptest() override {} | |
41 | |
42 protected: | |
43 ExampleServicePtr example_service_; | 89 ExampleServicePtr example_service_; |
44 ExampleClientImpl example_client_; | 90 ExampleClientImpl example_client_; |
45 | 91 |
46 private: | 92 private: |
93 RunLoop run_loop_; | |
94 ExampleClientApplication example_client_application_; | |
95 | |
47 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleApptest); | 96 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleApptest); |
48 }; | 97 }; |
49 | 98 |
50 TEST_F(ExampleApptest, PongClientDirectly) { | 99 TEST_F(ExampleApptest, PongClientDirectly) { |
51 // Test very basic standalone ExampleClient functionality. | 100 // Test very basic standalone ExampleClient functionality. |
52 ExampleClientImpl example_client; | 101 ExampleClientImpl example_client; |
53 EXPECT_EQ(0, example_client.last_pong_value()); | 102 EXPECT_EQ(0, example_client.last_pong_value()); |
54 example_client.Pong(1); | 103 example_client.Pong(1); |
55 EXPECT_EQ(1, example_client.last_pong_value()); | 104 EXPECT_EQ(1, example_client.last_pong_value()); |
56 } | 105 } |
(...skipping 21 matching lines...) Expand all Loading... | |
78 example_service_->RunCallback(SetAndQuit<bool>(&was_run, true)); | 127 example_service_->RunCallback(SetAndQuit<bool>(&was_run, true)); |
79 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); | 128 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); |
80 EXPECT_TRUE(was_run); | 129 EXPECT_TRUE(was_run); |
81 } | 130 } |
82 | 131 |
83 } // namespace | 132 } // namespace |
84 | 133 |
85 } // namespace mojo | 134 } // namespace mojo |
86 | 135 |
87 MojoResult MojoMain(MojoHandle shell_handle) { | 136 MojoResult MojoMain(MojoHandle shell_handle) { |
88 mojo::Environment env; | 137 mojo::Environment environment; |
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 | 138 |
97 { | 139 { |
140 // This RunLoop is used for init, and then destroyed before running tests. | |
141 mojo::RunLoop run_loop; | |
142 | |
143 // Construct an ApplicationImpl just for the GTEST commandline arguments. | |
144 // GTEST command line arguments are supported amid application arguments: | |
145 // mojo_shell 'mojo:mojo_example_apptest arg1 --gtest_filter=foo arg2' | |
146 mojo::ApplicationDelegate dummy_application_delegate; | |
147 mojo::ApplicationImpl app(&dummy_application_delegate, shell_handle); | |
148 MOJO_CHECK(app.WaitForInitialize()); | |
149 | |
98 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL. | 150 // InitGoogleTest expects (argc + 1) elements, including a terminating NULL. |
99 // It also removes GTEST arguments from |argv| and updates the |argc| count. | 151 // It also removes GTEST arguments from |argv| and updates the |argc| count. |
152 // TODO(msw): Provide tests access to these actual command line arguments. | |
100 const mojo::Array<mojo::String>& args = app.args(); | 153 const mojo::Array<mojo::String>& args = app.args(); |
101 MOJO_CHECK(args.size() < INT_MAX); | 154 MOJO_CHECK(args.size() < INT_MAX); |
102 int argc = static_cast<int>(args.size()); | 155 int argc = static_cast<int>(args.size()); |
103 std::vector<char*> argv(argc + 1); | 156 std::vector<char*> argv(argc + 1); |
104 for (int i = 0; i < argc; ++i) | 157 for (int i = 0; i < argc; ++i) |
105 argv[i] = const_cast<char*>(args[i].data()); | 158 argv[i] = const_cast<char*>(args[i].data()); |
106 argv[argc] = NULL; | 159 argv[argc] = NULL; |
107 testing::InitGoogleTest(&argc, &argv[0]); | 160 testing::InitGoogleTest(&argc, &argv[0]); |
161 mojo::g_shell_scoped_message_pipe_handle_hack = app.UnbindShell(); | |
108 } | 162 } |
109 | 163 |
110 mojo_ignore_result(RUN_ALL_TESTS()); | 164 mojo_ignore_result(RUN_ALL_TESTS()); |
111 | |
112 delete delegate; | |
113 return MOJO_RESULT_OK; | 165 return MOJO_RESULT_OK; |
114 } | 166 } |
OLD | NEW |