<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview The way these tests work is as follows:
 * C++ in net_internals_ui_browsertest.cc does any necessary setup, and then
 * calls the entry point for a test with RunJavascriptTest.  The called
 * function can then use the assert/expect functions defined in test_api.js.
 * All callbacks from the browser are wrapped in such a way that they can
 * also use the assert/expect functions.
 *
 * A test ends when testDone is called.  This can be done by the test itself,
 * but will also be done by the test framework when an assert/expect test fails
 * or an exception is thrown.
 */

// Start of namespace.
var netInternalsTest = (function() {
  /**
   * A shorter poll interval is used for tests, since a few tests wait for
   * polled values to change.
   * @type {number}
   * @const
   */
  var TESTING_POLL_INTERVAL_MS = 50;

  /**
   * Map of tab handle names to location hashes.
   * @type {object.<string, string>}
   */
  var hashToTabHandleIdMap = {
    capture: CaptureView.TAB_HANDLE_ID,
    export: ExportView.TAB_HANDLE_ID,
    import: ImportView.TAB_HANDLE_ID,
    proxy: ProxyView.TAB_HANDLE_ID,
    events: EventsView.TAB_HANDLE_ID,
    dns: DnsView.TAB_HANDLE_ID,
    sockets: SocketsView.TAB_HANDLE_ID,
    spdy: SpdyView.TAB_HANDLE_ID,
    httpCache: HttpCacheView.TAB_HANDLE_ID,
    httpThrottling: HttpThrottlingView.TAB_HANDLE_ID,
    serviceProviders: ServiceProvidersView.TAB_HANDLE_ID,
    tests: TestView.TAB_HANDLE_ID,
    hsts: HSTSView.TAB_HANDLE_ID,
    logs: LogsView.TAB_HANDLE_ID,
    prerender: PrerenderView.TAB_HANDLE_ID
  };

  /**
   * Creates a test function that can use the expect and assert functions
   * in test_api.js.  Will call testDone() in response to any failure.
   *
   * The resulting function has no return value.
   * @param {string} testName The name of the function, reported on error.
   * @param {Function} testFunction The function to run.
   * @return {function():void} Function that passes its parameters to
   *     testFunction, and passes any failures to the browser by calling
   *     testDone() after they occur.
   */
  function createTestFunction(testName, testFunction) {
    return function() {
      // Convert arguments to an array, as their map method may be called on
      // failure by runTestFunction.
      var testArguments = Array.prototype.slice.call(arguments, 0);

      var result = runTestFunction(testName, testFunction, testArguments);

      // If the first value is false, report the test failure.
      if (!result[0])
        testDone();
    };
  }

  /**
   * Used to declare a test function called by the NetInternals browser test.
   * Takes in a name and a function, and creates a global test function.
   * @param {string} testName The name of the test.
   * @param {Function} testFunction The test function.
   */
  function test(testName, testFunction) {
    window[testName] = runTest.bind(null, testName, testFunction);
  }

  /**
   * Called by the browser to start a test.  If constants haven't been
   * received from the browser yet, waits until they have been.
   * Experimentally, this never seems to happen, but may theoretically be
   * possible.
   * @param {string} testName The name of the test to run.
   * @param {Function} testFunction The test function.
   */
  function runTest(testName, testFunction) {
    var testArguments = Array.prototype.slice.call(arguments, 2);

    // If we've already received the constants, start the tests.
    if (Constants) {
      startNetInternalsTest(testName, testFunction, testArguments);
      return;
    }

    // Otherwise, wait until we do.
    console.log('Received constants late.');

    /**
     * Observer that starts the tests once we've received the constants.
     */
    function ConstantsObserver() {
      this.testStarted_ = false;
    }

    ConstantsObserver.prototype.onConstantsReceived = function() {
      if (!this.testStarted_) {
        this.testStarted_ = true;
        startNetInternalsTest(testFunction, testFunction, testArguments);
      }
    };

    g_browser.addConstantsObserver(new ConstantsObserver());
  }

  /**
   * Starts running the test.  A test is run until an assert/expect statement
   * fails or testDone is called.  Those functions can only be called in the
   * test function body, or in response to a message dispatched by
   * |g_browser.receive|.
   * @param {string} testName The of the test to run.
   * @param {Function} testArguments The test arguments.
   */
  function startNetInternalsTest(testName, testFunction, testArguments) {
    // Wrap g_browser.receive around a test function so that assert and expect
    // functions can be called from observers.
    g_browser.receive = createTestFunction('g_browser.receive', function() {
      BrowserBridge.prototype.receive.apply(g_browser, arguments);
    });<commit_msg>Fix a typo that makes NetInternals unit tests flaky.<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview The way these tests work is as follows:
 * C++ in net_internals_ui_browsertest.cc does any necessary setup, and then
 * calls the entry point for a test with RunJavascriptTest.  The called
 * function can then use the assert/expect functions defined in test_api.js.
 * All callbacks from the browser are wrapped in such a way that they can
 * also use the assert/expect functions.
 *
 * A test ends when testDone is called.  This can be done by the test itself,
 * but will also be done by the test framework when an assert/expect test fails
 * or an exception is thrown.
 */

// Start of namespace.
var netInternalsTest = (function() {
  /**
   * A shorter poll interval is used for tests, since a few tests wait for
   * polled values to change.
   * @type {number}
   * @const
   */
  var TESTING_POLL_INTERVAL_MS = 50;

  /**
   * Map of tab handle names to location hashes.
   * @type {object.<string, string>}
   */
  var hashToTabHandleIdMap = {
    capture: CaptureView.TAB_HANDLE_ID,
    export: ExportView.TAB_HANDLE_ID,
    import: ImportView.TAB_HANDLE_ID,
    proxy: ProxyView.TAB_HANDLE_ID,
    events: EventsView.TAB_HANDLE_ID,
    dns: DnsView.TAB_HANDLE_ID,
    sockets: SocketsView.TAB_HANDLE_ID,
    spdy: SpdyView.TAB_HANDLE_ID,
    httpCache: HttpCacheView.TAB_HANDLE_ID,
    httpThrottling: HttpThrottlingView.TAB_HANDLE_ID,
    serviceProviders: ServiceProvidersView.TAB_HANDLE_ID,
    tests: TestView.TAB_HANDLE_ID,
    hsts: HSTSView.TAB_HANDLE_ID,
    logs: LogsView.TAB_HANDLE_ID,
    prerender: PrerenderView.TAB_HANDLE_ID
  };

  /**
   * Creates a test function that can use the expect and assert functions
   * in test_api.js.  Will call testDone() in response to any failure.
   *
   * The resulting function has no return value.
   * @param {string} testName The name of the function, reported on error.
   * @param {Function} testFunction The function to run.
   * @return {function():void} Function that passes its parameters to
   *     testFunction, and passes any failures to the browser by calling
   *     testDone() after they occur.
   */
  function createTestFunction(testName, testFunction) {
    return function() {
      // Convert arguments to an array, as their map method may be called on
      // failure by runTestFunction.
      var testArguments = Array.prototype.slice.call(arguments, 0);

      var result = runTestFunction(testName, testFunction, testArguments);

      // If the first value is false, report the test failure.
      if (!result[0])
        testDone();
    };
  }

  /**
   * Used to declare a test function called by the NetInternals browser test.
   * Takes in a name and a function, and creates a global test function.
   * @param {string} testName The name of the test.
   * @param {Function} testFunction The test function.
   */
  function test(testName, testFunction) {
    window[testName] = runTest.bind(null, testName, testFunction);
  }

  /**
   * Called by the browser to start a test.  If constants haven't been
   * received from the browser yet, waits until they have been.
   * Experimentally, this never seems to happen, but may theoretically be
   * possible.
   * @param {string} testName The name of the test to run.
   * @param {Function} testFunction The test function.
   */
  function runTest(testName, testFunction) {
    var testArguments = Array.prototype.slice.call(arguments, 2);

    // If we've already received the constants, start the tests.
    if (Constants) {
      startNetInternalsTest(testName, testFunction, testArguments);
      return;
    }

    // Otherwise, wait until we do.
    console.log('Received constants late.');

    /**
     * Observer that starts the tests once we've received the constants.
     */
    function ConstantsObserver() {
      this.testStarted_ = false;
    }

    ConstantsObserver.prototype.onReceivedConstants = function() {
      if (!this.testStarted_) {
        this.testStarted_ = true;
        startNetInternalsTest(testFunction, testFunction, testArguments);
      }
    };

    g_browser.addConstantsObserver(new ConstantsObserver());
  }

  /**
   * Starts running the test.  A test is run until an assert/expect statement
   * fails or testDone is called.  Those functions can only be called in the
   * test function body, or in response to a message dispatched by
   * |g_browser.receive|.
   * @param {string} testName The of the test to run.
   * @param {Function} testArguments The test arguments.
   */
  function startNetInternalsTest(testName, testFunction, testArguments) {
    // Wrap g_browser.receive around a test function so that assert and expect
    // functions can be called from observers.
    g_browser.receive = createTestFunction('g_browser.receive', function() {
      BrowserBridge.prototype.receive.apply(g_browser, arguments);
    });
{'fraction_non_alphanumeric': 0.0822365165759525, 'fraction_numerical': 0.001781296387926769, 'mean_word_length': 3.2677364864864864, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7056876', 'n_tokens_mistral': 2957, 'n_tokens_neox': 2840, 'n_words': 1076}