OLD | NEW |
(Empty) | |
| 1 // This helper will setup a small test framework that will use TESTS and run |
| 2 // them sequentially and call self.postMessage('quit') when done. |
| 3 // This helper also exposes |client|, |postMessage()|, |runNextTestOrQuit()|, |
| 4 // |synthesizeNotificationClick()| and |initialize()|. |
| 5 importScripts('sw-test-helpers.js'); |
| 6 |
| 7 // Clients nested inside the main client (|client|) used in this test. |
| 8 var nestedClients = []; |
| 9 |
| 10 // Override self.initialize() from sw-test-helpers.js |
| 11 self.initialize = function() { |
| 12 return self.clients.getAll().then(function(clients) { |
| 13 clients.forEach(function(c) { |
| 14 // All clients are iframes but one of them embeds the other ones. We |
| 15 // want to use that one as the main |client|. |
| 16 // Its url ends with '.html' while the others ends with '.html?X' |
| 17 if (c.url.endsWith('.html')) |
| 18 client = c; |
| 19 else |
| 20 nestedClients.push(c); |
| 21 }); |
| 22 }); |
| 23 }; |
| 24 |
| 25 function getNumberOfFocusedClients() { |
| 26 return self.clients.getAll().then(function(clients) { |
| 27 var focusedClients = 0; |
| 28 clients.forEach(function(c) { |
| 29 if (c.focused) |
| 30 ++focusedClients; |
| 31 }); |
| 32 return focusedClients; |
| 33 }); |
| 34 } |
| 35 |
| 36 var TESTS = [ |
| 37 function testWithoutClick() { |
| 38 client.focus().catch(function(e) { |
| 39 self.postMessage('focus() can\'t focus a window without a user inter
action'); |
| 40 self.postMessage('focus() error is ' + e.name); |
| 41 }).then(runNextTestOrQuit); |
| 42 }, |
| 43 |
| 44 function testFocusingFrame() { |
| 45 synthesizeNotificationClick().then(function(e) { |
| 46 client.focus().then(function(c) { |
| 47 self.postMessage('focus() succeeded'); |
| 48 self.postMessage('focus() result: ' + c); |
| 49 self.postMessage(' url: ' + c.url); |
| 50 self.postMessage(' visibilityState: ' + c.visibilityState); |
| 51 self.postMessage(' focused: ' + c.focused); |
| 52 self.postMessage(' frameType: ' + c.frameType); |
| 53 }).then(getNumberOfFocusedClients) |
| 54 .then(function(count) { |
| 55 // There should be 1 focused client at this point. |
| 56 self.postMessage('focused clients: ' + count); |
| 57 }).then(runNextTestOrQuit); |
| 58 }); |
| 59 }, |
| 60 |
| 61 function testFocusNestedFrame() { |
| 62 synthesizeNotificationClick().then(function(e) { |
| 63 nestedClients[0].focus().then(function(c) { |
| 64 self.postMessage('focus() succeeded'); |
| 65 self.postMessage('focus() result: ' + c); |
| 66 self.postMessage(' url: ' + c.url); |
| 67 self.postMessage(' visibilityState: ' + c.visibilityState); |
| 68 self.postMessage(' focused: ' + c.focused); |
| 69 self.postMessage(' frameType: ' + c.frameType); |
| 70 }).then(getNumberOfFocusedClients) |
| 71 .then(function(count) { |
| 72 // There should be 2 focused clients at this point. |
| 73 // The nested frame and its parent. |
| 74 self.postMessage('focused clients: ' + count); |
| 75 }).then(runNextTestOrQuit); |
| 76 }); |
| 77 }, |
| 78 |
| 79 function testFocusOtherNestedFrame() { |
| 80 synthesizeNotificationClick().then(function(e) { |
| 81 nestedClients[1].focus().then(function(c) { |
| 82 self.postMessage('focus() succeeded'); |
| 83 self.postMessage('focus() result: ' + c); |
| 84 self.postMessage(' url: ' + c.url); |
| 85 self.postMessage(' visibilityState: ' + c.visibilityState); |
| 86 self.postMessage(' focused: ' + c.focused); |
| 87 self.postMessage(' frameType: ' + c.frameType); |
| 88 }).then(getNumberOfFocusedClients) |
| 89 .then(function(count) { |
| 90 // There should still be 2 focused clients at this point. |
| 91 // The nested frame and its parent. |
| 92 self.postMessage('focused clients: ' + count); |
| 93 }).then(runNextTestOrQuit); |
| 94 }); |
| 95 }, |
| 96 |
| 97 function testFocusOpenedWindow() { |
| 98 synthesizeNotificationClick().then(function(e) { |
| 99 return self.clients.openWindow('windowclient-focus.html?3'); |
| 100 }).then(function(c) { |
| 101 synthesizeNotificationClick().then(function(e) { |
| 102 c.focus().then(function() { |
| 103 self.postMessage('focus() succeeded'); |
| 104 self.postMessage('focus() result: ' + c); |
| 105 self.postMessage(' url: ' + c.url); |
| 106 self.postMessage(' visibilityState: ' + c.visibilityState); |
| 107 self.postMessage(' focused: ' + c.focused); |
| 108 self.postMessage(' frameType: ' + c.frameType); |
| 109 }).then(getNumberOfFocusedClients) |
| 110 .then(function(count) { |
| 111 // There should be 1 focused clients at this point. |
| 112 self.postMessage('focused clients: ' + count); |
| 113 }).then(runNextTestOrQuit); |
| 114 }); |
| 115 }); |
| 116 } |
| 117 ]; |
| 118 |
| 119 self.onmessage = function(e) { |
| 120 if (e.data == 'start') { |
| 121 initialize().then(runNextTestOrQuit); |
| 122 } else { |
| 123 initialize().then(function() { |
| 124 self.postMessage('received unexpected message'); |
| 125 self.postMessage('quit'); |
| 126 }); |
| 127 } |
| 128 }; |
OLD | NEW |