Files
cefsharp/CefSharp.Example/Resources/BindingTest.html
T
Alex Maitland 44fdfa94c8 Implement Javascript Binding v2 (#2247)
* JSB Rewrite - Add CefSharp.RegisterBoundObject javascript method

To get around the problem of IPC timing, I've reversed the communication, now it's the render process requesting
the bound objects, the implementation is incomplete at this stage, more just a basic proof of concept

Rewrite BindingTest.html using QUnit
One of the tests doesn't map correctly as it requires def user interaction

TODO:

- Get objects by name
- Rename Messages.h values that relate to this change so they are more descriptive
- Better error handling
- Caching of objects within a render process
- Investigate global context options, don't think it's possible to call an async method directly in the global context

# Conflicts:
#	CefSharp.Example/Resources/BindingTest.html

* JSB Improve Request/Response message names

* BindingTest.html - Output stress test results

* Remove JavascriptRootObject was leftover from time we used WCF to transmit objects

* JavascriptObjectRepository - Objects are now sent in the same list with an IsAsync flag

Separation when sending over IPC with two unique lists isn't necessary

* JavascriptObjectRepository - Return objects by name or return all by default

Untabify CefAppUnmanagedWrapper.cpp
Add minor comment to CefBrowserWrapper for a reminder that we use Frame Identifier as dictionary key

* Add //TODO: JSB comments for work that's left to be done

Ideally within a render process we cache bound objects once they've been communicated,
It would also be nice to make multiple binding requests so objects can later be added dynamically

* JSB Allow multiple calls to CefSharp.RegisterBoundObject

Update BindingTest.html to name two distinct calls to different bound objects

* JSB - Add some notes about caching, no working implementation yet

* JSB - Dynamically Register object on Request

* JSB Add ability to unbind an object and rename RegisterBoundObject to BindObjectAsync

# Conflicts:
#	CefSharp.Example/Resources/BindingTest.html

* JSB BindObjectAsync - if objects already bound then return false

* JSB - Ignore Indexer properties

Were previously throwing an exception

* JSB - Add LegacyJavascriptBindingEnabled option so preserve existing behaviour

This is only useful for SPA application and those that only navigate to pages
hosting within a single domain. Any sort of cross-site navigation and a new
render process will be spawned and objects won't be bound automatically
(You can use the new methods to request they're bound yourself, at least in theory,
more testing required on this)

* Add LegacyBindingTest.html

Current disabled by default. To enable uncomment CefSharpSettings.LegacyJavascriptBindingEnabled = true;
in CefExample.cs

* RegisterJsObject and RegisterAsyncJsObject can now only be used when CefSharpSettings.LegacyJavascriptBindingEnabled = true

See https://github.com/cefsharp/CefSharp/issues/2246 for details
2018-01-22 11:33:09 +10:00

388 lines
16 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Binding Test</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.4.1.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.4.1.js"></script>
<!--<script type="text/javascript">
(async function() {
// <embed user provided code here>
await CefSharp.BindObjectAsync("boundAsync", "bound");
})();
(async () => {
await CefSharp.BindObjectAsync("boundAsync", "bound");
})();
</script>-->
<script type="text/javascript">
(async () =>
{
await CefSharp.BindObjectAsync("boundAsync", "bound");
QUnit.test( "BindObjectAsync Second call", function( assert )
{
let asyncCallback = assert.async();
CefSharp.BindObjectAsync("boundAsync", "bound").then(function(res)
{
assert.equal(false, res, "Second call to BindObjectAsync with already bound objects as params returned false.");
asyncCallback();
});
});
QUnit.test( "bound.repeat('hi ', 5)", function( assert )
{
const expectedResult = "hi hi hi hi hi "
let actualResult = bound.repeat("hi ", 5);
assert.equal( actualResult, expectedResult, "We expect value to be " + actualResult );
});
QUnit.test( "Function delegate to c# method", function( assert )
{
function myFunction(functionParam)
{
return functionParam();
}
const expectedResult = "42"
let actualResult = myFunction(bound.echoMyProperty);
assert.equal( actualResult, expectedResult, "We expect value to be " + actualResult );
});
QUnit.test( "Function returning complex type", function( assert )
{
function myFunction(functionParam)
{
return functionParam();
}
const expectedResult = "This is a very simple property."
let actualResult = bound.getSubObject().simpleProperty;
assert.equal( actualResult, expectedResult, "Call to bound.getSubObject().simpleProperty resulted in : " + actualResult );
});
QUnit.test("Stress Test", function( assert )
{
let stressTestCallCount = 1000;
for (var i = 1; i <= stressTestCallCount; i++)
{
assert.ok(true, bound.repeat("hi ", 5));
}
assert.ok(true, "Stress Test done with : " + stressTestCallCount + " call to bound.repeat(\"hi \", 5)");
});
QUnit.test( "JSON Serializer Test", function( assert )
{
var json = bound.returnJsonEmployeeList();
var jsonObj = JSON.parse(json);
assert.ok(jsonObj.employees.length > 0, "Employee Count : " + jsonObj.employees.length);
var employees = jsonObj.employees;
assert.equal("John Doe", employees[0].firstName + " " + employees[0].lastName, "Employee : " + employees[0].firstName + " " + employees[0].lastName);
assert.equal("Anna Smith", employees[1].firstName + " " + employees[1].lastName, "Employee : " + employees[1].firstName + " " + employees[1].lastName);
assert.equal("Peter Jones", employees[2].firstName + " " + employees[2].lastName, "Employee : " + employees[2].firstName + " " + employees[2].lastName);
});
QUnit.test( "Javascript function sending variable number of parameters to Bound Object. (ParamArray Test)", function( assert )
{
assert.equal(bound.methodWithParams('With 1 Params', 'hello-world'), "Name:With 1 Params;Args:hello-world", "Method with paramArray with one param");
assert.equal(bound.methodWithParams('With 2 Params', 'hello-world', 'chris was here'), "Name:With 2 Params;Args:hello-world, chris was here", "Method with paramArray with two params");
assert.equal(bound.methodWithParams('With no Params'), "Name:With no Params;Args:", "Method with paramArary no params passed");
assert.equal(bound.methodWithoutParams('Normal Method', 'hello'), "Normal Method, hello", "Method with one param (not param array)");
assert.equal(bound.methodWithoutAnything(), "Method without anything called and returned successfully.", "Method without params");
assert.equal(bound.methodWithThreeParamsOneOptionalOneArray(null), "MethodWithThreeParamsOneOptionalOneArray:No Name Specified - No Optional Param Specified;Args:", "bound.methodWithThreeParamsOneOptionalOneArray(null)");
assert.equal(bound.methodWithThreeParamsOneOptionalOneArray(null, null), "MethodWithThreeParamsOneOptionalOneArray:No Name Specified - No Optional Param Specified;Args:", "bound.methodWithThreeParamsOneOptionalOneArray(null, null)");
assert.equal(bound.methodWithThreeParamsOneOptionalOneArray("Test", null), "MethodWithThreeParamsOneOptionalOneArray:Test - No Optional Param Specified;Args:", "bound.methodWithThreeParamsOneOptionalOneArray('Test', null)");
assert.equal(bound.methodWithThreeParamsOneOptionalOneArray(null, null, "Arg1", "Arg2"), "MethodWithThreeParamsOneOptionalOneArray:No Name Specified - No Optional Param Specified;Args:Arg1, Arg2", "bound.methodWithThreeParamsOneOptionalOneArray(null, null, 'Arg1', 'Arg2')");
});
QUnit.test("Methods and Properties on bound object 'bound':", function( assert )
{
for (var name in bound)
{
if (bound[name].constructor.name != 'Function') continue;
assert.ok(true, "Property: " + name);
}
for (var name in bound)
{
if (bound[name].constructor.name === 'Function') continue;
assert.ok(true, "Function: " + name);
}
for (var name in bound)
{
if (typeof bound[name] == "object" && bound[name] !== null)
{
for (var sub in bound[name])
{
var type = bound[name][sub].constructor.name === 'Function' ? "Function" : "Property";
assert.ok(true, name + "." + sub + "(" + type + ")");
}
}
}
});
QUnit.test( "Async call (Throw .Net Exception)", function( assert )
{
var asyncCallback = assert.async();
boundAsync.error().catch(function (e)
{
assert.ok( true, "Error: " + e );
asyncCallback();
});
});
QUnit.test( "Async call (Divide 16 / 2):", function( assert )
{
var asyncCallback = assert.async();
boundAsync.div(16, 2).then(function (actualResult)
{
const expectedResult = 8
assert.equal(expectedResult, actualResult, "Divide 16 / 2 resulted in " + expectedResult);
asyncCallback();
});
});
QUnit.test( "Async call (Divide 16 /0)", function( assert )
{
var asyncCallback = assert.async();
boundAsync.div(16, 0).then(function (res)
{
//Will always throw exception
},
function (e)
{
assert.ok(true, "Error: " + e + "(" + Date() + ")");
asyncCallback();
});
});
QUnit.test( "Async call (Hello):", function( assert )
{
var asyncCallback = assert.async();
boundAsync.hello('CefSharp').then(function (res)
{
assert.equal(res, "Hello CefSharp")
asyncCallback();
});
});
QUnit.test("Async call (Long Running Task):", function( assert )
{
var asyncCallback = assert.async();
boundAsync.doSomething().then(function (res)
{
assert.ok(true, "Slept for 1000ms")
asyncCallback();
});
});
QUnit.test("Async call (Object):", function( assert )
{
var asyncCallback = assert.async();
boundAsync.returnObject('CefSharp').then(function (res)
{
assert.equal(res.Value, "CefSharp", "JSON Object with a single field");
asyncCallback();
});
});
QUnit.test("Async call (returnStructArray):", function( assert )
{
var asyncCallback = assert.async();
boundAsync.returnStructArray('CefSharp').then(function (res)
{
assert.equal(res[0].Value, "CefSharpItem1", "Expected Result of CefSharpItem1");
assert.equal(res[1].Value, "CefSharpItem2", "Expected Result of CefSharpItem2");
asyncCallback();
});
});
QUnit.test("Async call (returnClassesArray):", function( assert )
{
var asyncCallback = assert.async();
boundAsync.returnClassesArray('CefSharp').then(function (res)
{
assert.equal(res[0].Value, "CefSharpItem1", "Expected Result of CefSharpItem1");
assert.equal(res[1].Value, "CefSharpItem2", "Expected Result of CefSharpItem2");
asyncCallback();
});
});
QUnit.test("Async call (echoArray):", function (assert)
{
var asyncCallback = assert.async();
boundAsync.echoArray(["one", null, "three"]).then(function (res)
{
assert.equal(res.length, 3, "Expected result to be length 3");
assert.equal(res[0], "one", "Expected Result of 1st item");
assert.equal(res[1], null, "Expected Result of 2nd item");
assert.equal(res[2], "three", "Expected Result of 3rd item");
asyncCallback();
});
});
QUnit.test("Async call (echoValueTypeArray):", function (assert)
{
var asyncCallback = assert.async();
boundAsync.echoValueTypeArray([1, null, 3]).then(function (res)
{
assert.equal(res.length, 3, "Expected result to be length 3");
assert.equal(res[0], 1, "Expected Result of 1st item");
assert.equal(res[1], 0, "Expected Result of 2nd item");
assert.equal(res[2], 3, "Expected Result of 3rd item");
asyncCallback();
});
});
QUnit.test("Async call (echoMultidimensionalArray):", function (assert)
{
var asyncCallback = assert.async();
boundAsync.echoMultidimensionalArray([[1, 2], null, [3, 4]]).then(function (res)
{
assert.equal(res.length, 3, "Expected result to be length 3");
assert.equal(res[0][0], 1, "Expected Result of 1st item");
assert.equal(res[0][1], 2, "Expected Result of 1st item");
assert.equal(res[1], null, "Expected Result of 2nd item");
assert.equal(res[2][0], 3, "Expected Result of 3rd item");
assert.equal(res[2][1], 4, "Expected Result of 3rd item");
asyncCallback();
});
});
QUnit.test("Javascript Callback Test using object as parameter", function( assert )
{
var asyncCallback = assert.async();
function objectCallback(s)
{
assert.ok(true, s);
asyncCallback();
}
bound.testCallbackFromObject(
{
Callback: objectCallback, TestString: "Hello", SubClasses:
[
{ PropertyOne: "Test Property One", Numbers: [1, 2, 3] },
{ PropertyOne: "Test Property Two", Numbers: [4, 5, 6] }
]
});
});
QUnit.test("Javascript Callback Test", function( assert )
{
var asyncCallback = assert.async();
function callback(s)
{
assert.equal(s.Response, "This callback from C# was delayed 1500ms", s.Response);
asyncCallback();
}
bound.testCallback(callback);
});
//TODO: Unable to map this to a qunit test
//QUnit.test("Disposed Javscript Callback Test (navigates to www.google.com before callback fires)", function( assert )
//{
// var asyncCallback = assert.async();
// function disposedCallback(s)
// {
// // This callback should be disposed and should not be called
// assert.ok(false, "This callback should not have been called");
// asyncCallback();
// }
// bound.testCallback(callback);
// window.location.assign("http://www.google.com");
//});
})();
</script>
<script type="text/javascript">
QUnit.test( "Bind boundAsync2 and call (Hello):", function( assert )
{
var asyncCallback = assert.async();
CefSharp.BindObjectAsync("boundAsync2").then(function(result)
{
boundAsync2.hello('CefSharp').then(function (res)
{
assert.equal(res, "Hello CefSharp")
assert.equal(true, CefSharp.DeleteBoundObject("boundAsync2"), "Object was unbound");
assert.ok(window.boundAsync2 === undefined, "boundAsync2 is now undefined");
asyncCallback();
});
});
});
//Repeat of the previous test to make sure binding a deleted object is working
QUnit.test( "Bind boundAsync2 and call (Hello) Second Attempt:", function( assert )
{
var asyncCallback = assert.async();
CefSharp.BindObjectAsync("boundAsync2").then(function(result)
{
boundAsync2.hello('CefSharp').then(function (res)
{
assert.equal(res, "Hello CefSharp")
assert.equal(true, CefSharp.DeleteBoundObject("boundAsync2"), "Object was unbound");
assert.ok(window.boundAsync2 === undefined, "boundAsync2 is now undefined");
asyncCallback();
});
});
});
</script>
</body>
</html>