summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_providerOpenTabs.js
blob: 689ecf614fb4d63243f213747d19c202f0ed7558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const userContextId1 = 3;
const userContextId2 = 5;
const url = "http://foo.mozilla.org/";
const url2 = "http://foo2.mozilla.org/";

add_task(async function test_openTabs() {
  UrlbarProviderOpenTabs.registerOpenTab(url, userContextId1, false);
  UrlbarProviderOpenTabs.registerOpenTab(url, userContextId1, false);
  UrlbarProviderOpenTabs.registerOpenTab(url2, userContextId1, false);
  UrlbarProviderOpenTabs.registerOpenTab(url, userContextId2, false);
  Assert.deepEqual(
    [url, url2],
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(userContextId1),
    "Found all the expected tabs"
  );
  Assert.deepEqual(
    [url],
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(userContextId2),
    "Found all the expected tabs"
  );
  await PlacesUtils.promiseLargeCacheDBConnection();
  await UrlbarProviderOpenTabs.promiseDBPopulated;
  Assert.deepEqual(
    [
      { url, userContextId: userContextId1, count: 2 },
      { url: url2, userContextId: userContextId1, count: 1 },
      { url, userContextId: userContextId2, count: 1 },
    ],
    await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests(),
    "Found all the expected tabs"
  );

  await UrlbarProviderOpenTabs.unregisterOpenTab(url2, userContextId1, false);
  Assert.deepEqual(
    [url],
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(userContextId1),
    "Found all the expected tabs"
  );
  await UrlbarProviderOpenTabs.unregisterOpenTab(url, userContextId1, false);
  Assert.deepEqual(
    [url],
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(userContextId1),
    "Found all the expected tabs"
  );
  Assert.deepEqual(
    [
      { url, userContextId: userContextId1, count: 1 },
      { url, userContextId: userContextId2, count: 1 },
    ],
    await UrlbarProviderOpenTabs.getDatabaseRegisteredOpenTabsForTests(),
    "Found all the expected tabs"
  );

  let context = createContext();
  let matchCount = 0;
  let callback = function (provider, match) {
    matchCount++;
    Assert.ok(
      provider instanceof UrlbarProviderOpenTabs,
      "Got the expected provider"
    );
    Assert.equal(
      match.type,
      UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
      "Got the expected result type"
    );
    Assert.equal(match.payload.url, url, "Got the expected url");
    Assert.equal(match.payload.title, undefined, "Got the expected title");
  };

  let provider = new UrlbarProviderOpenTabs();
  await provider.startQuery(context, callback);
  Assert.equal(matchCount, 2, "Found the expected number of matches");
  // Sanity check that this doesn't throw.
  provider.cancelQuery(context);
  await UrlbarProviderOpenTabs.unregisterOpenTab(url, userContextId1, false);
  await UrlbarProviderOpenTabs.unregisterOpenTab(url, userContextId2, false);
});

add_task(async function test_openTabs_mixedtype_input() {
  // Passing the userContextId as a string, rather than a number, is a fairly
  // common mistake, check the API handles both properly.
  Assert.deepEqual(
    [],
    UrlbarProviderOpenTabs.getOpenTabUrls(1),
    "Found all the expected tabs"
  );
  Assert.deepEqual(
    [],
    UrlbarProviderOpenTabs.getOpenTabUrls(2),
    "Found all the expected tabs"
  );
  UrlbarProviderOpenTabs.registerOpenTab(url, 1, false);
  UrlbarProviderOpenTabs.registerOpenTab(url, "2", false);
  Assert.deepEqual(
    [url],
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(1),
    "Found all the expected tabs"
  );
  Assert.deepEqual(
    [url],
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(2),
    "Found all the expected tabs"
  );
  Assert.deepEqual(
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(1),
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId("1"),
    "Also check getOpenTabs adapts to the argument type"
  );
  UrlbarProviderOpenTabs.unregisterOpenTab(url, "1", false);
  UrlbarProviderOpenTabs.unregisterOpenTab(url, 2, false);
  Assert.deepEqual(
    [],
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(1),
    "Found all the expected tabs"
  );
  Assert.deepEqual(
    [],
    UrlbarProviderOpenTabs.getOpenTabUrlsForUserContextId(2),
    "Found all the expected tabs"
  );
});

add_task(async function test_openTabs() {
  Assert.equal(
    0,
    UrlbarProviderOpenTabs.getOpenTabUrls().size,
    "Check there's no open tabs"
  );
  Assert.equal(
    0,
    UrlbarProviderOpenTabs.getOpenTabUrls(true).size,
    "Check there's no private open tabs"
  );
  await UrlbarProviderOpenTabs.registerOpenTab(url, userContextId1, false);
  await UrlbarProviderOpenTabs.registerOpenTab(url, userContextId2, false);
  await UrlbarProviderOpenTabs.registerOpenTab(url2, 0, true);
  Assert.equal(
    1,
    UrlbarProviderOpenTabs.getOpenTabUrls().size,
    "Check open tabs"
  );
  Assert.deepEqual(
    [userContextId1, userContextId2],
    Array.from(UrlbarProviderOpenTabs.getOpenTabUrls().get(url)),
    "Check the tab is in 2 userContextIds"
  );
  Assert.equal(
    1,
    UrlbarProviderOpenTabs.getOpenTabUrls(true).size,
    "Check open private tabs"
  );
  Assert.deepEqual(
    [-1],
    Array.from(UrlbarProviderOpenTabs.getOpenTabUrls(true).get(url2)),
    "Check the tab is in the private userContextId"
  );
});