summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contextualidentity/tests/unit/test_basic.js
blob: 0ee18693714c2cfbd5ba411ae9a9483b6e965a3e (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"use strict";

const profileDir = do_get_profile();

const { ContextualIdentityService } = ChromeUtils.importESModule(
  "resource://gre/modules/ContextualIdentityService.sys.mjs"
);

const TEST_STORE_FILE_PATH = PathUtils.join(
  profileDir.path,
  "test-containers.json"
);

let cis;

// Basic tests
add_task(function () {
  ok(!!ContextualIdentityService, "ContextualIdentityService exists");

  cis =
    ContextualIdentityService.createNewInstanceForTesting(TEST_STORE_FILE_PATH);
  ok(!!cis, "We have our instance of ContextualIdentityService");

  equal(cis.getPublicIdentities().length, 4, "By default, 4 containers.");
  equal(cis.getPublicIdentityFromId(0), null, "No identity with id 0");

  ok(!!cis.getPublicIdentityFromId(1), "Identity 1 exists");
  ok(!!cis.getPublicIdentityFromId(2), "Identity 2 exists");
  ok(!!cis.getPublicIdentityFromId(3), "Identity 3 exists");
  ok(!!cis.getPublicIdentityFromId(4), "Identity 4 exists");

  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    cis.getPublicIdentities().map(ident => ident.userContextId),
    "getPublicUserContextIds has matching user context IDs"
  );
});

// Make sure we are not allowed to only use whitespaces as a container name
add_task(function () {
  Assert.throws(
    () =>
      cis.create(
        "\u0009\u000B\u000C\u0020\u00A0\uFEFF\u000A\u000D\u2028\u2029",
        "icon",
        "color"
      ),
    /Contextual identity names cannot contain only whitespace./,
    "Contextual identity names cannot contain only whitespace."
  );
});

add_task(function () {
  ok(!!cis.getPublicIdentityFromId(2), "Identity 2 exists");
  Assert.throws(
    () =>
      cis.update(
        2,
        "\u0009\u000B\u000C\u0020\u00A0\uFEFF\u000A\u000D\u2028\u2029",
        "icon",
        "color"
      ),
    /Contextual identity names cannot contain only whitespace./,
    "Contextual identity names cannot contain only whitespace."
  );
});

// Create a new identity
add_task(function () {
  equal(cis.getPublicIdentities().length, 4, "By default, 4 containers.");

  let identity = cis.create("New Container", "Icon", "Color");
  ok(!!identity, "New container created");
  equal(identity.name, "New Container", "Name matches");
  equal(identity.icon, "Icon", "Icon matches");
  equal(identity.color, "Color", "Color matches");

  equal(cis.getPublicIdentities().length, 5, "Expected 5 containers.");

  ok(!!cis.getPublicIdentityFromId(identity.userContextId), "Identity exists");
  equal(
    cis.getPublicIdentityFromId(identity.userContextId).name,
    "New Container",
    "Identity name is OK"
  );
  equal(
    cis.getPublicIdentityFromId(identity.userContextId).icon,
    "Icon",
    "Identity icon is OK"
  );
  equal(
    cis.getPublicIdentityFromId(identity.userContextId).color,
    "Color",
    "Identity color is OK"
  );
  equal(
    cis.getUserContextLabel(identity.userContextId),
    "New Container",
    "Identity label is OK"
  );

  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    cis.getPublicIdentities().map(ident => ident.userContextId),
    "getPublicUserContextIds has matching user context IDs"
  );

  // Remove an identity
  equal(
    cis.remove(-1),
    false,
    "cis.remove() returns false if identity doesn't exist."
  );
  equal(cis.remove(1), true, "cis.remove() returns true if identity exists.");

  equal(cis.getPublicIdentities().length, 4, "Expected 4 containers.");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    cis.getPublicIdentities().map(ident => ident.userContextId),
    "getPublicUserContextIds has matching user context IDs"
  );
});

// Reorder identities
add_task(function () {
  equal(cis.getPublicIdentities().length, 4, "By default, 4 containers.");
  // Get whatever the initial order is
  const [id0, id1, id2, id3] = cis.getPublicUserContextIds();

  ok(cis.move([id3], 0), "Moving one valid id");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id3, id0, id1, id2],
    "Moving one valid ID works"
  );

  ok(cis.move([id1, id2], 1), "Moving several valid ids");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id3, id1, id2, id0],
    "Moving several valid IDs works"
  );

  ok(!cis.move([100], 0), "Moving non-existing ids");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id3, id1, id2, id0],
    "Moving only non-existing IDs leaves list unchanged"
  );

  ok(cis.move([100, id1], 1), "Moving non-existing and existing ids");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id3, id1, id2, id0],
    "Moving existing and non-existing IDs ignores non-existing ones"
  );

  ok(cis.move([id1, 100], 1), "Moving existing and non-existing ids");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id3, id1, id2, id0],
    "Moving existing and non-existing IDs ignores non-existing ones"
  );

  ok(cis.move([id2], -1), "Moving to -1");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id3, id1, id0, id2],
    "Moving to -1 works"
  );

  ok(!cis.move([id3], -10), "Moving to other negative positions");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id3, id1, id0, id2],
    "Moving to other negative positions leaves list unchanged"
  );

  ok(cis.move([id3], 3), "Moving to last public position");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id1, id0, id2, id3],
    "Moving to last position correctly skips private context ids"
  );

  ok(cis.move([id1, id2], 1), "Moving past current position");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id0, id1, id2, id3],
    "Target position is index in resulting list"
  );

  ok(cis.move([id2, id2], 2), "Moving duplicate ids");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id0, id1, id2, id3],
    "Resulting list does not contain duplicate ids"
  );

  ok(!cis.move([], 2), "Moving empty list");
  Assert.deepEqual(
    cis.getPublicUserContextIds(),
    [id0, id1, id2, id3],
    "Resulting list does not contain duplicate ids"
  );
});

// Update an identity
add_task(function () {
  ok(!!cis.getPublicIdentityFromId(2), "Identity 2 exists");

  equal(
    cis.update(-1, "Container", "Icon", "Color"),
    false,
    "Update returns false if the identity doesn't exist"
  );

  equal(
    cis.update(2, "Container", "Icon", "Color"),
    true,
    "Update returns true if everything is OK"
  );

  ok(!!cis.getPublicIdentityFromId(2), "Identity exists");
  equal(
    cis.getPublicIdentityFromId(2).name,
    "Container",
    "Identity name is OK"
  );
  equal(cis.getPublicIdentityFromId(2).icon, "Icon", "Identity icon is OK");
  equal(cis.getPublicIdentityFromId(2).color, "Color", "Identity color is OK");
  equal(cis.getUserContextLabel(2), "Container", "Identity label is OK");
});