OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <script src="../resources/testharness.js"></script> | 2 <script src="../resources/testharness.js"></script> |
3 <script src="../resources/testharnessreport.js"></script> | 3 <script src="../resources/testharnessreport.js"></script> |
4 <script src="../resources/mojo-helpers.js"></script> | |
5 <script src="resources/fake-devices.js"></script> | 4 <script src="resources/fake-devices.js"></script> |
6 <script src="resources/usb-helpers.js"></script> | 5 <script src="resources/usb-helpers.js"></script> |
| 6 <script src="resources/webusb-test.js"></script> |
7 <script> | 7 <script> |
8 'use strict'; | 8 'use strict'; |
9 | 9 |
10 function runIframeDisconnectTest(usb, onDeviceConnected) { | 10 function runIframeDisconnectTest(onDeviceConnected) { |
11 return new Promise((resolve, reject) => { | 11 return navigator.usb.test.initialize().then(() => { |
12 let mockDeviceManager = new usb.MockDeviceManager; | 12 return new Promise((resolve, reject) => { |
13 let opened = false; | 13 let opened = false; |
14 let iframe = document.createElement('iframe'); | 14 let fakeDeviceGuid = null; |
15 | 15 |
16 window.onmessage = messageEvent => { | 16 let iframe = document.createElement('iframe'); |
17 if (messageEvent.data === 'Ready') { | 17 iframe.src = 'resources/open-in-iframe.html'; |
18 mockDeviceManager.addMockDevice(usb.fakeDevices[0]); | 18 iframe.onload = () => { |
19 } else if (messageEvent.data === 'Success') { | 19 navigator.usb.test.attachToWindow(iframe.contentWindow).then(() => { |
20 opened = true; | 20 iframe.contentWindow.postMessage('Ready', '*'); |
21 onDeviceConnected(iframe); | 21 }); |
22 } else { | 22 }; |
23 reject(messageEvent.data); | |
24 } | |
25 }; | |
26 | 23 |
27 mockDeviceManager.setDeviceCloseHandler(info => { | 24 window.onmessage = messageEvent => { |
28 assert_equals(info, usb.fakeDevices[0]); | 25 if (messageEvent.data == 'Ready') { |
29 assert_true(opened); | 26 fakeDeviceGuid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
30 resolve(); | 27 } else if (messageEvent.data == 'Success') { |
| 28 opened = true; |
| 29 onDeviceConnected(iframe); |
| 30 } else { |
| 31 reject(messageEvent.data); |
| 32 } |
| 33 }; |
| 34 |
| 35 navigator.usb.test.ondeviceclose = guid => { |
| 36 assert_equals(guid, fakeDeviceGuid); |
| 37 assert_true(opened); |
| 38 resolve(); |
| 39 }; |
| 40 |
| 41 document.body.appendChild(iframe); |
31 }); | 42 }); |
32 | |
33 iframe.src = 'resources/open-in-iframe.html'; | |
34 iframe.onload = () => { | |
35 iframe.contentWindow.gin.define( | |
36 "Mojo Service Registry", | |
37 [ 'content/public/renderer/frame_interfaces' ], | |
38 (frameInterfaces) => { | |
39 frameInterfaces.addInterfaceOverrideForTesting( | |
40 usb.DeviceManager.name, | |
41 handle => { mockDeviceManager.bindingSet.addBinding( | |
42 mockDeviceManager, handle); }); | |
43 iframe.contentWindow.postMessage('Ready', '*'); | |
44 }); | |
45 } | |
46 document.body.appendChild(iframe); | |
47 }); | 43 }); |
48 } | 44 } |
49 | 45 |
50 usb_test(usb => { | 46 promise_test(() => { |
51 return runIframeDisconnectTest(usb, iframe => { | 47 return runIframeDisconnectTest(iframe => { |
52 document.body.removeChild(iframe); | 48 document.body.removeChild(iframe); |
53 }); | 49 }); |
54 }, 'detaching iframe disconnects device.'); | 50 }, 'detaching iframe disconnects device.'); |
55 | 51 |
56 usb_test(usb => { | 52 promise_test(() => { |
57 return runIframeDisconnectTest(usb, iframe => { | 53 return runIframeDisconnectTest(iframe => { |
58 iframe.src = 'about:blank'; | 54 iframe.src = 'about:blank'; |
59 }); | 55 }); |
60 }, 'navigating iframe disconnects device.'); | 56 }, 'navigating iframe disconnects device.'); |
61 | 57 |
62 usb_test(usb => { | 58 promise_test(() => { |
63 return new Promise((resolve, reject) => { | 59 return navigator.usb.test.initialize().then(() => { |
64 let mockDeviceManager = new usb.MockDeviceManager; | 60 return new Promise((resolve, reject) => { |
65 let iframe = document.createElement('iframe'); | 61 let opened = false; |
| 62 let fakeDeviceGuid = null; |
66 | 63 |
67 window.onmessage = messageEvent => { | 64 let iframe = document.createElement('iframe'); |
68 if (messageEvent.data === 'Ready') { | 65 iframe.src = 'resources/open-in-iframe.html'; |
69 document.body.removeChild(iframe); | 66 iframe.onload = () => { |
70 runGarbageCollection().then(() => { | 67 navigator.usb.test.attachToWindow(iframe.contentWindow).then(() => { |
71 mockDeviceManager.addMockDevice(usb.fakeDevices[0]); | 68 iframe.contentWindow.postMessage('Ready', '*'); |
72 resolve(); | |
73 }); | 69 }); |
74 } else { | 70 }; |
75 reject(messageEvent.data); | |
76 } | |
77 }; | |
78 | 71 |
79 iframe.src = 'resources/open-in-iframe.html'; | 72 window.onmessage = messageEvent => { |
80 iframe.onload = () => { | 73 if (messageEvent.data == 'Ready') { |
81 iframe.contentWindow.gin.define( | 74 document.body.removeChild(iframe); |
82 "Mojo Service Registry", | 75 runGarbageCollection().then(() => { |
83 [ 'content/public/renderer/frame_interfaces' ], | 76 navigator.usb.test.addFakeDevice(fakeDeviceInit); |
84 (frameInterfaces) => { | 77 resolve(); |
85 frameInterfaces.addInterfaceOverrideForTesting( | 78 }); |
86 usb.DeviceManager.name, | 79 } else { |
87 handle => { mockDeviceManager.bindingSet.addBinding( | 80 reject(messageEvent.data); |
88 mockDeviceManager, handle); }); | 81 } |
89 iframe.contentWindow.postMessage('Ready', '*'); | 82 }; |
90 }); | 83 |
91 } | 84 document.body.appendChild(iframe); |
92 document.body.appendChild(iframe); | 85 }); |
93 }); | 86 }); |
94 }, 'events are not delivered to detached frames'); | 87 }, 'events are not delivered to detached frames'); |
95 </script> | 88 </script> |
OLD | NEW |