Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(877)

Unified Diff: mojo/examples/test/example_unittest.cc

Issue 399653004: Add a Mojo example apptest that runs in mojo_shell. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments; ShellTestHelper crashes, use a hack workaround. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/examples/test/example_service_impl.cc ('k') | mojo/mojo.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..a13ccc92bc39b00038cb85b4fb8d82252626e465
--- /dev/null
+++ b/mojo/examples/test/example_unittest.cc
@@ -0,0 +1,134 @@
+// 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 "mojo/examples/test/example_service.mojom.h"
+#include "mojo/examples/test/example_service_impl.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/shell/init.h"
+#include "mojo/shell/shell_test_helper.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace mojo {
+
+namespace {
+
+// TODO(msw): Avoid this global by fixing ShellTestHelper ...
+ApplicationImpl* g_application_impl_hack_ = NULL;
+
+// 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() {
+ LOG(ERROR) << "MSW: ExampleClientImpl::PingExampleService.";
+ 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);
+ }
+
+ private:
+ ExampleServicePtr example_service_;
+ static const int16_t kMagicNumber = 42;
+ MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleClientImpl);
+};
+
+class ExampleServiceTest : public testing::Test {
+ public:
+ ExampleServiceTest() {}
+
+ virtual void SetUp() MOJO_OVERRIDE {
+ // TODO(msw): ShellTestHelper::Init causes an exception via Context's ctor:
+ // unknown file: error: SEH exception with code 0xc0000005 thrown in SetUp().
+ // With a workaround hack of GLES2Support::Init, it still asserts/crashes:
+ // Assertion failed: result == MOJO_RESULT_OK, file d:\src\chrome-git\src\mojo\public\cpp\system\core.h, line 153
+ //test_helper_.Init();
+
+ //ExampleServicePtr example_service;
+ //test_helper_.service_manager()->ConnectToService(
+ // GURL("mojo:mojo_example_service"),
+ // &example_service);
+ //example_client_.reset(new ExampleClientImpl(example_service.Pass()));
+
+ // TODO(msw): This is destroyed before example_client_'s Pong is called...
+ ExampleServicePtr example_service;
+ g_application_impl_hack_->ConnectToService("mojo:mojo_example_service",
+ &example_service);
+ example_client_.reset(new ExampleClientImpl(example_service.Pass()));
+ }
+
+ virtual void TearDown() MOJO_OVERRIDE {}
+
+ protected:
+ base::MessageLoop loop_;
+ shell::ShellTestHelper test_helper_;
+ scoped_ptr<ExampleClientImpl> example_client_;
+
+ private:
+ MOJO_DISALLOW_COPY_AND_ASSIGN(ExampleServiceTest);
+};
+
+// Test ExampleService's Ping.
+TEST_F(ExampleServiceTest, Ping) {
+ LOG(ERROR) << "MSW ExampleServiceTest.Ping";
+ example_client_->PingExampleService();
+}
+
+// TODO(msw): Writer better tests; eg. for ExampleService::Echo ...
+TEST_F(ExampleServiceTest, PingAgain) {
+ LOG(ERROR) << "MSW ExampleServiceTest.PingAgain";
+ example_client_->PingExampleService();
+}
+
+// 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();
+
+ 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
+
+ // TODO(msw): See mojo/common/test/run_all_unittests.cc
+ RUN_ALL_TESTS();
+ // TODO(msw): Quit properly...
+ }
+
+ private:
+ MOJO_DISALLOW_COPY_AND_ASSIGN(TestApp);
+};
+
+} // namespace
+
+// 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
« no previous file with comments | « mojo/examples/test/example_service_impl.cc ('k') | mojo/mojo.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698