tor-browser

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

test_providerOpenTabs.js (8233B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const userContextId1 = 3;
      7 const userContextId2 = 5;
      8 const tabGroupId = "1234567890-1";
      9 const url = "http://foo.mozilla.org/";
     10 const url2 = "http://foo2.mozilla.org/";
     11 
     12 add_task(async function test_openTabs() {
     13  UrlbarProviderOpenTabs.registerOpenTab(url, userContextId1, null, false);
     14  UrlbarProviderOpenTabs.registerOpenTab(url, userContextId1, null, false);
     15  UrlbarProviderOpenTabs.registerOpenTab(url2, userContextId1, null, false);
     16  UrlbarProviderOpenTabs.registerOpenTab(
     17    url,
     18    userContextId2,
     19    tabGroupId,
     20    false
     21  );
     22  Assert.deepEqual(
     23    [
     24      [url, userContextId1, null],
     25      [url2, userContextId1, null],
     26    ],
     27    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(userContextId1),
     28    "Found all the expected tabs"
     29  );
     30  Assert.deepEqual(
     31    [[url, userContextId2, tabGroupId]],
     32    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(userContextId2),
     33    "Found all the expected tabs"
     34  );
     35  await PlacesUtils.promiseLargeCacheDBConnection();
     36  await UrlbarProviderOpenTabs.promiseDBPopulated;
     37  Assert.deepEqual(
     38    [
     39      { url, userContextId: userContextId1, tabGroup: null, count: 2 },
     40      { url, userContextId: userContextId2, tabGroup: tabGroupId, count: 1 },
     41      { url: url2, userContextId: userContextId1, tabGroup: null, count: 1 },
     42    ],
     43    await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests(),
     44    "Found all the expected tabs"
     45  );
     46 
     47  await UrlbarProviderOpenTabs.unregisterOpenTab(
     48    url2,
     49    userContextId1,
     50    null,
     51    false
     52  );
     53  Assert.deepEqual(
     54    [[url, userContextId1, null]],
     55    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(userContextId1),
     56    "Found all the expected tabs"
     57  );
     58  await UrlbarProviderOpenTabs.unregisterOpenTab(
     59    url,
     60    userContextId1,
     61    null,
     62    false
     63  );
     64  Assert.deepEqual(
     65    [[url, userContextId1, null]],
     66    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(userContextId1),
     67    "Found all the expected tabs"
     68  );
     69  Assert.deepEqual(
     70    [
     71      { url, userContextId: userContextId1, tabGroup: null, count: 1 },
     72      { url, userContextId: userContextId2, tabGroup: tabGroupId, count: 1 },
     73    ],
     74    await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests(),
     75    "Found all the expected tabs"
     76  );
     77 
     78  let context = createContext();
     79  let matchCount = 0;
     80  let callback = function (provider, match) {
     81    matchCount++;
     82    Assert.ok(
     83      provider instanceof UrlbarProviderOpenTabs,
     84      "Got the expected provider"
     85    );
     86    Assert.equal(
     87      match.type,
     88      UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
     89      "Got the expected result type"
     90    );
     91    Assert.equal(match.payload.url, url, "Got the expected url");
     92    Assert.equal(match.payload.title, undefined, "Got the expected title");
     93  };
     94 
     95  let provider = new UrlbarProviderOpenTabs();
     96  await provider.startQuery(context, callback);
     97  Assert.equal(matchCount, 2, "Found the expected number of matches");
     98 
     99  // Make sure memory DB handles registerOpenTab correctly.
    100  await UrlbarProviderOpenTabs.registerOpenTab(
    101    url,
    102    userContextId1,
    103    null,
    104    false
    105  );
    106  Assert.deepEqual(
    107    [
    108      { url, userContextId: userContextId1, tabGroup: null, count: 2 },
    109      { url, userContextId: userContextId2, tabGroup: tabGroupId, count: 1 },
    110    ],
    111    await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests(),
    112    "No duplicate records found"
    113  );
    114  await UrlbarProviderOpenTabs.unregisterOpenTab(
    115    url,
    116    userContextId1,
    117    null,
    118    false
    119  );
    120  Assert.deepEqual(
    121    [
    122      { url, userContextId: userContextId1, tabGroup: null, count: 1 },
    123      { url, userContextId: userContextId2, tabGroup: tabGroupId, count: 1 },
    124    ],
    125    await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests(),
    126    "Record was kept until count reaches zero"
    127  );
    128 
    129  // Sanity check that this doesn't throw.
    130  provider.cancelQuery(context);
    131  await UrlbarProviderOpenTabs.unregisterOpenTab(
    132    url,
    133    userContextId1,
    134    null,
    135    false
    136  );
    137  await UrlbarProviderOpenTabs.unregisterOpenTab(
    138    url,
    139    userContextId2,
    140    tabGroupId,
    141    false
    142  );
    143  Assert.deepEqual(
    144    [],
    145    await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests(),
    146    "Records were cleaned up"
    147  );
    148 });
    149 
    150 add_task(async function test_openTabs_mixedtype_input() {
    151  // Passing the userContextId as a string, rather than a number, is a fairly
    152  // common mistake, check the API handles both properly.
    153  Assert.deepEqual(
    154    [],
    155    UrlbarProviderOpenTabs.getOpenTabUrls(1),
    156    "Found all the expected tabs"
    157  );
    158  Assert.deepEqual(
    159    [],
    160    UrlbarProviderOpenTabs.getOpenTabUrls(2),
    161    "Found all the expected tabs"
    162  );
    163  UrlbarProviderOpenTabs.registerOpenTab(url, 1, null, false);
    164  UrlbarProviderOpenTabs.registerOpenTab(url, "2", null, false);
    165  Assert.deepEqual(
    166    [[url, 1, null]],
    167    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(1),
    168    "Found all the expected tabs"
    169  );
    170  Assert.deepEqual(
    171    [[url, 2, null]],
    172    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(2),
    173    "Found all the expected tabs"
    174  );
    175  Assert.deepEqual(
    176    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(1),
    177    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId("1"),
    178    "Also check getOpenTabs adapts to the argument type"
    179  );
    180  UrlbarProviderOpenTabs.unregisterOpenTab(url, "1", null, false);
    181  UrlbarProviderOpenTabs.unregisterOpenTab(url, 2, null, false);
    182  Assert.deepEqual(
    183    [],
    184    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(1),
    185    "Found all the expected tabs"
    186  );
    187  Assert.deepEqual(
    188    [],
    189    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(2),
    190    "Found all the expected tabs"
    191  );
    192 });
    193 
    194 add_task(async function test_openTabs() {
    195  Assert.equal(
    196    0,
    197    UrlbarProviderOpenTabs.getOpenTabUrls().size,
    198    "Check there's no open tabs"
    199  );
    200  Assert.equal(
    201    0,
    202    UrlbarProviderOpenTabs.getOpenTabUrls(true).size,
    203    "Check there's no private open tabs"
    204  );
    205  await UrlbarProviderOpenTabs.registerOpenTab(
    206    url,
    207    userContextId1,
    208    null,
    209    false
    210  );
    211  await UrlbarProviderOpenTabs.registerOpenTab(
    212    url,
    213    userContextId2,
    214    tabGroupId,
    215    false
    216  );
    217  await UrlbarProviderOpenTabs.registerOpenTab(url2, 0, null, true);
    218  Assert.equal(
    219    1,
    220    UrlbarProviderOpenTabs.getOpenTabUrls().size,
    221    "Check open tabs"
    222  );
    223  Assert.deepEqual(
    224    [
    225      [userContextId1, null],
    226      [userContextId2, tabGroupId],
    227    ],
    228    Array.from(UrlbarProviderOpenTabs.getOpenTabUrls().get(url)),
    229    "Check the tab is in 2 userContextIds"
    230  );
    231  Assert.equal(
    232    1,
    233    UrlbarProviderOpenTabs.getOpenTabUrls(true).size,
    234    "Check open private tabs"
    235  );
    236  Assert.deepEqual(
    237    [[-1, null]],
    238    Array.from(UrlbarProviderOpenTabs.getOpenTabUrls(true).get(url2)),
    239    "Check the tab is in the private userContextId"
    240  );
    241  await UrlbarProviderOpenTabs.unregisterOpenTab(
    242    url,
    243    userContextId1,
    244    null,
    245    false
    246  );
    247  await UrlbarProviderOpenTabs.unregisterOpenTab(
    248    url,
    249    userContextId2,
    250    tabGroupId,
    251    false
    252  );
    253  await UrlbarProviderOpenTabs.unregisterOpenTab(url2, 0, null, true);
    254 });
    255 
    256 add_task(async function test_openTabsInGroup() {
    257  Assert.equal(
    258    0,
    259    UrlbarProviderOpenTabs.getOpenTabUrls().size,
    260    "Check there's no open tabs"
    261  );
    262 
    263  await UrlbarProviderOpenTabs.registerOpenTab(url2, 0, tabGroupId, false);
    264  let expected = {
    265    count: 1,
    266    tabGroup: tabGroupId,
    267    url: url2,
    268    userContextId: 0,
    269  };
    270  let result =
    271    await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests();
    272  Assert.deepEqual(result, [expected], "Open tab is registered with group");
    273 
    274  UrlbarProviderOpenTabs.unregisterOpenTab(url2, 0, null, false);
    275  result = await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests();
    276  Assert.deepEqual(
    277    result,
    278    [expected],
    279    "Open tab is still registered even when unregistering same URL/contextid not in group"
    280  );
    281 
    282  UrlbarProviderOpenTabs.unregisterOpenTab(url2, 0, tabGroupId, false);
    283  result = await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests();
    284  Assert.deepEqual(result, [], "Open tab is unregistered");
    285 });