browser_popupNotification_checkbox.js (8143B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 function test() { 6 waitForExplicitFinish(); 7 8 ok(PopupNotifications, "PopupNotifications object exists"); 9 ok(PopupNotifications.panel, "PopupNotifications panel exists"); 10 11 setup(); 12 } 13 14 function checkCheckbox(checkbox, label, checked = false, hidden = false) { 15 is(checkbox.label, label, "Checkbox should have the correct label"); 16 is(checkbox.hidden, hidden, "Checkbox should be shown"); 17 is(checkbox.checked, checked, "Checkbox should be checked by default"); 18 } 19 20 function checkMainAction(notification, disabled = false) { 21 let mainAction = notification.button; 22 let warningLabel = notification.querySelector(".popup-notification-warning"); 23 is(warningLabel.hidden, !disabled, "Warning label should be shown"); 24 is(mainAction.disabled, disabled, "MainAction should be disabled"); 25 } 26 27 function promiseElementVisible(element) { 28 // HTMLElement.offsetParent is null when the element is not visisble 29 // (or if the element has |position: fixed|). See: 30 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent 31 return TestUtils.waitForCondition( 32 () => element.offsetParent !== null, 33 "Waiting for element to be visible" 34 ); 35 } 36 37 var gNotification; 38 39 var tests = [ 40 // Test that passing the checkbox field shows the checkbox. 41 { 42 id: "show_checkbox", 43 run() { 44 this.notifyObj = new BasicNotification(this.id); 45 this.notifyObj.options.checkbox = { 46 label: "This is a checkbox", 47 }; 48 gNotification = showNotification(this.notifyObj); 49 }, 50 onShown(popup) { 51 checkPopup(popup, this.notifyObj); 52 let notification = popup.children[0]; 53 checkCheckbox(notification.checkbox, "This is a checkbox"); 54 triggerMainCommand(popup); 55 }, 56 onHidden() {}, 57 }, 58 59 // Test checkbox being checked by default 60 { 61 id: "checkbox_checked", 62 run() { 63 this.notifyObj = new BasicNotification(this.id); 64 this.notifyObj.options.checkbox = { 65 label: "Check this", 66 checked: true, 67 }; 68 gNotification = showNotification(this.notifyObj); 69 }, 70 onShown(popup) { 71 checkPopup(popup, this.notifyObj); 72 let notification = popup.children[0]; 73 checkCheckbox(notification.checkbox, "Check this", true); 74 triggerMainCommand(popup); 75 }, 76 onHidden() {}, 77 }, 78 79 // Test checkbox passing the checkbox state on mainAction 80 { 81 id: "checkbox_passCheckboxChecked_mainAction", 82 run() { 83 this.notifyObj = new BasicNotification(this.id); 84 this.notifyObj.mainAction.callback = ({ checkboxChecked }) => 85 (this.mainActionChecked = checkboxChecked); 86 this.notifyObj.options.checkbox = { 87 label: "This is a checkbox", 88 }; 89 gNotification = showNotification(this.notifyObj); 90 }, 91 async onShown(popup) { 92 checkPopup(popup, this.notifyObj); 93 let notification = popup.children[0]; 94 let checkbox = notification.checkbox; 95 checkCheckbox(checkbox, "This is a checkbox"); 96 await promiseElementVisible(checkbox); 97 EventUtils.synthesizeMouseAtCenter(checkbox, {}); 98 checkCheckbox(checkbox, "This is a checkbox", true); 99 triggerMainCommand(popup); 100 }, 101 onHidden() { 102 is( 103 this.mainActionChecked, 104 true, 105 "mainAction callback is passed the correct checkbox value" 106 ); 107 }, 108 }, 109 110 // Test checkbox passing the checkbox state on secondaryAction 111 { 112 id: "checkbox_passCheckboxChecked_secondaryAction", 113 run() { 114 this.notifyObj = new BasicNotification(this.id); 115 this.notifyObj.secondaryActions = [ 116 { 117 label: "Test Secondary", 118 accessKey: "T", 119 callback: ({ checkboxChecked }) => 120 (this.secondaryActionChecked = checkboxChecked), 121 }, 122 ]; 123 this.notifyObj.options.checkbox = { 124 label: "This is a checkbox", 125 }; 126 gNotification = showNotification(this.notifyObj); 127 }, 128 async onShown(popup) { 129 checkPopup(popup, this.notifyObj); 130 let notification = popup.children[0]; 131 let checkbox = notification.checkbox; 132 checkCheckbox(checkbox, "This is a checkbox"); 133 await promiseElementVisible(checkbox); 134 EventUtils.synthesizeMouseAtCenter(checkbox, {}); 135 checkCheckbox(checkbox, "This is a checkbox", true); 136 triggerSecondaryCommand(popup, 0); 137 }, 138 onHidden() { 139 is( 140 this.secondaryActionChecked, 141 true, 142 "secondaryAction callback is passed the correct checkbox value" 143 ); 144 }, 145 }, 146 147 // Test checkbox preserving its state through re-opening the doorhanger 148 { 149 id: "checkbox_reopen", 150 run() { 151 this.notifyObj = new BasicNotification(this.id); 152 this.notifyObj.options.checkbox = { 153 label: "This is a checkbox", 154 checkedState: { 155 disableMainAction: true, 156 warningLabel: "Testing disable", 157 }, 158 }; 159 gNotification = showNotification(this.notifyObj); 160 }, 161 async onShown(popup) { 162 checkPopup(popup, this.notifyObj); 163 let notification = popup.children[0]; 164 let checkbox = notification.checkbox; 165 checkCheckbox(checkbox, "This is a checkbox"); 166 await promiseElementVisible(checkbox); 167 EventUtils.synthesizeMouseAtCenter(checkbox, {}); 168 dismissNotification(popup); 169 }, 170 async onHidden(popup) { 171 let icon = document.getElementById("default-notification-icon"); 172 let shown = waitForNotificationPanel(); 173 EventUtils.synthesizeMouseAtCenter(icon, {}); 174 await shown; 175 let notification = popup.children[0]; 176 let checkbox = notification.checkbox; 177 checkCheckbox(checkbox, "This is a checkbox", true); 178 checkMainAction(notification, true); 179 gNotification.remove(); 180 }, 181 }, 182 183 // Test no checkbox hides warning label 184 { 185 id: "no_checkbox", 186 run() { 187 this.notifyObj = new BasicNotification(this.id); 188 this.notifyObj.options.checkbox = null; 189 gNotification = showNotification(this.notifyObj); 190 }, 191 onShown(popup) { 192 checkPopup(popup, this.notifyObj); 193 let notification = popup.children[0]; 194 checkCheckbox(notification.checkbox, "", false, true); 195 checkMainAction(notification); 196 triggerMainCommand(popup); 197 }, 198 onHidden() {}, 199 }, 200 ]; 201 202 // Test checkbox disabling the main action in different combinations 203 ["checkedState", "uncheckedState"].forEach(function (state) { 204 [true, false].forEach(function (checked) { 205 tests.push({ 206 id: `checkbox_disableMainAction_${state}_${ 207 checked ? "checked" : "unchecked" 208 }`, 209 run() { 210 this.notifyObj = new BasicNotification(this.id); 211 this.notifyObj.options.checkbox = { 212 label: "This is a checkbox", 213 checked, 214 [state]: { 215 disableMainAction: true, 216 warningLabel: "Testing disable", 217 }, 218 }; 219 gNotification = showNotification(this.notifyObj); 220 }, 221 async onShown(popup) { 222 checkPopup(popup, this.notifyObj); 223 let notification = popup.children[0]; 224 let checkbox = notification.checkbox; 225 let disabled = 226 (state === "checkedState" && checked) || 227 (state === "uncheckedState" && !checked); 228 229 checkCheckbox(checkbox, "This is a checkbox", checked); 230 checkMainAction(notification, disabled); 231 await promiseElementVisible(checkbox); 232 EventUtils.synthesizeMouseAtCenter(checkbox, {}); 233 checkCheckbox(checkbox, "This is a checkbox", !checked); 234 checkMainAction(notification, !disabled); 235 EventUtils.synthesizeMouseAtCenter(checkbox, {}); 236 checkCheckbox(checkbox, "This is a checkbox", checked); 237 checkMainAction(notification, disabled); 238 239 // Unblock the main command if it's currently disabled. 240 if (disabled) { 241 EventUtils.synthesizeMouseAtCenter(checkbox, {}); 242 } 243 triggerMainCommand(popup); 244 }, 245 onHidden() {}, 246 }); 247 }); 248 });