Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / serviceworker / chromium / resources / windowclient-focus.js
blob7b97ace2352a02a66b1b22ed5d82abc2249ad4a7
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');
7 // Clients nested inside the main client (|client|) used in this test.
8 var nestedClients = [];
10 // Override self.initialize() from sw-test-helpers.js
11 self.initialize = function() {
12     return self.clients.matchAll().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     });
25 function getNumberOfFocusedClients() {
26   return self.clients.matchAll().then(function(clients) {
27     var focusedClients = 0;
28     clients.forEach(function(c) {
29       if (c.focused)
30         ++focusedClients;
31     });
32     return focusedClients;
33   });
36 var TESTS = [
37     function testWithoutClick() {
38         client.focus().catch(function(e) {
39             self.postMessage('focus() can\'t focus a window without a user interaction');
40             self.postMessage('focus() error is ' + e.name);
41         }).then(runNextTestOrQuit);
42     },
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(' visibilityState: ' + c.visibilityState);
50                 self.postMessage(' focused: ' + c.focused);
51                 if (c.url == client.url)
52                     self.postMessage(' url is the same');
53                 if (c.frameType == client.frameType)
54                     self.postMessage(' frameType is the same');
55             }).then(getNumberOfFocusedClients)
56             .then(function(count) {
57                 // There should be 1 focused client at this point.
58                 self.postMessage('focused clients: ' + count);
59             }).then(runNextTestOrQuit);
60         });
61     },
63     function testFocusNestedFrame() {
64         synthesizeNotificationClick().then(function(e) {
65             nestedClients[0].focus().then(function(c) {
66                 self.postMessage('focus() succeeded');
67                 self.postMessage('focus() result: ' + c);
68                 self.postMessage(' visibilityState: ' + c.visibilityState);
69                 self.postMessage(' focused: ' + c.focused);
70                 if (c.url == nestedClients[0].url)
71                     self.postMessage(' url is the same');
72                 if (c.frameType == nestedClients[0].frameType)
73                     self.postMessage(' frameType is the same');
74             }).then(getNumberOfFocusedClients)
75             .then(function(count) {
76                 // There should be 2 focused clients at this point.
77                 // The nested frame and its parent.
78                 self.postMessage('focused clients: ' + count);
79             }).then(runNextTestOrQuit);
80         });
81     },
83     function testFocusOtherNestedFrame() {
84         synthesizeNotificationClick().then(function(e) {
85             nestedClients[1].focus().then(function(c) {
86                 self.postMessage('focus() succeeded');
87                 self.postMessage('focus() result: ' + c);
88                 self.postMessage(' visibilityState: ' + c.visibilityState);
89                 self.postMessage(' focused: ' + c.focused);
90                 if (c.url == nestedClients[1].url)
91                     self.postMessage(' url is the same');
92                 if (c.frameType == nestedClients[1].frameType)
93                     self.postMessage(' frameType is the same');
94             }).then(getNumberOfFocusedClients)
95             .then(function(count) {
96                 // There should still be 2 focused clients at this point.
97                 // The nested frame and its parent.
98                 self.postMessage('focused clients: ' + count);
99             }).then(runNextTestOrQuit);
100         });
101     },
103     function testFocusOpenedWindow() {
104         synthesizeNotificationClick().then(function(e) {
105             return self.clients.openWindow('windowclient-focus.html?3');
106         }).then(function(openedClient) {
107             synthesizeNotificationClick().then(function(e) {
108                 openedClient.focus().then(function(focusedClient) {
109                     self.postMessage('focus() succeeded');
110                     self.postMessage('focus() result: ' + focusedClient);
111                     self.postMessage(' visibilityState: ' + focusedClient.visibilityState);
112                     self.postMessage(' focused: ' + focusedClient.focused);
113                     if (openedClient.url == focusedClient.url)
114                         self.postMessage(' url is the same');
115                     if (openedClient.frameType == focusedClient.frameType)
116                         self.postMessage(' frameType is the same');
117                 }).then(getNumberOfFocusedClients)
118                 .then(function(count) {
119                     // There should be 1 focused clients at this point.
120                     self.postMessage('focused clients: ' + count);
121                 }).then(runNextTestOrQuit);
122             });
123         });
124     }
127 self.onmessage = function(e) {
128     if (e.data == 'start') {
129         initialize().then(runNextTestOrQuit);
130     } else {
131         initialize().then(function() {
132             self.postMessage('received unexpected message');
133             self.postMessage('quit');
134         });
135     }