summaryrefslogtreecommitdiffstats
path: root/devtools/client/styleeditor/test/browser_styleeditor_new.js
blob: f583759c00ceba27b018ad6058d8969cfee71969 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Test that new sheets can be added and edited.

const TESTCASE_URI = TEST_BASE_HTTP + "simple.html";

const TESTCASE_CSS_SOURCE = "body{background-color:red;";

add_task(async function () {
  const { panel, ui } = await openStyleEditorForURL(TESTCASE_URI);

  const editor = await createNewStyleSheet(ui, panel.panelWindow);
  await testInitialState(editor);

  const originalHref = editor.styleSheet.href;
  const waitForPropertyChange = onPropertyChange(editor);

  await typeInEditor(editor, panel.panelWindow);

  await waitForPropertyChange;

  await testUpdated(editor, originalHref);
});

function onPropertyChange(editor) {
  return new Promise(resolve => {
    editor.on("property-change", function onProp(property) {
      // wait for text to be entered fully
      const text = editor.sourceEditor.getText();
      if (property == "ruleCount" && text == TESTCASE_CSS_SOURCE + "}") {
        editor.off("property-change", onProp);
        resolve();
      }
    });
  });
}

async function testInitialState(editor) {
  info("Testing the initial state of the new editor");

  let summary = editor.summary;

  ok(editor.sourceLoaded, "new editor is loaded when attached");
  ok(editor.isNew, "new editor has isNew flag");

  if (!editor.sourceEditor.hasFocus()) {
    info("Waiting for stylesheet editor to gain focus");
    await editor.sourceEditor.once("focus");
  }
  ok(editor.sourceEditor.hasFocus(), "new editor has focus");

  summary = editor.summary;
  const ruleCount = summary.querySelector(".stylesheet-rule-count").textContent;
  is(parseInt(ruleCount, 10), 0, "new editor initially shows 0 rules");

  const color = await getComputedStyleProperty({
    selector: "body",
    name: "background-color",
  });
  is(
    color,
    "rgb(255, 255, 255)",
    "content's background color is initially white"
  );
}

function typeInEditor(editor, panelWindow) {
  return new Promise(resolve => {
    waitForFocus(function () {
      for (const c of TESTCASE_CSS_SOURCE) {
        EventUtils.synthesizeKey(c, {}, panelWindow);
      }
      ok(editor.unsaved, "new editor has unsaved flag");

      resolve();
    }, panelWindow);
  });
}

async function testUpdated(editor, originalHref) {
  info("Testing the state of the new editor after editing it");

  is(
    editor.sourceEditor.getText(),
    TESTCASE_CSS_SOURCE + "}",
    "rule bracket has been auto-closed"
  );

  const count = await waitFor(() => {
    const int = parseInt(
      editor.summary.querySelector(".stylesheet-rule-count").textContent,
      10
    );
    if (int == 0) {
      return false;
    }

    return int;
  });

  is(count, 1, "new editor shows 1 rule after modification");

  is(editor.styleSheet.href, originalHref, "style sheet href did not change");
}