summaryrefslogtreecommitdiffstats
path: root/b2g/components/test/mochitest/test_presentation_device_prompt.html
blob: 9feeca7952013fad21d07acdd6eb5bf16bc26646 (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
145
<!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 Presentation Device Selection</title>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Test for Presentation Device Selection</a>
<script type="application/javascript;version=1.8">

'use strict';

SimpleTest.waitForExplicitFinish();

var contentEventHandler = null;

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

function testSetup() {
  info('setup for device selection');
  return new Promise(function(resolve, reject) {
    let device = {
      id: 'test-id',
      name: 'test-name',
      type: 'test-type',
    };
    gScript.addMessageListener('setup-complete', function() {
      resolve(device);
    });
    gScript.sendAsyncMessage('setup', device);
  });
}

function testSelected(device) {
  info('test device selected by user');
  return new Promise(function(resolve, reject) {
    let request = {
      origin: 'test-origin',
      requestURL: 'test-requestURL',
    };

    gScript.addMessageListener('presentation-select-device', function contentEventHandler(detail) {
      gScript.removeMessageListener('presentation-select-device', contentEventHandler);
      ok(true, 'receive user prompt for device selection');
      is(detail.origin, request.origin, 'expected origin');
      is(detail.requestURL, request.requestURL, 'expected requestURL');
      let response = {
        id: detail.id,
        type: 'presentation-select-result',
        deviceId: device.id,
      };
      gScript.sendAsyncMessage('presentation-select-response', response);

      gScript.addMessageListener('presentation-select-result', function resultHandler(result) {
        gScript.removeMessageListener('presentation-select-result', resultHandler);
        is(result.type, 'select', 'expect device selected');
        is(result.device.id, device.id, 'expected device id');
        is(result.device.name, device.name, 'expected device name');
        is(result.device.type, device.type, 'expected devcie type');
        resolve();
      });
    });

    gScript.sendAsyncMessage('trigger-device-prompt', request);
  });
}

function testSelectedNotExisted() {
  info('test selected device doesn\'t exist');
  return new Promise(function(resolve, reject) {
    gScript.addMessageListener('presentation-select-device', function contentEventHandler(detail) {
      gScript.removeMessageListener('presentation-select-device', contentEventHandler);
      ok(true, 'receive user prompt for device selection');
      let response = {
        id: detail.id,
        type: 'presentation-select-deny',
        deviceId: undefined, // simulate device Id that doesn't exist
      };
      gScript.sendAsyncMessage('presentation-select-response', response);

      gScript.addMessageListener('presentation-select-result', function resultHandler(result) {
        gScript.removeMessageListener('presentation-select-result', resultHandler);
        is(result.type, 'cancel', 'expect user cancel');
        resolve();
      });
    });

    let request = {
      origin: 'test-origin',
      requestURL: 'test-requestURL',
    };
    gScript.sendAsyncMessage('trigger-device-prompt', request);
  });
}

function testDenied() {
  info('test denial of device selection');
  return new Promise(function(resolve, reject) {
    gScript.addMessageListener('presentation-select-device', function contentEventHandler(detail) {
      gScript.removeMessageListener('presentation-select-device', contentEventHandler);
      ok(true, 'receive user prompt for device selection');
      let response = {
        id: detail.id,
        type: 'presentation-select-deny',
      };
      gScript.sendAsyncMessage('presentation-select-response', response);

      gScript.addMessageListener('presentation-select-result', function resultHandler(result) {
        gScript.removeMessageListener('presentation-select-result', resultHandler);
        is(result.type, 'cancel', 'expect user cancel');
        resolve();
      });
    });

    let request = {
      origin: 'test-origin',
      requestURL: 'test-requestURL',
    };
    gScript.sendAsyncMessage('trigger-device-prompt', request);
  });
}

function runTests() {
  testSetup()
  .then(testSelected)
  .then(testSelectedNotExisted)
  .then(testDenied)
  .then(function() {
    info('test finished, teardown');
    gScript.sendAsyncMessage('teardown');
    gScript.destroy();
    SimpleTest.finish();
  });
}

window.addEventListener('load', runTests);
</script>
</pre>
</body>
</html>