OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <body> |
| 4 <script src="../../../resources/js-test.js"></script> |
| 5 <script> |
| 6 |
| 7 description('Tests using multiple event handlers for the Device Light API.'); |
| 8 |
| 9 var mockEvent; |
| 10 var expectedEvent; |
| 11 function setMockLight(value) { |
| 12 |
| 13 mockEvent = {value: value}; |
| 14 |
| 15 if (window.testRunner) |
| 16 testRunner.setMockDeviceLight(mockEvent.value); |
| 17 else |
| 18 debug('This test can not be run without the TestRunner'); |
| 19 } |
| 20 |
| 21 var deviceLightEvent; |
| 22 function checkLight(event) { |
| 23 deviceLightEvent = event; |
| 24 shouldBe('deviceLightEvent.value', 'expectedEvent.value'); |
| 25 } |
| 26 |
| 27 var counter = 0; |
| 28 function firstListener(event) { |
| 29 checkLight(event); |
| 30 counter++; |
| 31 proceedIfNecessary(); |
| 32 } |
| 33 |
| 34 function secondListener(event) { |
| 35 checkLight(event); |
| 36 counter++; |
| 37 proceedIfNecessary(); |
| 38 } |
| 39 |
| 40 function proceedIfNecessary() { |
| 41 if (counter == 2) { |
| 42 setMockLight(20); |
| 43 // Note: this should not stop Device Light updates, |
| 44 // because there is still one listener active. |
| 45 window.removeEventListener('devicelight', secondListener); |
| 46 setTimeout(function(){initThirdListener();}, 0); |
| 47 } |
| 48 } |
| 49 |
| 50 var childFrame; |
| 51 function initThirdListener() { |
| 52 childFrame = document.createElement('iframe'); |
| 53 document.body.appendChild(childFrame); |
| 54 childFrame.contentWindow.addEventListener('devicelight', thirdListener); |
| 55 } |
| 56 |
| 57 function thirdListener(event) { |
| 58 // Expect the cached event because Device Light was already active |
| 59 // when third listener was added. |
| 60 checkLight(event); |
| 61 window.removeEventListener('devicelight', firstListener); |
| 62 childFrame.contentWindow.removeEventListener('devicelight', thirdListener); |
| 63 setTimeout(function(){initFourthListener();}, 0); |
| 64 } |
| 65 |
| 66 function initFourthListener() { |
| 67 expectedEvent = mockEvent; |
| 68 window.addEventListener('devicelight', fourthListener); |
| 69 } |
| 70 |
| 71 function fourthListener(event) { |
| 72 checkLight(event); |
| 73 finishJSTest(); |
| 74 } |
| 75 |
| 76 setMockLight(10); |
| 77 expectedEvent = mockEvent; |
| 78 window.addEventListener('devicelight', firstListener); |
| 79 window.addEventListener('devicelight', secondListener); |
| 80 |
| 81 window.jsTestIsAsync = true; |
| 82 |
| 83 </script> |
| 84 </body> |
| 85 </html> |
OLD | NEW |