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