test_UserPromptHandler.js (9942B)
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 file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 PromptHandlerConfiguration, 9 PromptHandlers, 10 PromptTypes, 11 UserPromptHandler, 12 } = ChromeUtils.importESModule( 13 "chrome://remote/content/shared/webdriver/UserPromptHandler.sys.mjs" 14 ); 15 16 add_task(function test_PromptHandlerConfiguration_ctor() { 17 const config = new PromptHandlerConfiguration("accept", true); 18 equal(config.handler, "accept"); 19 equal(config.notify, true); 20 }); 21 22 add_task(function test_PromptHandlerConfiguration_toString() { 23 equal( 24 new PromptHandlerConfiguration().toString(), 25 "[object PromptHandlerConfiguration]" 26 ); 27 }); 28 29 add_task(function test_PromptHandlerConfiguration_toJSON() { 30 let configuration, serialized; 31 32 for (const behavior of Object.values(PromptHandlers)) { 33 console.log(`Test handler - ${behavior}`); 34 configuration = new PromptHandlerConfiguration(behavior, false); 35 serialized = configuration.toJSON(); 36 equal(serialized, behavior); 37 38 configuration = new PromptHandlerConfiguration(behavior, true); 39 serialized = configuration.toJSON(); 40 if ([PromptHandlers.Accept, PromptHandlers.Dismiss].includes(behavior)) { 41 equal(serialized, `${behavior} and notify`); 42 } else { 43 equal(serialized, behavior); 44 } 45 } 46 }); 47 48 add_task(function test_UserPromptHandler_ctor() { 49 const handler = new UserPromptHandler(); 50 equal(handler.activePromptHandlers, null); 51 }); 52 53 add_task(function test_UserPromptHandler_toString() { 54 equal(new UserPromptHandler().toString(), "[object UserPromptHandler]"); 55 }); 56 57 add_task(function test_UserPromptHandler_fromJSON() { 58 let promptHandler; 59 let serialized; 60 61 // Unhandled prompt behavior as string 62 for (const behavior of Object.values(PromptHandlers)) { 63 console.log(`Test as string for ${behavior}`); 64 promptHandler = UserPromptHandler.fromJSON(behavior); 65 equal(promptHandler.activePromptHandlers.size, 1); 66 ok(promptHandler.activePromptHandlers.has("fallbackDefault")); 67 const handler = promptHandler.activePromptHandlers.get("fallbackDefault"); 68 ok(behavior.startsWith(handler.handler)); 69 if (behavior == "ignore") { 70 ok(handler.notify); 71 } else { 72 equal(handler.notify, /and notify/.test(behavior)); 73 } 74 serialized = promptHandler.toJSON(); 75 equal(serialized, behavior); 76 } 77 78 // Unhandled prompt behavior as object 79 for (const promptType of Object.values(PromptTypes)) { 80 for (const behavior of Object.values(PromptHandlers)) { 81 console.log(`Test as object for ${promptType} - ${behavior}`); 82 promptHandler = UserPromptHandler.fromJSON({ [promptType]: behavior }); 83 equal(promptHandler.activePromptHandlers.size, 1); 84 ok(promptHandler.activePromptHandlers.has(promptType)); 85 const handler = promptHandler.activePromptHandlers.get(promptType); 86 ok(behavior.startsWith(handler.handler)); 87 if (behavior == "ignore") { 88 ok(handler.notify); 89 } else { 90 equal(handler.notify, /and notify/.test(behavior)); 91 } 92 serialized = promptHandler.toJSON(); 93 deepEqual(serialized, { [promptType]: behavior }); 94 } 95 } 96 97 // Empty object 98 promptHandler = UserPromptHandler.fromJSON({}); 99 equal(promptHandler.activePromptHandlers.size, 0); 100 serialized = promptHandler.toJSON(); 101 deepEqual(serialized, {}); 102 }); 103 104 add_task(function test_UserPromptHandler_fromJSON_invalid() { 105 for (const type of [ 106 undefined, 107 null, 108 true, 109 42, 110 [], 111 "default", 112 "beforeunload", 113 ]) { 114 Assert.throws( 115 () => UserPromptHandler.fromJSON(type), 116 /InvalidArgumentError/ 117 ); 118 } 119 120 // Invalid types for prompt types and handlers 121 for (const type of [undefined, null, true, 42, {}, []]) { 122 Assert.throws( 123 () => UserPromptHandler.fromJSON({ [type]: "accept" }), 124 /InvalidArgumentError/ 125 ); 126 Assert.throws( 127 () => UserPromptHandler.fromJSON({ alert: type }), 128 /InvalidArgumentError/ 129 ); 130 } 131 132 // Invalid values for prompt type and handlers 133 Assert.throws( 134 () => UserPromptHandler.fromJSON({ foo: "accept" }), 135 /InvalidArgumentError/ 136 ); 137 Assert.throws( 138 () => UserPromptHandler.fromJSON({ alert: "foo" }), 139 /InvalidArgumentError/ 140 ); 141 }); 142 143 add_task(function test_UserPromptHandler_getPromptHandler() { 144 let configuration, promptHandler; 145 146 // Check the default value with no handlers defined 147 for (const promptType of Object.values(PromptTypes)) { 148 console.log(`Test default behavior for ${promptType}`); 149 promptHandler = new UserPromptHandler(); 150 equal(promptHandler.activePromptHandlers, null); 151 const handler = promptHandler.getPromptHandler(promptType); 152 if (promptType === PromptTypes.BeforeUnload) { 153 equal(handler.handler, PromptHandlers.Accept); 154 equal(handler.notify, false); 155 } else { 156 equal(handler.handler, PromptHandlers.Dismiss); 157 equal(handler.notify, true); 158 } 159 } 160 161 // Check custom default behavior for all prompt types 162 for (const promptType of Object.values(PromptTypes)) { 163 console.log(`Test custom default behavior for ${promptType}`); 164 promptHandler = new UserPromptHandler(); 165 configuration = new PromptHandlerConfiguration( 166 PromptHandlers.Ignore, 167 false 168 ); 169 promptHandler.update(new Map([[PromptTypes.Default, configuration]])); 170 equal(promptHandler.activePromptHandlers.size, 1); 171 const handler = promptHandler.getPromptHandler(promptType); 172 equal(handler.handler, PromptHandlers.Ignore); 173 equal(handler.notify, false); 174 } 175 176 // Check custom fallbackDefault behavior for all prompt types 177 for (const promptType of Object.values(PromptTypes)) { 178 console.log(`Test custom fallbackDefault behavior for ${promptType}`); 179 promptHandler = new UserPromptHandler(); 180 configuration = new PromptHandlerConfiguration( 181 PromptHandlers.Ignore, 182 false 183 ); 184 promptHandler.update(new Map([["fallbackDefault", configuration]])); 185 equal(promptHandler.activePromptHandlers.size, 1); 186 const handler = promptHandler.getPromptHandler(promptType); 187 if (promptType === PromptTypes.BeforeUnload) { 188 equal(handler.handler, PromptHandlers.Accept); 189 equal(handler.notify, false); 190 } else { 191 equal(handler.handler, PromptHandlers.Ignore); 192 equal(handler.notify, false); 193 } 194 } 195 196 // Check custom behavior overrides default for all prompt types 197 for (const promptType of Object.values(PromptTypes)) { 198 if (promptType === PromptTypes.Default) { 199 continue; 200 } 201 202 console.log(`Test custom behavior overrides default for ${promptType}`); 203 promptHandler = new UserPromptHandler(); 204 configuration = new PromptHandlerConfiguration( 205 PromptHandlers.Ignore, 206 false 207 ); 208 promptHandler.update(new Map([[PromptTypes.Default, configuration]])); 209 configuration = new PromptHandlerConfiguration(PromptHandlers.Accept, true); 210 promptHandler.update(new Map([[promptType, configuration]])); 211 const handler = promptHandler.getPromptHandler(promptType); 212 equal(handler.handler, PromptHandlers.Accept); 213 equal(handler.notify, true); 214 } 215 216 // Check custom behavior overrides fallbackDefault for all prompt types 217 for (const promptType of Object.values(PromptTypes)) { 218 console.log( 219 `Test custom behavior overrides fallbackDefault for ${promptType}` 220 ); 221 promptHandler = new UserPromptHandler(); 222 configuration = new PromptHandlerConfiguration( 223 PromptHandlers.Ignore, 224 false 225 ); 226 promptHandler.update(new Map([["fallbackDefault", configuration]])); 227 configuration = new PromptHandlerConfiguration(PromptHandlers.Accept, true); 228 promptHandler.update(new Map([[promptType, configuration]])); 229 const handler = promptHandler.getPromptHandler(promptType); 230 equal(handler.handler, PromptHandlers.Accept); 231 equal(handler.notify, true); 232 } 233 234 // Check default behavior overrides fallbackDefault for all prompt types 235 promptHandler = new UserPromptHandler(); 236 promptHandler.update( 237 new Map([ 238 [ 239 "fallbackDefault", 240 new PromptHandlerConfiguration(PromptHandlers.Ignore, false), 241 ], 242 [ 243 PromptTypes.Default, 244 new PromptHandlerConfiguration(PromptHandlers.Accept, true), 245 ], 246 ]) 247 ); 248 for (const promptType of Object.values(PromptTypes)) { 249 console.log( 250 `Test default behavior overrides fallbackDefault for ${promptType}` 251 ); 252 const handler = promptHandler.getPromptHandler(promptType); 253 equal(handler.handler, PromptHandlers.Accept); 254 equal(handler.notify, true); 255 } 256 }); 257 258 add_task(function test_UserPromptHandler_toJSON() { 259 let configuration, promptHandler, serialized; 260 261 // Default behavior 262 promptHandler = new UserPromptHandler(); 263 serialized = promptHandler.toJSON(); 264 equal(serialized, "dismiss and notify"); 265 266 // Custom default behavior 267 promptHandler = new UserPromptHandler(); 268 configuration = new PromptHandlerConfiguration(PromptHandlers.Accept, false); 269 promptHandler.update(new Map([[PromptTypes.Default, configuration]])); 270 serialized = promptHandler.toJSON(); 271 deepEqual(serialized, { 272 default: "accept", 273 }); 274 275 // Multiple handler definitions 276 promptHandler = new UserPromptHandler(); 277 configuration = new PromptHandlerConfiguration(PromptHandlers.Ignore, false); 278 promptHandler.update(new Map([[PromptTypes.Default, configuration]])); 279 configuration = new PromptHandlerConfiguration(PromptHandlers.Accept, true); 280 promptHandler.update(new Map([[PromptTypes.Alert, configuration]])); 281 configuration = new PromptHandlerConfiguration(PromptHandlers.Dismiss, false); 282 promptHandler.update(new Map([[PromptTypes.Confirm, configuration]])); 283 284 serialized = promptHandler.toJSON(); 285 deepEqual(serialized, { 286 default: "ignore", 287 alert: "accept and notify", 288 confirm: "dismiss", 289 }); 290 });