Commit 61de9e87 authored by Nicolas Chevobbe's avatar Nicolas Chevobbe
Browse files

Bug 1770572 - [devtools] Fix browser_webconsole_location_styleeditor_link.js...

Bug 1770572 - [devtools] Fix browser_webconsole_location_styleeditor_link.js intermittent failure. r=jdescottes.

Wait for the cursor to be set.
We take this opportunity to inline the functions that were only called once,
as well as refactor a few things to make the test easier to read.

Differential Revision: https://phabricator.services.mozilla.com/D147259
parent a13336ea
Loading
Loading
Loading
Loading
+18 −43
Original line number Diff line number Diff line
@@ -46,20 +46,6 @@ async function testViewSource(hud, toolbox, text) {
    "The style editor is selected when clicking on the location element"
  );

  await onStyleEditorReady(panel);

  info("style editor window focused");
  const href = messageLocationNode.getAttribute("data-url");
  const line = messageLocationNode.getAttribute("data-line");
  const column = messageLocationNode.getAttribute("data-column");
  ok(line, "found source line");

  const editor = getEditorForHref(panel.UI, href);
  ok(editor, "found style editor for " + href);
  await checkCursorPosition(panel.UI, editor, line - 1, column - 1);
}

async function onStyleEditorReady(panel) {
  const win = panel.panelWindow;
  ok(win, "Style Editor Window is defined");
  is(
@@ -69,39 +55,28 @@ async function onStyleEditorReady(panel) {
  );

  info("Waiting the style editor to be focused");
  return new Promise(resolve => {
    waitForFocus(function() {
      resolve();
    }, win);
  });
}
  await new Promise(resolve => waitForFocus(resolve, win));

function getEditorForHref(styleEditorUI, href) {
  let foundEditor = null;
  for (const editor of styleEditorUI.editors) {
    if (editor.styleSheet.href == href) {
      foundEditor = editor;
      break;
    }
  }
  return foundEditor;
}
  info("style editor window focused");
  const href = messageLocationNode.getAttribute("data-url");
  const line = messageLocationNode.getAttribute("data-line");
  const column = messageLocationNode.getAttribute("data-column");
  ok(line, "found source line");

async function checkCursorPosition(styleEditorUI, editor, line, column) {
  info("wait for source editor to load");
  const editor = panel.UI.editors.find(e => e.styleSheet.href == href);
  ok(editor, "found style editor for " + href);
  await waitFor(
    () => panel.UI.selectedStyleSheetIndex == editor.styleSheet.styleSheetIndex
  );
  ok(true, "correct stylesheet is selected in the editor");

  info("wait for source editor to load and to move the cursor");
  await editor.getSourceEditor();
  await waitFor(() => editor.sourceEditor.getCursor().line !== 0);

  // Get the updated line and column position if the CSS source was prettified.
  const position = editor.translateCursorPosition(line, column);
  line = position.line;
  column = position.column;

  const position = editor.translateCursorPosition(line - 1, column - 1);
  const cursor = editor.sourceEditor.getCursor();
  is(cursor.line, line, "correct line is selected");
  is(cursor.ch, column, "correct column is selected");
  is(
    styleEditorUI.selectedStyleSheetIndex,
    editor.styleSheet.styleSheetIndex,
    "correct stylesheet is selected in the editor"
  );
  is(cursor.line, position.line, "correct line is selected");
  is(cursor.ch, position.column, "correct column is selected");
}