add_task(function* () {
  let toolbox = yield openNewTabAndToolbox(PAGE_URL, "jsdebugger");

  let controller = new SourceLocationController(toolbox.target);

  let checkedPretty = false;
  let checkedUnpretty = false;

  function onUpdate(oldLoc, newLoc) {
    if (oldLoc.line === 3) {
      checkPrettified(oldLoc, newLoc);
      checkedPretty = true;
    } else if (oldLoc.line === 9) {
      checkUnprettified(oldLoc, newLoc);
      checkedUnpretty = true;
    } else {
      throw new Error(`Unexpected location update: ${JSON.stringify(oldLoc)}`);
    }
  }

  controller.bindLocation({ url: JS_URL, line: 3 }, onUpdate);

  // Inject JS script
  let sourceShown = waitForSourceShown(toolbox.getCurrentPanel(), "code_ugly.js");
  yield createScript(JS_URL);
  yield sourceShown;

  let ppButton = toolbox.getCurrentPanel().panelWin.document.getElementById("pretty-print");
  sourceShown = waitForSourceShown(toolbox.getCurrentPanel(), "code_ugly.js");
  ppButton.click();
  yield sourceShown;
  yield waitUntil(() => checkedPretty);

  // TODO check unprettified change once bug 1177446 fixed
  /*
  sourceShown = waitForSourceShown(toolbox.getCurrentPanel(), "code_ugly.js");
  ppButton.click();
  yield sourceShown;
  yield waitUntil(() => checkedUnpretty);
  */

  yield toolbox.destroy();
  gBrowser.removeCurrentTab();
  finish();
});
add_task(function*() {
  let toolbox = yield openNewTabAndToolbox(PAGE_URL, "jsdebugger");

  let controller = new SourceLocationController(toolbox.target);

  let aggregator = [];

  function onUpdate (oldLoc, newLoc) {
    if (oldLoc.line === 6) {
      checkLoc1(oldLoc, newLoc);
    } else if (oldLoc.line === 8) {
      checkLoc2(oldLoc, newLoc);
    } else if (oldLoc.line === 2) {
      checkLoc3(oldLoc, newLoc);
    } else {
      throw new Error(`Unexpected location update: ${JSON.stringify(oldLoc)}`);
    }
    aggregator.push(newLoc);
  }

  let loc1 = { url: JS_URL, line: 6 };
  let loc2 = { url: JS_URL, line: 8, column: 3 };
  let loc3 = { url: COFFEE_URL, line: 2, column: 0 };

  controller.bindLocation(loc1, onUpdate);
  controller.bindLocation(loc2, onUpdate);
  controller.bindLocation(loc3, onUpdate);

  // Inject JS script
  yield createScript(JS_URL);

  yield waitUntil(() => aggregator.length === 3);

  ok(aggregator.find(i => i.url === COFFEE_URL && i.line === 4), "found first updated location");
  ok(aggregator.find(i => i.url === COFFEE_URL && i.line === 6), "found second updated location");
  ok(aggregator.find(i => i.url === COFFEE_URL && i.line === 2), "found third updated location");

  yield toolbox.destroy();
  gBrowser.removeCurrentTab();
  finish();
});