<!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>