1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/browser-element/mochitest/priority/test_HighPriority.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,129 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +Test that frames with mozapptype=critical which hold the "high-priority" or 1.8 +"cpu" wake locks get elevated process priority. 1.9 +--> 1.10 +<head> 1.11 + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <script type="application/javascript" src="../browserElementTestHelpers.js"></script> 1.13 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 1.14 +</head> 1.15 +<body> 1.16 + 1.17 +<script type="application/javascript;version=1.7"> 1.18 +"use strict"; 1.19 + 1.20 +SimpleTest.waitForExplicitFinish(); 1.21 +browserElementTestHelpers.setEnabledPref(true); 1.22 +browserElementTestHelpers.addPermission(); 1.23 +browserElementTestHelpers.enableProcessPriorityManager(); 1.24 + 1.25 +function runTest() { 1.26 + // To test bug 870480, run this test while holding the CPU and high-priority 1.27 + // wake locks. Without the fix for bug 870480, we won't set the priority of 1.28 + // the child process if the main process holds these wake locks and the test 1.29 + // will hang. 1.30 + navigator.requestWakeLock('cpu'); 1.31 + navigator.requestWakeLock('high-priority'); 1.32 + 1.33 + var iframe = document.createElement('iframe'); 1.34 + iframe.setAttribute('mozbrowser', true); 1.35 + iframe.setAttribute('mozapptype', 'critical'); 1.36 + iframe.src = 'file_HighPriority.html'; 1.37 + 1.38 + // We expect the following to happen: 1.39 + // 1.40 + // - Process is created. 1.41 + // - Its priority is set to FOREGROUND (when the process starts). 1.42 + // - wait_alert('step0', FOREGROUND_HIGH) 1.43 + // - wait_alert('step1', FOREGROUND) 1.44 + // - wait_alert('step2', FOREGROUND_HIGH) 1.45 + // 1.46 + // Where wait_alert(M, P) means that we expect the subprocess to 1.47 + // * do alert(M) and 1.48 + // * be set to priority P 1.49 + // in some order. If the alert occurs before the priority change, we block 1.50 + // the alert until we observe the priority change. So the subprocess only 1.51 + // has to do 1.52 + // 1.53 + // // set priority to FOREGROUND_HIGH 1.54 + // alert('step0'); 1.55 + // // set priority to FOREGROUND 1.56 + // alert('step1'); 1.57 + // 1.58 + // etc. 1.59 + 1.60 + var childID = null; 1.61 + var alertTimes = []; 1.62 + 1.63 + // Return a promise that's resolved once the child process calls alert() and 1.64 + // we get a priority change, in some order. 1.65 + // 1.66 + // We check that the text of the alert is |"step" + index|. 1.67 + // 1.68 + // If gracePeriod is given, we check that the priority change occurred at 1.69 + // least gracePeriod ms since the alert from the previous step (with a fudge 1.70 + // factor to account for inaccurate timers). 1.71 + function expectAlertAndPriorityChange(index, priority, /* optional */ gracePeriod) { 1.72 + function checkAlertInfo(e) { 1.73 + is(e.detail.message, 'step' + index, 'alert() number ' + index); 1.74 + alertTimes.push(new Date()); 1.75 + 1.76 + // Block the alert; we'll unblock it by calling e.detail.unblock() later. 1.77 + e.preventDefault(); 1.78 + return Promise.resolve(e.detail.unblock); 1.79 + } 1.80 + 1.81 + function checkGracePeriod() { 1.82 + if (gracePeriod) { 1.83 + var msSinceLastAlert = (new Date()) - alertTimes[index - 1]; 1.84 + 1.85 + // 50ms fudge factor. This test is set up so that, if nsITimers are 1.86 + // accurate, we don't need any fudge factor. Unfortunately our timers 1.87 + // are not accurate! There's little we can do here except fudge. 1.88 + // Thankfully all we're trying to test is that we get /some/ delay; the 1.89 + // exact amount of delay isn't so important. 1.90 + ok(msSinceLastAlert + 50 >= gracePeriod, 1.91 + msSinceLastAlert + "ms since last alert >= (" + gracePeriod + " - 50ms)"); 1.92 + } 1.93 + } 1.94 + 1.95 + return Promise.all( 1.96 + [expectMozbrowserEvent(iframe, 'showmodalprompt').then(checkAlertInfo), 1.97 + expectPriorityChange(childID, priority).then(checkGracePeriod)] 1.98 + ).then(function(results) { 1.99 + // checkAlertInfo returns the function to call to unblock the alert. 1.100 + // It comes to us as the first element of the results array. 1.101 + results[0](); 1.102 + }); 1.103 + } 1.104 + 1.105 + expectProcessCreated().then(function(chid) { 1.106 + childID = chid; 1.107 + return expectPriorityChange(childID, 'FOREGROUND'); 1.108 + }).then(function() { 1.109 + return expectAlertAndPriorityChange(0, 'FOREGROUND_HIGH'); 1.110 + }).then(function() { 1.111 + return expectAlertAndPriorityChange(1, 'FOREGROUND', priorityChangeGracePeriod); 1.112 + }).then(function() { 1.113 + return expectAlertAndPriorityChange(2, 'FOREGROUND_HIGH'); 1.114 + }).then(function() { 1.115 + return expectAlertAndPriorityChange(3, 'FOREGROUND', priorityChangeGracePeriod); 1.116 + }).then(SimpleTest.finish); 1.117 + 1.118 + document.body.appendChild(iframe); 1.119 +} 1.120 + 1.121 +const priorityChangeGracePeriod = 100; 1.122 +addEventListener('testready', function() { 1.123 + SpecialPowers.pushPrefEnv( 1.124 + {set: [['dom.ipc.processPriorityManager.backgroundGracePeriodMS', 1.125 + priorityChangeGracePeriod], 1.126 + ['dom.wakelock.enabled', true]]}, 1.127 + runTest); 1.128 +}); 1.129 + 1.130 +</script> 1.131 +</body> 1.132 +</html>