custom-preview.js 5.29 KB
Newer Older
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
// 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.

const {session, contextGroup, Protocol} =
    InspectorTest.start('RemoteObject.CustomPreview');

(async function test() {
  contextGroup.addScript(`
    var a = {name: 'a'};
    var b = {name: 'b'};
    var c = {name: 'c'};
    a.formattableBy1 = true;
    b.formattableBy2 = true;
    c.formattableBy1 = true;
    c.formattableBy2 = true;
    var formatter1 = {
      header: (x) => x.formattableBy1 ? ['span', {}, 'Header formatted by 1 ', x.name] : null,
      hasBody: () => true,
      body: (x) => ['span', {}, 'Body formatted by 1 ', x.name, ['object', {object: {}}]]
    };
    var formatter2 = {
      header: (x) => x.formattableBy2 ? ['span', {}, 'Header formatted by 2 ', x.name] : null,
      hasBody: (x) => true,
      body: (x) => ['span', {}, 'Body formatted by 2 ', x.name]
    };
    var configTest = {};
    var formatterWithConfig1 = {
      header: function(x, config) {
        if (x !== configTest || config)
          return null;
        return ['span', {}, 'Formatter with config ', ['object', {'object': x, 'config': {'info': 'additional info'}}]];
      },
      hasBody: (x) => false,
      body: (x) => { throw 'Unreachable'; }
    }
    var formatterWithConfig2 = {
      header: function(x, config) {
        if (x !== configTest || !config)
          return null;
        return ['span', {}, 'Header ', 'info: ', config.info];
      },
      hasBody: (x, config) => config && config.info,
      body: (x, config) => ['span', {}, 'body', 'info: ', config.info]
    }
    this.devtoolsFormatters = [formatter1, formatter2, formatterWithConfig1, formatterWithConfig2];
  `);

  Protocol.Runtime.enable();
  Protocol.Runtime.setCustomObjectFormatterEnabled({enabled: true});

  Protocol.Runtime.onConsoleAPICalled(m => InspectorTest.logMessage(m));
  InspectorTest.log('Dump custom previews..');
54 55 56 57
  await dumpCustomPreviewForEvaluate(await Protocol.Runtime.evaluate({expression: 'a'}));
  await dumpCustomPreviewForEvaluate(await Protocol.Runtime.evaluate({expression: 'b'}));
  await dumpCustomPreviewForEvaluate(await Protocol.Runtime.evaluate({expression: 'c'}));
  await dumpCustomPreviewForEvaluate(await Protocol.Runtime.evaluate({expression: 'configTest'}));
58 59 60 61
  InspectorTest.log('Change formatters order and dump again..');
  await Protocol.Runtime.evaluate({
    expression: 'this.devtoolsFormatters = [formatter2, formatter1, formatterWithConfig1, formatterWithConfig2]'
  });
62 63 64 65 66 67 68 69 70 71
  await dumpCustomPreviewForEvaluate(await Protocol.Runtime.evaluate({expression: 'a'}));
  await dumpCustomPreviewForEvaluate(await Protocol.Runtime.evaluate({expression: 'b'}));
  await dumpCustomPreviewForEvaluate(await Protocol.Runtime.evaluate({expression: 'c'}));
  await dumpCustomPreviewForEvaluate(await Protocol.Runtime.evaluate({expression: 'configTest'}));

  InspectorTest.log('Test Runtime.getProperties');
  const {result:{result:{objectId}}} = await Protocol.Runtime.evaluate({expression: '({a})'});
  const {result:{result}} = await Protocol.Runtime.getProperties({
    objectId, ownProperties: true, generatePreview: true});
  await dumpCustomPreview(result.find(value => value.name === 'a').value);
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

  InspectorTest.log('Try to break custom preview..');
  await Protocol.Runtime.evaluate({
    expression: `Object.defineProperty(this, 'devtoolsFormatters', {
      get: () => { throw 1; },
      configurable: true
    })`
  });
  Protocol.Runtime.evaluate({ expression: '({})', generatePreview: true });
  InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());

  await Protocol.Runtime.evaluate({
    expression: `Object.defineProperty(this, 'devtoolsFormatters', {
      get: () => {
        const arr = [1];
        Object.defineProperty(arr, 0, { get: () => { throw 2; }});
        return arr;
      },
      configurable: true
    })`
  });
  Protocol.Runtime.evaluate({ expression: '({})', generatePreview: true });
  InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());

  await Protocol.Runtime.evaluate({
    expression: `Object.defineProperty(this, 'devtoolsFormatters', {
      get: () => [{get header() { throw 3; }}],
      configurable: true
    })`
  });
  Protocol.Runtime.evaluate({ expression: '({})', generatePreview: true });
  InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());

  await Protocol.Runtime.evaluate({
    expression: `Object.defineProperty(this, 'devtoolsFormatters', {
      get: () => [{header: () => { throw 4; }}],
      configurable: true
    })`
  });
  Protocol.Runtime.evaluate({ expression: '({})', generatePreview: true });
  InspectorTest.logMessage(await Protocol.Runtime.onceConsoleAPICalled());
113

114 115 116
  InspectorTest.completeTest();
})()

117 118 119 120
function dumpCustomPreviewForEvaluate(result) {
  return dumpCustomPreview(result.result.result);
}

121
async function dumpCustomPreview(result) {
122
  const { objectId, customPreview } = result;
123 124 125 126 127 128 129 130 131 132 133
  InspectorTest.logMessage(customPreview);
  if (customPreview.bodyGetterId) {
    const body = await Protocol.Runtime.callFunctionOn({
      objectId,
      functionDeclaration: 'function(bodyGetter) { return bodyGetter.call(this); }',
      arguments: [ { objectId: customPreview.bodyGetterId } ],
      returnByValue: true
    });
    InspectorTest.logMessage(body);
  }
}