v8/test/inspector/runtime/add-binding.js
Alexey Kozyatinskiy 5cb11a17cd Reland "[inspector] added Runtime.installBinding method"
This is a reland of 49c4ac7753

Original change's description:
> [inspector] added Runtime.installBinding method
>
> A lot of different clients use console.debug as a message channel from
> page to protocol client. console.debug is a little slow and not
> designed for this use case.
>
> This CL introduces new method: Runtime.installBinding. This method
> installs binding function by given name on global object on each
> inspected context including any context created later.
> Binding function takes exactly one string argument. Each time when
> binding function is called, Runtime.bindingCalled notification is
> triggered and includes passed payload.
>
> Binding function survives page reload and reinstalled right after
> console object is setup. So installed binding can be used inside
> script added by Page.addScriptToEvaluateOnNewDocument so client may do
> something like:
> Runtime.installBinding({name: 'send'});
> Page.addScriptToEvaluateOnNewDocument({source: 'console.debug = send'});
> .. navigate page ..
>
> In microbenchmark this function is ~4.6 times faster then
> console.debug.
>
> R=lushnikov@chromium.org,pfeldman@chromium.org
>
> Bug: none
> Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel
> Change-Id: I3e0e231dde9d45116709d248f6e9e7ec7037e8e3
> Reviewed-on: https://chromium-review.googlesource.com/1077662
> Commit-Queue: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
> Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#53462}

TBR=dgozman@chromium.org

Bug: none
Change-Id: I58d053581a86f15338dea621498058b7b75c7c85
Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel
Reviewed-on: https://chromium-review.googlesource.com/1081833
Reviewed-by: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Commit-Queue: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53479}
2018-06-01 22:52:55 +00:00

83 lines
3.3 KiB
JavaScript

// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
InspectorTest.log('Test for Runtime.addBinding.');
InspectorTest.runAsyncTestSuite([
async function testBasic() {
const {contextGroup, sessions: [session1, session2]} = setupSessions(2);
InspectorTest.log('\nAdd binding inside session1..');
session1.Protocol.Runtime.addBinding({name: 'send'});
InspectorTest.log('Call binding..');
await session1.Protocol.Runtime.evaluate({expression: `send('payload')`});
InspectorTest.log('\nAdd binding inside session2..');
session2.Protocol.Runtime.addBinding({name: 'send'});
InspectorTest.log('Call binding..');
await session2.Protocol.Runtime.evaluate({expression: `send('payload')`});
InspectorTest.log('\nDisable agent inside session1..');
session1.Protocol.Runtime.disable();
InspectorTest.log('Call binding..');
await session2.Protocol.Runtime.evaluate({expression: `send('payload')`});
InspectorTest.log('\nDisable agent inside session2..');
session2.Protocol.Runtime.disable();
InspectorTest.log('Call binding..');
await session2.Protocol.Runtime.evaluate({expression: `send('payload')`});
InspectorTest.log('\nEnable agent inside session1..');
session1.Protocol.Runtime.enable();
InspectorTest.log('Call binding..');
await session2.Protocol.Runtime.evaluate({expression: `send('payload')`});
},
async function testReconnect() {
const {contextGroup, sessions: [session]} = setupSessions(1);
InspectorTest.log('\nAdd binding inside session..');
await session.Protocol.Runtime.addBinding({name: 'send'});
InspectorTest.log('Reconnect..');
session.reconnect();
await session.Protocol.Runtime.evaluate({expression: `send('payload')`});
},
async function testBindingOverrides() {
const {contextGroup, sessions: [session]} = setupSessions(1);
InspectorTest.log('\nAdd send function on global object..');
session.Protocol.Runtime.evaluate({expression: 'send = () => 42'});
InspectorTest.log('Add binding inside session..');
session.Protocol.Runtime.addBinding({name: 'send'});
InspectorTest.log('Call binding..');
await session.Protocol.Runtime.evaluate({expression: `send('payload')`});
},
async function testRemoveBinding() {
const {contextGroup, sessions: [session]} = setupSessions(1);
InspectorTest.log('\nAdd binding inside session..');
session.Protocol.Runtime.addBinding({name: 'send'});
InspectorTest.log('Call binding..');
await session.Protocol.Runtime.evaluate({expression: `send('payload')`});
InspectorTest.log('Remove binding inside session..');
session.Protocol.Runtime.removeBinding({name: 'send'});
InspectorTest.log('Call binding..');
await session.Protocol.Runtime.evaluate({expression: `send('payload')`});
}
]);
function setupSessions(num) {
const contextGroup = new InspectorTest.ContextGroup();
const sessions = [];
for (let i = 0; i < num; ++i) {
const session = contextGroup.connect();
sessions.push(session);
session.Protocol.Runtime.enable();
session.Protocol.Runtime.onBindingCalled(msg => {
InspectorTest.log(`binding called in session${i + 1}`);
InspectorTest.logMessage(msg);
});
}
return {contextGroup, sessions};
}