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 "base/message_loop/message_loop.h" | |
9 //#include "base/run_loop.h" | |
10 //#include "mojo/common/common_type_converters.h" | |
11 #include "mojo/examples/test/example_service.mojom.h" | |
12 #include "mojo/examples/test/example_service_impl.h" | |
13 //#include "mojo/public/cpp/application/application_connection.h" | |
14 #include "mojo/public/cpp/application/application_delegate.h" | |
15 #include "mojo/public/cpp/application/application_impl.h" | |
16 #include "mojo/public/cpp/system/macros.h" | |
17 #include "mojo/public/cpp/utility/run_loop.h" | |
18 #include "mojo/service_manager/service_manager.h" | |
19 //#include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" | |
20 //#include "mojo/shell/shell_test_helper.h" | |
21 #include "mojo/shell/init.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 namespace mojo { | |
25 | |
26 //class ApplicationImpl; | |
27 | |
28 class ExampleServiceTest : public testing::Test { | |
29 public: | |
30 ExampleServiceTest() {} | |
31 | |
32 virtual void SetUp() MOJO_OVERRIDE { | |
33 //ServiceManager* service_manager = shell_context()->service_manager(); | |
34 ServiceManager* service_manager = ServiceManager::GetInstance(); | |
35 LOG(ERROR) << "MSW ServiceManager::GetInstance(): " << service_manager; | |
36 //test_api_.reset(new ServiceManager::TestAPI(service_manager)); | |
37 //test_helper_.Init(); | |
38 | |
39 //service_manager->SetLoaderForURL( | |
40 // scoped_ptr<ServiceLoader>(new EmbedServiceLoader()), | |
41 // GURL(kTestServiceURL)); | |
42 | |
43 // TODO(msw): ServiceManager::GetInstance()->ConnectToService(...) Crashes: | |
44 // mojo_service_manager.dll!mojo::InterfaceImpl<mojo::ServiceProvider>::inte rnal_state() Line 50 C++ | |
45 // mojo_service_manager.dll!mojo::BindToProxy<mojo::`anonymous namespace'::S tubServiceProvider,mojo::ServiceProvider>(mojo::`anonymous-namespace'::StubServi ceProvider * instance, mojo::InterfacePtr<mojo::ServiceProvider> * ptr, const Mo joAsyncWaiter * waiter) Line 92 C++ | |
46 // mojo_service_manager.dll!mojo::ServiceManager::ConnectToServiceByName(con st GURL & application_url, const std::basic_string<char, std::char_traits<char>, std::allocator<char> > & interface_name) Line 181 C++ | |
47 // F143.tmp!mojo::ServiceManager::ConnectToService<mojo::ExampleService>(con st GURL & application_url, mojo::InterfacePtr<mojo::ExampleService> * ptr) Line 63 C++ | |
48 //service_manager->ConnectToService( | |
49 // GURL("mojo:mojo_example_service"), | |
50 // &example_service_); | |
51 | |
52 //test_helper_.SetLoaderForURL( | |
53 // scoped_ptr<ServiceLoader>(new EmbedServiceLoader()), | |
54 // GURL(kTestServiceURL)); | |
55 //test_helper_.service_manager()->ConnectToService( | |
56 // GURL("mojo:mojo_view_manager"), | |
57 // &view_manager_init_); | |
58 //ASSERT_TRUE(EmbedRoot(view_manager_init_.get(), kTestServiceURL)); | |
59 //connection_ = ViewManagerProxy::WaitForInstance(); | |
60 //ASSERT_TRUE(connection_ != NULL); | |
61 //connection_->DoRunLoopUntilChangesCount(1); | |
Ben Goodger (Google)
2014/07/18 20:02:52
Check out the code I recently landed here:
http:/
msw
2014/07/21 18:58:22
I'm having issues using ShellTestHelper here, and
Ben Goodger (Google)
2014/07/21 20:02:38
So I don't think you can use a ShellTestHelper...
| |
62 } | |
63 | |
64 virtual void TearDown() MOJO_OVERRIDE {} | |
65 | |
66 protected: | |
67 //ExampleServicePtr example_service_; | |
68 //shell::ShellTestHelper test_helper_; | |
69 //scoped_ptr<ServiceManager::TestAPI> test_api_; | |
70 | |
71 private: | |
72 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest); | |
73 }; | |
74 | |
75 // Test ExampleService's Ping. | |
76 TEST_F(ExampleServiceTest, Ping) { | |
77 //example_service_->Echo(String::From("Echo string"), base::Bind(&OnEcho)); | |
78 // TODO(msw): Verify that OnEcho was called... | |
79 } | |
80 | |
81 | |
82 // TODO(msw): Is this needed ??? Move this elsewhere ... | |
Ben Goodger (Google)
2014/07/18 20:02:52
No. You should be relying on the shell's service l
msw
2014/07/21 18:58:22
Done.
| |
83 class ExampleServiceLoader : public ServiceLoader, public ApplicationDelegate { | |
84 public: | |
85 ExampleServiceLoader() {} | |
86 | |
87 virtual ~ExampleServiceLoader() { | |
88 test_app_.reset(NULL); | |
89 } | |
90 | |
91 private: | |
92 virtual void LoadService( | |
93 ServiceManager* manager, | |
94 const GURL& url, | |
95 ScopedMessagePipeHandle service_provider_handle) MOJO_OVERRIDE { | |
96 test_app_.reset(new ApplicationImpl(this, service_provider_handle.Pass())); | |
97 } | |
98 | |
99 virtual void OnServiceError(ServiceManager* manager, | |
100 const GURL& url) MOJO_OVERRIDE { | |
101 } | |
102 | |
103 virtual bool ConfigureIncomingConnection( | |
104 ApplicationConnection* connection) MOJO_OVERRIDE { | |
105 // TODO(msw): connection->AddService<ExampleServiceImpl>(context_); | |
106 return true; | |
107 } | |
108 | |
109 scoped_ptr<ApplicationImpl> test_app_; | |
110 DISALLOW_COPY_AND_ASSIGN(ExampleServiceLoader); | |
111 }; | |
112 | |
113 // TODO(msw): Move this elsewhere ... | |
114 class ExampleClientImpl : public InterfaceImpl<ExampleClient> { | |
115 public: | |
116 explicit ExampleClientImpl(ExampleServicePtr example_service) | |
117 : example_service_(example_service.Pass()) { | |
118 example_service_.set_client(this); | |
119 } | |
120 virtual ~ExampleClientImpl() { | |
121 example_service_.reset(); | |
122 } | |
123 | |
124 void PingExampleService() { | |
125 // TODO(msw): Can't supply a callback from this process... | |
126 //example_service_->Echo(String::From("Echo string"), base::Bind(&OnEcho)); | |
127 example_service_->Ping(kMagicNumber); | |
128 } | |
129 | |
130 protected: | |
131 // InterfaceImpl<ExampleClient> overrides. | |
132 virtual void Pong(uint16_t pong_value) MOJO_OVERRIDE { | |
133 LOG(ERROR) << "MSW: ExampleClientImpl::Pong: " << pong_value; | |
134 EXPECT_EQ(kMagicNumber, pong_value); | |
135 // TODO(msw): Disconnect from ExampleService??? Quit properly.... | |
136 //base::MessageLoop::current()->Quit(); | |
137 RunLoop::current()->Quit(); | |
138 } | |
139 | |
140 private: | |
141 ExampleServicePtr example_service_; | |
142 static const int16_t kMagicNumber = 42; | |
143 MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleClientImpl); | |
144 }; | |
145 | |
146 | |
147 // TODO(msw): Generalize a GTest ApplicationDelegeate for all such tests? | |
148 class TestApp : public ApplicationDelegate { | |
149 public: | |
150 TestApp() {} | |
151 virtual ~TestApp() {} | |
152 | |
153 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE{ | |
154 printf("Running TestApp::Initialize() from example_unittest.cc\n"); | |
155 // TODO(msw): Get actual commandline arguments... | |
156 int argc = 0; | |
157 char** argv = NULL; | |
158 testing::InitGoogleTest(&argc, argv); | |
159 mojo::shell::InitializeLogging(); | |
160 | |
161 ServiceManager* service_manager = ServiceManager::GetInstance(); | |
Ben Goodger (Google)
2014/07/18 20:02:52
I *think* you should be able to write this test wi
msw
2014/07/21 18:58:22
Done (with a hacky global for now, need help).
| |
162 LOG(ERROR) << "MSW ServiceManager::GetInstance(): " << service_manager; | |
163 //ExampleServiceLoader* default_loader = new ExampleServiceLoader; | |
164 //default_loader->set_context(&context_); | |
165 service_manager->set_default_loader( | |
166 scoped_ptr<ServiceLoader>(new ExampleServiceLoader)); | |
167 | |
168 // TODO(msw): Connect to service in the test... | |
169 ExampleServicePtr example_service; | |
170 LOG(ERROR) << "MSW 0"; | |
171 //service_manager->ConnectToService( | |
172 // GURL("mojo:mojo_example_service"), | |
173 // &example_service); | |
174 app->ConnectToService("mojo:mojo_example_service", &example_service); | |
175 LOG(ERROR) << "MSW 1"; | |
176 example_client_.reset(new ExampleClientImpl(example_service.Pass())); | |
177 LOG(ERROR) << "MSW 2"; | |
178 example_client_->PingExampleService(); | |
179 // TODO(msw): Run tests here? | |
180 RUN_ALL_TESTS(); | |
181 LOG(ERROR) << "MSW 3"; | |
182 | |
183 // TODO(msw): Disconnect from ExampleService??? Quit properly.... | |
184 //base::MessageLoop::current()->Quit(); | |
185 //RunLoop::current()->RunUntilIdle(); | |
186 RunLoop::current()->Quit(); | |
187 } | |
188 | |
189 private: | |
190 // TODO(msw): Expose to ExampleServiceTest ... | |
191 scoped_ptr<ExampleClientImpl> example_client_; | |
192 MOJO_DISALLOW_COPY_AND_ASSIGN(TestApp); | |
193 }; | |
194 | |
195 // TODO(msw): Move this and TestApp to a common mojo test helper... | |
196 // TODO(msw): Make a AppTestBase like ShellTestBase, etc. ??? | |
197 // static | |
198 ApplicationDelegate* ApplicationDelegate::Create() { | |
199 return new TestApp(); | |
200 } | |
201 | |
202 } // namespace mojo | |
OLD | NEW |