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 // README: To run these tests from out/<Debug|Release>/, run: |
| 6 // ./mojo_shell --origin=file://`pwd` --disable-cache mojo:mojo_example_apptests |
| 7 |
| 8 // TODO(msw): Use mojo_main_chromium.cc mojo_environment_chromium for base/ |
| 9 // not mojo_main_standalone.cc and mojo_environment_standalone ??? |
| 10 // (a mojo_main_chromium app can test standalone services and apps) |
| 11 |
| 12 #include "mojo/examples/test/example_service.mojom.h" |
| 13 #include "mojo/public/cpp/application/application_delegate.h" |
| 14 #include "mojo/public/cpp/application/application_impl.h" |
| 15 #include "mojo/public/cpp/bindings/callback.h" |
| 16 #include "mojo/public/cpp/environment/environment.h" |
| 17 #include "mojo/public/cpp/system/macros.h" |
| 18 #include "mojo/public/cpp/utility/run_loop.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // TODO(msw): Remove these once we can get ApplicationImpl from TLS. ??? |
| 24 mojo::ApplicationImpl* g_application_impl_hack = NULL; |
| 25 mojo::RunLoop* g_run_loop_hack = NULL; |
| 26 |
| 27 } // namespace |
| 28 |
| 29 namespace mojo { |
| 30 |
| 31 namespace { |
| 32 |
| 33 // TODO(msw): Split example_service_apptest and example_client_apptest ??? |
| 34 class ExampleClientImpl : public InterfaceImpl<ExampleClient> { |
| 35 public: |
| 36 explicit ExampleClientImpl() : last_pong_value_(0) {} |
| 37 virtual ~ExampleClientImpl() {} |
| 38 |
| 39 int16_t last_pong_value() const { return last_pong_value_; } |
| 40 |
| 41 private: |
| 42 // InterfaceImpl<ExampleClient> overrides. |
| 43 virtual void Pong(uint16_t pong_value) MOJO_OVERRIDE { |
| 44 last_pong_value_ = pong_value; |
| 45 g_run_loop_hack->Quit(); |
| 46 } |
| 47 |
| 48 int16_t last_pong_value_; |
| 49 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleClientImpl); |
| 50 }; |
| 51 |
| 52 // TODO(msw): Split example_service_apptest and example_client_apptest ??? |
| 53 class ExampleServiceTest : public testing::Test { |
| 54 public: |
| 55 ExampleServiceTest() { |
| 56 g_application_impl_hack->ConnectToService("mojo:mojo_example_service", |
| 57 &example_service_); |
| 58 example_service_.set_client(&example_client_); |
| 59 } |
| 60 |
| 61 virtual ~ExampleServiceTest() MOJO_OVERRIDE { |
| 62 example_service_.reset(); |
| 63 } |
| 64 |
| 65 protected: |
| 66 ExampleServicePtr example_service_; |
| 67 ExampleClientImpl example_client_; |
| 68 |
| 69 private: |
| 70 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest); |
| 71 }; |
| 72 |
| 73 TEST_F(ExampleServiceTest, Ping) { |
| 74 EXPECT_EQ(0, example_client_.last_pong_value()); |
| 75 example_service_->Ping(1); |
| 76 g_run_loop_hack->Run(); |
| 77 EXPECT_EQ(1, example_client_.last_pong_value()); |
| 78 } |
| 79 |
| 80 // TODO(msw): Attempt mojo::Callbacks without base::Bind ??? |
| 81 template <typename T> |
| 82 struct SetAndQuit : public Callback<void()>::Runnable { |
| 83 SetAndQuit(T* val, T result) : val_(val), result_(result) {} |
| 84 virtual ~SetAndQuit() {} |
| 85 virtual void Run() const MOJO_OVERRIDE{ |
| 86 *val_ = result_; |
| 87 g_run_loop_hack->Quit(); |
| 88 } |
| 89 T* val_; |
| 90 T result_; |
| 91 }; |
| 92 |
| 93 TEST_F(ExampleServiceTest, RunCallback) { |
| 94 bool was_run = false; |
| 95 example_service_->RunCallback(SetAndQuit<bool>(&was_run, true)); |
| 96 g_run_loop_hack->Run(); |
| 97 EXPECT_TRUE(was_run); |
| 98 } |
| 99 |
| 100 // TODO(msw): Attempt mojo::Callbacks without base::Bind ??? |
| 101 struct RunAllTests : public Callback<void()>::Runnable { |
| 102 RunAllTests() {} |
| 103 virtual ~RunAllTests() {} |
| 104 virtual void Run() const MOJO_OVERRIDE { |
| 105 int success = RUN_ALL_TESTS(); |
| 106 printf("RUN_ALL_TESTS returned %d.\n", success); |
| 107 } |
| 108 }; |
| 109 |
| 110 // TODO(msw): Generalize a base class like this for ShellHooks help, etc. ??? |
| 111 class TestApp : public ApplicationDelegate { |
| 112 public: |
| 113 TestApp() {} |
| 114 virtual ~TestApp() {} |
| 115 |
| 116 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
| 117 printf("Running TestApp::Initialize() from example_apptest.cc\n"); |
| 118 // TODO(msw): Get actual commandline arguments ... |
| 119 int argc = 0; |
| 120 char** argv = NULL; |
| 121 testing::InitGoogleTest(&argc, argv); |
| 122 |
| 123 // TODO(msw): Calling RUN_ALL_TESTS here yields "Bus error: 10" ... |
| 124 MojoTimeTicks run_all_tests_delay = 0; |
| 125 g_run_loop_hack->PostDelayedTask(RunAllTests(), run_all_tests_delay); |
| 126 } |
| 127 |
| 128 private: |
| 129 MOJO_DISALLOW_COPY_AND_ASSIGN(TestApp); |
| 130 }; |
| 131 |
| 132 } // namespace |
| 133 |
| 134 // TODO(msw): Move this and TestApp to a common mojo test helper ... |
| 135 // static |
| 136 ApplicationDelegate* ApplicationDelegate::Create() { |
| 137 return new TestApp(); |
| 138 } |
| 139 |
| 140 } // namespace mojo |
| 141 |
| 142 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain( |
| 143 MojoHandle shell_handle) { |
| 144 mojo::Environment env; |
| 145 // TODO(msw): Don't run a loop here, only within tests as needed ??? |
| 146 mojo::RunLoop loop; |
| 147 g_run_loop_hack = &loop; |
| 148 |
| 149 mojo::ApplicationDelegate* delegate = mojo::ApplicationDelegate::Create(); |
| 150 mojo::ApplicationImpl app(delegate); |
| 151 app.BindShell(shell_handle); |
| 152 g_application_impl_hack = &app; |
| 153 g_run_loop_hack->RunUntilIdle(); |
| 154 |
| 155 printf("Exiting MojoMain.\n"); |
| 156 |
| 157 delete delegate; |
| 158 return MOJO_RESULT_OK; |
| 159 } |
OLD | NEW |