Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <!-- |
michael@0 | 4 | Test that frames with mozapptype=critical which hold the "high-priority" or |
michael@0 | 5 | "cpu" wake locks get elevated process priority. |
michael@0 | 6 | --> |
michael@0 | 7 | <head> |
michael@0 | 8 | <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 9 | <script type="application/javascript" src="../browserElementTestHelpers.js"></script> |
michael@0 | 10 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 11 | </head> |
michael@0 | 12 | <body> |
michael@0 | 13 | |
michael@0 | 14 | <script type="application/javascript;version=1.7"> |
michael@0 | 15 | "use strict"; |
michael@0 | 16 | |
michael@0 | 17 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 18 | browserElementTestHelpers.setEnabledPref(true); |
michael@0 | 19 | browserElementTestHelpers.addPermission(); |
michael@0 | 20 | browserElementTestHelpers.enableProcessPriorityManager(); |
michael@0 | 21 | |
michael@0 | 22 | function runTest() { |
michael@0 | 23 | // To test bug 870480, run this test while holding the CPU and high-priority |
michael@0 | 24 | // wake locks. Without the fix for bug 870480, we won't set the priority of |
michael@0 | 25 | // the child process if the main process holds these wake locks and the test |
michael@0 | 26 | // will hang. |
michael@0 | 27 | navigator.requestWakeLock('cpu'); |
michael@0 | 28 | navigator.requestWakeLock('high-priority'); |
michael@0 | 29 | |
michael@0 | 30 | var iframe = document.createElement('iframe'); |
michael@0 | 31 | iframe.setAttribute('mozbrowser', true); |
michael@0 | 32 | iframe.setAttribute('mozapptype', 'critical'); |
michael@0 | 33 | iframe.src = 'file_HighPriority.html'; |
michael@0 | 34 | |
michael@0 | 35 | // We expect the following to happen: |
michael@0 | 36 | // |
michael@0 | 37 | // - Process is created. |
michael@0 | 38 | // - Its priority is set to FOREGROUND (when the process starts). |
michael@0 | 39 | // - wait_alert('step0', FOREGROUND_HIGH) |
michael@0 | 40 | // - wait_alert('step1', FOREGROUND) |
michael@0 | 41 | // - wait_alert('step2', FOREGROUND_HIGH) |
michael@0 | 42 | // |
michael@0 | 43 | // Where wait_alert(M, P) means that we expect the subprocess to |
michael@0 | 44 | // * do alert(M) and |
michael@0 | 45 | // * be set to priority P |
michael@0 | 46 | // in some order. If the alert occurs before the priority change, we block |
michael@0 | 47 | // the alert until we observe the priority change. So the subprocess only |
michael@0 | 48 | // has to do |
michael@0 | 49 | // |
michael@0 | 50 | // // set priority to FOREGROUND_HIGH |
michael@0 | 51 | // alert('step0'); |
michael@0 | 52 | // // set priority to FOREGROUND |
michael@0 | 53 | // alert('step1'); |
michael@0 | 54 | // |
michael@0 | 55 | // etc. |
michael@0 | 56 | |
michael@0 | 57 | var childID = null; |
michael@0 | 58 | var alertTimes = []; |
michael@0 | 59 | |
michael@0 | 60 | // Return a promise that's resolved once the child process calls alert() and |
michael@0 | 61 | // we get a priority change, in some order. |
michael@0 | 62 | // |
michael@0 | 63 | // We check that the text of the alert is |"step" + index|. |
michael@0 | 64 | // |
michael@0 | 65 | // If gracePeriod is given, we check that the priority change occurred at |
michael@0 | 66 | // least gracePeriod ms since the alert from the previous step (with a fudge |
michael@0 | 67 | // factor to account for inaccurate timers). |
michael@0 | 68 | function expectAlertAndPriorityChange(index, priority, /* optional */ gracePeriod) { |
michael@0 | 69 | function checkAlertInfo(e) { |
michael@0 | 70 | is(e.detail.message, 'step' + index, 'alert() number ' + index); |
michael@0 | 71 | alertTimes.push(new Date()); |
michael@0 | 72 | |
michael@0 | 73 | // Block the alert; we'll unblock it by calling e.detail.unblock() later. |
michael@0 | 74 | e.preventDefault(); |
michael@0 | 75 | return Promise.resolve(e.detail.unblock); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | function checkGracePeriod() { |
michael@0 | 79 | if (gracePeriod) { |
michael@0 | 80 | var msSinceLastAlert = (new Date()) - alertTimes[index - 1]; |
michael@0 | 81 | |
michael@0 | 82 | // 50ms fudge factor. This test is set up so that, if nsITimers are |
michael@0 | 83 | // accurate, we don't need any fudge factor. Unfortunately our timers |
michael@0 | 84 | // are not accurate! There's little we can do here except fudge. |
michael@0 | 85 | // Thankfully all we're trying to test is that we get /some/ delay; the |
michael@0 | 86 | // exact amount of delay isn't so important. |
michael@0 | 87 | ok(msSinceLastAlert + 50 >= gracePeriod, |
michael@0 | 88 | msSinceLastAlert + "ms since last alert >= (" + gracePeriod + " - 50ms)"); |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | return Promise.all( |
michael@0 | 93 | [expectMozbrowserEvent(iframe, 'showmodalprompt').then(checkAlertInfo), |
michael@0 | 94 | expectPriorityChange(childID, priority).then(checkGracePeriod)] |
michael@0 | 95 | ).then(function(results) { |
michael@0 | 96 | // checkAlertInfo returns the function to call to unblock the alert. |
michael@0 | 97 | // It comes to us as the first element of the results array. |
michael@0 | 98 | results[0](); |
michael@0 | 99 | }); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | expectProcessCreated().then(function(chid) { |
michael@0 | 103 | childID = chid; |
michael@0 | 104 | return expectPriorityChange(childID, 'FOREGROUND'); |
michael@0 | 105 | }).then(function() { |
michael@0 | 106 | return expectAlertAndPriorityChange(0, 'FOREGROUND_HIGH'); |
michael@0 | 107 | }).then(function() { |
michael@0 | 108 | return expectAlertAndPriorityChange(1, 'FOREGROUND', priorityChangeGracePeriod); |
michael@0 | 109 | }).then(function() { |
michael@0 | 110 | return expectAlertAndPriorityChange(2, 'FOREGROUND_HIGH'); |
michael@0 | 111 | }).then(function() { |
michael@0 | 112 | return expectAlertAndPriorityChange(3, 'FOREGROUND', priorityChangeGracePeriod); |
michael@0 | 113 | }).then(SimpleTest.finish); |
michael@0 | 114 | |
michael@0 | 115 | document.body.appendChild(iframe); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | const priorityChangeGracePeriod = 100; |
michael@0 | 119 | addEventListener('testready', function() { |
michael@0 | 120 | SpecialPowers.pushPrefEnv( |
michael@0 | 121 | {set: [['dom.ipc.processPriorityManager.backgroundGracePeriodMS', |
michael@0 | 122 | priorityChangeGracePeriod], |
michael@0 | 123 | ['dom.wakelock.enabled', true]]}, |
michael@0 | 124 | runTest); |
michael@0 | 125 | }); |
michael@0 | 126 | |
michael@0 | 127 | </script> |
michael@0 | 128 | </body> |
michael@0 | 129 | </html> |