<!DOCTYPE HTML>
<html lang="en">
<head>
  <meta charset="utf8">
  <title>Test for JavaScript terminal functionality</title>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript;version=1.8" src="common.js"></script>
  <!-- Any copyright is dedicated to the Public Domain.
     - http://creativecommons.org/publicdomain/zero/1.0/ -->
</head>
<body>
<p>Test for JavaScript terminal autocomplete functionality</p>

<script class="testbody" type="text/javascript;version=1.8">
SimpleTest.waitForExplicitFinish();

let gState;
let {MAX_AUTOCOMPLETE_ATTEMPTS,MAX_AUTOCOMPLETIONS} = require("devtools/shared/webconsole/js-property-provider");

function evaluateJS(input, options = {}) {
  return new Promise((resolve, reject) => {
    gState.client.evaluateJSAsync(input, resolve, options);
  });
}

function autocompletePromise(str, cursor, frameActor) {
  return new Promise(resolve => {
    gState.client.autocomplete(str, cursor, resolve, frameActor);
  });
}

// This test runs all of its assertions twice - once with
// the tab as a target and once with a worker
let runningInTab = true;
function startTest({worker}) {
  if (worker) {
    attachConsoleToWorker(["PageError"], onAttach);
  } else {
    attachConsoleToTab(["PageError"], onAttach);
  }
};

let onAttach = Task.async(function*(aState, response) {
  gState = aState;

  let longStrLength = DebuggerServer.LONG_STRING_LENGTH;

  // Set up the global variables needed to test autocompletion
  // in the target.
  let script = `
    // This is for workers so autocomplete acts the same
    if (!this.window) {
      window = this;
    }

    window.foobarObject = Object.create(null);
    window.foobarObject.foo = 1;
    window.foobarObject.foobar = 2;
    window.foobarObject.foobaz = 3;
    window.foobarObject.omg = 4;
    window.foobarObject.omgfoo = 5;
    window.foobarObject.strfoo = "foobarz";
    window.foobarObject.omgstr = "foobarz" +
      (new Array(${longStrLength})).join("abb");
    window.largeObject1 = Object.create(null);
    for (let i = 0; i < ${MAX_AUTOCOMPLETE_ATTEMPTS + 1}; i++) {
      window.largeObject1['a' + i] = i;
    }

    window.largeObject2 = Object.create(null);
    for (let i = 0; i < ${MAX_AUTOCOMPLETIONS * 2}; i++) {
      window.largeObject2['a' + i] = i;
    }
  `;

  yield evaluateJS(script);

  let tests = [doAutocomplete1, doAutocomplete2, doAutocomplete3,
               doAutocomplete4, doAutocompleteLarge1,
               doAutocompleteLarge2].map(t => {
                 return Task.async(t);
               });

  runTests(tests, testEnd);
});

function* doAutocomplete1() {
  info("test autocomplete for 'window.foo'");
  let response = yield autocompletePromise("window.foo", 10);
  let matches = response.matches;

  is(response.matchProp, "foo", "matchProp");
  is(matches.length, 1, "matches.length");
  is(matches[0], "foobarObject", "matches[0]");

  nextTest();
}

function* doAutocomplete2() {
  info("test autocomplete for 'window.foobarObject.'");
  let response = yield autocompletePromise("window.foobarObject.", 20);
  let matches = response.matches;

  ok(!response.matchProp, "matchProp");
  is(matches.length, 7, "matches.length");
  checkObject(matches,
    ["foo", "foobar", "foobaz", "omg", "omgfoo", "omgstr", "strfoo"]);

  nextTest();
}

function* doAutocomplete3() {
  // Check that completion suggestions are offered inside the string.
  info("test autocomplete for 'dump(window.foobarObject.)'");
  let response = yield autocompletePromise("dump(window.foobarObject.)", 25);
  let matches = response.matches;

  ok(!response.matchProp, "matchProp");
  is(matches.length, 7, "matches.length");
  checkObject(matches,
    ["foo", "foobar", "foobaz", "omg", "omgfoo", "omgstr", "strfoo"]);

  nextTest();
}

function* doAutocomplete4() {
  // Check that completion requests can have no suggestions.
  info("test autocomplete for 'dump(window.foobarObject.)'");
  let response = yield autocompletePromise("dump(window.foobarObject.)", 26);
  ok(!response.matchProp, "matchProp");
  is(response.matches.length, 0, "matches.length");

  nextTest();
}

function* doAutocompleteLarge1() {
  // Check that completion requests with too large objects will
  // have no suggestions.
  info("test autocomplete for 'window.largeObject1.'");
  let response = yield autocompletePromise("window.largeObject1.", 20);
  ok(!response.matchProp, "matchProp");
  info (response.matches.join("|"));
  is(response.matches.length, 0, "Bailed out with too many properties");

  nextTest();
}

function* doAutocompleteLarge2() {
  // Check that completion requests with pretty large objects will
  // have MAX_AUTOCOMPLETIONS suggestions
  info("test autocomplete for 'window.largeObject2.'");
  let response = yield autocompletePromise("window.largeObject2.", 20);
  ok(!response.matchProp, "matchProp");
  is(response.matches.length, MAX_AUTOCOMPLETIONS, "matches.length is MAX_AUTOCOMPLETIONS");

  nextTest();
}

function testEnd()
{
  // If this is the first run, reload the page and do it again
  // in a worker.  Otherwise, end the test.
  closeDebugger(gState, function() {
    gState = null;
    if (runningInTab) {
      runningInTab = false;
      startTest({
        worker: true
      });
    } else {
      SimpleTest.finish();
    }
  });
}

addEventListener("load", () => {
  startTest({
    worker: false
  });
});
</script>
</body>
</html>