diff options
Diffstat (limited to 'mojo/edk/js/tests')
-rw-r--r-- | mojo/edk/js/tests/BUILD.gn | 21 | ||||
-rw-r--r-- | mojo/edk/js/tests/DEPS | 7 | ||||
-rw-r--r-- | mojo/edk/js/tests/connection_tests.js | 261 | ||||
-rw-r--r-- | mojo/edk/js/tests/js_to_cpp.mojom | 53 | ||||
-rw-r--r-- | mojo/edk/js/tests/js_to_cpp_tests.cc | 418 | ||||
-rw-r--r-- | mojo/edk/js/tests/js_to_cpp_tests.js | 221 | ||||
-rw-r--r-- | mojo/edk/js/tests/run_js_tests.cc | 64 | ||||
-rw-r--r-- | mojo/edk/js/tests/sample_service_tests.js | 168 |
8 files changed, 1136 insertions, 77 deletions
diff --git a/mojo/edk/js/tests/BUILD.gn b/mojo/edk/js/tests/BUILD.gn index 1555328..c61ba37 100644 --- a/mojo/edk/js/tests/BUILD.gn +++ b/mojo/edk/js/tests/BUILD.gn @@ -2,17 +2,26 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -test("js_unittests") { +import("//mojo/public/tools/bindings/mojom.gni") + +source_set("js_to_cpp_tests") { + testonly = true + deps = [ + ":js_to_cpp_bindings", "//gin:gin_test", "//mojo/edk/js", - "//mojo/edk/js:js_unittests", - "//mojo/edk/test:run_all_unittests", "//mojo/edk/test:test_support", - "//mojo/public/cpp/environment:standalone", - "//mojo/public/cpp/utility", + "//mojo/public/cpp/bindings", + "//mojo/public/cpp/system", "//mojo/public/interfaces/bindings/tests:test_interfaces", ] - sources = [ "run_js_tests.cc" ] + sources = [ + "js_to_cpp_tests.cc", + ] +} + +mojom("js_to_cpp_bindings") { + sources = [ "js_to_cpp.mojom" ] } diff --git a/mojo/edk/js/tests/DEPS b/mojo/edk/js/tests/DEPS deleted file mode 100644 index 190ee62..0000000 --- a/mojo/edk/js/tests/DEPS +++ /dev/null @@ -1,7 +0,0 @@ -include_rules = [ - "+base", - "+gin", - "+v8", - "+mojo/edk/js/core.h", - "+mojo/edk/js/support.h", -] diff --git a/mojo/edk/js/tests/connection_tests.js b/mojo/edk/js/tests/connection_tests.js new file mode 100644 index 0000000..6649dfe --- /dev/null +++ b/mojo/edk/js/tests/connection_tests.js @@ -0,0 +1,261 @@ +// Copyright 2013 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. + +// Mock out the support module to avoid depending on the message loop. +define("mojo/public/js/support", ["timer"], function(timer) { + var waitingCallbacks = []; + + function WaitCookie(id) { + this.id = id; + } + + function asyncWait(handle, flags, callback) { + var id = waitingCallbacks.length; + waitingCallbacks.push(callback); + return new WaitCookie(id); + } + + function cancelWait(cookie) { + waitingCallbacks[cookie.id] = null; + } + + function numberOfWaitingCallbacks() { + var count = 0; + for (var i = 0; i < waitingCallbacks.length; ++i) { + if (waitingCallbacks[i]) + ++count; + } + return count; + } + + function pumpOnce(result) { + var callbacks = waitingCallbacks; + waitingCallbacks = []; + for (var i = 0; i < callbacks.length; ++i) { + if (callbacks[i]) + callbacks[i](result); + } + } + + // Queue up a pumpOnce call to execute after the stack unwinds. Use + // this to trigger a pump after all Promises are executed. + function queuePump(result) { + timer.createOneShot(0, pumpOnce.bind(undefined, result)); + } + + var exports = {}; + exports.asyncWait = asyncWait; + exports.cancelWait = cancelWait; + exports.numberOfWaitingCallbacks = numberOfWaitingCallbacks; + exports.pumpOnce = pumpOnce; + exports.queuePump = queuePump; + return exports; +}); + +define([ + "gin/test/expect", + "mojo/public/js/support", + "mojo/public/js/core", + "mojo/public/js/connection", + "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom", + "mojo/public/interfaces/bindings/tests/sample_service.mojom", + "mojo/public/js/threading", + "gc", +], function(expect, + mockSupport, + core, + connection, + sample_interfaces, + sample_service, + threading, + gc) { + testClientServer(); + testWriteToClosedPipe(); + testRequestResponse().then(function() { + this.result = "PASS"; + gc.collectGarbage(); // should not crash + threading.quit(); + }.bind(this)).catch(function(e) { + this.result = "FAIL: " + (e.stack || e); + threading.quit(); + }.bind(this)); + + function testClientServer() { + var receivedFrobinate = false; + var receivedDidFrobinate = false; + + // ServiceImpl ------------------------------------------------------------ + + function ServiceImpl(peer) { + this.peer = peer; + } + + ServiceImpl.prototype = Object.create( + sample_service.Service.stubClass.prototype); + + ServiceImpl.prototype.frobinate = function(foo, baz, port) { + receivedFrobinate = true; + + expect(foo.name).toBe("Example name"); + expect(baz).toBeTruthy(); + expect(core.close(port)).toBe(core.RESULT_OK); + + this.peer.didFrobinate(42); + }; + + // ServiceClientImpl ------------------------------------------------------ + + function ServiceClientImpl(peer) { + this.peer = peer; + } + + ServiceClientImpl.prototype = + Object.create(sample_service.ServiceClient.stubClass.prototype); + + ServiceClientImpl.prototype.didFrobinate = function(result) { + receivedDidFrobinate = true; + + expect(result).toBe(42); + }; + + var pipe = core.createMessagePipe(); + var anotherPipe = core.createMessagePipe(); + var sourcePipe = core.createMessagePipe(); + + var connection0 = new connection.Connection( + pipe.handle0, ServiceImpl, sample_service.ServiceClient.proxyClass); + + var connection1 = new connection.Connection( + pipe.handle1, ServiceClientImpl, sample_service.Service.proxyClass); + + var foo = new sample_service.Foo(); + foo.bar = new sample_service.Bar(); + foo.name = "Example name"; + foo.source = sourcePipe.handle0; + connection1.remote.frobinate(foo, true, anotherPipe.handle0); + + mockSupport.pumpOnce(core.RESULT_OK); + + expect(receivedFrobinate).toBeTruthy(); + expect(receivedDidFrobinate).toBeTruthy(); + + connection0.close(); + connection1.close(); + + expect(mockSupport.numberOfWaitingCallbacks()).toBe(0); + + // sourcePipe.handle0 was closed automatically when sent over IPC. + expect(core.close(sourcePipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); + // sourcePipe.handle1 hasn't been closed yet. + expect(core.close(sourcePipe.handle1)).toBe(core.RESULT_OK); + + // anotherPipe.handle0 was closed automatically when sent over IPC. + expect(core.close(anotherPipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); + // anotherPipe.handle1 hasn't been closed yet. + expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK); + + // The Connection object is responsible for closing these handles. + expect(core.close(pipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT); + expect(core.close(pipe.handle1)).toBe(core.RESULT_INVALID_ARGUMENT); + } + + function testWriteToClosedPipe() { + var pipe = core.createMessagePipe(); + + var connection1 = new connection.Connection( + pipe.handle1, function() {}, sample_service.Service.proxyClass); + + // Close the other end of the pipe. + core.close(pipe.handle0); + + // Not observed yet because we haven't pumped events yet. + expect(connection1.encounteredError()).toBeFalsy(); + + var foo = new sample_service.Foo(); + foo.bar = new sample_service.Bar(); + // TODO(darin): crbug.com/357043: pass null in place of |foo| here. + connection1.remote.frobinate(foo, true, null); + + // Write failures are not reported. + expect(connection1.encounteredError()).toBeFalsy(); + + // Pump events, and then we should start observing the closed pipe. + mockSupport.pumpOnce(core.RESULT_OK); + + expect(connection1.encounteredError()).toBeTruthy(); + + connection1.close(); + } + + function testRequestResponse() { + + // ProviderImpl ------------------------------------------------------------ + + function ProviderImpl(peer) { + this.peer = peer; + } + + ProviderImpl.prototype = + Object.create(sample_interfaces.Provider.stubClass.prototype); + + ProviderImpl.prototype.echoString = function(a) { + mockSupport.queuePump(core.RESULT_OK); + return Promise.resolve({a: a}); + }; + + ProviderImpl.prototype.echoStrings = function(a, b) { + mockSupport.queuePump(core.RESULT_OK); + return Promise.resolve({a: a, b: b}); + }; + + // ProviderClientImpl ------------------------------------------------------ + + function ProviderClientImpl(peer) { + this.peer = peer; + } + + ProviderClientImpl.prototype = + Object.create(sample_interfaces.ProviderClient.stubClass.prototype); + + var pipe = core.createMessagePipe(); + + var connection0 = new connection.Connection( + pipe.handle0, + ProviderImpl, + sample_interfaces.ProviderClient.proxyClass); + + var connection1 = new connection.Connection( + pipe.handle1, + ProviderClientImpl, + sample_interfaces.Provider.proxyClass); + + var origReadMessage = core.readMessage; + // echoString + mockSupport.queuePump(core.RESULT_OK); + return connection1.remote.echoString("hello").then(function(response) { + expect(response.a).toBe("hello"); + }).then(function() { + // echoStrings + mockSupport.queuePump(core.RESULT_OK); + return connection1.remote.echoStrings("hello", "world"); + }).then(function(response) { + expect(response.a).toBe("hello"); + expect(response.b).toBe("world"); + }).then(function() { + // Mock a read failure, expect it to fail. + core.readMessage = function() { + return { result: core.RESULT_UNKNOWN }; + }; + mockSupport.queuePump(core.RESULT_OK); + return connection1.remote.echoString("goodbye"); + }).then(function() { + throw Error("Expected echoString to fail."); + }, function(error) { + expect(error.message).toBe("Connection error: " + core.RESULT_UNKNOWN); + + // Clean up. + core.readMessage = origReadMessage; + }); + } +}); diff --git a/mojo/edk/js/tests/js_to_cpp.mojom b/mojo/edk/js/tests/js_to_cpp.mojom new file mode 100644 index 0000000..69f67b6 --- /dev/null +++ b/mojo/edk/js/tests/js_to_cpp.mojom @@ -0,0 +1,53 @@ +module js_to_cpp; + +// This struct encompasses all of the basic types, so that they +// may be sent from C++ to JS and back for validation. +struct EchoArgs { + int64 si64; + int32 si32; + int16 si16; + int8 si8; + uint64 ui64; + uint32 ui32; + uint16 ui16; + uint8 ui8; + float float_val; + float float_inf; + float float_nan; + double double_val; + double double_inf; + double double_nan; + string? name; + array<string>? string_array; + handle<message_pipe>? message_handle; + handle<data_pipe_consumer>? data_handle; +}; + +struct EchoArgsList { + EchoArgsList? next; + EchoArgs? item; +}; + +// Note: For messages which control test flow, pick numbers that are unlikely +// to be hit as a result of our deliberate corruption of response messages. +interface CppSide { + // Sent for all tests to notify that the JS side is now ready. + StartTest@88888888(); + + // Indicates end for echo, bit-flip, and back-pointer tests. + TestFinished@99999999(); + + // Responses from specific tests. + PingResponse(); + EchoResponse(EchoArgsList list); + BitFlipResponse(EchoArgsList arg); + BackPointerResponse(EchoArgsList arg); +}; + +[Client=CppSide] +interface JsSide { + Ping(); + Echo(int32 numIterations, EchoArgs arg); + BitFlip(EchoArgs arg); + BackPointer(EchoArgs arg); +}; diff --git a/mojo/edk/js/tests/js_to_cpp_tests.cc b/mojo/edk/js/tests/js_to_cpp_tests.cc new file mode 100644 index 0000000..1da70c2 --- /dev/null +++ b/mojo/edk/js/tests/js_to_cpp_tests.cc @@ -0,0 +1,418 @@ +// 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/at_exit.h" +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/macros.h" +#include "base/message_loop/message_loop.h" +#include "base/run_loop.h" +#include "base/strings/utf_string_conversions.h" +#include "gin/array_buffer.h" +#include "gin/public/isolate_holder.h" +#include "mojo/edk/js/mojo_runner_delegate.h" +#include "mojo/edk/js/tests/js_to_cpp.mojom.h" +#include "mojo/edk/test/test_utils.h" +#include "mojo/public/cpp/system/core.h" +#include "mojo/public/cpp/system/macros.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace mojo { +namespace js { + +// Global value updated by some checks to prevent compilers from optimizing +// reads out of existence. +uint32 g_waste_accumulator = 0; + +namespace { + +// Negative numbers with different values in each byte, the last of +// which can survive promotion to double and back. +const int8 kExpectedInt8Value = -65; +const int16 kExpectedInt16Value = -16961; +const int32 kExpectedInt32Value = -1145258561; +const int64 kExpectedInt64Value = -77263311946305LL; + +// Positive numbers with different values in each byte, the last of +// which can survive promotion to double and back. +const uint8 kExpectedUInt8Value = 65; +const uint16 kExpectedUInt16Value = 16961; +const uint32 kExpectedUInt32Value = 1145258561; +const uint64 kExpectedUInt64Value = 77263311946305LL; + +// Double/float values, including special case constants. +const double kExpectedDoubleVal = 3.14159265358979323846; +const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); +const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); +const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); +const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); +const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); + +// NaN has the property that it is not equal to itself. +#define EXPECT_NAN(x) EXPECT_NE(x, x) + +void CheckDataPipe(MojoHandle data_pipe_handle) { + unsigned char buffer[100]; + uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); + MojoResult result = MojoReadData( + data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); + EXPECT_EQ(MOJO_RESULT_OK, result); + EXPECT_EQ(64u, buffer_size); + for (int i = 0; i < 64; ++i) { + EXPECT_EQ(i, buffer[i]); + } +} + +void CheckMessagePipe(MojoHandle message_pipe_handle) { + unsigned char buffer[100]; + uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); + MojoResult result = MojoReadMessage( + message_pipe_handle, buffer, &buffer_size, 0, 0, 0); + EXPECT_EQ(MOJO_RESULT_OK, result); + EXPECT_EQ(64u, buffer_size); + for (int i = 0; i < 64; ++i) { + EXPECT_EQ(255 - i, buffer[i]); + } +} + +js_to_cpp::EchoArgsPtr BuildSampleEchoArgs() { + js_to_cpp::EchoArgsPtr args(js_to_cpp::EchoArgs::New()); + args->si64 = kExpectedInt64Value; + args->si32 = kExpectedInt32Value; + args->si16 = kExpectedInt16Value; + args->si8 = kExpectedInt8Value; + args->ui64 = kExpectedUInt64Value; + args->ui32 = kExpectedUInt32Value; + args->ui16 = kExpectedUInt16Value; + args->ui8 = kExpectedUInt8Value; + args->float_val = kExpectedFloatVal; + args->float_inf = kExpectedFloatInf; + args->float_nan = kExpectedFloatNan; + args->double_val = kExpectedDoubleVal; + args->double_inf = kExpectedDoubleInf; + args->double_nan = kExpectedDoubleNan; + args->name = "coming"; + Array<String> string_array(3); + string_array[0] = "one"; + string_array[1] = "two"; + string_array[2] = "three"; + args->string_array = string_array.Pass(); + return args.Pass(); +} + +void CheckSampleEchoArgs(const js_to_cpp::EchoArgs& arg) { + EXPECT_EQ(kExpectedInt64Value, arg.si64); + EXPECT_EQ(kExpectedInt32Value, arg.si32); + EXPECT_EQ(kExpectedInt16Value, arg.si16); + EXPECT_EQ(kExpectedInt8Value, arg.si8); + EXPECT_EQ(kExpectedUInt64Value, arg.ui64); + EXPECT_EQ(kExpectedUInt32Value, arg.ui32); + EXPECT_EQ(kExpectedUInt16Value, arg.ui16); + EXPECT_EQ(kExpectedUInt8Value, arg.ui8); + EXPECT_EQ(kExpectedFloatVal, arg.float_val); + EXPECT_EQ(kExpectedFloatInf, arg.float_inf); + EXPECT_NAN(arg.float_nan); + EXPECT_EQ(kExpectedDoubleVal, arg.double_val); + EXPECT_EQ(kExpectedDoubleInf, arg.double_inf); + EXPECT_NAN(arg.double_nan); + EXPECT_EQ(std::string("coming"), arg.name.get()); + EXPECT_EQ(std::string("one"), arg.string_array[0].get()); + EXPECT_EQ(std::string("two"), arg.string_array[1].get()); + EXPECT_EQ(std::string("three"), arg.string_array[2].get()); + CheckDataPipe(arg.data_handle.get().value()); + CheckMessagePipe(arg.message_handle.get().value()); +} + +void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { + if (list.is_null()) + return; + CheckSampleEchoArgs(*list->item); + CheckSampleEchoArgsList(list->next); +} + +// More forgiving checks are needed in the face of potentially corrupt +// messages. The values don't matter so long as all accesses are within +// bounds. +void CheckCorruptedString(const String& arg) { + if (arg.is_null()) + return; + for (size_t i = 0; i < arg.size(); ++i) + g_waste_accumulator += arg[i]; +} + +void CheckCorruptedStringArray(const Array<String>& string_array) { + if (string_array.is_null()) + return; + for (size_t i = 0; i < string_array.size(); ++i) + CheckCorruptedString(string_array[i]); +} + +void CheckCorruptedDataPipe(MojoHandle data_pipe_handle) { + unsigned char buffer[100]; + uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); + MojoResult result = MojoReadData( + data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); + if (result != MOJO_RESULT_OK) + return; + for (uint32_t i = 0; i < buffer_size; ++i) + g_waste_accumulator += buffer[i]; +} + +void CheckCorruptedMessagePipe(MojoHandle message_pipe_handle) { + unsigned char buffer[100]; + uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); + MojoResult result = MojoReadMessage( + message_pipe_handle, buffer, &buffer_size, 0, 0, 0); + if (result != MOJO_RESULT_OK) + return; + for (uint32_t i = 0; i < buffer_size; ++i) + g_waste_accumulator += buffer[i]; +} + +void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgsPtr& arg) { + if (arg.is_null()) + return; + CheckCorruptedString(arg->name); + CheckCorruptedStringArray(arg->string_array); + if (arg->data_handle.is_valid()) + CheckCorruptedDataPipe(arg->data_handle.get().value()); + if (arg->message_handle.is_valid()) + CheckCorruptedMessagePipe(arg->message_handle.get().value()); +} + +void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { + if (list.is_null()) + return; + CheckCorruptedEchoArgs(list->item); + CheckCorruptedEchoArgsList(list->next); +} + +// 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 CppSideConnection : public js_to_cpp::CppSide { + public: + CppSideConnection() : + run_loop_(NULL), + js_side_(NULL), + mishandled_messages_(0) { + } + ~CppSideConnection() override {} + + void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } + base::RunLoop* run_loop() { return run_loop_; } + + void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } + js_to_cpp::JsSide* js_side() { return js_side_; } + + // js_to_cpp::CppSide: + void StartTest() override { NOTREACHED(); } + + void TestFinished() override { NOTREACHED(); } + + void PingResponse() override { mishandled_messages_ += 1; } + + void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { + mishandled_messages_ += 1; + } + + void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) override { + mishandled_messages_ += 1; + } + + void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { + mishandled_messages_ += 1; + } + + protected: + base::RunLoop* run_loop_; + js_to_cpp::JsSide* js_side_; + int mishandled_messages_; + + private: + DISALLOW_COPY_AND_ASSIGN(CppSideConnection); +}; + +// Trivial test to verify a message sent from JS is received. +class PingCppSideConnection : public CppSideConnection { + public: + PingCppSideConnection() : got_message_(false) {} + ~PingCppSideConnection() override {} + + // js_to_cpp::CppSide: + void StartTest() override { js_side_->Ping(); } + + void PingResponse() override { + got_message_ = true; + run_loop()->Quit(); + } + + bool DidSucceed() { + return got_message_ && !mishandled_messages_; + } + + private: + bool got_message_; + DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); +}; + +// Test that parameters are passed with correct values. +class EchoCppSideConnection : public CppSideConnection { + public: + EchoCppSideConnection() : + message_count_(0), + termination_seen_(false) { + } + ~EchoCppSideConnection() override {} + + // js_to_cpp::CppSide: + void StartTest() override { + js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); + } + + void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { + const js_to_cpp::EchoArgsPtr& special_arg = list->item; + message_count_ += 1; + EXPECT_EQ(-1, special_arg->si64); + EXPECT_EQ(-1, special_arg->si32); + EXPECT_EQ(-1, special_arg->si16); + EXPECT_EQ(-1, special_arg->si8); + EXPECT_EQ(std::string("going"), special_arg->name.To<std::string>()); + CheckSampleEchoArgsList(list->next); + } + + void TestFinished() override { + termination_seen_ = true; + run_loop()->Quit(); + } + + bool DidSucceed() { + return termination_seen_ && + !mishandled_messages_ && + message_count_ == kExpectedMessageCount; + } + + private: + static const int kExpectedMessageCount = 10; + int message_count_; + bool termination_seen_; + DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); +}; + +// Test that corrupted messages don't wreak havoc. +class BitFlipCppSideConnection : public CppSideConnection { + public: + BitFlipCppSideConnection() : termination_seen_(false) {} + ~BitFlipCppSideConnection() override {} + + // js_to_cpp::CppSide: + void StartTest() override { js_side_->BitFlip(BuildSampleEchoArgs()); } + + void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) override { + CheckCorruptedEchoArgsList(list); + } + + void TestFinished() override { + termination_seen_ = true; + run_loop()->Quit(); + } + + bool DidSucceed() { + return termination_seen_; + } + + private: + bool termination_seen_; + DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection); +}; + +// Test that severely random messages don't wreak havoc. +class BackPointerCppSideConnection : public CppSideConnection { + public: + BackPointerCppSideConnection() : termination_seen_(false) {} + ~BackPointerCppSideConnection() override {} + + // js_to_cpp::CppSide: + void StartTest() override { js_side_->BackPointer(BuildSampleEchoArgs()); } + + void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { + CheckCorruptedEchoArgsList(list); + } + + void TestFinished() override { + termination_seen_ = true; + run_loop()->Quit(); + } + + bool DidSucceed() { + return termination_seen_; + } + + private: + bool termination_seen_; + DISALLOW_COPY_AND_ASSIGN(BackPointerCppSideConnection); +}; + +} // namespace + +class JsToCppTest : public testing::Test { + public: + JsToCppTest() {} + + void RunTest(const std::string& test, CppSideConnection* cpp_side) { + cpp_side->set_run_loop(&run_loop_); + + MessagePipe pipe; + js_to_cpp::JsSidePtr js_side = + MakeProxy<js_to_cpp::JsSide>(pipe.handle0.Pass()); + js_side.set_client(cpp_side); + + js_side.internal_state()->router_for_testing()->EnableTestingMode(); + + cpp_side->set_js_side(js_side.get()); + + gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, + gin::ArrayBufferAllocator::SharedInstance()); + gin::IsolateHolder instance; + MojoRunnerDelegate delegate; + gin::ShellRunner runner(&delegate, instance.isolate()); + delegate.Start(&runner, pipe.handle1.release().value(), test); + + run_loop_.Run(); + } + + private: + base::ShadowingAtExitManager at_exit_; + base::MessageLoop loop; + base::RunLoop run_loop_; + + DISALLOW_COPY_AND_ASSIGN(JsToCppTest); +}; + +TEST_F(JsToCppTest, Ping) { + PingCppSideConnection cpp_side_connection; + RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); + EXPECT_TRUE(cpp_side_connection.DidSucceed()); +} + +TEST_F(JsToCppTest, Echo) { + EchoCppSideConnection cpp_side_connection; + RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); + EXPECT_TRUE(cpp_side_connection.DidSucceed()); +} + +TEST_F(JsToCppTest, BitFlip) { + BitFlipCppSideConnection cpp_side_connection; + RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); + EXPECT_TRUE(cpp_side_connection.DidSucceed()); +} + +TEST_F(JsToCppTest, BackPointer) { + BackPointerCppSideConnection cpp_side_connection; + RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); + EXPECT_TRUE(cpp_side_connection.DidSucceed()); +} + +} // namespace js +} // namespace mojo diff --git a/mojo/edk/js/tests/js_to_cpp_tests.js b/mojo/edk/js/tests/js_to_cpp_tests.js new file mode 100644 index 0000000..c32f0af --- /dev/null +++ b/mojo/edk/js/tests/js_to_cpp_tests.js @@ -0,0 +1,221 @@ +// 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/edk/js/tests/js_to_cpp_tests', [ + 'console', + 'mojo/edk/js/tests/js_to_cpp.mojom', + 'mojo/public/js/connection', + 'mojo/public/js/connector', + 'mojo/public/js/core', +], function (console, jsToCpp, connection, connector, core) { + var retainedConnection; + var sampleData; + var sampleMessage; + var BAD_VALUE = 13; + var DATA_PIPE_PARAMS = { + flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, + elementNumBytes: 1, + capacityNumBytes: 64 + }; + + function JsSideConnection(cppSide) { + this.cppSide_ = cppSide; + cppSide.startTest(); + } + + JsSideConnection.prototype = + Object.create(jsToCpp.JsSide.stubClass.prototype); + + JsSideConnection.prototype.ping = function (arg) { + this.cppSide_.pingResponse(); + }; + + JsSideConnection.prototype.echo = function (numIterations, arg) { + var dataPipe1; + var dataPipe2; + var i; + var messagePipe1; + var messagePipe2; + var specialArg; + + // Ensure expected negative values are negative. + if (arg.si64 > 0) + arg.si64 = BAD_VALUE; + + if (arg.si32 > 0) + arg.si32 = BAD_VALUE; + + if (arg.si16 > 0) + arg.si16 = BAD_VALUE; + + if (arg.si8 > 0) + arg.si8 = BAD_VALUE; + + for (i = 0; i < numIterations; ++i) { + dataPipe1 = core.createDataPipe(DATA_PIPE_PARAMS); + dataPipe2 = core.createDataPipe(DATA_PIPE_PARAMS); + messagePipe1 = core.createMessagePipe(); + messagePipe2 = core.createMessagePipe(); + + arg.data_handle = dataPipe1.consumerHandle; + arg.message_handle = messagePipe1.handle1; + + specialArg = new jsToCpp.EchoArgs(); + specialArg.si64 = -1; + specialArg.si32 = -1; + specialArg.si16 = -1; + specialArg.si8 = -1; + specialArg.name = 'going'; + specialArg.data_handle = dataPipe2.consumerHandle; + specialArg.message_handle = messagePipe2.handle1; + + writeDataPipe(dataPipe1, sampleData); + writeDataPipe(dataPipe2, sampleData); + writeMessagePipe(messagePipe1, sampleMessage); + writeMessagePipe(messagePipe2, sampleMessage); + + this.cppSide_.echoResponse(createEchoArgsList(specialArg, arg)); + + core.close(dataPipe1.producerHandle); + core.close(dataPipe2.producerHandle); + core.close(messagePipe1.handle0); + core.close(messagePipe2.handle0); + } + this.cppSide_.testFinished(); + }; + + JsSideConnection.prototype.bitFlip = function (arg) { + var iteration = 0; + var dataPipe; + var messagePipe; + var proto = connector.Connector.prototype; + var stopSignalled = false; + + proto.realAccept = proto.accept; + proto.accept = function (message) { + var offset = iteration / 8; + var mask; + var value; + if (offset < message.buffer.arrayBuffer.byteLength) { + mask = 1 << (iteration % 8); + value = message.buffer.getUint8(offset) ^ mask; + message.buffer.setUint8(offset, value); + return this.realAccept(message); + } + stopSignalled = true; + return false; + }; + + while (!stopSignalled) { + dataPipe = core.createDataPipe(DATA_PIPE_PARAMS); + messagePipe = core.createMessagePipe(); + writeDataPipe(dataPipe, sampleData); + writeMessagePipe(messagePipe, sampleMessage); + arg.data_handle = dataPipe.consumerHandle; + arg.message_handle = messagePipe.handle1; + + this.cppSide_.bitFlipResponse(createEchoArgsList(arg)); + + core.close(dataPipe.producerHandle); + core.close(messagePipe.handle0); + iteration += 1; + } + + proto.accept = proto.realAccept; + proto.realAccept = null; + this.cppSide_.testFinished(); + }; + + JsSideConnection.prototype.backPointer = function (arg) { + var iteration = 0; + var dataPipe; + var messagePipe; + var proto = connector.Connector.prototype; + var stopSignalled = false; + + proto.realAccept = proto.accept; + proto.accept = function (message) { + var delta = 8 * (1 + iteration % 32); + var offset = 8 * ((iteration / 32) | 0); + if (offset < message.buffer.arrayBuffer.byteLength - 4) { + message.buffer.dataView.setUint32(offset, 0x100000000 - delta, true); + message.buffer.dataView.setUint32(offset + 4, 0xffffffff, true); + return this.realAccept(message); + } + stopSignalled = true; + return false; + }; + + while (!stopSignalled) { + dataPipe = core.createDataPipe(DATA_PIPE_PARAMS); + messagePipe = core.createMessagePipe(); + writeDataPipe(dataPipe, sampleData); + writeMessagePipe(messagePipe, sampleMessage); + arg.data_handle = dataPipe.consumerHandle; + arg.message_handle = messagePipe.handle1; + + this.cppSide_.backPointerResponse(createEchoArgsList(arg)); + + core.close(dataPipe.producerHandle); + core.close(messagePipe.handle0); + iteration += 1; + } + + proto.accept = proto.realAccept; + proto.realAccept = null; + this.cppSide_.testFinished(); + }; + + function writeDataPipe(pipe, data) { + var writeResult = core.writeData( + pipe.producerHandle, data, core.WRITE_DATA_FLAG_ALL_OR_NONE); + + if (writeResult.result != core.RESULT_OK) { + console.log('ERROR: Data pipe write result was ' + writeResult.result); + return false; + } + if (writeResult.numBytes != data.length) { + console.log('ERROR: Data pipe write length was ' + writeResult.numBytes); + return false; + } + return true; + } + + function writeMessagePipe(pipe, arrayBuffer) { + var result = core.writeMessage(pipe.handle0, arrayBuffer, [], 0); + if (result != core.RESULT_OK) { + console.log('ERROR: Message pipe write result was ' + result); + return false; + } + return true; + } + + function createEchoArgsListElement(item, next) { + var list = new jsToCpp.EchoArgsList(); + list.item = item; + list.next = next; + return list; + } + + function createEchoArgsList() { + var genuineArray = Array.prototype.slice.call(arguments); + return genuineArray.reduceRight(function (previous, current) { + return createEchoArgsListElement(current, previous); + }, null); + } + + return function(handle) { + var i; + sampleData = new Uint8Array(DATA_PIPE_PARAMS.capacityNumBytes); + for (i = 0; i < sampleData.length; ++i) { + sampleData[i] = i; + } + sampleMessage = new Uint8Array(DATA_PIPE_PARAMS.capacityNumBytes); + for (i = 0; i < sampleMessage.length; ++i) { + sampleMessage[i] = 255 - i; + } + retainedConnection = new connection.Connection(handle, JsSideConnection, + jsToCpp.CppSide.proxyClass); + }; +}); diff --git a/mojo/edk/js/tests/run_js_tests.cc b/mojo/edk/js/tests/run_js_tests.cc deleted file mode 100644 index 4246f8e..0000000 --- a/mojo/edk/js/tests/run_js_tests.cc +++ /dev/null @@ -1,64 +0,0 @@ -// 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/files/file_path.h" -#include "base/path_service.h" -#include "gin/modules/console.h" -#include "gin/modules/module_registry.h" -#include "gin/modules/timer.h" -#include "gin/test/file_runner.h" -#include "gin/test/gtest.h" -#include "mojo/edk/js/core.h" -#include "mojo/edk/js/support.h" -#include "mojo/public/cpp/environment/environment.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace mojo { -namespace js { -namespace { - -class TestRunnerDelegate : public gin::FileRunnerDelegate { - public: - TestRunnerDelegate() { - AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule); - AddBuiltinModule(Core::kModuleName, Core::GetModule); - AddBuiltinModule(Support::kModuleName, Support::GetModule); - } - - private: - DISALLOW_COPY_AND_ASSIGN(TestRunnerDelegate); -}; - -void RunTest(std::string test, bool run_until_idle) { - Environment env; - base::FilePath path; - PathService::Get(base::DIR_SOURCE_ROOT, &path); - path = path.AppendASCII("mojo") - .AppendASCII("public") - .AppendASCII("js") - .AppendASCII(test); - TestRunnerDelegate delegate; - gin::RunTestFromFile(path, &delegate, run_until_idle); -} - -// TODO(abarth): Should we autogenerate these stubs from GYP? -TEST(JSTest, core) { - RunTest("core_unittests.js", true); -} - -TEST(JSTest, codec) { - RunTest("codec_unittests.js", true); -} - -TEST(JSTest, struct) { - RunTest("struct_unittests.js", true); -} - -TEST(JSTest, validation) { - RunTest("validation_unittests.js", true); -} - -} // namespace -} // namespace js -} // namespace mojo diff --git a/mojo/edk/js/tests/sample_service_tests.js b/mojo/edk/js/tests/sample_service_tests.js new file mode 100644 index 0000000..ca4f8e6 --- /dev/null +++ b/mojo/edk/js/tests/sample_service_tests.js @@ -0,0 +1,168 @@ +// Copyright 2013 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([ + "console", + "mojo/edk/js/test/hexdump", + "gin/test/expect", + "mojo/public/interfaces/bindings/tests/sample_service.mojom", + "mojo/public/interfaces/bindings/tests/sample_import.mojom", + "mojo/public/interfaces/bindings/tests/sample_import2.mojom", + ], function(console, hexdump, expect, sample, imported, imported2) { + + var global = this; + + // Set this variable to true to print the binary message in hex. + var dumpMessageAsHex = false; + + function makeFoo() { + var bar = new sample.Bar(); + bar.alpha = 20; + bar.beta = 40; + bar.gamma = 60; + bar.type = sample.Bar.Type.VERTICAL; + + var extra_bars = new Array(3); + for (var i = 0; i < extra_bars.length; ++i) { + var base = i * 100; + var type = i % 2 ? + sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL; + extra_bars[i] = new sample.Bar(); + extra_bars[i].alpha = base; + extra_bars[i].beta = base + 20; + extra_bars[i].gamma = base + 40; + extra_bars[i].type = type; + } + + var data = new Array(10); + for (var i = 0; i < data.length; ++i) { + data[i] = data.length - i; + } + + var source = 0xFFFF; // Invent a dummy handle. + + var foo = new sample.Foo(); + foo.name = "foopy"; + foo.x = 1; + foo.y = 2; + foo.a = false; + foo.b = true; + foo.c = false; + foo.bar = bar; + foo.extra_bars = extra_bars; + foo.data = data; + foo.source = source; + return foo; + } + + // Check that the given |Foo| is identical to the one made by |MakeFoo()|. + function checkFoo(foo) { + expect(foo.name).toBe("foopy"); + expect(foo.x).toBe(1); + expect(foo.y).toBe(2); + expect(foo.a).toBeFalsy(); + expect(foo.b).toBeTruthy(); + expect(foo.c).toBeFalsy(); + expect(foo.bar.alpha).toBe(20); + expect(foo.bar.beta).toBe(40); + expect(foo.bar.gamma).toBe(60); + expect(foo.bar.type).toBe(sample.Bar.Type.VERTICAL); + + expect(foo.extra_bars.length).toBe(3); + for (var i = 0; i < foo.extra_bars.length; ++i) { + var base = i * 100; + var type = i % 2 ? + sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL; + expect(foo.extra_bars[i].alpha).toBe(base); + expect(foo.extra_bars[i].beta).toBe(base + 20); + expect(foo.extra_bars[i].gamma).toBe(base + 40); + expect(foo.extra_bars[i].type).toBe(type); + } + + expect(foo.data.length).toBe(10); + for (var i = 0; i < foo.data.length; ++i) + expect(foo.data[i]).toBe(foo.data.length - i); + + expect(foo.source).toBe(0xFFFF); + } + + // Check that values are set to the defaults if we don't override them. + function checkDefaultValues() { + var bar = new sample.Bar(); + expect(bar.alpha).toBe(255); + expect(bar.type).toBe(sample.Bar.Type.VERTICAL); + + var foo = new sample.Foo(); + expect(foo.name).toBe("Fooby"); + expect(foo.a).toBeTruthy(); + expect(foo.data).toBeNull(); + + var defaults = new sample.DefaultsTest(); + expect(defaults.a0).toBe(-12); + expect(defaults.a1).toBe(sample.kTwelve); + expect(defaults.a2).toBe(1234); + expect(defaults.a3).toBe(34567); + expect(defaults.a4).toBe(123456); + expect(defaults.a5).toBe(3456789012); + expect(defaults.a6).toBe(-111111111111); + // JS doesn't have a 64 bit integer type so this is just checking that the + // expected and actual values have the same closest double value. + expect(defaults.a7).toBe(9999999999999999999); + expect(defaults.a8).toBe(0x12345); + expect(defaults.a9).toBe(-0x12345); + expect(defaults.a10).toBe(1234); + expect(defaults.a11).toBe(true); + expect(defaults.a12).toBe(false); + expect(defaults.a13).toBe(123.25); + expect(defaults.a14).toBe(1234567890.123); + expect(defaults.a15).toBe(1E10); + expect(defaults.a16).toBe(-1.2E+20); + expect(defaults.a17).toBe(1.23E-20); + expect(defaults.a20).toBe(sample.Bar.Type.BOTH); + expect(defaults.a21).toBeNull(); + expect(defaults.a22).toBeTruthy(); + expect(defaults.a22.shape).toBe(imported.Shape.RECTANGLE); + expect(defaults.a22.color).toBe(imported2.Color.BLACK); + expect(defaults.a21).toBeNull(); + expect(defaults.a23).toBe(0xFFFFFFFFFFFFFFFF); + expect(defaults.a24).toBe(0x123456789); + expect(defaults.a25).toBe(-0x123456789); + } + + function ServiceImpl() { + } + + ServiceImpl.prototype = Object.create(sample.Service.stubClass.prototype); + + ServiceImpl.prototype.frobinate = function(foo, baz, port) { + checkFoo(foo); + expect(baz).toBe(sample.Service.BazOptions.EXTRA); + expect(port).toBe(10); + global.result = "PASS"; + }; + + function SimpleMessageReceiver() { + } + + SimpleMessageReceiver.prototype.accept = function(message) { + if (dumpMessageAsHex) { + var uint8Array = new Uint8Array(message.buffer.arrayBuffer); + console.log(hexdump.dumpArray(uint8Array)); + } + // Imagine some IPC happened here. + var serviceImpl = new ServiceImpl(); + serviceImpl.accept(message); + }; + + var receiver = new SimpleMessageReceiver(); + var serviceProxy = new sample.Service.proxyClass(receiver); + + checkDefaultValues(); + + var foo = makeFoo(); + checkFoo(foo); + + var port = 10; + serviceProxy.frobinate(foo, sample.Service.BazOptions.EXTRA, port); +}); |