chromium/third_party/blink/web_tests/http/tests/devtools/console/console-dir-global.js

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {TestRunner} from 'test_runner';
import {ConsoleTestRunner} from 'console_test_runner';

import * as ObjectUI from 'devtools/ui/legacy/components/object_ui/object_ui.js';

(async function() {
  TestRunner.addResult(`Tests that console dumps global object with properties.\n`);

  await TestRunner.showPanel('console');

  await TestRunner.evaluateInPagePromise(`
    function doit()
    {
        console.dir(window);
    };
  `);

  TestRunner.RuntimeAgent.evaluate('window', 'console', false).then(evalCallback);

  function evalCallback(result) {
    if (!result) {
      testController.notifyDone('Exception');
      return;
    }

    if (result.type === 'error')
      testController.notifyDone('Exception:' + result);

    var objectProxy = TestRunner.runtimeModel.createRemoteObject(result);
    objectProxy.getOwnProperties(false).then(getPropertiesCallback);
  }

  function getPropertiesCallback(allProperties) {
    const properties = allProperties.properties;
    properties.sort(ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection.compareProperties);

    var golden = {
      'window': 1,
      'document': 1,
      'eval': 1,
      'console': 1,
      'frames': 1,
      'Array': 1,
      'doit': 1
    };

    var result = {};

    for (var i = 0; i < properties.length; ++i) {
      var name = properties[i].name;

      if (golden[name])
        result[name] = 1;
    }

    TestRunner.addObject(result);
    TestRunner.completeTest();
  }
})();