1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/image/test/browser/browser_bug666317.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,62 @@ 1.4 +waitForExplicitFinish(); 1.5 + 1.6 +let pageSource = 1.7 + '<html><body>' + 1.8 + '<img id="testImg" src="' + TESTROOT + 'big.png">' + 1.9 + '</body></html>'; 1.10 + 1.11 +let oldDiscardingPref, oldTab, newTab; 1.12 +let prefBranch = Cc["@mozilla.org/preferences-service;1"] 1.13 + .getService(Ci.nsIPrefService) 1.14 + .getBranch('image.mem.'); 1.15 + 1.16 +function isImgDecoded() { 1.17 + let img = gBrowser.getBrowserForTab(newTab).contentWindow 1.18 + .document.getElementById('testImg'); 1.19 + img.QueryInterface(Ci.nsIImageLoadingContent); 1.20 + let request = img.getRequest(Ci.nsIImageLoadingContent.CURRENT_REQUEST); 1.21 + return request.imageStatus & Ci.imgIRequest.STATUS_FRAME_COMPLETE ? true : false; 1.22 +} 1.23 + 1.24 +// Ensure that the image is decoded by drawing it to a canvas. 1.25 +function forceDecodeImg() { 1.26 + let doc = gBrowser.getBrowserForTab(newTab).contentWindow.document; 1.27 + let img = doc.getElementById('testImg'); 1.28 + let canvas = doc.createElement('canvas'); 1.29 + let ctx = canvas.getContext('2d'); 1.30 + ctx.drawImage(img, 0, 0); 1.31 +} 1.32 + 1.33 +function test() { 1.34 + // Enable the discarding pref. 1.35 + oldDiscardingPref = prefBranch.getBoolPref('discardable'); 1.36 + prefBranch.setBoolPref('discardable', true); 1.37 + 1.38 + // Create and focus a new tab. 1.39 + oldTab = gBrowser.selectedTab; 1.40 + newTab = gBrowser.addTab('data:text/html,' + pageSource); 1.41 + gBrowser.selectedTab = newTab; 1.42 + 1.43 + // Run step2 after the tab loads. 1.44 + gBrowser.getBrowserForTab(newTab) 1.45 + .addEventListener("pageshow", step2 ); 1.46 +} 1.47 + 1.48 +function step2() { 1.49 + // Check that the image is decoded. 1.50 + forceDecodeImg(); 1.51 + ok(isImgDecoded(), 'Image should initially be decoded.'); 1.52 + 1.53 + // Focus the old tab, then fire a memory-pressure notification. This should 1.54 + // cause the decoded image in the new tab to be discarded. 1.55 + gBrowser.selectedTab = oldTab; 1.56 + var os = Cc["@mozilla.org/observer-service;1"] 1.57 + .getService(Ci.nsIObserverService); 1.58 + os.notifyObservers(null, 'memory-pressure', 'heap-minimize'); 1.59 + ok(!isImgDecoded(), 'Image should be discarded.'); 1.60 + 1.61 + // And we're done. 1.62 + gBrowser.removeTab(newTab); 1.63 + prefBranch.setBoolPref('discardable', oldDiscardingPref); 1.64 + finish(); 1.65 +}