2011-07-01 01:13:56 +04:00
|
|
|
waitForExplicitFinish();
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var pageSource =
|
2014-11-27 04:42:04 +03:00
|
|
|
'<html><body>' +
|
2011-07-01 01:13:56 +04:00
|
|
|
'<img id="testImg" src="' + TESTROOT + 'big.png">' +
|
|
|
|
'</body></html>';
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var oldDiscardingPref, oldTab, newTab;
|
|
|
|
var prefBranch = Cc["@mozilla.org/preferences-service;1"]
|
2011-07-01 01:13:56 +04:00
|
|
|
.getService(Ci.nsIPrefService)
|
|
|
|
.getBranch('image.mem.');
|
|
|
|
|
2015-07-11 02:37:03 +03:00
|
|
|
var gWaitingForDiscard = false;
|
|
|
|
var gScriptedObserver;
|
|
|
|
var gClonedRequest;
|
|
|
|
|
2015-07-17 06:32:24 +03:00
|
|
|
function ImageObserver(decodeCallback, discardCallback) {
|
|
|
|
this.decodeComplete = function onDecodeComplete(aRequest) {
|
|
|
|
decodeCallback();
|
|
|
|
}
|
|
|
|
|
2014-11-27 04:42:04 +03:00
|
|
|
this.discard = function onDiscard(request)
|
|
|
|
{
|
2015-07-11 02:37:03 +03:00
|
|
|
if (!gWaitingForDiscard) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-27 04:42:04 +03:00
|
|
|
this.synchronous = false;
|
2015-07-17 06:32:24 +03:00
|
|
|
discardCallback();
|
2014-11-27 01:29:52 +03:00
|
|
|
}
|
2014-11-07 04:33:59 +03:00
|
|
|
|
2014-11-27 04:42:04 +03:00
|
|
|
this.synchronous = true;
|
|
|
|
}
|
2014-11-07 04:33:59 +03:00
|
|
|
|
2014-11-27 04:42:04 +03:00
|
|
|
function currentRequest() {
|
|
|
|
let img = gBrowser.getBrowserForTab(newTab).contentWindow
|
|
|
|
.document.getElementById('testImg');
|
|
|
|
img.QueryInterface(Ci.nsIImageLoadingContent);
|
|
|
|
return img.getRequest(Ci.nsIImageLoadingContent.CURRENT_REQUEST);
|
|
|
|
}
|
2014-11-07 04:33:59 +03:00
|
|
|
|
2014-11-27 04:42:04 +03:00
|
|
|
function isImgDecoded() {
|
|
|
|
let request = currentRequest();
|
2015-07-17 06:32:24 +03:00
|
|
|
return request.imageStatus & Ci.imgIRequest.STATUS_DECODE_COMPLETE ? true : false;
|
2014-11-27 04:42:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the image is decoded by drawing it to a canvas.
|
|
|
|
function forceDecodeImg() {
|
|
|
|
let doc = gBrowser.getBrowserForTab(newTab).contentWindow.document;
|
|
|
|
let img = doc.getElementById('testImg');
|
|
|
|
let canvas = doc.createElement('canvas');
|
|
|
|
let ctx = canvas.getContext('2d');
|
|
|
|
ctx.drawImage(img, 0, 0);
|
2011-07-01 01:13:56 +04:00
|
|
|
}
|
|
|
|
|
2015-06-20 01:55:12 +03:00
|
|
|
function runAfterAsyncEvents(aCallback) {
|
|
|
|
function handlePostMessage(aEvent) {
|
|
|
|
if (aEvent.data == 'next') {
|
|
|
|
window.removeEventListener('message', handlePostMessage, false);
|
|
|
|
aCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('message', handlePostMessage, false);
|
|
|
|
|
|
|
|
// We'll receive the 'message' event after everything else that's currently in
|
|
|
|
// the event queue (which is a stronger guarantee than setTimeout, because
|
|
|
|
// setTimeout events may be coalesced). This lets us ensure that we run
|
|
|
|
// aCallback *after* any asynchronous events are delivered.
|
|
|
|
window.postMessage('next', '*');
|
|
|
|
}
|
|
|
|
|
2011-07-01 01:13:56 +04:00
|
|
|
function test() {
|
|
|
|
// Enable the discarding pref.
|
|
|
|
oldDiscardingPref = prefBranch.getBoolPref('discardable');
|
|
|
|
prefBranch.setBoolPref('discardable', true);
|
|
|
|
|
|
|
|
// Create and focus a new tab.
|
|
|
|
oldTab = gBrowser.selectedTab;
|
|
|
|
newTab = gBrowser.addTab('data:text/html,' + pageSource);
|
|
|
|
gBrowser.selectedTab = newTab;
|
|
|
|
|
|
|
|
// Run step2 after the tab loads.
|
|
|
|
gBrowser.getBrowserForTab(newTab)
|
2015-07-11 02:37:03 +03:00
|
|
|
.addEventListener("pageshow", step2);
|
2011-07-01 01:13:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function step2() {
|
2015-07-17 06:32:24 +03:00
|
|
|
// Create the image observer.
|
|
|
|
var observer =
|
|
|
|
new ImageObserver(() => runAfterAsyncEvents(step3), // DECODE_COMPLETE
|
|
|
|
() => runAfterAsyncEvents(step5)); // DISCARD
|
2015-07-11 02:37:03 +03:00
|
|
|
gScriptedObserver = Cc["@mozilla.org/image/tools;1"]
|
|
|
|
.getService(Ci.imgITools)
|
|
|
|
.createScriptedObserver(observer);
|
2015-02-01 02:29:48 +03:00
|
|
|
|
|
|
|
// Clone the current imgIRequest with our new observer.
|
|
|
|
var request = currentRequest();
|
2015-07-11 02:37:03 +03:00
|
|
|
gClonedRequest = request.clone(gScriptedObserver);
|
2014-11-07 04:33:59 +03:00
|
|
|
|
2011-07-01 01:13:56 +04:00
|
|
|
// Check that the image is decoded.
|
2014-11-27 04:42:04 +03:00
|
|
|
forceDecodeImg();
|
2015-06-20 01:55:12 +03:00
|
|
|
|
2015-07-17 06:32:24 +03:00
|
|
|
// The DECODE_COMPLETE notification is delivered asynchronously. ImageObserver will
|
|
|
|
// eventually call step3.
|
2015-06-20 01:55:12 +03:00
|
|
|
}
|
|
|
|
|
2015-07-11 02:37:03 +03:00
|
|
|
function step3() {
|
2014-11-27 04:42:04 +03:00
|
|
|
ok(isImgDecoded(), 'Image should initially be decoded.');
|
|
|
|
|
|
|
|
// Focus the old tab, then fire a memory-pressure notification. This should
|
|
|
|
// cause the decoded image in the new tab to be discarded.
|
|
|
|
gBrowser.selectedTab = oldTab;
|
2015-07-11 02:37:03 +03:00
|
|
|
|
|
|
|
// Allow time to process the tab change.
|
|
|
|
runAfterAsyncEvents(step4);
|
|
|
|
}
|
|
|
|
|
|
|
|
function step4() {
|
|
|
|
gWaitingForDiscard = true;
|
|
|
|
|
2014-11-27 04:42:04 +03:00
|
|
|
var os = Cc["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Ci.nsIObserverService);
|
|
|
|
os.notifyObservers(null, 'memory-pressure', 'heap-minimize');
|
2015-02-01 02:29:48 +03:00
|
|
|
|
2015-07-17 06:32:24 +03:00
|
|
|
// The DISCARD notification is delivered asynchronously. ImageObserver will
|
|
|
|
// eventually call step5. (Or else, sadly, the test will time out.)
|
2015-02-01 02:29:48 +03:00
|
|
|
}
|
|
|
|
|
2015-07-11 02:37:03 +03:00
|
|
|
function step5() {
|
|
|
|
ok(true, 'Image should be discarded.');
|
2014-11-27 04:42:04 +03:00
|
|
|
|
|
|
|
// And we're done.
|
|
|
|
gBrowser.removeTab(newTab);
|
|
|
|
prefBranch.setBoolPref('discardable', oldDiscardingPref);
|
2015-07-11 02:37:03 +03:00
|
|
|
|
|
|
|
gClonedRequest.cancelAndForgetObserver(0);
|
|
|
|
|
2014-11-27 04:42:04 +03:00
|
|
|
finish();
|
2011-07-01 01:13:56 +04:00
|
|
|
}
|