summaryrefslogtreecommitdiffstats
path: root/devtools/client/styleeditor/test/browser_styleeditor_scroll.js
blob: 6524c14039c132bafa29f2a6c7e1724a72c26059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Test that editor scrolls to correct line if it's selected with
//  * selectStyleSheet (specified line)
//  * click on the sidebar item (line before the editor was unselected)
// See bug 1148086.

const SIMPLE = TEST_BASE_HTTP + "simple.css";
const LONG = TEST_BASE_HTTP + "doc_long.css";
const DOCUMENT_WITH_LONG_SHEET = "data:text/html;charset=UTF-8," +
        encodeURIComponent(
          ["<!DOCTYPE html>",
           "<html>",
           " <head>",
           "  <title>Editor scroll test page</title>",
           '  <link rel="stylesheet" type="text/css" href="' + SIMPLE + '">',
           '  <link rel="stylesheet" type="text/css" href="' + LONG + '">',
           " </head>",
           " <body>Editor scroll test page</body>",
           "</html>"
          ].join("\n"));
const LINE_TO_SELECT = 201;

add_task(function* () {
  let { ui } = yield openStyleEditorForURL(DOCUMENT_WITH_LONG_SHEET);

  is(ui.editors.length, 2, "Two editors present.");

  let simpleEditor = ui.editors[0];
  let longEditor = ui.editors[1];

  info(`Selecting doc_long.css and scrolling to line ${LINE_TO_SELECT}`);

  // We need to wait for editor-selected if we want to check the scroll
  // position as scrolling occurs after selectStyleSheet resolves but before the
  // event is emitted.
  let selectEventPromise = waitForEditorToBeSelected(longEditor, ui);
  ui.selectStyleSheet(longEditor.styleSheet, LINE_TO_SELECT);
  yield selectEventPromise;

  info("Checking that the correct line is visible after initial load");

  let { from, to } = longEditor.sourceEditor.getViewport();
  info(`Lines ${from}-${to} are visible (expected ${LINE_TO_SELECT}).`);

  ok(from <= LINE_TO_SELECT, "The editor scrolled too much.");
  ok(to >= LINE_TO_SELECT, "The editor scrolled too little.");

  let initialScrollTop = longEditor.sourceEditor.getScrollInfo().top;
  info(`Storing scrollTop = ${initialScrollTop} for later comparison.`);

  info("Selecting the first editor (simple.css)");
  yield ui.selectStyleSheet(simpleEditor.styleSheet);

  info("Selecting doc_long.css again.");
  selectEventPromise = waitForEditorToBeSelected(longEditor, ui);

  // Can't use ui.selectStyleSheet here as it will scroll the editor back to top
  // and we want to check that the previous scroll position is restored.
  let summary = yield ui.getEditorSummary(longEditor);
  ui._view.activeSummary = summary;

  info("Waiting for doc_long.css to be selected.");
  yield selectEventPromise;

  let scrollTop = longEditor.sourceEditor.getScrollInfo().top;
  is(scrollTop, initialScrollTop,
    "Scroll top was restored after the sheet was selected again.");
});

/**
 * A helper that waits "editor-selected" event for given editor.
 *
 * @param {StyleSheetEditor} editor
 *        The editor to wait for.
 * @param {StyleEditorUI} ui
 *        The StyleEditorUI the editor belongs to.
 */
var waitForEditorToBeSelected = Task.async(function* (editor, ui) {
  info(`Waiting for ${editor.friendlyName} to be selected.`);
  let selected = yield ui.once("editor-selected");
  while (selected != editor) {
    info(`Ignored editor-selected for editor ${editor.friendlyName}.`);
    selected = yield ui.once("editor-selected");
  }

  info(`Got editor-selected for ${editor.friendlyName}.`);
});