OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "mojo/examples/apptest/example_client_impl.h" |
| 6 #include "mojo/examples/apptest/example_service.mojom.h" |
| 7 #include "mojo/public/cpp/application/application_delegate.h" |
| 8 #include "mojo/public/cpp/application/application_impl.h" |
| 9 #include "mojo/public/cpp/bindings/callback.h" |
| 10 #include "mojo/public/cpp/environment/environment.h" |
| 11 #include "mojo/public/cpp/system/macros.h" |
| 12 #include "mojo/public/cpp/utility/run_loop.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // TODO(msw): Remove this once we can get ApplicationImpl from TLS. |
| 18 mojo::ApplicationImpl* g_application_impl_hack = NULL; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 namespace mojo { |
| 23 |
| 24 namespace { |
| 25 |
| 26 class ExampleServiceTest : public testing::Test { |
| 27 public: |
| 28 ExampleServiceTest() { |
| 29 g_application_impl_hack->ConnectToService("mojo:mojo_example_service", |
| 30 &example_service_); |
| 31 example_service_.set_client(&example_client_); |
| 32 } |
| 33 |
| 34 virtual ~ExampleServiceTest() MOJO_OVERRIDE {} |
| 35 |
| 36 protected: |
| 37 ExampleServicePtr example_service_; |
| 38 ExampleClientImpl example_client_; |
| 39 |
| 40 private: |
| 41 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest); |
| 42 }; |
| 43 |
| 44 TEST_F(ExampleServiceTest, Ping) { |
| 45 EXPECT_EQ(0, example_client_.last_pong_value()); |
| 46 example_service_->Ping(1); |
| 47 mojo::RunLoop::current()->Run(); |
| 48 EXPECT_EQ(1, example_client_.last_pong_value()); |
| 49 } |
| 50 |
| 51 template <typename T> |
| 52 struct SetAndQuit : public Callback<void()>::Runnable { |
| 53 SetAndQuit(T* val, T result) : val_(val), result_(result) {} |
| 54 virtual ~SetAndQuit() {} |
| 55 virtual void Run() const MOJO_OVERRIDE{ |
| 56 *val_ = result_; |
| 57 mojo::RunLoop::current()->Quit(); |
| 58 } |
| 59 T* val_; |
| 60 T result_; |
| 61 }; |
| 62 |
| 63 TEST_F(ExampleServiceTest, RunCallback) { |
| 64 bool was_run = false; |
| 65 example_service_->RunCallback(SetAndQuit<bool>(&was_run, true)); |
| 66 mojo::RunLoop::current()->Run(); |
| 67 EXPECT_TRUE(was_run); |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
| 72 } // namespace mojo |
| 73 |
| 74 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain( |
| 75 MojoHandle shell_handle) { |
| 76 mojo::Environment env; |
| 77 mojo::RunLoop loop; |
| 78 |
| 79 mojo::ApplicationDelegate* delegate = mojo::ApplicationDelegate::Create(); |
| 80 mojo::ApplicationImpl app(delegate); |
| 81 app.BindShell(shell_handle); |
| 82 g_application_impl_hack = &app; |
| 83 |
| 84 // TODO(msw): Get actual commandline arguments. |
| 85 int argc = 0; |
| 86 char** argv = NULL; |
| 87 testing::InitGoogleTest(&argc, argv); |
| 88 mojo_ignore_result(RUN_ALL_TESTS()); |
| 89 |
| 90 delete delegate; |
| 91 return MOJO_RESULT_OK; |
| 92 } |
OLD | NEW |