summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/test_video_discovery.html
blob: f6fb60bbe56bb56c85ad38cfcaf3676a25cb8e6f (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
146
147
148
149
150
151
152
153
154
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=953381
Migrated from Robocop: https://bugzilla.mozilla.org/show_bug.cgi?id=1184186
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 953381</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <script type="application/javascript;version=1.7">

  "use strict";

  /*globals SimpleServiceDiscovery */

  const { classes: Cc, interfaces: Ci, utils: Cu } = Components;

  Cu.import("resource://gre/modules/Services.jsm");
  Cu.import("resource://gre/modules/SimpleServiceDiscovery.jsm");

  // The chrome window
  let chromeWin;

  // Track the <browser> where the tests are happening
  let browser;

  function middle(element) {
    let rect = element.getBoundingClientRect();
    let x = (rect.right - rect.left) / 2 + rect.left;
    let y = (rect.bottom - rect.top) / 2 + rect.top;
    return [x, y];
  }

  // We must register a device and make a "mock" service for the device
  var testDevice = {
    id: "test:dummy",
    target: "test:service",
    factory: function(service) { /* dummy */  },
    types: ["video/mp4", "video/webm"],
    extensions: ["mp4", "webm"]
  };

  function setup_browser() {
    chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
    let BrowserApp = chromeWin.BrowserApp;

    SimpleTest.registerCleanupFunction(function cleanup() {
      BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
      SimpleServiceDiscovery.unregisterDevice(testDevice);
    });

    // We need to register a device or processService will ignore us
    SimpleServiceDiscovery.registerDevice(testDevice);

    // Create a pretend service
    let service = {
      location: "http://mochi.test:8888/chrome/mobile/android/tests/browser/chrome/simpleservice.xml",
      target: "test:service"
    };

    dump("Force a detailed ping from a pretend service");

    // Poke the service directly to get the discovery to happen
    SimpleServiceDiscovery._processService(service);

    // Load our test web page with <video> elements
    let url = "http://mochi.test:8888/chrome/mobile/android/tests/browser/chrome/video_discovery.html";
    browser = BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id }).browser;
    browser.addEventListener("load", function startTests(event) {
      browser.removeEventListener("load", startTests, true);
      Services.tm.mainThread.dispatch(test_video, Ci.nsIThread.DISPATCH_NORMAL);
    }, true);
  }

  let videoDiscoveryTests = [
    { id: "simple-mp4", source: "http://mochi.test:8888/simple.mp4", poster: "http://mochi.test:8888/simple.png", text: "simple video with mp4 src" },
    { id: "simple-fail", pass: false, text: "simple video with no mp4 src" },
    { id: "with-sources-mp4", source: "http://mochi.test:8888/simple.mp4", text: "video with mp4 extension source child" },
    { id: "with-sources-webm", source: "http://mochi.test:8888/simple.webm", text: "video with webm extension source child" },
    { id: "with-sources-fail", pass: false, text: "video with no mp4 extension source child" },
    { id: "with-sources-mimetype-mp4", source: "http://mochi.test:8888/simple-video-mp4", text: "video with mp4 mimetype source child" },
    { id: "with-sources-mimetype-webm", source: "http://mochi.test:8888/simple-video-webm", text: "video with webm mimetype source child" },
    { id: "simple-fetch-pass", source: "http://mochi.test:8888/chrome/mobile/android/tests/browser/chrome/video_discovery.sjs?type=video/mp4", text: "simple video with mp4 mimetype fetched from server" },
    { id: "simple-fetch-fail", pass: false, text: "simple video with non-video mimetype fetched from server" },
    { id: "video-overlay", source: "http://mochi.test:8888/simple.mp4", text: "div overlay covering a simple video with mp4 src" }
  ];

  let expectedTests = videoDiscoveryTests.length;

  function execute_video_test(test) {
    let element = browser.contentDocument.getElementById(test.id);
    if (element) {
      let [x, y] = middle(element);
      dump("Starting to getVideo");
      chromeWin.CastingApps.getVideo(element, x, y, (video) => {
        dump("Completed getVideo");
        if (video) {
          dump("video source: " + video.source);

          let matchPoster = (test.poster == video.poster);
          let matchSource = (test.source == video.source);
          ok(matchPoster && matchSource && test.pass, test.text);
        } else {
          ok(!test.pass, test.text);
        }
        expectedTests--;
        if (expectedTests == 0) {
          SimpleTest.finish();
        }
      });
    } else {
      ok(false, "test element not found: [" + test.id + "]");
      SimpleTest.finish();
    }
  }

  function test_video() {
    let videoTest;
    while ((videoTest = videoDiscoveryTests.shift())) {
      if (!("poster" in videoTest)) {
        videoTest.poster = "";
      }
      if (!("pass" in videoTest)) {
        videoTest.pass = true;
      }
      execute_video_test(videoTest);
    }
  }

  SimpleTest.waitForExplicitFinish();

  // On debug runs, 10 assertions typically observed; 5 each of:
  //  - ASSERTION: cancel with non-failure status code: 'NS_FAILED(status)'
  //  - ASSERTION: OnDataAvailable implementation consumed no data: 'Error'
  SimpleTest.expectAssertions(0,10);
  setup_browser();

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=953381">Mozilla Bug 953381</a>
<br>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1184186">Migrated from Robocop testVideoDiscovery</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>