summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/chrome/notification_common.js
blob: e8a52929de3bc2c8806ef53edf9a41329cfccb62 (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
/*
 * Initialization: for each test, remove any prior notifications.
 */
function cleanUpPopupNotifications() {
    var container = getPopupNotifications(window.top);
    var notes = container._currentNotifications;
    info(true, "Removing " + notes.length + " popup notifications.");
    for (var i = notes.length - 1; i >= 0; i--) {
	notes[i].remove();
    }
}
cleanUpPopupNotifications();

/*
 * getPopupNotifications
 *
 * Fetches the popup notification for the specified window.
 */
function getPopupNotifications(aWindow) {
    var Ci = SpecialPowers.Ci;
    var Cc = SpecialPowers.Cc;
    ok(Ci != null, "Access Ci");
    ok(Cc != null, "Access Cc");

    var chromeWin = SpecialPowers.wrap(aWindow)
                                 .QueryInterface(Ci.nsIInterfaceRequestor)
                                 .getInterface(Ci.nsIWebNavigation)
                                 .QueryInterface(Ci.nsIDocShell)
                                 .chromeEventHandler.ownerDocument.defaultView;

    var popupNotifications = chromeWin.PopupNotifications;
    return popupNotifications;
}


/**
 * Checks if we have a password popup notification
 * of the right type and with the right label.
 *
 * @deprecated Write a browser-chrome test instead and use the fork of this method there.
 * @returns the found password popup notification.
 */
function getPopup(aPopupNote, aKind) {
    ok(true, "Looking for " + aKind + " popup notification");
    var notification = aPopupNote.getNotification("password");
    if (notification) {
      is(notification.options.passwordNotificationType, aKind, "Notification type matches.");
      if (aKind == "password-change") {
        is(notification.mainAction.label, "Update", "Main action label matches update doorhanger.");
      } else if (aKind == "password-save") {
        is(notification.mainAction.label, "Remember", "Main action label matches save doorhanger.");
      }
    }
    return notification;
}


/**
 * @deprecated - Use a browser chrome test instead.
 *
 * Clicks the specified popup notification button.
 */
function clickPopupButton(aPopup, aButtonIndex) {
    ok(true, "Looking for action at index " + aButtonIndex);

    var notifications = SpecialPowers.wrap(aPopup.owner).panel.childNodes;
    ok(notifications.length > 0, "at least one notification displayed");
    ok(true, notifications.length + " notifications");
    var notification = notifications[0];

    if (aButtonIndex == 0) {
        ok(true, "Triggering main action");
        notification.button.doCommand();
    } else if (aButtonIndex <= aPopup.secondaryActions.length) {
        var index = aButtonIndex;
        ok(true, "Triggering secondary action " + index);
        notification.childNodes[index].doCommand();
    }
}

const kRememberButton = 0;
const kNeverButton = 1;

const kChangeButton = 0;
const kDontChangeButton = 1;

function dumpNotifications() {
  try {
    // PopupNotifications
    var container = getPopupNotifications(window.top);
    ok(true, "is popup panel open? " + container.isPanelOpen);
    var notes = container._currentNotifications;
    ok(true, "Found " + notes.length + " popup notifications.");
    for (let i = 0; i < notes.length; i++) {
        ok(true, "#" + i + ": " + notes[i].id);
    }

    // Notification bars
    var chromeWin = SpecialPowers.wrap(window.top)
                           .QueryInterface(Ci.nsIInterfaceRequestor)
                           .getInterface(Ci.nsIWebNavigation)
                           .QueryInterface(Ci.nsIDocShell)
                           .chromeEventHandler.ownerDocument.defaultView;
    var nb = chromeWin.getNotificationBox(window.top);
    notes = nb.allNotifications;
    ok(true, "Found " + notes.length + " notification bars.");
    for (let i = 0; i < notes.length; i++) {
        ok(true, "#" + i + ": " + notes[i].getAttribute("value"));
    }
  } catch (e) { todo(false, "WOAH! " + e); }
}