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 "base/bind.h" | |
| 6 #include "base/logging.h" | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "mojo/examples/test/example_service.mojom.h" | |
| 9 #include "mojo/examples/test/example_service_impl.h" | |
| 10 #include "mojo/public/cpp/application/application_delegate.h" | |
| 11 #include "mojo/public/cpp/application/application_impl.h" | |
| 12 #include "mojo/public/cpp/system/macros.h" | |
| 13 #include "mojo/shell/init.h" | |
| 14 #include "mojo/shell/shell_test_helper.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // TODO(msw): Avoid this global by fixing ShellTestHelper ... | |
| 22 ApplicationImpl* g_application_impl_hack_ = NULL; | |
| 23 | |
| 24 // TODO(msw): Move this elsewhere ... | |
| 25 class ExampleClientImpl : public InterfaceImpl<ExampleClient> { | |
| 26 public: | |
| 27 explicit ExampleClientImpl(ExampleServicePtr example_service) | |
| 28 : example_service_(example_service.Pass()) { | |
| 29 example_service_.set_client(this); | |
| 30 } | |
| 31 virtual ~ExampleClientImpl() { | |
| 32 example_service_.reset(); | |
| 33 } | |
| 34 | |
| 35 void PingExampleService() { | |
| 36 LOG(ERROR) << "MSW: ExampleClientImpl::PingExampleService."; | |
| 37 example_service_->Ping(kMagicNumber); | |
| 38 } | |
| 39 | |
| 40 protected: | |
| 41 // InterfaceImpl<ExampleClient> overrides. | |
| 42 virtual void Pong(uint16_t pong_value) MOJO_OVERRIDE{ | |
| 43 LOG(ERROR) << "MSW: ExampleClientImpl::Pong: " << pong_value; | |
| 44 EXPECT_EQ(kMagicNumber, pong_value); | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 ExampleServicePtr example_service_; | |
| 49 static const int16_t kMagicNumber = 42; | |
| 50 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleClientImpl); | |
| 51 }; | |
| 52 | |
| 53 class ExampleServiceTest : public testing::Test { | |
| 54 public: | |
| 55 ExampleServiceTest() {} | |
| 56 | |
| 57 virtual void SetUp() MOJO_OVERRIDE { | |
| 58 // TODO(msw): ShellTestHelper::Init causes an exception via Context's ctor: | |
| 59 // unknown file: error: SEH exception with code 0xc0000005 thrown in SetUp() . | |
| 60 // With a workaround hack of GLES2Support::Init, it still asserts/crashes: | |
| 61 // Assertion failed: result == MOJO_RESULT_OK, file d:\src\chrome-git\src\mo jo\public\cpp\system\core.h, line 153 | |
| 62 //test_helper_.Init(); | |
| 63 | |
| 64 //ExampleServicePtr example_service; | |
| 65 //test_helper_.service_manager()->ConnectToService( | |
| 66 // GURL("mojo:mojo_example_service"), | |
| 67 // &example_service); | |
| 68 //example_client_.reset(new ExampleClientImpl(example_service.Pass())); | |
| 69 | |
| 70 // TODO(msw): This is destroyed before example_client_'s Pong is called... | |
| 71 ExampleServicePtr example_service; | |
| 72 g_application_impl_hack_->ConnectToService("mojo:mojo_example_service", | |
| 73 &example_service); | |
| 74 example_client_.reset(new ExampleClientImpl(example_service.Pass())); | |
| 75 } | |
| 76 | |
| 77 virtual void TearDown() MOJO_OVERRIDE {} | |
| 78 | |
| 79 protected: | |
| 80 base::MessageLoop loop_; | |
| 81 shell::ShellTestHelper test_helper_; | |
| 82 scoped_ptr<ExampleClientImpl> example_client_; | |
| 83 | |
| 84 private: | |
| 85 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest); | |
| 86 }; | |
| 87 | |
| 88 // Test ExampleService's Ping. | |
| 89 TEST_F(ExampleServiceTest, Ping) { | |
| 90 LOG(ERROR) << "MSW ExampleServiceTest.Ping"; | |
| 91 example_client_->PingExampleService(); | |
| 92 } | |
| 93 | |
| 94 // TODO(msw): Writer better tests; eg. for ExampleService::Echo ... | |
| 95 TEST_F(ExampleServiceTest, PingAgain) { | |
| 96 LOG(ERROR) << "MSW ExampleServiceTest.PingAgain"; | |
| 97 example_client_->PingExampleService(); | |
| 98 } | |
| 99 | |
| 100 // TODO(msw): Generalize a GTest ApplicationDelegeate for all such tests? | |
| 101 class TestApp : public ApplicationDelegate { | |
| 102 public: | |
| 103 TestApp() {} | |
| 104 virtual ~TestApp() {} | |
| 105 | |
| 106 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE{ | |
| 107 printf("Running TestApp::Initialize() from example_unittest.cc\n"); | |
| 108 // TODO(msw): Get actual commandline arguments... | |
| 109 int argc = 0; | |
| 110 char** argv = NULL; | |
| 111 testing::InitGoogleTest(&argc, argv); | |
| 112 mojo::shell::InitializeLogging(); | |
| 113 | |
| 114 g_application_impl_hack_ = app; | |
|
Ben Goodger (Google)
2014/07/21 20:02:39
This is the kind of thing that I think could be im
| |
| 115 | |
| 116 // TODO(msw): See mojo/common/test/run_all_unittests.cc | |
| 117 RUN_ALL_TESTS(); | |
| 118 // TODO(msw): Quit properly... | |
| 119 } | |
| 120 | |
| 121 private: | |
| 122 MOJO_DISALLOW_COPY_AND_ASSIGN(TestApp); | |
| 123 }; | |
| 124 | |
| 125 } // namespace | |
| 126 | |
| 127 // TODO(msw): Move this and TestApp to a common mojo test helper... | |
| 128 // TODO(msw): Make a AppTestBase like ShellTestBase, etc. ??? | |
| 129 // static | |
| 130 ApplicationDelegate* ApplicationDelegate::Create() { | |
| 131 return new TestApp(); | |
| 132 } | |
| 133 | |
| 134 } // namespace mojo | |
| OLD | NEW |