- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / web_view / pointerlock / embedder.js
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var embedder = {};
6 embedder.test = {};
7 embedder.triggerNavUrl =
8     'data:text/html,<html><body>trigger navigation<body></html>';
9
10 window.runTest = function(testName) {
11   if (!embedder.test.testList[testName]) {
12     console.log('Incorrect testName: ' + testName);
13     embedder.test.fail();
14     return;
15   }
16
17   // Run the test.
18   embedder.test.testList[testName]();
19 };
20 // window.* exported functions end.
21
22 /** @private */
23 embedder.setUpGuest_ = function(partitionName) {
24   document.querySelector('#webview-tag-container').innerHTML =
25       '<webview style="width: 100px; height: 100px;"></webview>';
26   var webview = document.querySelector('webview');
27   if (partitionName) {
28     webview.partition = partitionName;
29   }
30   if (!webview) {
31     embedder.test.fail('No <webview> element created');
32   }
33   return webview;
34 };
35
36 embedder.test = {};
37 embedder.test.succeed = function() {
38   chrome.test.sendMessage('TEST_PASSED');
39 };
40
41 embedder.test.fail = function() {
42   chrome.test.sendMessage('TEST_FAILED');
43 };
44
45 embedder.test.assertEq = function(a, b) {
46   if (a != b) {
47     console.log('assertion failed: ' + a + ' != ' + b);
48     embedder.test.fail();
49   }
50 };
51
52 embedder.test.assertTrue = function(condition) {
53   if (!condition) {
54     console.log('assertion failed: true != ' + condition);
55     embedder.test.fail();
56   }
57 };
58
59 embedder.test.assertFalse = function(condition) {
60   if (condition) {
61     console.log('assertion failed: false != ' + condition);
62     embedder.test.fail();
63   }
64 };
65
66 // Tests begin.
67 // This test verifies that a webview loses pointer lock when it loses focus.
68 function testPointerLockLostWithFocus() {
69   var webview1 = document.createElement('webview');
70   var webview2 = document.createElement('webview');
71
72   // Wait until both webviews finish loading.
73   var loadstopCount = 0;
74   var onLoadStop = function(e) {
75     if (++loadstopCount < 2) {
76       return;
77     }
78     console.log('webview1 and webview2 have loaded.');
79     webview1.focus();
80     webview1.executeScript(
81       {file: 'inject_pointer_lock.js'},
82       function(results) {
83         console.log('Injected script into webview1.');
84         // Establish a communication channel with the webview1's guest.
85         var msg = ['connect'];
86         webview1.contentWindow.postMessage(JSON.stringify(msg), '*');
87       });
88   };
89   webview1.addEventListener('loadstop', onLoadStop);
90   webview2.addEventListener('loadstop', onLoadStop);
91
92   window.addEventListener('message', function(e) {
93     var data = JSON.parse(e.data);
94     if (data[0] == 'connected') {
95       console.log('Established communication channel with webview1.');
96       // Once a communication channel has been established, we start the test.
97       var msg = ['start-pointerlock'];
98       webview1.contentWindow.postMessage(JSON.stringify(msg), '*');
99       return;
100     }
101
102     if (data[0] == 'acquired-pointerlock') {
103       console.log('webview1 has successfully acquired pointer lock.');
104       webview2.focus();
105       console.log('webview2 has taken focus.');
106       return;
107     }
108
109     embedder.test.assertEq('lost-pointerlock', data[0]);
110     console.log('webview1 has lost pointer lock.');
111     embedder.test.succeed();
112   });
113
114   webview1.addEventListener('permissionrequest', function(e) {
115     console.log('webview1 has requested pointer lock.');
116     e.request.allow();
117     console.log('webview1 has been granted pointer lock.');
118   });
119
120   webview1.setAttribute('src', embedder.triggerNavUrl);
121   webview2.setAttribute('src', embedder.triggerNavUrl);
122   document.body.appendChild(webview1);
123   document.body.appendChild(webview2);
124 }
125
126 embedder.test.testList = {
127   'testPointerLockLostWithFocus': testPointerLockLostWithFocus,
128 };
129
130 onload = function() {
131   chrome.test.getConfig(function(config) {
132     chrome.test.sendMessage('Launched');
133   });
134 };