js_to_cpp_unittest.cc revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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/file_util.h" 6#include "base/files/file_path.h" 7#include "base/message_loop/message_loop.h" 8#include "base/run_loop.h" 9#include "base/strings/utf_string_conversions.h" 10#include "gin/public/isolate_holder.h" 11#include "mojo/apps/js/mojo_runner_delegate.h" 12#include "mojo/common/common_type_converters.h" 13#include "mojo/common/test/test_utils.h" 14#include "mojo/public/cpp/bindings/remote_ptr.h" 15#include "mojo/public/cpp/environment/environment.h" 16#include "mojo/public/cpp/system/core.h" 17#include "mojo/public/cpp/system/macros.h" 18#include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" 19#include "testing/gtest/include/gtest/gtest.h" 20 21namespace mojo { 22namespace js { 23namespace { 24 25// Base Provider implementation class. It's expected that tests subclass and 26// override the appropriate Provider functions. When test is done quit the 27// run_loop(). 28class ProviderConnection : public sample::Provider { 29 public: 30 ProviderConnection() : run_loop_(NULL), client_(NULL) { 31 } 32 virtual ~ProviderConnection() {} 33 34 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } 35 base::RunLoop* run_loop() { return run_loop_; } 36 37 void set_client(sample::ProviderClient* client) { client_ = client; } 38 sample::ProviderClient* client() { return client_; } 39 40 // sample::Provider: 41 virtual void EchoString(const String& a, 42 const Callback<void(String)>& callback) OVERRIDE { 43 NOTREACHED(); 44 } 45 virtual void EchoStrings( 46 const String& a, 47 const String& b, 48 const Callback<void(String, String)>& callback) OVERRIDE { 49 NOTREACHED(); 50 } 51 virtual void EchoMessagePipeHandle( 52 ScopedMessagePipeHandle a, 53 const Callback<void(ScopedMessagePipeHandle)>& callback) OVERRIDE { 54 NOTREACHED(); 55 } 56 virtual void EchoEnum(sample::Enum a, 57 const Callback<void(sample::Enum)>& callback) 58 OVERRIDE { 59 NOTREACHED(); 60 } 61 62 private: 63 base::RunLoop* run_loop_; 64 sample::ProviderClient* client_; 65 66 DISALLOW_COPY_AND_ASSIGN(ProviderConnection); 67}; 68 69class JsToCppTest : public testing::Test { 70 public: 71 JsToCppTest() {} 72 73 void RunTest(const std::string& test, ProviderConnection* provider) { 74 provider->set_run_loop(&run_loop_); 75 InterfacePipe<sample::Provider, sample::ProviderClient> pipe; 76 RemotePtr<sample::ProviderClient> provider_client; 77 provider_client.reset(pipe.handle_to_peer.Pass(), provider); 78 79 provider->set_client(provider_client.get()); 80 81 gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); 82 apps::MojoRunnerDelegate delegate; 83 gin::ShellRunner runner(&delegate, instance.isolate()); 84 delegate.Start(&runner, pipe.handle_to_self.release().value(), 85 test); 86 87 run_loop_.Run(); 88 } 89 90 private: 91 Environment environment; 92 base::MessageLoop loop; 93 base::RunLoop run_loop_; 94 95 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); 96}; 97 98// Trivial test to verify a message sent from JS is received. 99class FromJsProviderConnection : public ProviderConnection { 100 public: 101 explicit FromJsProviderConnection() {} 102 virtual ~FromJsProviderConnection() { 103 } 104 105 const base::string16& echo_string() const { return echo_string_; } 106 107 // Provider: 108 virtual void EchoString(const String& a, 109 const Callback<void(String)>& callback) OVERRIDE { 110 echo_string_ = a.To<base::string16>(); 111 run_loop()->Quit(); 112 } 113 114 private: 115 base::string16 echo_string_; 116 117 DISALLOW_COPY_AND_ASSIGN(FromJsProviderConnection); 118}; 119 120TEST_F(JsToCppTest, FromJS) { 121 // TODO(yzshen): Remove this check once isolated tests are supported on the 122 // Chromium waterfall. (http://crbug.com/351214) 123 const base::FilePath test_file_path( 124 test::GetFilePathForJSResource( 125 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); 126 if (!base::PathExists(test_file_path)) { 127 LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; 128 return; 129 } 130 131 FromJsProviderConnection provider; 132 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &provider); 133 EXPECT_EQ("message", base::UTF16ToASCII(provider.echo_string())); 134} 135 136} // namespace 137} // namespace js 138} // namespace mojo 139