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