Bug 769726: Tests for idle API interactions with the bfcache. r=khuey

This commit is contained in:
Bonnie Surender 2012-07-30 15:18:38 -07:00
Родитель 01dd952591
Коммит 63f7370e42
5 изменённых файлов: 426 добавлений и 0 удалений

Просмотреть файл

@ -21,6 +21,10 @@ MOCHITEST_FILES = \
MOCHITEST_CHROME_FILES = \
test_bug715041.xul \
test_bug715041_removal.xul \
MockIdleService.js \
bfcache_iframe1.html \
bfcache_iframe2.html \
test_bug769726.xul \
$(NULL)
include $(topsrcdir)/config/rules.mk

Просмотреть файл

@ -0,0 +1,223 @@
// Mock Idle Service
//Global Variables
var numIdleObserversRemoved = 0;
var numIdleObserversAdded = 0;
var addedIdleObserver = false;
var removedIdleObserver = false;
var idleServiceCID;
var idleServiceContractID;
var oldIdleService;
var componentMgr;
var oldIdleServiceFactoryObj;
var oldIdleServiceCID;
var window;
/*******************************************************************************
* Class Mock Idle Service
*******************************************************************************/
var idleServiceObj = {
observers: [],
windowObservers: [],
idleTimeInMS: 5000, // In milli seconds
// Takes note of the idle observers added as the minimum idle observer
// with the idle service
timesAdded: [],
QueryInterface: function(iid) {
if (iid.equals(Components.interfaces.nsISupports) ||
iid.equals(Components.interfaces.nsIFactory) ||
iid.equals(Components.interfaces.nsIIdleService)) {
return this;
}
throw Components.results.NS_ERROR_NO_INTERFACE;
},
createInstance: function(outer, iid) {
return this.QueryInterface(iid);
},
get idleTime() {
return this.idleTimeInMS;
},
set idleTime(timeInMS) {
this.idleTimeInMS = timeInMS;
},
getWindowFromObserver: function(observer) {
try {
var interfaceRequestor = observer.
QueryInterface(Components.interfaces.nsIInterfaceRequestor);
window = interfaceRequestor.
getInterface(Components.interfaces.nsIDOMWindow);
return window;
}
catch (e) {}
return null;
},
testIdleBackService: function(observer, topic) {
dump("\nMOCK IDLE SERVICE: testIdleBackService()\n");
window = this.getWindowFromObserver(observer);
dump("window in mock idle service is: " + window + "\n");
dump("JS NUM OBSERVERS: " + this.observers.length + "\n");
for (i=1; i<this.observers.length; i++) {
this.observers[i].observer.observe(observer, topic, '\0');
dump("JS CALLED OBSERVE FUNCTION!!!\n\n");
}
},
addIdleObserver: function(observer, time) {
dump("\nJS FAKE IDLE SERVICE add idle observer before\n");
dump("JS NUM OBSERVERS: " + this.observers.length + "\n");
window = this.getWindowFromObserver(observer);
if (window) {
dump("window is: " + window + "\n");
this.observers.push({ observer: observer, time: time, });
addedIdleObserver = true;
numIdleObserversAdded++;
this.timesAdded.push(time);
dump("\nMOCK IDLE SERVICE ADDING idle observer with time: " + time + "\n");
dump("MOCK IDLE SERVICE: num idle observers added: " +
numIdleObserversAdded + "\n\n");
}
else {
dump("SHOULD NEVER GET HERE!");
oldIdleService.addIdleObserver(observer, time);
addedIdleObserver = false;
}
dump("\nJS FAKE IDLE SERVICE end of add idle observer\n");
dump("JS NUM OBSERVERS: " + this.observers.length + "\n");
},
removeIdleObserver: function(observer, time) {
dump("\nJS REMOVE IDLE OBSERVER () time to be removed: " + time + "\n\n\n");
window = this.getWindowFromObserver(observer);
if (!window) {
oldIdleService.removeIdleObserver(observer, time);
}
else {
var observerIndex = -1;
for (var i=0; i<this.observers.length; i++) {
dump("JS removeIdleObserver() observer time: " +
this.observers[i].time + "\n");
if (this.observers[i].time === time) {
observerIndex = i;
break;
}
}
if (observerIndex != -1 && this.observers.length > 0) {
numIdleObserversRemoved++;
this.observers.splice(observerIndex, 1);
removedIdleObserver = true;
dump("MOCK IDLE SERVICE REMOVING idle observer with time " +
time + "\n");
dump("MOCK IDLE SERVICE numIdleObserversRemoved: " +
numIdleObserversRemoved + " numIdleObserversAdded: " +
numIdleObserversAdded + "\n\n");
}
else {
removedIdleObserver = false;
}
}
dump("\nJS FAKE IDLE SERVICE end of remove idle observer\n");
dump("JS NUM OBSERVERS: " + this.observers.length + "\n");
},
};
/*******************************************************************************
* RegisterMockIdleService()
*******************************************************************************/
function RegisterMockIdleService() {
try {
idleServiceCID = Components.ID("287075a6-f968-4516-8043-406c46f503b4");
idleServiceContractID = "@mozilla.org/widget/idleservice;1";
oldIdleService = Components.classes[idleServiceContractID].
getService(Components.interfaces.nsIIdleService);
}
catch(ex) {
dump("MockIdleService.js: 1) Failed to get old idle service.\n");
}
try {
// Registering new moch JS idle service
componentMgr = Components.manager.
QueryInterface(Components.interfaces.nsIComponentRegistrar);
}
catch(err) {
dump("MockIdleService.js: Failed to query component registrar interface.\n");
}
try {
oldIdleServiceFactoryObj =
componentMgr.getClassObjectByContractID(idleServiceContractID,
Components.interfaces.nsIFactory);
}
catch(err) {
dump("MockIdleService.js: Failed to get old idle service.\n");
}
try {
oldIdleServiceCID = componentMgr.contractIDToCID(idleServiceContractID);
}
catch(err) {
dump("MockIdleService.js: " +
"Failed to convert ID to CID for old idle service.\n");
}
try {
componentMgr.unregisterFactory(oldIdleServiceCID, oldIdleServiceFactoryObj);
}
catch(err) {
dump("MockIdleService.js: " +
"Failed to unregister old idle service factory object!\n");
}
try {
componentMgr.registerFactory(idleServiceCID,
"Test Simple Idle/Back Notifications",
idleServiceContractID, idleServiceObj);
}
catch(err) {
dump("MockIdleService.js: Failed to register mock idle service.\n");
}
}
/*******************************************************************************
* FinishTest()
*******************************************************************************/
function FinishTest() {
SpecialPowers.setBoolPref("browser.sessionhistory.cache_subframes", false);
try {
componentMgr.unregisterFactory(idleServiceCID, idleServiceObj);
}
catch(err) {
dump("MockIdleService.js: ShiftLocalTimerBackCleanUp() " +
"Failed to unregister factory, mock idle service!\n");
}
try {
componentMgr.registerFactory(oldIdleServiceCID,
"Re registering old idle service",
idleServiceContractID, oldIdleServiceFactoryObj);
}
catch(err) {
dump("MockIdleService.js: ShiftLocalTimerBackCleanUp() " +
"Failed to register factory, original idle service!\n");
}
ok(true, "Failed test 769726");
SimpleTest.finish();
}

Просмотреть файл

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<script>
var msg2Count = 0;
function IdleHandler2() {
msg2Count++;
dump("msg 2 Count: " + msg2Count + "\n");
if (msg2Count === 1) {
window.parent.IFrame1_Callback();
}
}
var idleObserver2 = {time: 2, onidle: IdleHandler2, onactive: IdleHandler2};
function Test() {
dump("Test1\n");
window.navigator.addIdleObserver(idleObserver2);
}
</script>
</head>
<body onload="Test();">
This is iframe 1.
</body>
</html>

Просмотреть файл

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<script>
var msg4Count = 0;
function IdleHandler4() {
msg4Count++;
dump("msg 4 Count: " + msg4Count + "\n");
if (msg4Count === 1) {
window.parent.IFrame2_Callback();
}
}
var idleObserver4 = {time: 4, onidle: IdleHandler4, onactive: IdleHandler4};
function Test() {
dump("Test2\n");
window.navigator.addIdleObserver(idleObserver4);
window.parent.idleServiceObj.idleTime = 1000;
window.parent.idleServiceObj.testIdleBackService(idleObserver4, "idle");
}
</script>
</head>
<body onload="Test();">
This is iframe 2.
</body>
</html>

Просмотреть файл

@ -0,0 +1,143 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=769726
-->
<window title="Mozilla Bug 769726"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="MockIdleService.js"></script>
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=769726"
target="_blank">Mozilla Bug 769726</a>
</body>
<!-- test code goes here -->
<script type="application/javascript"><![CDATA[
// msgXCount
var msg0Count = 0;
var msg2Count = 0;
var msg4Count = 0;
var IdleHandler0 = function() { dump("rmsg 0, should never be used!\n"); };
var idleObserver0 = {time: 1, onidle: IdleHandler0, onactive: IdleHandler0};
/* Start test bug 769726 */
SimpleTest.waitForExplicitFinish();
SpecialPowers.setBoolPref("dom.idle-observers-api.fuzz_time.disabled", true);
addLoadEvent(nextTest);
var gen = doTest();
/*******************************************************************************
* setCachePref()
*******************************************************************************/
var gOrigMaxTotalViewers = undefined;
function setCachePref(enabled) {
var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
if (enabled) {
is(typeof gOrigMaxTotalViewers, "undefined", "don't double-enable bfcache");
prefBranch.setBoolPref("browser.sessionhistory.cache_subframes", true);
gOrigMaxTotalViewers =
prefBranch.getIntPref("browser.sessionhistory.max_total_viewers");
prefBranch.setIntPref("browser.sessionhistory.max_total_viewers", 10);
}
else {
is(typeof gOrigMaxTotalViewers, "number", "don't double-disable bfcache");
prefBranch.setIntPref("browser.sessionhistory.max_total_viewers",
gOrigMaxTotalViewers);
gOrigMaxTotalViewers = undefined;
try {
prefBranch.clearUserPref("browser.sessionhistory.cache_subframes");
} catch (e) { /* Pref didn't exist, meh */ }
}
}
/*******************************************************************************
* IFrame1_Callback()
*******************************************************************************/
function IFrame1_Callback() {
dump("IFrame1_Callback()\n");
let passed = false;
msg2Count++;
ok(true, "Failed notifying idle observer when web page 1 thawed. " +
"Idle observer is registered within web page 1 " +
"in file bfcache_iframe2.html.\n");
history.forward();
if (msg2Count === 1 && msg4Count === 1) {
dump("msg2Count: " + msg2Count + " msg4Count " + msg4Count + "\n");
passed = true;
}
ok(passed, "Failed OS Idle API bug 769726 bfcache test.\n");
SimpleTest.executeSoon(nextTest);
}
/*******************************************************************************
* IFrame2_Callback()
*******************************************************************************/
function IFrame2_Callback() {
dump("IFrame2_Callback()\n");
msg4Count++;
ok(true, "Failed notifying idle observer registered within web page 2 " +
"in file bfcache_iframe2.html.\n");
history.back();
}
/*******************************************************************************
* nextTest()
*******************************************************************************/
function nextTest() {
try {
gen.next();
} catch (e if e instanceof StopIteration) {
}
}
/*******************************************************************************
* doTest()
*******************************************************************************/
function doTest() {
var container = document.getElementById('container');
setCachePref(true);
RegisterMockIdleService();
window.idleServiceObj = idleServiceObj;
window.idleServiceObj.observers.push( {observer: idleObserver0, time: 0, } );
idleServiceObj.idleTime = 500;
// create a new iframe
var iframe = document.createElementNS("http://www.w3.org/1999/xhtml",
"iframe");
container.appendChild(iframe);
iframe.contentWindow.x = "y";
// Try loading in an iframe
iframe.setAttribute("src", "bfcache_iframe1.html");
iframe.onload = nextTest;
event = yield;
SimpleTest.executeSoon(nextTest);
event = yield;
// Try loading again in an iframe
iframe.setAttribute("src", "bfcache_iframe2.html");
iframe.onload = nextTest;
yield;
yield;
setCachePref(false);
SimpleTest.finish();
yield;
}
]]></script>
<vbox id="container" flex="1">
<description>Below will an iframe be added</description>
</vbox>
</window>