michael@0: waitForExplicitFinish(); michael@0: michael@0: let pageSource = michael@0: '' + michael@0: '' + michael@0: ''; michael@0: michael@0: let oldDiscardingPref, oldTab, newTab; michael@0: let prefBranch = Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(Ci.nsIPrefService) michael@0: .getBranch('image.mem.'); michael@0: michael@0: function isImgDecoded() { michael@0: let img = gBrowser.getBrowserForTab(newTab).contentWindow michael@0: .document.getElementById('testImg'); michael@0: img.QueryInterface(Ci.nsIImageLoadingContent); michael@0: let request = img.getRequest(Ci.nsIImageLoadingContent.CURRENT_REQUEST); michael@0: return request.imageStatus & Ci.imgIRequest.STATUS_FRAME_COMPLETE ? true : false; michael@0: } michael@0: michael@0: // Ensure that the image is decoded by drawing it to a canvas. michael@0: function forceDecodeImg() { michael@0: let doc = gBrowser.getBrowserForTab(newTab).contentWindow.document; michael@0: let img = doc.getElementById('testImg'); michael@0: let canvas = doc.createElement('canvas'); michael@0: let ctx = canvas.getContext('2d'); michael@0: ctx.drawImage(img, 0, 0); michael@0: } michael@0: michael@0: function test() { michael@0: // Enable the discarding pref. michael@0: oldDiscardingPref = prefBranch.getBoolPref('discardable'); michael@0: prefBranch.setBoolPref('discardable', true); michael@0: michael@0: // Create and focus a new tab. michael@0: oldTab = gBrowser.selectedTab; michael@0: newTab = gBrowser.addTab('data:text/html,' + pageSource); michael@0: gBrowser.selectedTab = newTab; michael@0: michael@0: // Run step2 after the tab loads. michael@0: gBrowser.getBrowserForTab(newTab) michael@0: .addEventListener("pageshow", step2 ); michael@0: } michael@0: michael@0: function step2() { michael@0: // Check that the image is decoded. michael@0: forceDecodeImg(); michael@0: ok(isImgDecoded(), 'Image should initially be decoded.'); michael@0: michael@0: // Focus the old tab, then fire a memory-pressure notification. This should michael@0: // cause the decoded image in the new tab to be discarded. michael@0: gBrowser.selectedTab = oldTab; michael@0: var os = Cc["@mozilla.org/observer-service;1"] michael@0: .getService(Ci.nsIObserverService); michael@0: os.notifyObservers(null, 'memory-pressure', 'heap-minimize'); michael@0: ok(!isImgDecoded(), 'Image should be discarded.'); michael@0: michael@0: // And we're done. michael@0: gBrowser.removeTab(newTab); michael@0: prefBranch.setBoolPref('discardable', oldDiscardingPref); michael@0: finish(); michael@0: }