test_warnings.js (1770B)
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 makeBuffer(length) { 6 return new Array(length + 1).join("x"); 7 } 8 9 /** 10 * @returns {Promise<boolean>} 11 * True if execution proceeded without warning, false if there was a warning. 12 */ 13 function checkWarning(pref, buffer) { 14 return new Promise(resolve => { 15 let complete = false; 16 let listener = { 17 observe(event) { 18 let message = event.message; 19 if ( 20 !( 21 message.startsWith("Warning: attempting to write") && 22 message.includes(pref) 23 ) 24 ) { 25 return; 26 } 27 if (complete) { 28 return; 29 } 30 complete = true; 31 info("Warning while setting " + pref); 32 Services.console.unregisterListener(listener); 33 resolve(true); 34 }, 35 }; 36 do_timeout(1000, function () { 37 if (complete) { 38 return; 39 } 40 complete = true; 41 info("No warning while setting " + pref); 42 Services.console.unregisterListener(listener); 43 resolve(false); 44 }); 45 Services.console.registerListener(listener); 46 Services.prefs.setCharPref(pref, buffer); 47 }); 48 } 49 50 add_task(async function () { 51 // Simple change, shouldn't cause a warning 52 info("Checking that a simple change doesn't cause a warning"); 53 let buf = makeBuffer(100); 54 let warned = await checkWarning("string.accept", buf); 55 Assert.ok(!warned); 56 57 // Large change, should cause a warning 58 info("Checking that a large change causes a warning"); 59 buf = makeBuffer(32 * 1024); 60 warned = await checkWarning("string.warn", buf); 61 Assert.ok(warned); 62 });