summaryrefslogtreecommitdiff
path: root/deps/v8/test/inspector/runtime/add-binding.js
blob: 95f4549d4f48f1635a0943999629bad390528f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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')`});
  },

  async function testAddBindingToContextById() {
    const {contextGroup, sessions: [session]} = setupSessions(1);
    const contextId1 = (await session.Protocol.Runtime.onceExecutionContextCreated()).params.context.id;

    contextGroup.createContext();
    const contextId2 = (await session.Protocol.Runtime.onceExecutionContextCreated()).params.context.id;

    await session.Protocol.Runtime.addBinding({name: 'frobnicate', executionContextId: contextId2});
    const expression = `frobnicate('message')`;

    InspectorTest.log('Call binding in default context (binding should NOT be exposed)');
    await session.Protocol.Runtime.evaluate({expression});

    InspectorTest.log('Call binding in target context (binding should be exposed)');
    await session.Protocol.Runtime.evaluate({expression, contextId: contextId2});

    InspectorTest.log('Call binding in newly created context (binding should NOT be exposed)');
    contextGroup.createContext();
    const contextId3 = (await session.Protocol.Runtime.onceExecutionContextCreated()).params.context.id;
    await session.Protocol.Runtime.evaluate({expression, contextId: contextId3});
  },

  async function testAddBindingToContextByName() {
    const {contextGroup, sessions: [session]} = setupSessions(1);
    const defaultContext = (await session.Protocol.Runtime.onceExecutionContextCreated()).params.context.id;

    contextGroup.createContext("foo");
    const contextFoo = (await session.Protocol.Runtime.onceExecutionContextCreated()).params.context.id;

    contextGroup.createContext("bar");
    const contextBar = (await session.Protocol.Runtime.onceExecutionContextCreated()).params.context.id;

    await session.Protocol.Runtime.addBinding({name: 'frobnicate', executionContextName: 'foo'});
    const expression = `frobnicate('message')`;

    InspectorTest.log('Call binding in default context (binding should NOT be exposed)');
    await session.Protocol.Runtime.evaluate({expression});

    InspectorTest.log('Call binding in Foo (binding should be exposed)');
    await session.Protocol.Runtime.evaluate({expression, contextId: contextFoo});

    InspectorTest.log('Call binding in Bar (binding should NOT be exposed)');
    await session.Protocol.Runtime.evaluate({expression, contextId: contextBar});

    contextGroup.createContext("foo");
    const contextFoo2 = (await session.Protocol.Runtime.onceExecutionContextCreated()).params.context.id;

    InspectorTest.log('Call binding in newly-created Foo (binding should be exposed)');
    await session.Protocol.Runtime.evaluate({expression, contextId: contextFoo2});

    contextGroup.createContext("bazz");
    const contextBazz = (await session.Protocol.Runtime.onceExecutionContextCreated()).params.context.id;

    InspectorTest.log('Call binding in newly-created Bazz (binding should NOT be exposed)');
    await session.Protocol.Runtime.evaluate({expression, contextId: contextBazz});
  },

  async function testErrors() {
    const {contextGroup, sessions: [session]} = setupSessions(1);
    let err = await session.Protocol.Runtime.addBinding({name: 'frobnicate', executionContextName: ''});
    InspectorTest.logMessage(err);
    err = await session.Protocol.Runtime.addBinding({name: 'frobnicate', executionContextName: 'foo', executionContextId: 1});
    InspectorTest.logMessage(err);
    err = await session.Protocol.Runtime.addBinding({name: 'frobnicate', executionContextId: 2128506});
    InspectorTest.logMessage(err);
  }

]);

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};
}