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" |
(...skipping 14 matching lines...) Expand all Loading... |
25 // This global shell handle is needed for repeated use by test applications. | 25 // This global shell handle is needed for repeated use by test applications. |
26 MessagePipeHandle g_shell_message_pipe_handle_hack; | 26 MessagePipeHandle g_shell_message_pipe_handle_hack; |
27 | 27 |
28 // Apptest is a GTEST base class for application testing executed in mojo_shell. | 28 // Apptest is a GTEST base class for application testing executed in mojo_shell. |
29 class Apptest : public testing::Test { | 29 class Apptest : public testing::Test { |
30 public: | 30 public: |
31 explicit Apptest(Array<String> args) | 31 explicit Apptest(Array<String> args) |
32 : args_(args.Pass()), | 32 : args_(args.Pass()), |
33 application_impl_(nullptr) { | 33 application_impl_(nullptr) { |
34 } | 34 } |
35 virtual ~Apptest() override {} | 35 ~Apptest() override {} |
36 | 36 |
37 protected: | 37 protected: |
38 ApplicationImpl* application_impl() { return application_impl_; } | 38 ApplicationImpl* application_impl() { return application_impl_; } |
39 | 39 |
40 // Get the ApplicationDelegate for the application to be tested. | 40 // Get the ApplicationDelegate for the application to be tested. |
41 virtual ApplicationDelegate* GetApplicationDelegate() = 0; | 41 virtual ApplicationDelegate* GetApplicationDelegate() = 0; |
42 | 42 |
43 // testing::Test: | 43 // testing::Test: |
44 virtual void SetUp() override { | 44 void SetUp() override { |
45 // New applications are constructed for each test to avoid persisting state. | 45 // New applications are constructed for each test to avoid persisting state. |
46 MOJO_CHECK(g_shell_message_pipe_handle_hack.is_valid()); | 46 MOJO_CHECK(g_shell_message_pipe_handle_hack.is_valid()); |
47 application_impl_ = new ApplicationImpl( | 47 application_impl_ = new ApplicationImpl( |
48 GetApplicationDelegate(), | 48 GetApplicationDelegate(), |
49 MakeScopedHandle(g_shell_message_pipe_handle_hack)); | 49 MakeScopedHandle(g_shell_message_pipe_handle_hack)); |
50 | 50 |
51 // Fake application initialization with the given command line arguments. | 51 // Fake application initialization with the given command line arguments. |
52 application_impl_->Initialize(args_.Clone()); | 52 application_impl_->Initialize(args_.Clone()); |
53 } | 53 } |
54 virtual void TearDown() override { | 54 void TearDown() override { |
55 g_shell_message_pipe_handle_hack = | 55 g_shell_message_pipe_handle_hack = |
56 application_impl_->UnbindShell().release(); | 56 application_impl_->UnbindShell().release(); |
57 delete application_impl_; | 57 delete application_impl_; |
58 } | 58 } |
59 | 59 |
60 private: | 60 private: |
61 // The command line arguments supplied to each test application instance. | 61 // The command line arguments supplied to each test application instance. |
62 Array<String> args_; | 62 Array<String> args_; |
63 | 63 |
64 // The application implementation instance, reconstructed for each test. | 64 // The application implementation instance, reconstructed for each test. |
65 ApplicationImpl* application_impl_; | 65 ApplicationImpl* application_impl_; |
66 | 66 |
67 // A run loop is needed for ApplicationImpl initialization and communication. | 67 // A run loop is needed for ApplicationImpl initialization and communication. |
68 RunLoop run_loop_; | 68 RunLoop run_loop_; |
69 | 69 |
70 MOJO_DISALLOW_COPY_AND_ASSIGN(Apptest); | 70 MOJO_DISALLOW_COPY_AND_ASSIGN(Apptest); |
71 }; | 71 }; |
72 | 72 |
73 // ExampleApptest exemplifies Apptest's application testing pattern. | 73 // ExampleApptest exemplifies Apptest's application testing pattern. |
74 class ExampleApptest : public Apptest { | 74 class ExampleApptest : public Apptest { |
75 public: | 75 public: |
76 // TODO(msw): Exemplify the use of actual command line arguments. | 76 // TODO(msw): Exemplify the use of actual command line arguments. |
77 ExampleApptest() : Apptest(Array<String>()) {} | 77 ExampleApptest() : Apptest(Array<String>()) {} |
78 virtual ~ExampleApptest() override {} | 78 ~ExampleApptest() override {} |
79 | 79 |
80 protected: | 80 protected: |
81 // Apptest: | 81 // Apptest: |
82 virtual ApplicationDelegate* GetApplicationDelegate() override { | 82 ApplicationDelegate* GetApplicationDelegate() override { |
83 return &example_client_application_; | 83 return &example_client_application_; |
84 } | 84 } |
85 virtual void SetUp() override { | 85 void SetUp() override { |
86 Apptest::SetUp(); | 86 Apptest::SetUp(); |
87 application_impl()->ConnectToService("mojo:example_service", | 87 application_impl()->ConnectToService("mojo:example_service", |
88 &example_service_); | 88 &example_service_); |
89 example_service_.set_client(&example_client_); | 89 example_service_.set_client(&example_client_); |
90 } | 90 } |
91 | 91 |
92 ExampleServicePtr example_service_; | 92 ExampleServicePtr example_service_; |
93 ExampleClientImpl example_client_; | 93 ExampleClientImpl example_client_; |
94 | 94 |
95 private: | 95 private: |
(...skipping 14 matching lines...) Expand all Loading... |
110 // Test ExampleClient and ExampleService interaction. | 110 // Test ExampleClient and ExampleService interaction. |
111 EXPECT_EQ(0, example_client_.last_pong_value()); | 111 EXPECT_EQ(0, example_client_.last_pong_value()); |
112 example_service_->Ping(1); | 112 example_service_->Ping(1); |
113 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); | 113 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); |
114 EXPECT_EQ(1, example_client_.last_pong_value()); | 114 EXPECT_EQ(1, example_client_.last_pong_value()); |
115 } | 115 } |
116 | 116 |
117 template <typename T> | 117 template <typename T> |
118 struct SetCallback : public Callback<void()>::Runnable { | 118 struct SetCallback : public Callback<void()>::Runnable { |
119 SetCallback(T* val, T result) : val_(val), result_(result) {} | 119 SetCallback(T* val, T result) : val_(val), result_(result) {} |
120 virtual ~SetCallback() {} | 120 ~SetCallback() override {} |
121 virtual void Run() const override { *val_ = result_; } | 121 void Run() const override { *val_ = result_; } |
122 T* val_; | 122 T* val_; |
123 T result_; | 123 T result_; |
124 }; | 124 }; |
125 | 125 |
126 TEST_F(ExampleApptest, RunCallbackViaService) { | 126 TEST_F(ExampleApptest, RunCallbackViaService) { |
127 // Test ExampleService callback functionality. | 127 // Test ExampleService callback functionality. |
128 bool was_run = false; | 128 bool was_run = false; |
129 example_service_->RunCallback(SetCallback<bool>(&was_run, true)); | 129 example_service_->RunCallback(SetCallback<bool>(&was_run, true)); |
130 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); | 130 EXPECT_TRUE(example_service_.WaitForIncomingMethodCall()); |
131 EXPECT_TRUE(was_run); | 131 EXPECT_TRUE(was_run); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 } | 164 } |
165 | 165 |
166 mojo_ignore_result(RUN_ALL_TESTS()); | 166 mojo_ignore_result(RUN_ALL_TESTS()); |
167 | 167 |
168 MojoResult result = MojoClose(mojo::g_shell_message_pipe_handle_hack.value()); | 168 MojoResult result = MojoClose(mojo::g_shell_message_pipe_handle_hack.value()); |
169 MOJO_ALLOW_UNUSED_LOCAL(result); | 169 MOJO_ALLOW_UNUSED_LOCAL(result); |
170 assert(result == MOJO_RESULT_OK); | 170 assert(result == MOJO_RESULT_OK); |
171 | 171 |
172 return MOJO_RESULT_OK; | 172 return MOJO_RESULT_OK; |
173 } | 173 } |
OLD | NEW |