tor-browser

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

test_quickactions.js (4805B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 ChromeUtils.defineESModuleGetters(this, {
      8  ActionsProviderQuickActions:
      9    "moz-src:///browser/components/urlbar/ActionsProviderQuickActions.sys.mjs",
     10 });
     11 
     12 add_setup(async () => {
     13  UrlbarPrefs.set("secondaryActions.featureGate", true);
     14 
     15  ActionsProviderQuickActions.addAction("newaction", {
     16    commands: ["newaction"],
     17  });
     18 
     19  registerCleanupFunction(async () => {
     20    UrlbarPrefs.clear("secondaryActions.featureGate");
     21    ActionsProviderQuickActions.removeAction("newaction");
     22  });
     23 });
     24 
     25 add_task(async function nomatch() {
     26  let context = createContext("this doesnt match", {});
     27  let results = await ActionsProviderQuickActions.queryActions(context);
     28  Assert.strictEqual(results, null, "there were no matches");
     29 });
     30 
     31 add_task(async function quickactions_match() {
     32  let context = createContext("new", {});
     33  let results = await ActionsProviderQuickActions.queryActions(context);
     34  Assert.equal(results[0].key, "newaction", "Matched the new action");
     35 });
     36 
     37 add_task(async function quickactions_match_multiple() {
     38  ActionsProviderQuickActions.addAction("multiaction", {
     39    commands: ["testcommand1", "commandtest2"],
     40  });
     41 
     42  let context = createContext("testcommand1", {});
     43  let results = await ActionsProviderQuickActions.queryActions(context);
     44  Assert.equal(
     45    results[0].key,
     46    "multiaction",
     47    "Matched the action with first keyword"
     48  );
     49 
     50  context = createContext("commandtest2", {});
     51  results = await ActionsProviderQuickActions.queryActions(context);
     52  Assert.equal(
     53    results[0].key,
     54    "multiaction",
     55    "Matched the action with first keyword"
     56  );
     57 
     58  ActionsProviderQuickActions.removeAction("multiaction");
     59 });
     60 
     61 add_task(async function duplicate_matches() {
     62  ActionsProviderQuickActions.addAction("testaction", {
     63    commands: ["testaction", "test"],
     64  });
     65 
     66  let context = createContext("test", {});
     67  let results = await ActionsProviderQuickActions.queryActions(context);
     68 
     69  Assert.equal(results[0].key, "testaction", "Matched the test action");
     70 
     71  ActionsProviderQuickActions.removeAction("testaction");
     72 });
     73 
     74 add_task(async function remove_action() {
     75  ActionsProviderQuickActions.addAction("testaction", {
     76    commands: ["testaction"],
     77  });
     78  ActionsProviderQuickActions.removeAction("testaction");
     79 
     80  let context = createContext("test", {});
     81  let result = await ActionsProviderQuickActions.queryActions(context);
     82 
     83  Assert.strictEqual(result, null, "there were no matches");
     84 });
     85 
     86 add_task(async function minimum_search_string() {
     87  let searchString = "newa";
     88  for (let minimumSearchString of [3]) {
     89    info(`Setting 'minimumSearchString' to ${minimumSearchString}`);
     90    UrlbarPrefs.set("quickactions.minimumSearchString", minimumSearchString);
     91    for (let i = 1; i < 4; i++) {
     92      let context = createContext(searchString.substring(0, i), {});
     93      let result = await ActionsProviderQuickActions.queryActions(context);
     94      let isActive = await ActionsProviderQuickActions.isActive(context);
     95 
     96      if (i >= minimumSearchString) {
     97        Assert.equal(result[0].key, "newaction", "Matched the new action");
     98        Assert.equal(isActive, true, "Provider is active");
     99      } else {
    100        Assert.equal(isActive, false, "Provider is not active");
    101      }
    102    }
    103  }
    104  UrlbarPrefs.clear("quickactions.minimumSearchString");
    105 });
    106 
    107 add_task(async function interventions_disabled() {
    108  let interventionsProvider = UrlbarProvidersManager.getProvider(
    109    "UrlbarProviderInterventions"
    110  );
    111  // Mock the relevent method of Query so we don't have to start a real one.
    112  interventionsProvider.queryInstance = {
    113    getProvider: name => UrlbarProvidersManager.getProvider(name),
    114  };
    115  let context = createContext("test", { isPrivate: false });
    116 
    117  Assert.ok(
    118    !(await interventionsProvider.isActive(context)),
    119    "Urlbar interventions are disabled when actions are enabled"
    120  );
    121  interventionsProvider.queryInstance = null;
    122 });
    123 
    124 add_task(async function test_multiple_exact_matches() {
    125  ActionsProviderQuickActions.addAction("multiaction1", {
    126    commands: ["testcommand1"],
    127  });
    128  ActionsProviderQuickActions.addAction("multiaction2", {
    129    commands: ["testcommand1"],
    130  });
    131 
    132  let context = createContext("testcommand1", {});
    133  let results = await ActionsProviderQuickActions.queryActions(context);
    134 
    135  Assert.equal(results.length, 2, "Matched both actions");
    136  Assert.equal(results[0].key, "multiaction1", "Matched the test action");
    137  Assert.equal(results[1].key, "multiaction2", "Matched the test action");
    138 
    139  ActionsProviderQuickActions.removeAction("multiaction1");
    140  ActionsProviderQuickActions.removeAction("multiaction2");
    141 });