summaryrefslogtreecommitdiffstats
path: root/b2g/components/test/mochitest/test_presentation_request_ui_glue.html
blob: 29ac37221b23d3b30cdf4d324f4157987f412f03 (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
<!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 UI Glue</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 UI Glue</a>
<script type="application/javascript;version=1.8">

'use strict';

SimpleTest.waitForExplicitFinish();

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

var obs = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
          .getService(SpecialPowers.Ci.nsIObserverService);

var url = 'http://example.com';
var sessionId = 'sessionId';

function testLaunchReceiver() {
  return new Promise(function(aResolve, aReject) {
    gScript.addMessageListener('presentation-launch-receiver', function launchReceiverHandler(aDetail) {
      gScript.removeMessageListener('presentation-launch-receiver', launchReceiverHandler);
      ok(true, "A presentation-launch-receiver mozPresentationChromeEvent should be received.");
      is(aDetail.url, url, "Url should be the same.");
      is(aDetail.id, sessionId, "Session ID should be the same.");

      aResolve();
    });

    gScript.sendAsyncMessage('trigger-ui-glue',
                             { url: url,
                               sessionId : sessionId });
  });
}

function testReceiverLaunched() {
  return new Promise(function(aResolve, aReject) {
    gScript.addMessageListener('iframe-resolved', function iframeResolvedHandler(aFrame) {
      gScript.removeMessageListener('iframe-resolved', iframeResolvedHandler);
      ok(true, "The promise should be resolved.");

      aResolve();
    });

    var iframe = document.createElement('iframe');
    iframe.setAttribute('remote', 'true');
    iframe.setAttribute('mozbrowser', 'true');
    iframe.setAttribute('src', 'http://example.com');
    document.body.appendChild(iframe);

    gScript.sendAsyncMessage('trigger-presentation-content-event',
                             { type: 'presentation-receiver-launched',
                               id: sessionId,
                               frame: iframe });
  });
}

function testLaunchError() {
  return new Promise(function(aResolve, aReject) {
    gScript.addMessageListener('presentation-launch-receiver', function launchReceiverHandler(aDetail) {
      gScript.removeMessageListener('presentation-launch-receiver', launchReceiverHandler);
      ok(true, "A presentation-launch-receiver mozPresentationChromeEvent should be received.");
      is(aDetail.url, url, "Url should be the same.");
      is(aDetail.id, sessionId, "Session ID should be the same.");

      gScript.addMessageListener('iframe-rejected', function iframeRejectedHandler() {
        gScript.removeMessageListener('iframe-rejected', iframeRejectedHandler);
        ok(true, "The promise should be rejected.");
        aResolve();
      });

    gScript.sendAsyncMessage('trigger-presentation-content-event',
                             { type: 'presentation-receiver-permission-denied',
                               id: sessionId });
    });

    gScript.sendAsyncMessage('trigger-ui-glue',
                             { url: url,
                               sessionId : sessionId });
  });
}

function runTests() {
  testLaunchReceiver()
  .then(testReceiverLaunched)
  .then(testLaunchError)
  .then(function() {
    info('test finished, teardown');
    gScript.destroy();
    SimpleTest.finish();
  });
}

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