tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_styleeditor_new.js (2454B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 // Test that new sheets can be added and edited.
      6 
      7 const TESTCASE_URI = TEST_BASE_HTTP + "simple.html";
      8 
      9 const TESTCASE_CSS_SOURCE = "body{background-color:red;";
     10 
     11 add_task(async function () {
     12  const { panel, ui } = await openStyleEditorForURL(TESTCASE_URI);
     13 
     14  const editor = await createNewStyleSheet(ui, panel.panelWindow);
     15  await testInitialState(editor);
     16 
     17  const originalHref = editor.styleSheet.href;
     18  const waitForPropertyChange = onPropertyChange(editor);
     19 
     20  await typeInEditor(editor, panel.panelWindow);
     21 
     22  await waitForPropertyChange;
     23 
     24  await testUpdated(editor, originalHref);
     25 });
     26 
     27 function onPropertyChange(editor) {
     28  return new Promise(resolve => {
     29    editor.on("property-change", function onProp(property) {
     30      // wait for text to be entered fully
     31      const text = editor.sourceEditor.getText();
     32      if (property == "ruleCount" && text == TESTCASE_CSS_SOURCE + "}") {
     33        editor.off("property-change", onProp);
     34        resolve();
     35      }
     36    });
     37  });
     38 }
     39 
     40 async function testInitialState(editor) {
     41  info("Testing the initial state of the new editor");
     42 
     43  ok(editor.sourceLoaded, "new editor is loaded when attached");
     44  ok(editor.isNew, "new editor has isNew flag");
     45 
     46  if (!editor.sourceEditor.hasFocus()) {
     47    info("Waiting for stylesheet editor to gain focus");
     48    await editor.sourceEditor.once("focus");
     49  }
     50  ok(editor.sourceEditor.hasFocus(), "new editor has focus");
     51 
     52  await assertRuleCount(editor, 0);
     53 
     54  const color = await getComputedStyleProperty({
     55    selector: "body",
     56    name: "background-color",
     57  });
     58  is(
     59    color,
     60    "rgb(255, 255, 255)",
     61    "content's background color is initially white"
     62  );
     63 }
     64 
     65 function typeInEditor(editor, panelWindow) {
     66  return new Promise(resolve => {
     67    waitForFocus(function () {
     68      for (const c of TESTCASE_CSS_SOURCE) {
     69        EventUtils.synthesizeKey(c, {}, panelWindow);
     70      }
     71      ok(editor.unsaved, "new editor has unsaved flag");
     72 
     73      resolve();
     74    }, panelWindow);
     75  });
     76 }
     77 
     78 async function testUpdated(editor, originalHref) {
     79  info("Testing the state of the new editor after editing it");
     80 
     81  is(
     82    editor.sourceEditor.getText(),
     83    TESTCASE_CSS_SOURCE + "}",
     84    "rule bracket has been auto-closed"
     85  );
     86 
     87  await assertRuleCount(editor, 1);
     88 
     89  is(editor.styleSheet.href, originalHref, "style sheet href did not change");
     90 }