Chromium Code Reviews| 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 example_service_.reset(); | |
|
Ben Goodger (Google)
2014/08/07 15:53:09
should not be necessary.
msw
2014/08/07 18:31:15
Done.
| |
| 36 } | |
| 37 | |
| 38 protected: | |
| 39 ExampleServicePtr example_service_; | |
| 40 ExampleClientImpl example_client_; | |
| 41 | |
| 42 private: | |
| 43 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest); | |
| 44 }; | |
| 45 | |
| 46 TEST_F(ExampleServiceTest, Ping) { | |
| 47 EXPECT_EQ(0, example_client_.last_pong_value()); | |
| 48 example_service_->Ping(1); | |
| 49 mojo::RunLoop::current()->Run(); | |
| 50 EXPECT_EQ(1, example_client_.last_pong_value()); | |
| 51 } | |
| 52 | |
| 53 template <typename T> | |
| 54 struct SetAndQuit : public Callback<void()>::Runnable { | |
| 55 SetAndQuit(T* val, T result) : val_(val), result_(result) {} | |
| 56 virtual ~SetAndQuit() {} | |
| 57 virtual void Run() const MOJO_OVERRIDE{ | |
| 58 *val_ = result_; | |
| 59 mojo::RunLoop::current()->Quit(); | |
| 60 } | |
| 61 T* val_; | |
| 62 T result_; | |
| 63 }; | |
| 64 | |
| 65 TEST_F(ExampleServiceTest, RunCallback) { | |
| 66 bool was_run = false; | |
| 67 example_service_->RunCallback(SetAndQuit<bool>(&was_run, true)); | |
| 68 mojo::RunLoop::current()->Run(); | |
| 69 EXPECT_TRUE(was_run); | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 74 } // namespace mojo | |
| 75 | |
| 76 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain( | |
| 77 MojoHandle shell_handle) { | |
| 78 mojo::Environment env; | |
| 79 mojo::RunLoop loop; | |
| 80 | |
| 81 mojo::ApplicationDelegate* delegate = mojo::ApplicationDelegate::Create(); | |
| 82 mojo::ApplicationImpl app(delegate); | |
| 83 app.BindShell(shell_handle); | |
| 84 g_application_impl_hack = &app; | |
| 85 | |
| 86 // TODO(msw): Get actual commandline arguments. | |
| 87 int argc = 0; | |
| 88 char** argv = NULL; | |
| 89 testing::InitGoogleTest(&argc, argv); | |
| 90 RUN_ALL_TESTS(); | |
| 91 | |
| 92 delete delegate; | |
| 93 printf("Exiting MojoMain.\n"); | |
|
Ben Goodger (Google)
2014/08/07 15:53:09
delete
msw
2014/08/07 18:31:15
Done.
| |
| 94 return MOJO_RESULT_OK; | |
| 95 } | |
| OLD | NEW |