Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | waitForExplicitFinish(); |
michael@0 | 2 | |
michael@0 | 3 | let pageSource = |
michael@0 | 4 | '<html><body>' + |
michael@0 | 5 | '<img id="testImg" src="' + TESTROOT + 'big.png">' + |
michael@0 | 6 | '</body></html>'; |
michael@0 | 7 | |
michael@0 | 8 | let oldDiscardingPref, oldTab, newTab; |
michael@0 | 9 | let prefBranch = Cc["@mozilla.org/preferences-service;1"] |
michael@0 | 10 | .getService(Ci.nsIPrefService) |
michael@0 | 11 | .getBranch('image.mem.'); |
michael@0 | 12 | |
michael@0 | 13 | function isImgDecoded() { |
michael@0 | 14 | let img = gBrowser.getBrowserForTab(newTab).contentWindow |
michael@0 | 15 | .document.getElementById('testImg'); |
michael@0 | 16 | img.QueryInterface(Ci.nsIImageLoadingContent); |
michael@0 | 17 | let request = img.getRequest(Ci.nsIImageLoadingContent.CURRENT_REQUEST); |
michael@0 | 18 | return request.imageStatus & Ci.imgIRequest.STATUS_FRAME_COMPLETE ? true : false; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | // Ensure that the image is decoded by drawing it to a canvas. |
michael@0 | 22 | function forceDecodeImg() { |
michael@0 | 23 | let doc = gBrowser.getBrowserForTab(newTab).contentWindow.document; |
michael@0 | 24 | let img = doc.getElementById('testImg'); |
michael@0 | 25 | let canvas = doc.createElement('canvas'); |
michael@0 | 26 | let ctx = canvas.getContext('2d'); |
michael@0 | 27 | ctx.drawImage(img, 0, 0); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | function test() { |
michael@0 | 31 | // Enable the discarding pref. |
michael@0 | 32 | oldDiscardingPref = prefBranch.getBoolPref('discardable'); |
michael@0 | 33 | prefBranch.setBoolPref('discardable', true); |
michael@0 | 34 | |
michael@0 | 35 | // Create and focus a new tab. |
michael@0 | 36 | oldTab = gBrowser.selectedTab; |
michael@0 | 37 | newTab = gBrowser.addTab('data:text/html,' + pageSource); |
michael@0 | 38 | gBrowser.selectedTab = newTab; |
michael@0 | 39 | |
michael@0 | 40 | // Run step2 after the tab loads. |
michael@0 | 41 | gBrowser.getBrowserForTab(newTab) |
michael@0 | 42 | .addEventListener("pageshow", step2 ); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | function step2() { |
michael@0 | 46 | // Check that the image is decoded. |
michael@0 | 47 | forceDecodeImg(); |
michael@0 | 48 | ok(isImgDecoded(), 'Image should initially be decoded.'); |
michael@0 | 49 | |
michael@0 | 50 | // Focus the old tab, then fire a memory-pressure notification. This should |
michael@0 | 51 | // cause the decoded image in the new tab to be discarded. |
michael@0 | 52 | gBrowser.selectedTab = oldTab; |
michael@0 | 53 | var os = Cc["@mozilla.org/observer-service;1"] |
michael@0 | 54 | .getService(Ci.nsIObserverService); |
michael@0 | 55 | os.notifyObservers(null, 'memory-pressure', 'heap-minimize'); |
michael@0 | 56 | ok(!isImgDecoded(), 'Image should be discarded.'); |
michael@0 | 57 | |
michael@0 | 58 | // And we're done. |
michael@0 | 59 | gBrowser.removeTab(newTab); |
michael@0 | 60 | prefBranch.setBoolPref('discardable', oldDiscardingPref); |
michael@0 | 61 | finish(); |
michael@0 | 62 | } |