summaryrefslogtreecommitdiffstats
path: root/browser/components/tests/browser/browser_quit_multiple_tabs.js
blob: fa0cbc7a4cfce77c930ac3a6382416b1730b8a50 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Ensure that when different combinations of warnings are enabled,
 * quitting produces the correct warning (if any), and the checkbox
 * is also correct.
 */
add_task(async function test_check_right_prompt() {
  let tests = [
    {
      warnOnQuitShortcut: true,
      warnOnClose: false,
      expectedDialog: "shortcut",
      messageSuffix: "with shortcut but no tabs warning",
    },
    {
      warnOnQuitShortcut: false,
      warnOnClose: true,
      expectedDialog: "tabs",
      messageSuffix: "with tabs but no shortcut warning",
    },
    {
      warnOnQuitShortcut: false,
      warnOnClose: false,
      messageSuffix: "with no warning",
      expectedDialog: null,
    },
    {
      warnOnQuitShortcut: true,
      warnOnClose: true,
      messageSuffix: "with both warnings",
      // Note: this is somewhat arbitrary; I don't think there's a right/wrong
      // here, so if this changes due to implementation details, updating the
      // text expectation to be "tabs" should be OK.
      expectedDialog: "shortcut",
    },
  ];
  let tab = BrowserTestUtils.addTab(gBrowser);

  function checkDialog(dialog, expectedDialog, messageSuffix) {
    let dialogElement = dialog.document.getElementById("commonDialog");
    let acceptLabel = dialogElement.getButton("accept").label;
    is(
      acceptLabel.startsWith("Quit"),
      expectedDialog == "shortcut",
      `dialog label ${
        expectedDialog == "shortcut" ? "should" : "should not"
      } start with Quit ${messageSuffix}`
    );
    let checkLabel = dialogElement.querySelector("checkbox").label;
    is(
      checkLabel.includes("before quitting with"),
      expectedDialog == "shortcut",
      `checkbox label ${
        expectedDialog == "shortcut" ? "should" : "should not"
      } be for quitting ${messageSuffix}`
    );

    dialogElement.getButton("cancel").click();
  }

  let dialogOpened = false;
  function setDialogOpened() {
    dialogOpened = true;
  }
  Services.obs.addObserver(setDialogOpened, "common-dialog-loaded");
  for (let {
    warnOnClose,
    warnOnQuitShortcut,
    expectedDialog,
    messageSuffix,
  } of tests) {
    dialogOpened = false;
    let promise = null;
    await SpecialPowers.pushPrefEnv({
      set: [
        ["browser.tabs.warnOnClose", warnOnClose],
        ["browser.warnOnQuitShortcut", warnOnQuitShortcut],
        ["browser.warnOnQuit", true],
      ],
    });
    if (expectedDialog) {
      promise = BrowserTestUtils.promiseAlertDialogOpen("", undefined, {
        callback(win) {
          checkDialog(win, expectedDialog, messageSuffix);
        },
      });
    }
    is(
      !canQuitApplication(undefined, "shortcut"),
      !!expectedDialog,
      `canQuitApplication ${
        expectedDialog ? "should" : "should not"
      } block ${messageSuffix}.`
    );
    await promise;
    is(
      dialogOpened,
      !!expectedDialog,
      `Should ${
        expectedDialog ? "" : "not "
      }have opened a dialog ${messageSuffix}.`
    );
  }
  Services.obs.removeObserver(setDialogOpened, "common-dialog-loaded");
  BrowserTestUtils.removeTab(tab);
});