michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // michael@0: // Pref management. michael@0: // michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: var gPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); michael@0: michael@0: function setWatchdogEnabled(enabled) { michael@0: gPrefs.setBoolPref("dom.use_watchdog", enabled); michael@0: } michael@0: michael@0: function isWatchdogEnabled() { michael@0: return gPrefs.getBoolPref("dom.use_watchdog"); michael@0: } michael@0: michael@0: // michael@0: // Utilities. michael@0: // michael@0: michael@0: function busyWait(ms) { michael@0: var start = new Date(); michael@0: while ((new Date()) - start < ms) {} michael@0: } michael@0: michael@0: function do_log_info(aMessage) michael@0: { michael@0: print("TEST-INFO | " + _TEST_FILE + " | " + aMessage); michael@0: } michael@0: michael@0: // We don't use do_execute_soon, because that inserts a michael@0: // do_test_{pending,finished} pair that gets screwed up when we terminate scripts michael@0: // from the operation callback. michael@0: function executeSoon(fn) { michael@0: var tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager); michael@0: tm.mainThread.dispatch({run: fn}, Ci.nsIThread.DISPATCH_NORMAL); michael@0: } michael@0: michael@0: // michael@0: // Asynchronous watchdog diagnostics. michael@0: // michael@0: // When running, the watchdog wakes up every second, and fires the operation michael@0: // callback if the script has been running for >= one second. As such, a script michael@0: // should never be able to run for two seconds or longer without servicing the michael@0: // operation callback. We wait 3 seconds, just to be safe. michael@0: // michael@0: michael@0: function checkWatchdog(expectInterrupt, continuation) { michael@0: var lastWatchdogWakeup = Cu.getWatchdogTimestamp("WatchdogWakeup"); michael@0: setInterruptCallback(function() { michael@0: // If the watchdog didn't actually trigger the operation callback, ignore michael@0: // this call. This allows us to test the actual watchdog behavior without michael@0: // interference from other sites where we trigger the operation callback. michael@0: if (lastWatchdogWakeup == Cu.getWatchdogTimestamp("WatchdogWakeup")) { michael@0: return true; michael@0: } michael@0: do_check_true(expectInterrupt); michael@0: setInterruptCallback(undefined); michael@0: // Schedule our continuation before we kill this script. michael@0: executeSoon(continuation); michael@0: return false; michael@0: }); michael@0: executeSoon(function() { michael@0: busyWait(3000); michael@0: do_check_true(!expectInterrupt); michael@0: setInterruptCallback(undefined); michael@0: continuation(); michael@0: }); michael@0: } michael@0: michael@0: var gGenerator; michael@0: function continueTest() { michael@0: gGenerator.next(); michael@0: } michael@0: michael@0: function run_test() { michael@0: michael@0: // Run async. michael@0: do_test_pending(); michael@0: michael@0: // Instantiate the generator and kick it off. michael@0: gGenerator = testBody(); michael@0: gGenerator.next(); michael@0: } michael@0: