image/test/mochitest/imgutils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/test/mochitest/imgutils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +// Helper file for shared image functionality
     1.6 +// 
     1.7 +// Note that this is use by tests elsewhere in the source tree. When in doubt,
     1.8 +// check mxr before removing or changing functionality.
     1.9 +
    1.10 +// Helper function to clear the image cache of content images
    1.11 +function clearImageCache()
    1.12 +{
    1.13 +  var tools = SpecialPowers.Cc["@mozilla.org/image/tools;1"]
    1.14 +                             .getService(SpecialPowers.Ci.imgITools);
    1.15 +  var imageCache = tools.getImgCacheForDocument(window.document);
    1.16 +  imageCache.clearCache(false); // true=chrome, false=content
    1.17 +}
    1.18 +
    1.19 +// Helper function to determine if the frame is decoded for a given image id
    1.20 +function isFrameDecoded(id)
    1.21 +{
    1.22 +  return (getImageStatus(id) &
    1.23 +          SpecialPowers.Ci.imgIRequest.STATUS_FRAME_COMPLETE)
    1.24 +         ? true : false;
    1.25 +}
    1.26 +
    1.27 +// Helper function to determine if the image is loaded for a given image id
    1.28 +function isImageLoaded(id)
    1.29 +{
    1.30 +  return (getImageStatus(id) &
    1.31 +          SpecialPowers.Ci.imgIRequest.STATUS_LOAD_COMPLETE)
    1.32 +         ? true : false;
    1.33 +}
    1.34 +
    1.35 +// Helper function to get the status flags of an image
    1.36 +function getImageStatus(id)
    1.37 +{
    1.38 +  // Get the image
    1.39 +  var img = SpecialPowers.wrap(document.getElementById(id));
    1.40 +
    1.41 +  // QI the image to nsImageLoadingContent
    1.42 +  img.QueryInterface(SpecialPowers.Ci.nsIImageLoadingContent);
    1.43 +
    1.44 +  // Get the request
    1.45 +  var request = img.getRequest(SpecialPowers.Ci
    1.46 +                                         .nsIImageLoadingContent
    1.47 +                                         .CURRENT_REQUEST);
    1.48 +
    1.49 +  // Return the status
    1.50 +  return request.imageStatus;
    1.51 +}
    1.52 +
    1.53 +// Forces a synchronous decode of an image by drawing it to a canvas. Only
    1.54 +// really meaningful if the image is fully loaded first
    1.55 +function forceDecode(id)
    1.56 +{
    1.57 +  // Get the image
    1.58 +  var img = document.getElementById(id);
    1.59 +
    1.60 +  // Make a new canvas
    1.61 +  var canvas = document.createElement("canvas");
    1.62 +
    1.63 +  // Draw the image to the canvas. This forces a synchronous decode
    1.64 +  var ctx = canvas.getContext("2d");
    1.65 +  ctx.drawImage(img, 0, 0);
    1.66 +}
    1.67 +
    1.68 +
    1.69 +// Functions to facilitate getting/setting various image-related prefs
    1.70 +//
    1.71 +// If you change a pref in a mochitest, Don't forget to reset it to its 
    1.72 +// original value!
    1.73 +//
    1.74 +// Null indicates no pref set
    1.75 +
    1.76 +const DISCARD_ENABLED_PREF = {name: "discardable", branch: "image.mem.", type: "bool"};
    1.77 +const DECODEONDRAW_ENABLED_PREF = {name: "decodeondraw", branch: "image.mem.", type: "bool"};
    1.78 +const DISCARD_TIMEOUT_PREF = {name: "min_discard_timeout_ms", branch: "image.mem.", type: "int"};
    1.79 +
    1.80 +function setImagePref(pref, val)
    1.81 +{
    1.82 +  var prefService = SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
    1.83 +                                 .getService(SpecialPowers.Ci.nsIPrefService);
    1.84 +  var branch = prefService.getBranch(pref.branch);
    1.85 +  if (val != null) {
    1.86 +    switch(pref.type) {
    1.87 +      case "bool":
    1.88 +        branch.setBoolPref(pref.name, val);
    1.89 +        break;
    1.90 +      case "int":
    1.91 +        branch.setIntPref(pref.name, val);
    1.92 +        break;
    1.93 +      default:
    1.94 +        throw new Error("Unknown pref type");
    1.95 +    }
    1.96 +  }
    1.97 +  else if (branch.prefHasUserValue(pref.name))
    1.98 +    branch.clearUserPref(pref.name);
    1.99 +}
   1.100 +
   1.101 +function getImagePref(pref)
   1.102 +{
   1.103 +  var prefService = SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
   1.104 +                                 .getService(SpecialPowers.Ci.nsIPrefService);
   1.105 +  var branch = prefService.getBranch(pref.branch);
   1.106 +  if (branch.prefHasUserValue(pref.name)) {
   1.107 +    switch (pref.type) {
   1.108 +      case "bool":
   1.109 +        return branch.getBoolPref(pref.name);
   1.110 +      case "int":
   1.111 +        return branch.getIntPref(pref.name);
   1.112 +      default:
   1.113 +        throw new Error("Unknown pref type");
   1.114 +    }
   1.115 +  }
   1.116 +  else
   1.117 +    return null;
   1.118 +}
   1.119 +
   1.120 +// JS implementation of imgIScriptedNotificationObserver with stubs for all of its methods.
   1.121 +function ImageDecoderObserverStub()
   1.122 +{
   1.123 +  this.sizeAvailable = function sizeAvailable(aRequest)   {}
   1.124 +  this.frameComplete = function frameComplete(aRequest)   {}
   1.125 +  this.decodeComplete = function decodeComplete(aRequest) {}
   1.126 +  this.loadComplete = function loadComplete(aRequest)     {}
   1.127 +  this.frameUpdate = function frameUpdate(aRequest)       {}
   1.128 +  this.discard = function discard(aRequest)               {}
   1.129 +  this.isAnimated = function isAnimated(aRequest)         {}
   1.130 +}

mercurial