image/test/mochitest/imgutils.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 // Helper file for shared image functionality
michael@0 3 //
michael@0 4 // Note that this is use by tests elsewhere in the source tree. When in doubt,
michael@0 5 // check mxr before removing or changing functionality.
michael@0 6
michael@0 7 // Helper function to clear the image cache of content images
michael@0 8 function clearImageCache()
michael@0 9 {
michael@0 10 var tools = SpecialPowers.Cc["@mozilla.org/image/tools;1"]
michael@0 11 .getService(SpecialPowers.Ci.imgITools);
michael@0 12 var imageCache = tools.getImgCacheForDocument(window.document);
michael@0 13 imageCache.clearCache(false); // true=chrome, false=content
michael@0 14 }
michael@0 15
michael@0 16 // Helper function to determine if the frame is decoded for a given image id
michael@0 17 function isFrameDecoded(id)
michael@0 18 {
michael@0 19 return (getImageStatus(id) &
michael@0 20 SpecialPowers.Ci.imgIRequest.STATUS_FRAME_COMPLETE)
michael@0 21 ? true : false;
michael@0 22 }
michael@0 23
michael@0 24 // Helper function to determine if the image is loaded for a given image id
michael@0 25 function isImageLoaded(id)
michael@0 26 {
michael@0 27 return (getImageStatus(id) &
michael@0 28 SpecialPowers.Ci.imgIRequest.STATUS_LOAD_COMPLETE)
michael@0 29 ? true : false;
michael@0 30 }
michael@0 31
michael@0 32 // Helper function to get the status flags of an image
michael@0 33 function getImageStatus(id)
michael@0 34 {
michael@0 35 // Get the image
michael@0 36 var img = SpecialPowers.wrap(document.getElementById(id));
michael@0 37
michael@0 38 // QI the image to nsImageLoadingContent
michael@0 39 img.QueryInterface(SpecialPowers.Ci.nsIImageLoadingContent);
michael@0 40
michael@0 41 // Get the request
michael@0 42 var request = img.getRequest(SpecialPowers.Ci
michael@0 43 .nsIImageLoadingContent
michael@0 44 .CURRENT_REQUEST);
michael@0 45
michael@0 46 // Return the status
michael@0 47 return request.imageStatus;
michael@0 48 }
michael@0 49
michael@0 50 // Forces a synchronous decode of an image by drawing it to a canvas. Only
michael@0 51 // really meaningful if the image is fully loaded first
michael@0 52 function forceDecode(id)
michael@0 53 {
michael@0 54 // Get the image
michael@0 55 var img = document.getElementById(id);
michael@0 56
michael@0 57 // Make a new canvas
michael@0 58 var canvas = document.createElement("canvas");
michael@0 59
michael@0 60 // Draw the image to the canvas. This forces a synchronous decode
michael@0 61 var ctx = canvas.getContext("2d");
michael@0 62 ctx.drawImage(img, 0, 0);
michael@0 63 }
michael@0 64
michael@0 65
michael@0 66 // Functions to facilitate getting/setting various image-related prefs
michael@0 67 //
michael@0 68 // If you change a pref in a mochitest, Don't forget to reset it to its
michael@0 69 // original value!
michael@0 70 //
michael@0 71 // Null indicates no pref set
michael@0 72
michael@0 73 const DISCARD_ENABLED_PREF = {name: "discardable", branch: "image.mem.", type: "bool"};
michael@0 74 const DECODEONDRAW_ENABLED_PREF = {name: "decodeondraw", branch: "image.mem.", type: "bool"};
michael@0 75 const DISCARD_TIMEOUT_PREF = {name: "min_discard_timeout_ms", branch: "image.mem.", type: "int"};
michael@0 76
michael@0 77 function setImagePref(pref, val)
michael@0 78 {
michael@0 79 var prefService = SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
michael@0 80 .getService(SpecialPowers.Ci.nsIPrefService);
michael@0 81 var branch = prefService.getBranch(pref.branch);
michael@0 82 if (val != null) {
michael@0 83 switch(pref.type) {
michael@0 84 case "bool":
michael@0 85 branch.setBoolPref(pref.name, val);
michael@0 86 break;
michael@0 87 case "int":
michael@0 88 branch.setIntPref(pref.name, val);
michael@0 89 break;
michael@0 90 default:
michael@0 91 throw new Error("Unknown pref type");
michael@0 92 }
michael@0 93 }
michael@0 94 else if (branch.prefHasUserValue(pref.name))
michael@0 95 branch.clearUserPref(pref.name);
michael@0 96 }
michael@0 97
michael@0 98 function getImagePref(pref)
michael@0 99 {
michael@0 100 var prefService = SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
michael@0 101 .getService(SpecialPowers.Ci.nsIPrefService);
michael@0 102 var branch = prefService.getBranch(pref.branch);
michael@0 103 if (branch.prefHasUserValue(pref.name)) {
michael@0 104 switch (pref.type) {
michael@0 105 case "bool":
michael@0 106 return branch.getBoolPref(pref.name);
michael@0 107 case "int":
michael@0 108 return branch.getIntPref(pref.name);
michael@0 109 default:
michael@0 110 throw new Error("Unknown pref type");
michael@0 111 }
michael@0 112 }
michael@0 113 else
michael@0 114 return null;
michael@0 115 }
michael@0 116
michael@0 117 // JS implementation of imgIScriptedNotificationObserver with stubs for all of its methods.
michael@0 118 function ImageDecoderObserverStub()
michael@0 119 {
michael@0 120 this.sizeAvailable = function sizeAvailable(aRequest) {}
michael@0 121 this.frameComplete = function frameComplete(aRequest) {}
michael@0 122 this.decodeComplete = function decodeComplete(aRequest) {}
michael@0 123 this.loadComplete = function loadComplete(aRequest) {}
michael@0 124 this.frameUpdate = function frameUpdate(aRequest) {}
michael@0 125 this.discard = function discard(aRequest) {}
michael@0 126 this.isAnimated = function isAnimated(aRequest) {}
michael@0 127 }

mercurial