test_policy_search_engine.js (13297B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 "use strict"; 4 5 const { SearchTestUtils } = ChromeUtils.importESModule( 6 "resource://testing-common/SearchTestUtils.sys.mjs" 7 ); 8 const { TestUtils } = ChromeUtils.importESModule( 9 "resource://testing-common/TestUtils.sys.mjs" 10 ); 11 var { AddonTestUtils } = ChromeUtils.importESModule( 12 "resource://testing-common/AddonTestUtils.sys.mjs" 13 ); 14 15 Services.prefs.setBoolPref("browser.search.log", true); 16 SearchTestUtils.init(this); 17 18 AddonTestUtils.init(this, false); 19 AddonTestUtils.createAppInfo( 20 "xpcshell@tests.mozilla.org", 21 "XPCShell", 22 "48", 23 "48" 24 ); 25 26 add_setup(async () => { 27 await AddonTestUtils.promiseStartupManager(); 28 await Services.search.init(); 29 console.log("done init"); 30 }); 31 32 add_task(async function test_install_and_set_default() { 33 // Make sure we are starting in an expected state to avoid false positive 34 // test results. 35 Assert.notEqual( 36 (await Services.search.getDefault()).name, 37 "MozSearch", 38 "Default search engine should not be MozSearch when test starts" 39 ); 40 Assert.equal( 41 Services.search.getEngineByName("Foo"), 42 null, 43 'Engine "Foo" should not be present when test starts' 44 ); 45 46 await setupPolicyEngineWithJsonWithSearch({ 47 policies: { 48 SearchEngines: { 49 Add: [ 50 { 51 Name: "MozSearch", 52 URLTemplate: "http://example.com/?q={searchTerms}", 53 }, 54 ], 55 Default: "MozSearch", 56 }, 57 }, 58 }); 59 // Get in line, because the Search policy callbacks are async. 60 await TestUtils.waitForTick(); 61 62 // If this passes, it means that the new search engine was properly installed 63 // *and* was properly set as the default. 64 Assert.equal( 65 (await Services.search.getDefault()).name, 66 "MozSearch", 67 "Specified search engine should be the default" 68 ); 69 70 // Clean up 71 await setupPolicyEngineWithJsonWithSearch({}); 72 EnterprisePolicyTesting.resetRunOnceState(); 73 }); 74 75 add_task(async function test_install_and_set_default_private() { 76 // Make sure we are starting in an expected state to avoid false positive 77 // test results. 78 Assert.notEqual( 79 (await Services.search.getDefaultPrivate()).name, 80 "MozSearch", 81 "Default search engine should not be MozSearch when test starts" 82 ); 83 Assert.equal( 84 Services.search.getEngineByName("Foo"), 85 null, 86 'Engine "Foo" should not be present when test starts' 87 ); 88 89 await setupPolicyEngineWithJsonWithSearch({ 90 policies: { 91 SearchEngines: { 92 Add: [ 93 { 94 Name: "MozSearch", 95 URLTemplate: "http://example.com/?q={searchTerms}", 96 }, 97 ], 98 DefaultPrivate: "MozSearch", 99 }, 100 }, 101 }); 102 // Get in line, because the Search policy callbacks are async. 103 await TestUtils.waitForTick(); 104 105 // If this passes, it means that the new search engine was properly installed 106 // *and* was properly set as the default. 107 Assert.equal( 108 (await Services.search.getDefaultPrivate()).name, 109 "MozSearch", 110 "Specified search engine should be the default private engine" 111 ); 112 113 // Clean up 114 await setupPolicyEngineWithJsonWithSearch({}); 115 EnterprisePolicyTesting.resetRunOnceState(); 116 }); 117 118 // Same as the last test, but with "PreventInstalls" set to true to make sure 119 // it does not prevent search engines from being installed properly 120 add_task(async function test_install_and_set_default_prevent_installs() { 121 Assert.notEqual( 122 (await Services.search.getDefault()).name, 123 "MozSearch", 124 "Default search engine should not be MozSearch when test starts" 125 ); 126 Assert.equal( 127 Services.search.getEngineByName("Foo"), 128 null, 129 'Engine "Foo" should not be present when test starts' 130 ); 131 132 await setupPolicyEngineWithJsonWithSearch({ 133 policies: { 134 SearchEngines: { 135 Add: [ 136 { 137 Name: "MozSearch", 138 URLTemplate: "http://example.com/?q={searchTerms}", 139 }, 140 ], 141 Default: "MozSearch", 142 PreventInstalls: true, 143 }, 144 }, 145 }); 146 // Get in line, because the Search policy callbacks are async. 147 await TestUtils.waitForTick(); 148 149 Assert.equal( 150 (await Services.search.getDefault()).name, 151 "MozSearch", 152 "Specified search engine should be the default" 153 ); 154 155 // Clean up 156 await setupPolicyEngineWithJsonWithSearch({}); 157 EnterprisePolicyTesting.resetRunOnceState(); 158 }); 159 160 add_task(async function test_install_and_remove() { 161 let iconURL = 162 "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="; 163 164 Assert.equal( 165 Services.search.getEngineByName("Foo"), 166 null, 167 'Engine "Foo" should not be present when test starts' 168 ); 169 170 await setupPolicyEngineWithJsonWithSearch({ 171 policies: { 172 SearchEngines: { 173 Add: [ 174 { 175 Name: "Foo", 176 URLTemplate: "http://example.com/?q={searchTerms}", 177 IconURL: iconURL, 178 }, 179 ], 180 }, 181 }, 182 }); 183 // Get in line, because the Search policy callbacks are async. 184 await TestUtils.waitForTick(); 185 186 // If this passes, it means that the new search engine was properly installed 187 188 let engine = Services.search.getEngineByName("Foo"); 189 Assert.notEqual(engine, null, "Specified search engine should be installed"); 190 191 Assert.equal( 192 await engine.wrappedJSObject.getIconURL(), 193 iconURL, 194 "Icon should be present" 195 ); 196 Assert.equal( 197 engine.wrappedJSObject.queryCharset, 198 "UTF-8", 199 "Should default to utf-8" 200 ); 201 202 await setupPolicyEngineWithJsonWithSearch({ 203 policies: { 204 SearchEngines: { 205 Remove: ["Foo"], 206 }, 207 }, 208 }); 209 // Get in line, because the Search policy callbacks are async. 210 await TestUtils.waitForTick(); 211 212 // If this passes, it means that the specified engine was properly removed 213 Assert.equal( 214 Services.search.getEngineByName("Foo"), 215 null, 216 "Specified search engine should not be installed" 217 ); 218 219 await setupPolicyEngineWithJsonWithSearch({}); 220 EnterprisePolicyTesting.resetRunOnceState(); 221 }); 222 223 add_task(async function test_install_post_method_engine() { 224 Assert.equal( 225 Services.search.getEngineByName("Post"), 226 null, 227 'Engine "Post" should not be present when test starts' 228 ); 229 230 await setupPolicyEngineWithJsonWithSearch({ 231 policies: { 232 SearchEngines: { 233 Add: [ 234 { 235 Name: "Post", 236 Method: "POST", 237 PostData: "q={searchTerms}&anotherParam=yes", 238 URLTemplate: "http://example.com/", 239 }, 240 ], 241 }, 242 }, 243 }); 244 // Get in line, because the Search policy callbacks are async. 245 await TestUtils.waitForTick(); 246 247 let engine = Services.search.getEngineByName("Post"); 248 Assert.notEqual(engine, null, "Specified search engine should be installed"); 249 250 Assert.equal( 251 engine.wrappedJSObject._urls[0].method, 252 "POST", 253 "Method should be POST" 254 ); 255 256 let submission = engine.getSubmission("term", "text/html"); 257 Assert.notEqual(submission.postData, null, "Post data should not be null"); 258 259 let scriptableInputStream = Cc[ 260 "@mozilla.org/scriptableinputstream;1" 261 ].createInstance(Ci.nsIScriptableInputStream); 262 scriptableInputStream.init(submission.postData); 263 Assert.equal( 264 scriptableInputStream.read(scriptableInputStream.available()), 265 "q=term&anotherParam=yes", 266 "Post data should be present" 267 ); 268 269 await setupPolicyEngineWithJsonWithSearch({}); 270 EnterprisePolicyTesting.resetRunOnceState(); 271 }); 272 273 add_task(async function test_install_with_encoding() { 274 // Make sure we are starting in an expected state to avoid false positive 275 // test results. 276 Assert.equal( 277 Services.search.getEngineByName("Encoding"), 278 null, 279 'Engine "Encoding" should not be present when test starts' 280 ); 281 282 await setupPolicyEngineWithJsonWithSearch({ 283 policies: { 284 SearchEngines: { 285 Add: [ 286 { 287 Name: "Encoding", 288 Encoding: "windows-1252", 289 URLTemplate: "http://example.com/?q={searchTerms}", 290 }, 291 ], 292 }, 293 }, 294 }); 295 // Get in line, because the Search policy callbacks are async. 296 await TestUtils.waitForTick(); 297 298 let engine = Services.search.getEngineByName("Encoding"); 299 Assert.equal( 300 engine.wrappedJSObject.queryCharset, 301 "windows-1252", 302 "Should have correct encoding" 303 ); 304 305 // Clean up 306 await setupPolicyEngineWithJsonWithSearch({}); 307 EnterprisePolicyTesting.resetRunOnceState(); 308 }); 309 310 add_task(async function test_install_and_update() { 311 await setupPolicyEngineWithJsonWithSearch({ 312 policies: { 313 SearchEngines: { 314 Add: [ 315 { 316 Name: "ToUpdate", 317 URLTemplate: "http://initial.example.com/?q={searchTerms}", 318 }, 319 ], 320 }, 321 }, 322 }); 323 // Get in line, because the Search policy callbacks are async. 324 await TestUtils.waitForTick(); 325 326 let engine = Services.search.getEngineByName("ToUpdate"); 327 Assert.notEqual(engine, null, "Specified search engine should be installed"); 328 329 Assert.equal( 330 engine.getSubmission("test").uri.spec, 331 "http://initial.example.com/?q=test", 332 "Initial submission URL should be correct." 333 ); 334 335 await setupPolicyEngineWithJsonWithSearch({ 336 policies: { 337 SearchEngines: { 338 Add: [ 339 { 340 Name: "ToUpdate", 341 URLTemplate: "http://update.example.com/?q={searchTerms}", 342 }, 343 ], 344 }, 345 }, 346 }); 347 // Get in line, because the Search policy callbacks are async. 348 await TestUtils.waitForTick(); 349 350 engine = Services.search.getEngineByName("ToUpdate"); 351 Assert.notEqual(engine, null, "Specified search engine should be installed"); 352 353 Assert.equal( 354 engine.getSubmission("test").uri.spec, 355 "http://update.example.com/?q=test", 356 "Updated Submission URL should be correct." 357 ); 358 359 // Clean up 360 await setupPolicyEngineWithJsonWithSearch({}); 361 EnterprisePolicyTesting.resetRunOnceState(); 362 }); 363 364 add_task(async function test_install_with_suggest() { 365 // Make sure we are starting in an expected state to avoid false positive 366 // test results. 367 Assert.equal( 368 Services.search.getEngineByName("Suggest"), 369 null, 370 'Engine "Suggest" should not be present when test starts' 371 ); 372 373 await setupPolicyEngineWithJsonWithSearch({ 374 policies: { 375 SearchEngines: { 376 Add: [ 377 { 378 Name: "Suggest", 379 URLTemplate: "http://example.com/?q={searchTerms}", 380 SuggestURLTemplate: "http://suggest.example.com/?q={searchTerms}", 381 }, 382 ], 383 }, 384 }, 385 }); 386 // Get in line, because the Search policy callbacks are async. 387 await TestUtils.waitForTick(); 388 389 let engine = Services.search.getEngineByName("Suggest"); 390 391 Assert.equal( 392 engine.getSubmission("test", "application/x-suggestions+json").uri.spec, 393 "http://suggest.example.com/?q=test", 394 "Updated Submission URL should be correct." 395 ); 396 397 // Clean up 398 await setupPolicyEngineWithJsonWithSearch({}); 399 EnterprisePolicyTesting.resetRunOnceState(); 400 }); 401 402 add_task(async function test_install_and_restart_keeps_settings() { 403 // Make sure we are starting in an expected state to avoid false positive 404 // test results. 405 Assert.equal( 406 Services.search.getEngineByName("Settings"), 407 null, 408 'Engine "Settings" should not be present when test starts' 409 ); 410 411 await setupPolicyEngineWithJsonWithSearch({ 412 policies: { 413 SearchEngines: { 414 Add: [ 415 { 416 Name: "Settings", 417 URLTemplate: "http://example.com/?q={searchTerms}", 418 }, 419 ], 420 }, 421 }, 422 }); 423 // Get in line, because the Search policy callbacks are async. 424 await TestUtils.waitForTick(); 425 426 let settingsWritten = SearchTestUtils.promiseSearchNotification( 427 "write-settings-to-disk-complete" 428 ); 429 let engine = Services.search.getEngineByName("Settings"); 430 engine.hidden = true; 431 engine.alias = "settings"; 432 await settingsWritten; 433 434 await setupPolicyEngineWithJsonWithSearch({ 435 policies: { 436 SearchEngines: { 437 Add: [ 438 { 439 Name: "Settings", 440 URLTemplate: "http://example.com/?q={searchTerms}", 441 }, 442 ], 443 }, 444 }, 445 }); 446 447 engine = Services.search.getEngineByName("Settings"); 448 449 Assert.ok(engine.hidden, "Should have kept the engine hidden after restart"); 450 Assert.equal( 451 engine.alias, 452 "settings", 453 "Should have kept the engine alias after restart" 454 ); 455 456 // Clean up 457 await setupPolicyEngineWithJsonWithSearch({}); 458 EnterprisePolicyTesting.resetRunOnceState(); 459 }); 460 461 add_task(async function test_reset_default() { 462 await setupPolicyEngineWithJsonWithSearch({ 463 policies: { 464 SearchEngines: { 465 Remove: ["DuckDuckGo"], 466 }, 467 }, 468 }); 469 // Get in line, because the Search policy callbacks are async. 470 await TestUtils.waitForTick(); 471 472 let engine = Services.search.getEngineByName("DuckDuckGo"); 473 474 Assert.equal( 475 engine.hidden, 476 true, 477 "Application specified engine should be hidden." 478 ); 479 480 await Services.search.restoreDefaultEngines(); 481 482 engine = Services.search.getEngineByName("DuckDuckGo"); 483 Assert.equal( 484 engine.hidden, 485 false, 486 "Application specified engine should not be hidden" 487 ); 488 489 EnterprisePolicyTesting.resetRunOnceState(); 490 });