summaryrefslogtreecommitdiffstats
path: root/dom/presentation/tests/mochitest/test_presentation_device_info.html
blob: 77253e41dba36652e587b424fa3ba728666a576f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE HTML>
<html>
<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<head>
  <meta charset="utf-8">
  <title>Test for B2G Presentation Device Info API</title>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1080474">Test for B2G Presentation Device Info API</a>
<script type="application/javascript;version=1.8">

'use strict';

SimpleTest.waitForExplicitFinish();

var testDevice = {
  id: 'id',
  name: 'name',
  type: 'type',
};

var gUrl = SimpleTest.getTestFileURL('PresentationDeviceInfoChromeScript.js');
var gScript = SpecialPowers.loadChromeScript(gUrl);

function testSetup() {
  return new Promise(function(resolve, reject) {
    gScript.addMessageListener('setup-complete', function() {
      resolve();
    });
    gScript.sendAsyncMessage('setup');
  });
}

function testForceDiscovery() {
  info('test force discovery');
  return new Promise(function(resolve, reject) {
    gScript.addMessageListener('force-discovery', function() {
      ok(true, 'nsIPresentationDeviceProvider.forceDiscovery is invoked');
      resolve();
    });
    navigator.mozPresentationDeviceInfo.forceDiscovery();
  });
}

function testDeviceAdd() {
  info('test device add');
  return new Promise(function(resolve, reject) {
    navigator.mozPresentationDeviceInfo.addEventListener('devicechange', function deviceChangeHandler(e) {
      navigator.mozPresentationDeviceInfo.removeEventListener('devicechange', deviceChangeHandler);
      let detail = e.detail;
      is(detail.type, 'add', 'expected update type');
      is(detail.deviceInfo.id, testDevice.id, 'expected device id');
      is(detail.deviceInfo.name, testDevice.name, 'expected device name');
      is(detail.deviceInfo.type, testDevice.type, 'expected device type');

      navigator.mozPresentationDeviceInfo.getAll()
      .then(function(devices) {
        is(devices.length, 1, 'expected 1 available device');
        is(devices[0].id, testDevice.id, 'expected device id');
        is(devices[0].name, testDevice.name, 'expected device name');
        is(devices[0].type, testDevice.type, 'expected device type');
        resolve();
      });
    });
    gScript.sendAsyncMessage('trigger-device-add', testDevice);
  });
}

function testDeviceUpdate() {
  info('test device update');
  return new Promise(function(resolve, reject) {
    testDevice.name = 'name-update';

    navigator.mozPresentationDeviceInfo.addEventListener('devicechange', function deviceChangeHandler(e) {
      navigator.mozPresentationDeviceInfo.removeEventListener('devicechange', deviceChangeHandler);
      let detail = e.detail;
      is(detail.type, 'update', 'expected update type');
      is(detail.deviceInfo.id, testDevice.id, 'expected device id');
      is(detail.deviceInfo.name, testDevice.name, 'expected device name');
      is(detail.deviceInfo.type, testDevice.type, 'expected device type');

      navigator.mozPresentationDeviceInfo.getAll()
      .then(function(devices) {
        is(devices.length, 1, 'expected 1 available device');
        is(devices[0].id, testDevice.id, 'expected device id');
        is(devices[0].name, testDevice.name, 'expected device name');
        is(devices[0].type, testDevice.type, 'expected device type');
        resolve();
      });
    });
    gScript.sendAsyncMessage('trigger-device-update', testDevice);
  });
}

function testDeviceRemove() {
  info('test device remove');
  return new Promise(function(resolve, reject) {
    navigator.mozPresentationDeviceInfo.addEventListener('devicechange', function deviceChangeHandler(e) {
      navigator.mozPresentationDeviceInfo.removeEventListener('devicechange', deviceChangeHandler);
      let detail = e.detail;
      is(detail.type, 'remove', 'expected update type');
      is(detail.deviceInfo.id, testDevice.id, 'expected device id');
      is(detail.deviceInfo.name, testDevice.name, 'expected device name');
      is(detail.deviceInfo.type, testDevice.type, 'expected device type');

      navigator.mozPresentationDeviceInfo.getAll()
      .then(function(devices) {
        is(devices.length, 0, 'expected 0 available device');
        resolve();
      });
    });
    gScript.sendAsyncMessage('trigger-device-remove');
  });
}

function runTests() {
  testSetup()
  .then(testForceDiscovery)
  .then(testDeviceAdd)
  .then(testDeviceUpdate)
  .then(testDeviceRemove)
  .then(function() {
    info('test finished, teardown');
    gScript.sendAsyncMessage('teardown', '');
    gScript.destroy();
    SimpleTest.finish();
  });
}

window.addEventListener('load', function() {
  SpecialPowers.pushPrefEnv({
    'set': [
      ['dom.presentation.enabled', true],
    ]
  }, runTests);
});

</script>
</pre>
</body>
</html>