toolkit/components/mediasniffer/test/unit/test_mediasniffer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/mediasniffer/test/unit/test_mediasniffer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Ci = Components.interfaces;
     1.9 +const Cu = Components.utils;
    1.10 +
    1.11 +Cu.import("resource://testing-common/httpd.js");
    1.12 +
    1.13 +const PATH = "/file.meh";
    1.14 +var httpserver = new HttpServer();
    1.15 +
    1.16 +// Each time, the data consist in a string that should be sniffed as Ogg.
    1.17 +const data = "OggS\0meeeh.";
    1.18 +var testRan = 0;
    1.19 +
    1.20 +// If the content-type is not present, or if it's application/octet-stream, it
    1.21 +// should be sniffed to application/ogg by the media sniffer. Otherwise, it
    1.22 +// should not be changed.
    1.23 +const tests = [
    1.24 +  // Those three first case are the case of a media loaded in a media element.
    1.25 +  // The first two should be sniffeed.
    1.26 +  { contentType: "",
    1.27 +    expectedContentType: "application/ogg",
    1.28 +    flags: Ci.nsIChannel.LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN },
    1.29 +  { contentType: "application/octet-stream",
    1.30 +    expectedContentType: "application/ogg",
    1.31 +    flags: Ci.nsIChannel.LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN },
    1.32 +  { contentType: "application/something",
    1.33 +    expectedContentType: "application/something",
    1.34 +    flags: Ci.nsIChannel.LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN },
    1.35 +  // This last case tests the case of a channel opened while allowing content
    1.36 +  // sniffers to override the content-type, like in the docshell.
    1.37 +  { contentType: "application/octet-stream",
    1.38 +    expectedContentType: "application/ogg",
    1.39 +    flags: Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS },
    1.40 +  { contentType: "",
    1.41 +    expectedContentType: "application/ogg",
    1.42 +    flags: Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS },
    1.43 +];
    1.44 +
    1.45 +// A basic listener that reads checks the if we sniffed properly.
    1.46 +var listener = {
    1.47 +  onStartRequest: function(request, context) {
    1.48 +    do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType,
    1.49 +                tests[testRan].expectedContentType);
    1.50 +  },
    1.51 +
    1.52 +  onDataAvailable: function(request, context, stream, offset, count) {
    1.53 +    try {
    1.54 +      var bis = Components.classes["@mozilla.org/binaryinputstream;1"]
    1.55 +                          .createInstance(Components.interfaces.nsIBinaryInputStream);
    1.56 +      bis.setInputStream(stream);
    1.57 +      bis.readByteArray(bis.available());
    1.58 +    } catch (ex) {
    1.59 +      do_throw("Error in onDataAvailable: " + ex);
    1.60 +    }
    1.61 +  },
    1.62 +
    1.63 +  onStopRequest: function(request, context, status) {
    1.64 +    testRan++;
    1.65 +    runNext();
    1.66 +  }
    1.67 +};
    1.68 +
    1.69 +function setupChannel(url, flags)
    1.70 +{
    1.71 +  var ios = Components.classes["@mozilla.org/network/io-service;1"].
    1.72 +                       getService(Ci.nsIIOService);
    1.73 +  var chan = ios.newChannel("http://localhost:" +
    1.74 +                           httpserver.identity.primaryPort + url, "", null);
    1.75 +  chan.loadFlags |= flags;
    1.76 +  var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
    1.77 +  return httpChan;
    1.78 +}
    1.79 +
    1.80 +function runNext() {
    1.81 +  if (testRan == tests.length) {
    1.82 +    do_test_finished();
    1.83 +    return;
    1.84 +  }
    1.85 +  var channel = setupChannel(PATH, tests[testRan].flags);
    1.86 +  httpserver.registerPathHandler(PATH, function(request, response) {
    1.87 +    response.setHeader("Content-Type", tests[testRan].contentType, false);
    1.88 +    response.bodyOutputStream.write(data, data.length);
    1.89 +  });
    1.90 +  channel.asyncOpen(listener, channel, null);
    1.91 +}
    1.92 +
    1.93 +function run_test() {
    1.94 +  httpserver.start(-1);
    1.95 +  do_test_pending();
    1.96 +  try {
    1.97 +    runNext();
    1.98 +  } catch (e) {
    1.99 +    print("ERROR - " + e + "\n");
   1.100 +  }
   1.101 +}

mercurial