tor-browser

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

browser_jsterm_autocomplete_cached_results.js (4630B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Tests that the cached autocomplete results are used when the new
      5 // user input is a subset of the existing completion results.
      6 
      7 "use strict";
      8 
      9 const TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html><script>
     10    x = Object.create(null, Object.getOwnPropertyDescriptors({
     11      dog: "woof",
     12      dos: "-",
     13      dot: ".",
     14      duh: 1,
     15      wut: 2,
     16    }))
     17  </script>`;
     18 
     19 add_task(async function () {
     20  const hud = await openNewTabAndConsole(TEST_URI);
     21  const { jsterm } = hud;
     22  const { autocompletePopup: popup } = jsterm;
     23 
     24  const jstermComplete = (value, pos) =>
     25    setInputValueForAutocompletion(hud, value, pos);
     26 
     27  await jstermComplete("x.");
     28  is(
     29    getAutocompletePopupLabels(popup).toString(),
     30    ["dog", "dos", "dot", "duh", "wut"].toString(),
     31    "'x.' gave a list of suggestions"
     32  );
     33  ok(popup.isOpen, "popup is opened");
     34 
     35  info("Add a property on the object");
     36  let result = await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     37    content.wrappedJSObject.x.docfoobar = "added";
     38    return content.wrappedJSObject.x.docfoobar;
     39  });
     40 
     41  is(result, "added", "The property was added on the window object");
     42 
     43  info("Test typing d (i.e. input is now 'x.d')");
     44  let onUpdated = jsterm.once("autocomplete-updated");
     45  EventUtils.synthesizeKey("d");
     46  await onUpdated;
     47  ok(
     48    hasExactPopupLabels(popup, ["dog", "dos", "dot", "duh"]),
     49    "autocomplete popup does not contain docfoobar. List has not been updated"
     50  );
     51 
     52  // Test typing o (i.e. input is now 'x.do').
     53  onUpdated = jsterm.once("autocomplete-updated");
     54  EventUtils.synthesizeKey("o");
     55  await onUpdated;
     56  ok(
     57    hasExactPopupLabels(popup, ["dog", "dos", "dot"]),
     58    "autocomplete popup still does not contain docfoobar. List has not been updated"
     59  );
     60 
     61  // Test that backspace does not cause a request to the server
     62  onUpdated = jsterm.once("autocomplete-updated");
     63  EventUtils.synthesizeKey("KEY_Backspace");
     64  await onUpdated;
     65  ok(
     66    hasExactPopupLabels(popup, ["dog", "dos", "dot", "duh"]),
     67    "autocomplete cached results do not contain docfoobar. list has not been updated"
     68  );
     69 
     70  result = await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     71    content.wrappedJSObject.x.docfoobar = "added";
     72    delete content.wrappedJSObject.x.docfoobar;
     73    return typeof content.wrappedJSObject.x.docfoobar;
     74  });
     75  is(result, "undefined", "The property was removed");
     76 
     77  // Test if 'window.getC' gives 'getComputedStyle'
     78  await jstermComplete("window.");
     79  await jstermComplete("window.getC");
     80  ok(
     81    hasPopupLabel(popup, "getComputedStyle"),
     82    "autocomplete results do contain getComputedStyle"
     83  );
     84 
     85  // Test if 'dump(d' gives non-zero results
     86  await jstermComplete("dump(d");
     87  ok(!!popup.getItems().length, "'dump(d' gives non-zero results");
     88 
     89  // Test that 'dump(x.)' works.
     90  await jstermComplete("dump(x)", -1);
     91  onUpdated = jsterm.once("autocomplete-updated");
     92  EventUtils.sendString(".");
     93  await onUpdated;
     94  ok(
     95    hasExactPopupLabels(popup, ["dog", "dos", "dot", "duh", "wut"]),
     96    "'dump(x.' gave a list of suggestions"
     97  );
     98 
     99  info("Add a property on the x object");
    100  result = await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    101    content.wrappedJSObject.x.docfoobar = "added";
    102    return content.wrappedJSObject.x.docfoobar;
    103  });
    104  is(result, "added", "The property was added on the x object again");
    105 
    106  // Make sure 'dump(x.d)' does not contain 'docfoobar'.
    107  onUpdated = jsterm.once("autocomplete-updated");
    108  EventUtils.sendString("d");
    109  await onUpdated;
    110 
    111  ok(
    112    !hasPopupLabel(popup, "docfoobar"),
    113    "autocomplete cached results do not contain docfoobar. list has not been updated"
    114  );
    115 
    116  info("Ensure filtering from the cache does work");
    117  execute(
    118    hud,
    119    `
    120    window.testObject = Object.create(null);
    121    window.testObject.zz = "zz";
    122    window.testObject.zzz = "zzz";
    123    window.testObject.zzzz = "zzzz";
    124  `
    125  );
    126  await jstermComplete("window.testObject.");
    127  await jstermComplete("window.testObject.z");
    128  ok(
    129    hasExactPopupLabels(popup, ["zz", "zzz", "zzzz"]),
    130    "results are the expected ones"
    131  );
    132 
    133  onUpdated = jsterm.once("autocomplete-updated");
    134  EventUtils.sendString("z");
    135  await onUpdated;
    136  ok(
    137    hasExactPopupLabels(popup, ["zz", "zzz", "zzzz"]),
    138    "filtering from the cache works - step 1"
    139  );
    140 
    141  onUpdated = jsterm.once("autocomplete-updated");
    142  EventUtils.sendString("z");
    143  await onUpdated;
    144  ok(
    145    hasExactPopupLabels(popup, ["zzz", "zzzz"]),
    146    "filtering from the cache works - step 2"
    147  );
    148 });