Index: mojo/examples/test/example_unittest.cc |
diff --git a/mojo/examples/test/example_unittest.cc b/mojo/examples/test/example_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2448fdc04afca61266f66eb7ef8704cbb9b4bea5 |
--- /dev/null |
+++ b/mojo/examples/test/example_unittest.cc |
@@ -0,0 +1,202 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/bind.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+//#include "base/message_loop/message_loop.h" |
+//#include "base/run_loop.h" |
+//#include "mojo/common/common_type_converters.h" |
+#include "mojo/examples/test/example_service.mojom.h" |
+#include "mojo/examples/test/example_service_impl.h" |
+//#include "mojo/public/cpp/application/application_connection.h" |
+#include "mojo/public/cpp/application/application_delegate.h" |
+#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/public/cpp/system/macros.h" |
+#include "mojo/public/cpp/utility/run_loop.h" |
+#include "mojo/service_manager/service_manager.h" |
+//#include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" |
+//#include "mojo/shell/shell_test_helper.h" |
+#include "mojo/shell/init.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace mojo { |
+ |
+//class ApplicationImpl; |
+ |
+class ExampleServiceTest : public testing::Test { |
+ public: |
+ ExampleServiceTest() {} |
+ |
+ virtual void SetUp() MOJO_OVERRIDE { |
+ //ServiceManager* service_manager = shell_context()->service_manager(); |
+ ServiceManager* service_manager = ServiceManager::GetInstance(); |
+ LOG(ERROR) << "MSW ServiceManager::GetInstance(): " << service_manager; |
+ //test_api_.reset(new ServiceManager::TestAPI(service_manager)); |
+ //test_helper_.Init(); |
+ |
+ //service_manager->SetLoaderForURL( |
+ // scoped_ptr<ServiceLoader>(new EmbedServiceLoader()), |
+ // GURL(kTestServiceURL)); |
+ |
+ // TODO(msw): ServiceManager::GetInstance()->ConnectToService(...) Crashes: |
+ // mojo_service_manager.dll!mojo::InterfaceImpl<mojo::ServiceProvider>::internal_state() Line 50 C++ |
+ // mojo_service_manager.dll!mojo::BindToProxy<mojo::`anonymous namespace'::StubServiceProvider,mojo::ServiceProvider>(mojo::`anonymous-namespace'::StubServiceProvider * instance, mojo::InterfacePtr<mojo::ServiceProvider> * ptr, const MojoAsyncWaiter * waiter) Line 92 C++ |
+ // mojo_service_manager.dll!mojo::ServiceManager::ConnectToServiceByName(const GURL & application_url, const std::basic_string<char, std::char_traits<char>, std::allocator<char> > & interface_name) Line 181 C++ |
+ // F143.tmp!mojo::ServiceManager::ConnectToService<mojo::ExampleService>(const GURL & application_url, mojo::InterfacePtr<mojo::ExampleService> * ptr) Line 63 C++ |
+ //service_manager->ConnectToService( |
+ // GURL("mojo:mojo_example_service"), |
+ // &example_service_); |
+ |
+ //test_helper_.SetLoaderForURL( |
+ // scoped_ptr<ServiceLoader>(new EmbedServiceLoader()), |
+ // GURL(kTestServiceURL)); |
+ //test_helper_.service_manager()->ConnectToService( |
+ // GURL("mojo:mojo_view_manager"), |
+ // &view_manager_init_); |
+ //ASSERT_TRUE(EmbedRoot(view_manager_init_.get(), kTestServiceURL)); |
+ //connection_ = ViewManagerProxy::WaitForInstance(); |
+ //ASSERT_TRUE(connection_ != NULL); |
+ //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...
|
+ } |
+ |
+ virtual void TearDown() MOJO_OVERRIDE {} |
+ |
+ protected: |
+ //ExampleServicePtr example_service_; |
+ //shell::ShellTestHelper test_helper_; |
+ //scoped_ptr<ServiceManager::TestAPI> test_api_; |
+ |
+ private: |
+ MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest); |
+}; |
+ |
+// Test ExampleService's Ping. |
+TEST_F(ExampleServiceTest, Ping) { |
+ //example_service_->Echo(String::From("Echo string"), base::Bind(&OnEcho)); |
+ // TODO(msw): Verify that OnEcho was called... |
+} |
+ |
+ |
+// 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.
|
+class ExampleServiceLoader : public ServiceLoader, public ApplicationDelegate { |
+ public: |
+ ExampleServiceLoader() {} |
+ |
+ virtual ~ExampleServiceLoader() { |
+ test_app_.reset(NULL); |
+ } |
+ |
+ private: |
+ virtual void LoadService( |
+ ServiceManager* manager, |
+ const GURL& url, |
+ ScopedMessagePipeHandle service_provider_handle) MOJO_OVERRIDE { |
+ test_app_.reset(new ApplicationImpl(this, service_provider_handle.Pass())); |
+ } |
+ |
+ virtual void OnServiceError(ServiceManager* manager, |
+ const GURL& url) MOJO_OVERRIDE { |
+ } |
+ |
+ virtual bool ConfigureIncomingConnection( |
+ ApplicationConnection* connection) MOJO_OVERRIDE { |
+ // TODO(msw): connection->AddService<ExampleServiceImpl>(context_); |
+ return true; |
+ } |
+ |
+ scoped_ptr<ApplicationImpl> test_app_; |
+ DISALLOW_COPY_AND_ASSIGN(ExampleServiceLoader); |
+}; |
+ |
+// TODO(msw): Move this elsewhere ... |
+class ExampleClientImpl : public InterfaceImpl<ExampleClient> { |
+ public: |
+ explicit ExampleClientImpl(ExampleServicePtr example_service) |
+ : example_service_(example_service.Pass()) { |
+ example_service_.set_client(this); |
+ } |
+ virtual ~ExampleClientImpl() { |
+ example_service_.reset(); |
+ } |
+ |
+ void PingExampleService() { |
+ // TODO(msw): Can't supply a callback from this process... |
+ //example_service_->Echo(String::From("Echo string"), base::Bind(&OnEcho)); |
+ example_service_->Ping(kMagicNumber); |
+ } |
+ |
+ protected: |
+ // InterfaceImpl<ExampleClient> overrides. |
+ virtual void Pong(uint16_t pong_value) MOJO_OVERRIDE { |
+ LOG(ERROR) << "MSW: ExampleClientImpl::Pong: " << pong_value; |
+ EXPECT_EQ(kMagicNumber, pong_value); |
+ // TODO(msw): Disconnect from ExampleService??? Quit properly.... |
+ //base::MessageLoop::current()->Quit(); |
+ RunLoop::current()->Quit(); |
+ } |
+ |
+ private: |
+ ExampleServicePtr example_service_; |
+ static const int16_t kMagicNumber = 42; |
+ MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleClientImpl); |
+}; |
+ |
+ |
+// TODO(msw): Generalize a GTest ApplicationDelegeate for all such tests? |
+class TestApp : public ApplicationDelegate { |
+ public: |
+ TestApp() {} |
+ virtual ~TestApp() {} |
+ |
+ virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE{ |
+ printf("Running TestApp::Initialize() from example_unittest.cc\n"); |
+ // TODO(msw): Get actual commandline arguments... |
+ int argc = 0; |
+ char** argv = NULL; |
+ testing::InitGoogleTest(&argc, argv); |
+ mojo::shell::InitializeLogging(); |
+ |
+ 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).
|
+ LOG(ERROR) << "MSW ServiceManager::GetInstance(): " << service_manager; |
+ //ExampleServiceLoader* default_loader = new ExampleServiceLoader; |
+ //default_loader->set_context(&context_); |
+ service_manager->set_default_loader( |
+ scoped_ptr<ServiceLoader>(new ExampleServiceLoader)); |
+ |
+ // TODO(msw): Connect to service in the test... |
+ ExampleServicePtr example_service; |
+ LOG(ERROR) << "MSW 0"; |
+ //service_manager->ConnectToService( |
+ // GURL("mojo:mojo_example_service"), |
+ // &example_service); |
+ app->ConnectToService("mojo:mojo_example_service", &example_service); |
+ LOG(ERROR) << "MSW 1"; |
+ example_client_.reset(new ExampleClientImpl(example_service.Pass())); |
+ LOG(ERROR) << "MSW 2"; |
+ example_client_->PingExampleService(); |
+ // TODO(msw): Run tests here? |
+ RUN_ALL_TESTS(); |
+ LOG(ERROR) << "MSW 3"; |
+ |
+ // TODO(msw): Disconnect from ExampleService??? Quit properly.... |
+ //base::MessageLoop::current()->Quit(); |
+ //RunLoop::current()->RunUntilIdle(); |
+ RunLoop::current()->Quit(); |
+ } |
+ |
+ private: |
+ // TODO(msw): Expose to ExampleServiceTest ... |
+ scoped_ptr<ExampleClientImpl> example_client_; |
+ MOJO_DISALLOW_COPY_AND_ASSIGN(TestApp); |
+}; |
+ |
+// TODO(msw): Move this and TestApp to a common mojo test helper... |
+// TODO(msw): Make a AppTestBase like ShellTestBase, etc. ??? |
+// static |
+ApplicationDelegate* ApplicationDelegate::Create() { |
+ return new TestApp(); |
+} |
+ |
+} // namespace mojo |