tor-browser

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

browser_jsterm_history_persist.js (5157B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that console command input is persisted across toolbox loads.
      5 // See Bug 943306.
      6 
      7 "use strict";
      8 
      9 requestLongerTimeout(2);
     10 
     11 const TEST_URI =
     12  "data:text/html;charset=utf-8,<!DOCTYPE html>Web Console test for persisting history";
     13 const INPUT_HISTORY_COUNT = 10;
     14 
     15 const {
     16  getHistoryEntries,
     17 } = require("resource://devtools/client/webconsole/selectors/history.js");
     18 
     19 add_task(async function () {
     20  info("Setting custom input history pref to " + INPUT_HISTORY_COUNT);
     21  Services.prefs.setIntPref(
     22    "devtools.webconsole.inputHistoryCount",
     23    INPUT_HISTORY_COUNT
     24  );
     25 
     26  // First tab: run a bunch of commands and then make sure that you can
     27  // navigate through their history.
     28  const hud1 = await openNewTabAndConsole(TEST_URI);
     29  let state1 = hud1.ui.wrapper.getStore().getState();
     30  is(
     31    JSON.stringify(getHistoryEntries(state1)),
     32    "[]",
     33    "No history on first tab initially"
     34  );
     35  await populateInputHistory(hud1);
     36 
     37  state1 = hud1.ui.wrapper.getStore().getState();
     38  is(
     39    JSON.stringify(getHistoryEntries(state1)),
     40    '["0","1","2","3","4","5","6","7","8","9"]',
     41    "First tab has populated history"
     42  );
     43 
     44  // Second tab: Just make sure that you can navigate through the history
     45  // generated by the first tab.
     46  const hud2 = await openNewTabAndConsole(TEST_URI, false);
     47  let state2 = hud2.ui.wrapper.getStore().getState();
     48  is(
     49    JSON.stringify(getHistoryEntries(state2)),
     50    '["0","1","2","3","4","5","6","7","8","9"]',
     51    "Second tab has populated history"
     52  );
     53  await testNavigatingHistoryInUI(hud2);
     54 
     55  state2 = hud2.ui.wrapper.getStore().getState();
     56  is(
     57    JSON.stringify(getHistoryEntries(state2)),
     58    '["0","1","2","3","4","5","6","7","8","9"]',
     59    "An empty entry has been added in the second tab due to history perusal"
     60  );
     61  is(
     62    state2.history.originalUserValue,
     63    "",
     64    "An empty value has been stored as the current input value"
     65  );
     66 
     67  // Third tab: Should have the same history as first tab, but if we run a
     68  // command, then the history of the first and second shouldn't be affected
     69  const hud3 = await openNewTabAndConsole(TEST_URI, false);
     70  let state3 = hud3.ui.wrapper.getStore().getState();
     71 
     72  is(
     73    JSON.stringify(getHistoryEntries(state3)),
     74    '["0","1","2","3","4","5","6","7","8","9"]',
     75    "Third tab has populated history"
     76  );
     77 
     78  // Set input value separately from execute so UP arrow accurately navigates
     79  // history.
     80  setInputValue(hud3, '"hello from third tab"');
     81  await executeAndWaitForResultMessage(
     82    hud3,
     83    '"hello from third tab"',
     84    '"hello from third tab"'
     85  );
     86 
     87  state1 = hud1.ui.wrapper.getStore().getState();
     88  is(
     89    JSON.stringify(getHistoryEntries(state1)),
     90    '["0","1","2","3","4","5","6","7","8","9"]',
     91    "First tab history hasn't changed due to command in third tab"
     92  );
     93 
     94  state2 = hud2.ui.wrapper.getStore().getState();
     95  is(
     96    JSON.stringify(getHistoryEntries(state2)),
     97    '["0","1","2","3","4","5","6","7","8","9"]',
     98    "Second tab history hasn't changed due to command in third tab"
     99  );
    100  is(
    101    state2.history.originalUserValue,
    102    "",
    103    "Current input value hasn't changed due to command in third tab"
    104  );
    105 
    106  state3 = hud3.ui.wrapper.getStore().getState();
    107  is(
    108    JSON.stringify(getHistoryEntries(state3)),
    109    '["1","2","3","4","5","6","7","8","9","\\"hello from third tab\\""]',
    110    "Third tab has updated history (and purged the first result) after " +
    111      "running a command"
    112  );
    113 
    114  // Fourth tab: Should have the latest command from the third tab, followed
    115  // by the rest of the history from the first tab.
    116  const hud4 = await openNewTabAndConsole(TEST_URI, false);
    117  let state4 = hud4.ui.wrapper.getStore().getState();
    118  is(
    119    JSON.stringify(getHistoryEntries(state4)),
    120    '["1","2","3","4","5","6","7","8","9","\\"hello from third tab\\""]',
    121    "Fourth tab has most recent history"
    122  );
    123 
    124  await hud4.ui.wrapper.dispatchClearHistory();
    125  state4 = hud4.ui.wrapper.getStore().getState();
    126  is(
    127    JSON.stringify(getHistoryEntries(state4)),
    128    "[]",
    129    "Clearing history for a tab works"
    130  );
    131 
    132  const hud5 = await openNewTabAndConsole(TEST_URI, false);
    133  const state5 = hud5.ui.wrapper.getStore().getState();
    134  is(
    135    JSON.stringify(getHistoryEntries(state5)),
    136    "[]",
    137    "Clearing history carries over to a new tab"
    138  );
    139 });
    140 
    141 /**
    142 * Populate the history by running the following commands:
    143 *  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    144 */
    145 async function populateInputHistory(hud) {
    146  for (let i = 0; i < INPUT_HISTORY_COUNT; i++) {
    147    const input = i.toString();
    148    await executeAndWaitForResultMessage(hud, input, input);
    149  }
    150 }
    151 
    152 /**
    153 * Check pressing up results in history traversal like:
    154 *  [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    155 */
    156 function testNavigatingHistoryInUI(hud) {
    157  const { jsterm } = hud;
    158  jsterm.focus();
    159 
    160  // Count backwards from original input and make sure that pressing up
    161  // restores this.
    162  for (let i = INPUT_HISTORY_COUNT - 1; i >= 0; i--) {
    163    EventUtils.synthesizeKey("KEY_ArrowUp");
    164    is(getInputValue(hud), i.toString(), "Pressing up restores last input");
    165  }
    166 }