summaryrefslogtreecommitdiffstats
path: root/browser/components/shopping/tests/browser/browser_shopping_urlbar.js
blob: 9eb396e84695302be20786eb91b963a060c60dfd (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const CONTENT_PAGE = "https://example.com";
const PRODUCT_PAGE = "https://example.com/product/B09TJGHL5F";

add_task(async function test_button_hidden() {
  await BrowserTestUtils.withNewTab(CONTENT_PAGE, async function (browser) {
    let shoppingButton = document.getElementById("shopping-sidebar-button");
    ok(
      BrowserTestUtils.isHidden(shoppingButton),
      "Shopping Button should be hidden on a content page"
    );
  });
});

add_task(async function test_button_shown() {
  await BrowserTestUtils.withNewTab(PRODUCT_PAGE, async function (browser) {
    let shoppingButton = document.getElementById("shopping-sidebar-button");
    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "Shopping Button should be visible on a product page"
    );
  });
});

// Button is hidden on navigation to a content page
add_task(async function test_button_changes_with_location() {
  await BrowserTestUtils.withNewTab(CONTENT_PAGE, async function (browser) {
    let shoppingButton = document.getElementById("shopping-sidebar-button");
    ok(
      BrowserTestUtils.isHidden(shoppingButton),
      "Shopping Button should be hidden on a content page"
    );
    BrowserTestUtils.startLoadingURIString(browser, PRODUCT_PAGE);
    await BrowserTestUtils.browserLoaded(browser);
    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "Shopping Button should be visible on a product page"
    );
    BrowserTestUtils.startLoadingURIString(browser, CONTENT_PAGE);
    await BrowserTestUtils.browserLoaded(browser);
    ok(
      BrowserTestUtils.isHidden(shoppingButton),
      "Shopping Button should be hidden on a content page"
    );
  });
});

add_task(async function test_button_active() {
  Services.prefs.setBoolPref("browser.shopping.experience2023.active", true);

  await BrowserTestUtils.withNewTab(PRODUCT_PAGE, async function (browser) {
    let shoppingButton = document.getElementById("shopping-sidebar-button");
    Assert.equal(
      shoppingButton.getAttribute("shoppingsidebaropen"),
      "true",
      "Shopping Button should be active when sidebar is open"
    );
  });
});

add_task(async function test_button_inactive() {
  Services.prefs.setBoolPref("browser.shopping.experience2023.active", false);

  await BrowserTestUtils.withNewTab(PRODUCT_PAGE, async function (browser) {
    let shoppingButton = document.getElementById("shopping-sidebar-button");
    Assert.equal(
      shoppingButton.getAttribute("shoppingsidebaropen"),
      "false",
      "Shopping Button should be inactive when sidebar is closed"
    );
  });
});

// Switching Tabs shows and hides the button
add_task(async function test_button_changes_with_tabswitch() {
  Services.prefs.setBoolPref("browser.shopping.experience2023.active", true);

  let shoppingButton = document.getElementById("shopping-sidebar-button");

  let productTab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: PRODUCT_PAGE,
  });
  let contentTab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: CONTENT_PAGE,
  });

  await BrowserTestUtils.switchTab(gBrowser, productTab);
  ok(
    BrowserTestUtils.isVisible(shoppingButton),
    "Shopping Button should be visible on a product page"
  );

  await BrowserTestUtils.switchTab(gBrowser, contentTab);
  ok(
    BrowserTestUtils.isHidden(shoppingButton),
    "Shopping Button should be hidden on a content page"
  );

  await BrowserTestUtils.removeTab(productTab);
  await BrowserTestUtils.removeTab(contentTab);
});

add_task(async function test_button_toggles_sidebars() {
  Services.prefs.setBoolPref("browser.shopping.experience2023.active", false);

  await BrowserTestUtils.withNewTab(CONTENT_PAGE, async function (browser) {
    let shoppingButton = document.getElementById("shopping-sidebar-button");
    let browserPanel = gBrowser.getPanel(browser);

    BrowserTestUtils.startLoadingURIString(browser, PRODUCT_PAGE);
    await BrowserTestUtils.browserLoaded(browser);

    let sidebar = browserPanel.querySelector("shopping-sidebar");

    is(sidebar, null, "Shopping sidebar should be closed");

    // open
    shoppingButton.click();
    await BrowserTestUtils.waitForMutationCondition(
      shoppingButton,
      {
        attributeFilter: ["shoppingsidebaropen"],
      },
      () => shoppingButton.getAttribute("shoppingsidebaropen") == "true"
    );

    sidebar = browserPanel.querySelector("shopping-sidebar");
    ok(BrowserTestUtils.isVisible(sidebar), "Shopping sidebar should be open");

    // close
    shoppingButton.click();
    await BrowserTestUtils.waitForMutationCondition(
      shoppingButton,
      {
        attributeFilter: ["shoppingsidebaropen"],
      },
      () => shoppingButton.getAttribute("shoppingsidebaropen") == "false"
    );

    ok(BrowserTestUtils.isHidden(sidebar), "Shopping sidebar should be closed");
  });
});

// Button changes all Windows
add_task(async function test_button_toggles_all_windows() {
  Services.prefs.setBoolPref("browser.shopping.experience2023.active", false);

  let shoppingButton = document.getElementById("shopping-sidebar-button");

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PRODUCT_PAGE);

  let browserPanelA = gBrowser.getPanel(gBrowser.selectedBrowser);
  let sidebarA = browserPanelA.querySelector("shopping-sidebar");

  let newWindow = await BrowserTestUtils.openNewBrowserWindow();

  BrowserTestUtils.startLoadingURIString(
    newWindow.gBrowser.selectedBrowser,
    PRODUCT_PAGE
  );
  await BrowserTestUtils.browserLoaded(newWindow.gBrowser.selectedBrowser);

  let browserPanelB = newWindow.gBrowser.getPanel(
    newWindow.gBrowser.selectedBrowser
  );
  let sidebarB = browserPanelB.querySelector("shopping-sidebar");

  is(
    sidebarA,
    null,
    "Shopping sidebar should not exist yet for new tab in current window"
  );
  is(sidebarB, null, "Shopping sidebar closed in new window");

  // open
  shoppingButton.click();
  await BrowserTestUtils.waitForMutationCondition(
    shoppingButton,
    {
      attributeFilter: ["shoppingsidebaropen"],
    },
    () => shoppingButton.getAttribute("shoppingsidebaropen") == "true"
  );
  sidebarA = browserPanelA.querySelector("shopping-sidebar");
  ok(
    BrowserTestUtils.isVisible(sidebarA),
    "Shopping sidebar should be open in current window"
  );
  sidebarB = browserPanelB.querySelector("shopping-sidebar");
  ok(
    BrowserTestUtils.isVisible(sidebarB),
    "Shopping sidebar should be open in new window"
  );

  // close
  shoppingButton.click();
  await BrowserTestUtils.waitForMutationCondition(
    shoppingButton,
    {
      attributeFilter: ["shoppingsidebaropen"],
    },
    () => shoppingButton.getAttribute("shoppingsidebaropen") == "false"
  );

  ok(
    BrowserTestUtils.isHidden(sidebarA),
    "Shopping sidebar should be closed in current window"
  );
  ok(
    BrowserTestUtils.isHidden(sidebarB),
    "Shopping sidebar should be closed in new window"
  );

  BrowserTestUtils.removeTab(tab);
  await BrowserTestUtils.closeWindow(newWindow);
});

add_task(async function test_button_right_click_doesnt_affect_sidebars() {
  Services.prefs.setBoolPref("browser.shopping.experience2023.active", false);

  await BrowserTestUtils.withNewTab(CONTENT_PAGE, async function (browser) {
    let shoppingButton = document.getElementById("shopping-sidebar-button");
    let browserPanel = gBrowser.getPanel(browser);

    BrowserTestUtils.startLoadingURIString(browser, PRODUCT_PAGE);
    await BrowserTestUtils.browserLoaded(browser);

    let sidebar = browserPanel.querySelector("shopping-sidebar");

    is(sidebar, null, "Shopping sidebar should be closed");
    EventUtils.synthesizeMouseAtCenter(shoppingButton, { button: 1 });
    // Wait a tick.
    await new Promise(executeSoon);
    sidebar = browserPanel.querySelector("shopping-sidebar");
    is(sidebar, null, "Shopping sidebar should still be closed");
  });
});

add_task(async function test_button_deals_with_tabswitches() {
  Services.prefs.setBoolPref("browser.shopping.experience2023.active", true);

  await BrowserTestUtils.withNewTab(CONTENT_PAGE, async function (browser) {
    let shoppingButton = document.getElementById("shopping-sidebar-button");

    ok(
      BrowserTestUtils.isHidden(shoppingButton),
      "The shopping button is hidden on a non product page"
    );

    let newProductTab = BrowserTestUtils.addTab(gBrowser, PRODUCT_PAGE);
    let newProductBrowser = newProductTab.linkedBrowser;
    await BrowserTestUtils.browserLoaded(
      newProductBrowser,
      false,
      PRODUCT_PAGE
    );

    ok(
      BrowserTestUtils.isHidden(shoppingButton),
      "The shopping button is still hidden after opening a background product tab"
    );

    let shoppingButtonVisiblePromise =
      BrowserTestUtils.waitForMutationCondition(
        shoppingButton,
        { attributes: true, attributeFilter: ["hidden"] },
        () => !shoppingButton.hidden
      );
    await BrowserTestUtils.switchTab(gBrowser, newProductTab);
    await shoppingButtonVisiblePromise;

    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "The shopping button is now visible"
    );

    let newProductTab2 = BrowserTestUtils.addTab(gBrowser, PRODUCT_PAGE);
    let newProductBrowser2 = newProductTab2.linkedBrowser;
    await BrowserTestUtils.browserLoaded(
      newProductBrowser2,
      false,
      PRODUCT_PAGE
    );

    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "The shopping button is still visible after opening background product tab"
    );

    shoppingButtonVisiblePromise = BrowserTestUtils.waitForMutationCondition(
      shoppingButton,
      { attributes: true, attributeFilter: ["hidden"] },
      () => !shoppingButton.hidden
    );
    await BrowserTestUtils.switchTab(gBrowser, newProductTab2);
    await shoppingButtonVisiblePromise;

    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "The shopping button is still visible"
    );

    BrowserTestUtils.removeTab(newProductTab2);

    BrowserTestUtils.removeTab(newProductTab);
  });
});

add_task(async function test_button_deals_with_tabswitches_post_optout() {
  Services.prefs.setBoolPref("browser.shopping.experience2023.active", true);

  await BrowserTestUtils.withNewTab(CONTENT_PAGE, async function (browser) {
    let shoppingButton = document.getElementById("shopping-sidebar-button");

    ok(
      BrowserTestUtils.isHidden(shoppingButton),
      "The shopping button is hidden on a non product page"
    );

    let newProductTab = BrowserTestUtils.addTab(gBrowser, PRODUCT_PAGE);
    let newProductBrowser = newProductTab.linkedBrowser;
    await BrowserTestUtils.browserLoaded(
      newProductBrowser,
      false,
      PRODUCT_PAGE
    );

    ok(
      BrowserTestUtils.isHidden(shoppingButton),
      "The shopping button is still hidden after opening a background product tab"
    );

    let shoppingButtonVisiblePromise =
      BrowserTestUtils.waitForMutationCondition(
        shoppingButton,
        { attributes: true, attributeFilter: ["hidden"] },
        () => !shoppingButton.hidden
      );
    await BrowserTestUtils.switchTab(gBrowser, newProductTab);
    await shoppingButtonVisiblePromise;

    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "The shopping button is now visible"
    );

    let newProductTab2 = BrowserTestUtils.addTab(gBrowser, PRODUCT_PAGE);
    let newProductBrowser2 = newProductTab2.linkedBrowser;
    await BrowserTestUtils.browserLoaded(
      newProductBrowser2,
      false,
      PRODUCT_PAGE
    );

    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "The shopping button is still visible after opening background product tab"
    );

    shoppingButtonVisiblePromise = BrowserTestUtils.waitForMutationCondition(
      shoppingButton,
      { attributes: true, attributeFilter: ["hidden"] },
      () => !shoppingButton.hidden
    );
    await BrowserTestUtils.switchTab(gBrowser, newProductTab2);
    await shoppingButtonVisiblePromise;

    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "The shopping button is still visible"
    );

    // Simulate opt-out
    await SpecialPowers.pushPrefEnv({
      set: [
        ["browser.shopping.experience2023.active", false],
        ["browser.shopping.experience2023.optedIn", 2],
      ],
    });

    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "The shopping button is still visible after opting out."
    );
    Assert.equal(
      shoppingButton.getAttribute("shoppingsidebaropen"),
      "false",
      "Button not marked as open."
    );

    // Switch to non-product tab.
    await BrowserTestUtils.switchTab(
      gBrowser,
      gBrowser.getTabForBrowser(browser)
    );
    ok(
      BrowserTestUtils.isHidden(shoppingButton),
      "The shopping button is hidden on non-product page."
    );
    Assert.equal(
      shoppingButton.getAttribute("shoppingsidebaropen"),
      "false",
      "Button not marked as open."
    );
    // Switch to non-product tab.
    await BrowserTestUtils.switchTab(gBrowser, newProductTab);
    ok(
      BrowserTestUtils.isVisible(shoppingButton),
      "The shopping button is still visible on a different product tab after opting out."
    );
    Assert.equal(
      shoppingButton.getAttribute("shoppingsidebaropen"),
      "false",
      "Button not marked as open."
    );

    BrowserTestUtils.removeTab(newProductTab2);

    BrowserTestUtils.removeTab(newProductTab);
  });
});