tor-browser

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

browser_rules_edit-property-cancel.js (3994B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Tests editing a property name or value and escaping will revert the
      7 // changes and restore the original value.
      8 
      9 const TEST_URI = `
     10  <style type='text/css'>
     11  #testid {
     12    background-color: #00F;
     13  }
     14  </style>
     15  <div id='testid'>Styled Node</div>
     16 `;
     17 
     18 add_task(async function () {
     19  await addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
     20  const { inspector, view } = await openRuleView({ overrideDebounce: false });
     21  await selectNode("#testid", inspector);
     22 
     23  const ruleEditor = getRuleViewRuleEditor(view, 1);
     24  const prop = getTextProperty(view, 1, { "background-color": "#00F" });
     25  const propEditor = prop.editor;
     26 
     27  await focusEditableField(view, propEditor.nameSpan);
     28  await sendKeysAndWaitForFocus(view, ruleEditor.element, ["DELETE", "ESCAPE"]);
     29 
     30  is(
     31    propEditor.nameSpan.textContent,
     32    "background-color",
     33    "'background-color' property name is correctly set."
     34  );
     35  is(
     36    await getComputedStyleProperty("#testid", null, "background-color"),
     37    "rgb(0, 0, 255)",
     38    "#00F background color is set."
     39  );
     40 
     41  const editor = await focusEditableField(view, propEditor.valueSpan);
     42  info("Delete input value");
     43  const onValueDeleted = view.once("ruleview-changed");
     44  EventUtils.sendKey("DELETE", view.styleWindow);
     45  await onValueDeleted;
     46 
     47  is(editor.input.value, "", "value input is empty");
     48 
     49  await waitFor(() => view.popup?.isOpen);
     50  ok(true, "autocomplete popup opened");
     51 
     52  info("Hide autocomplete popup");
     53  const onPopupClosed = once(view.popup, "popup-closed");
     54  EventUtils.sendKey("ESCAPE", view.styleWindow);
     55  await onPopupClosed;
     56  ok(true, "popup was closed");
     57 
     58  info("Cancel edit with escape key");
     59  const onRuleViewChanged = view.once("ruleview-changed");
     60  EventUtils.sendKey("ESCAPE", view.styleWindow);
     61  await onRuleViewChanged;
     62 
     63  is(
     64    propEditor.valueSpan.textContent,
     65    "#00F",
     66    "'#00F' property value is correctly set."
     67  );
     68  is(
     69    await getComputedStyleProperty("#testid", null, "background-color"),
     70    "rgb(0, 0, 255)",
     71    "#00F background color is set."
     72  );
     73 
     74  ok(!propEditor.warning, "warning icon is hidden after cancelling the edit");
     75 });
     76 
     77 const TEST_URI_BLUR = `
     78  <style type='text/css'>
     79  #testid {
     80    background-color: #CFF;
     81  }
     82  </style>
     83  <input id='testid'></div>
     84 `;
     85 
     86 add_task(async function testCancelOnBlur() {
     87  await addTab(
     88    "data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI_BLUR)
     89  );
     90 
     91  info("Initially move the focus in the input");
     92  Services.focus.setFocus(gBrowser.selectedBrowser, Services.focus.FLAG_BYKEY);
     93  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
     94    const input = content.document.querySelector("input");
     95    Services.focus.setFocus(input, Services.focus.FLAG_BYKEY);
     96  });
     97 
     98  info("Open DevTools and start editing a value in the rule view");
     99  const { inspector, view } = await openRuleView({ overrideDebounce: false });
    100  await selectNode("#testid", inspector);
    101  const prop = getTextProperty(view, 1, { "background-color": "#CFF" });
    102  const propEditor = prop.editor;
    103  await focusEditableField(view, propEditor.valueSpan);
    104 
    105  // Note: Using Services.focus here, but in theory this is more likely to be
    106  // triggered via a click in the content page. But our various helpers to
    107  // simulate events don't seem to trigger the expected blur from DevTools.
    108  info("Move the focus back to the content page");
    109  const onRuleviewChanged = view.once("ruleview-changed");
    110  Services.focus.setFocus(gBrowser.selectedBrowser, Services.focus.FLAG_BYKEY);
    111  await onRuleviewChanged;
    112  await wait(1000);
    113 
    114  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    115    const input = content.document.querySelector("input");
    116    ok(content.document.hasFocus(), "The content document is still focused");
    117    is(content.document.activeElement, input, "The input is still focused");
    118  });
    119 });