diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-30 14:52:45 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-30 14:52:45 +0000 |
commit | 2031f930f895d1a308e85036139abc2fb5a98c61 (patch) | |
tree | 0c57c454dbfbaf944e0198452e9e29c66740fc2a /mojo/apps | |
parent | d3f475ad87623c6d46a7f9f4755fa29e182c9907 (diff) | |
download | chromium_src-2031f930f895d1a308e85036139abc2fb5a98c61.zip chromium_src-2031f930f895d1a308e85036139abc2fb5a98c61.tar.gz chromium_src-2031f930f895d1a308e85036139abc2fb5a98c61.tar.bz2 |
Adds mojo test that exercises mojo bindings to c++
BUG=none
TEST=none
R=mpcomplete@chromium.org
Review URL: https://codereview.chromium.org/253173003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267208 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/apps')
-rw-r--r-- | mojo/apps/js/mojo_runner_delegate.cc | 4 | ||||
-rw-r--r-- | mojo/apps/js/test/js_to_cpp_unittest.cc | 138 | ||||
-rw-r--r-- | mojo/apps/js/test/js_to_cpp_unittest.js | 23 |
3 files changed, 164 insertions, 1 deletions
diff --git a/mojo/apps/js/mojo_runner_delegate.cc b/mojo/apps/js/mojo_runner_delegate.cc index 13081e4..94500c2 100644 --- a/mojo/apps/js/mojo_runner_delegate.cc +++ b/mojo/apps/js/mojo_runner_delegate.cc @@ -17,6 +17,7 @@ #include "mojo/bindings/js/core.h" #include "mojo/bindings/js/handle.h" #include "mojo/bindings/js/support.h" +#include "mojo/bindings/js/unicode.h" namespace mojo { namespace apps { @@ -53,7 +54,8 @@ MojoRunnerDelegate::MojoRunnerDelegate() AddBuiltinModule(gin::TimerModule::kName, gin::TimerModule::GetModule); AddBuiltinModule(js::Core::kModuleName, js::Core::GetModule); AddBuiltinModule(js::Support::kModuleName, js::Support::GetModule); - AddBuiltinModule(mojo::js::gl::kModuleName, mojo::js::gl::GetModule); + AddBuiltinModule(js::Unicode::kModuleName,js::Unicode::GetModule); + AddBuiltinModule(js::gl::kModuleName, mojo::js::gl::GetModule); AddBuiltinModule(MonotonicClock::kModuleName, MonotonicClock::GetModule); AddBuiltinModule(Threading::kModuleName, Threading::GetModule); } diff --git a/mojo/apps/js/test/js_to_cpp_unittest.cc b/mojo/apps/js/test/js_to_cpp_unittest.cc new file mode 100644 index 0000000..3b19142 --- /dev/null +++ b/mojo/apps/js/test/js_to_cpp_unittest.cc @@ -0,0 +1,138 @@ +// 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/file_util.h" +#include "base/files/file_path.h" +#include "base/message_loop/message_loop.h" +#include "base/run_loop.h" +#include "base/strings/utf_string_conversions.h" +#include "gin/public/isolate_holder.h" +#include "mojo/apps/js/mojo_runner_delegate.h" +#include "mojo/common/common_type_converters.h" +#include "mojo/common/test/test_utils.h" +#include "mojo/public/cpp/bindings/remote_ptr.h" +#include "mojo/public/cpp/environment/environment.h" +#include "mojo/public/cpp/system/core.h" +#include "mojo/public/cpp/system/macros.h" +#include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace mojo { +namespace js { +namespace { + +// Base Provider implementation class. It's expected that tests subclass and +// override the appropriate Provider functions. When test is done quit the +// run_loop(). +class ProviderConnection : public sample::Provider { + public: + ProviderConnection() : run_loop_(NULL), client_(NULL) { + } + virtual ~ProviderConnection() {} + + void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } + base::RunLoop* run_loop() { return run_loop_; } + + void set_client(sample::ProviderClient* client) { client_ = client; } + sample::ProviderClient* client() { return client_; } + + // sample::Provider: + virtual void EchoString(const String& a, + const Callback<void(String)>& callback) OVERRIDE { + NOTREACHED(); + } + virtual void EchoStrings( + const String& a, + const String& b, + const Callback<void(String, String)>& callback) OVERRIDE { + NOTREACHED(); + } + virtual void EchoMessagePipeHandle( + ScopedMessagePipeHandle a, + const Callback<void(ScopedMessagePipeHandle)>& callback) OVERRIDE { + NOTREACHED(); + } + virtual void EchoEnum(sample::Enum a, + const Callback<void(sample::Enum)>& callback) + OVERRIDE { + NOTREACHED(); + } + + private: + base::RunLoop* run_loop_; + sample::ProviderClient* client_; + + DISALLOW_COPY_AND_ASSIGN(ProviderConnection); +}; + +class JsToCppTest : public testing::Test { + public: + JsToCppTest() {} + + void RunTest(const std::string& test, ProviderConnection* provider) { + provider->set_run_loop(&run_loop_); + InterfacePipe<sample::Provider, sample::ProviderClient> pipe; + RemotePtr<sample::ProviderClient> provider_client; + provider_client.reset(pipe.handle_to_peer.Pass(), provider); + + provider->set_client(provider_client.get()); + + gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); + apps::MojoRunnerDelegate delegate; + gin::ShellRunner runner(&delegate, instance.isolate()); + delegate.Start(&runner, pipe.handle_to_self.release().value(), + test); + + run_loop_.Run(); + } + + private: + Environment environment; + base::MessageLoop loop; + base::RunLoop run_loop_; + + DISALLOW_COPY_AND_ASSIGN(JsToCppTest); +}; + +// Trivial test to verify a message sent from JS is received. +class FromJsProviderConnection : public ProviderConnection { + public: + explicit FromJsProviderConnection() {} + virtual ~FromJsProviderConnection() { + } + + const base::string16& echo_string() const { return echo_string_; } + + // Provider: + virtual void EchoString(const String& a, + const Callback<void(String)>& callback) OVERRIDE { + echo_string_ = a.To<base::string16>(); + run_loop()->Quit(); + } + + private: + base::string16 echo_string_; + + DISALLOW_COPY_AND_ASSIGN(FromJsProviderConnection); +}; + +TEST_F(JsToCppTest, FromJS) { + // TODO(yzshen): Remove this check once isolated tests are supported on the + // Chromium waterfall. (http://crbug.com/351214) + const base::FilePath test_file_path( + test::GetFilePathForJSResource( + "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); + if (!base::PathExists(test_file_path)) { + LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; + return; + } + + FromJsProviderConnection provider; + RunTest("mojo/apps/js/test/js_to_cpp_unittest", &provider); + EXPECT_EQ("message", base::UTF16ToASCII(provider.echo_string())); +} + +} // namespace +} // namespace js +} // namespace mojo diff --git a/mojo/apps/js/test/js_to_cpp_unittest.js b/mojo/apps/js/test/js_to_cpp_unittest.js new file mode 100644 index 0000000..45bb411 --- /dev/null +++ b/mojo/apps/js/test/js_to_cpp_unittest.js @@ -0,0 +1,23 @@ +// 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. + +define("mojo/apps/js/test/js_to_cpp_unittest", [ + 'mojo/public/js/bindings/connection', + "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom", +], function(connector, provider) { + var connection; + + function ProviderClientConnection(provider) { + this.provider_ = provider; + provider.echoString("message"); + } + + ProviderClientConnection.prototype = + Object.create(provider.ProviderClientStub.prototype); + + return function(handle) { + connection = new connector.Connection(handle, ProviderClientConnection, + provider.ProviderProxy); + }; +}); |