Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Bug 784224: Page-mod tests now work on Fennec
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvold committed Feb 15, 2013
1 parent 1afc3d7 commit bb75129
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 71 deletions.
6 changes: 4 additions & 2 deletions data/test-iframe.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

var count = 0
var count = 0;

setTimeout(function() {
window.addEventListener("message", function(msg) {
if (++count > 1) self.postMessage(msg.data);
if (++count > 1) {
self.postMessage(msg.data);
}
else msg.source.postMessage(msg.data, '*');
});

Expand Down
12 changes: 11 additions & 1 deletion lib/sdk/tabs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@

const { getTabForContentWindow } = require('./utils');
const { Tab } = require('./tab');
const { tabNS } = require('./namespace');

function getTabForWindow(win) {
let tab = getTabForContentWindow(win);
// We were unable to find the related tab!
if (!tab)
return null;

return Tab({ tab: tab });
return getTabForRawTab(tab) || Tab({ tab: tab });
}
exports.getTabForWindow = getTabForWindow;

// only works on fennec atm
function getTabForRawTab(rawTab) {
let tab = tabNS(rawTab).tab;
if (tab && tabNS(tab).tab === rawTab) {
return tab;
}
return null;
}
56 changes: 52 additions & 4 deletions lib/sdk/tabs/tab-fennec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,36 @@ const { Cc, Ci } = require('chrome');
const { Class } = require('../core/heritage');
const { tabNS } = require('./namespace');
const { EventTarget } = require('../event/target');
const { activateTab, getTabTitle, setTabTitle, closeTab, getTabURL,
const { activateTab, getTabTitle, setTabTitle, closeTab, getTabURL, getRawTabForBrowser,
setTabURL, getOwnerWindow, getTabContentType, getTabId } = require('./utils');
const { emit } = require('../event/core');
const { when: unload } = require('../system/unload');

const { EVENTS } = require('./events');

const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec';

const Tab = Class({
extends: EventTarget,
initialize: function initialize(options) {
options = options.tab ? options : { tab: options };
let tab = options.tab;

EventTarget.prototype.initialize.call(this, options);
let tabInternals = tabNS(this);
tabNS(tab).tab = this;

let window = tabInternals.window = options.window || getOwnerWindow(tab);
tabInternals.tab = tab;

tabInternals.window = options.window || getOwnerWindow(options.tab);
tabInternals.tab = options.tab;
// TabReady
let onReady = tabInternals.onReady = onTabReady.bind(this);
tab.browser.addEventListener(EVENTS.ready.dom, onReady, false);

// TabClose
let onClose = tabInternals.onClose = onTabClose.bind(this);
window.BrowserApp.deck.addEventListener(EVENTS.close.dom, onClose, false);

unload(cleanupTab.bind(null, this));
},

/**
Expand Down Expand Up @@ -143,3 +155,39 @@ const Tab = Class({
}
});
exports.Tab = Tab;

function cleanupTab(tab) {
let tabInternals = tabNS(tab);
if (!tabInternals.tab)
return;

if (tabInternals.tab.browser) {
tabInternals.tab.browser.removeEventListener(EVENTS.ready.dom, tabInternals.onReady, false);
}
tabInternals.onReady = null;
tabInternals.window.BrowserApp.deck.removeEventListener(EVENTS.close.dom, tabInternals.onClose, false);
tabInternals.onClose = null;
tabNS(tabInternals.tab).tab = null;
tabInternals.tab = null;
tabInternals.window = null;
}

function onTabReady(event) {
let win = event.target.defaultView;

// ignore frames
if (win === win.top) {
emit(this, 'ready', this);
}
}

// TabClose
function onTabClose(event) {
let tab = getRawTabForBrowser(tabNS(this).window.BrowserApp.tabs, event.target);
if (this !== tabNS(tab).tab)
return;

emit(this, EVENTS.close.name, tab);

cleanupTab(tab);
};
40 changes: 35 additions & 5 deletions lib/sdk/tabs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function getTabs(window) {
exports.getTabs = getTabs;

function getActiveTab(window) {
return window.gBrowser.selectedTab;
return getSelectedTab(window);
}
exports.getActiveTab = getActiveTab;

Expand Down Expand Up @@ -103,7 +103,13 @@ function openTab(window, url, options) {
pinned: options.isPinned || false
});
}
return window.gBrowser.addTab(url);

// firefox
let newTab = window.gBrowser.addTab(url);
if (options.inBackground === false) {
window.gBrowser.selectedTab = newTab;
}
return newTab;
};
exports.openTab = openTab;

Expand Down Expand Up @@ -185,16 +191,20 @@ function getAllTabContentWindows() {
}
exports.getAllTabContentWindows = getAllTabContentWindows;

// gets the tab containing the provided window
function getTabForContentWindow(window) {
// Retrieve the topmost frame container. It can be either <xul:browser>,
// <xul:iframe/> or <html:iframe/>. But in our case, it should be xul:browser.
let browser = window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell)
.chromeEventHandler;

// Is null for toplevel documents
if (!browser)
if (!browser) {
return false;
}

// Retrieve the owner window, should be browser.xul one
let chromeWindow = browser.ownerDocument.defaultView;

Expand All @@ -211,15 +221,24 @@ function getTabForContentWindow(window) {
return chromeWindow.gBrowser.tabs[i];
return null;
}
// Fennec
else if ('BrowserApp' in chromeWindow) {
// Looks like we are on Firefox Mobile
return chromeWindow.BrowserApp.getTabForWindow(window)
return getTabForWindow(chromeWindow.BrowserApp._tabs, window);
}

return null;
}
exports.getTabForContentWindow = getTabForContentWindow;

// used on fennec
function getTabForWindow(tabs, window) {
for (let i = 0; i < tabs.length; i++) {
if (tabs[i].browser.contentWindow == window.top)
return tabs[i];
}
return null;
}

function getTabURL(tab) {
if (tab.browser) // fennec
return String(tab.browser.currentURI.spec);
Expand Down Expand Up @@ -252,3 +271,14 @@ function getSelectedTab(window) {
return null;
}
exports.getSelectedTab = getSelectedTab;


function getRawTabForBrowser(tabs, browser) {
for (let i = 0; i < tabs.length; i++) {
let tab = tabs[i];
if (tab.browser === browser)
return tab
}
return null;
}
exports.getRawTabForBrowser = getRawTabForBrowser;
38 changes: 5 additions & 33 deletions lib/sdk/windows/tabs-fennec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { Tab } = require('../tabs/tab');
const { browserWindows } = require('./fennec');
const { windowNS } = require('../window/namespace');
const { tabsNS, tabNS } = require('../tabs/namespace');
const { openTab, getTabs, getSelectedTab } = require('../tabs/utils');
const { openTab, getTabs, getSelectedTab, getRawTabForBrowser } = require('../tabs/utils');
const { Options } = require('../tabs/common');
const { on, once, off, emit } = require('../event/core');
const { method } = require('../lang/functional');
Expand Down Expand Up @@ -108,17 +108,7 @@ function removeTab(tab) {
}

function getTabForBrowser(browser) {
return getTabForRawTab(getRawTabForBrowser(browser));
}

function getRawTabForBrowser(browser) {
let tabs = mainWindow.BrowserApp.tabs;
for (let i = 0; i < tabs.length; i++) {
let tab = tabs[i];
if (tab.browser === browser)
return tab
}
return null;
return getTabForRawTab(getRawTabForBrowser(mainWindow.BrowserApp.tabs, browser));
}

// TabOpen
Expand All @@ -127,27 +117,20 @@ function onTabOpen(event) {

let tab = getTabForBrowser(browser);
if (tab === null) {
let rawTab = getRawTabForBrowser(browser);
let rawTab = getRawTabForBrowser(mainWindow.BrowserApp.tabs, browser);

// create a Tab instance for this new tab
tab = addTab(Tab(rawTab));
}

tabNS(tab).opened = true;

// TabReady
let onReady = tabNS(tab).onReady = onTabReady.bind(tab);
browser.addEventListener(EVENTS.ready.dom, onReady, false);
tab.on('ready', function() emit(gTabs, 'ready', tab));
emit(tab, 'open', tab);
emit(gTabs, 'open', tab);
};
function onTabReady() {
emit(this, 'ready', this);
emit(gTabs, 'ready', this);
}

// TabSelect
function onTabSelect(event) {
// Set value whenever new tab becomes active.
Expand All @@ -167,8 +150,7 @@ function onTabClose(event) {
let tab = getTabForBrowser(event.target);
removeTab(tab);

emit(gTabs, 'close', tab);
emit(tab, 'close', tab);
emit(gTabs, EVENTS.close.name, tab);
};

function getTabForRawTab(rawTab) {
Expand All @@ -178,13 +160,3 @@ function getTabForRawTab(rawTab) {
}
return null;
}

unload(function() {
for each (let tab in gTabs) {
let tabInternals = tabNS(tab);
tabInternals.tab.browser.removeEventListener(EVENTS.ready.dom, tabInternals.onReady, false);
tabInternals.onReady = null;
tabInternals.tab = null;
tabInternals.window = null;
}
});
11 changes: 6 additions & 5 deletions test/pagemod-test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {Cc,Ci} = require("chrome");
const timer = require("sdk/timers");
const xulApp = require("sdk/system/xul-app");
const { Loader } = require("sdk/test/loader");
const { openTab, getBrowserForTab, closeTab } = require("sdk/tabs/utils");

/**
* A helper function that creates a PageMod, then opens the specified URL
Expand Down Expand Up @@ -40,10 +41,10 @@ exports.testPageMod = function testPageMod(test, testURL, pageModOptions,

var pageMods = [new pageMod.PageMod(opts) for each(opts in pageModOptions)];

var tabBrowser = browserWindow.gBrowser;
var newTab = tabBrowser.addTab(testURL);
tabBrowser.selectedTab = newTab;
var b = tabBrowser.getBrowserForTab(newTab);
let newTab = openTab(browserWindow, testURL, {
inBackground: false
});
var b = getBrowserForTab(newTab);

function onPageLoad() {
b.removeEventListener("load", onPageLoad, true);
Expand All @@ -56,7 +57,7 @@ exports.testPageMod = function testPageMod(test, testURL, pageModOptions,
function done() {
pageMods.forEach(function(mod) mod.destroy());
// XXX leaks reported if we don't close the tab?
tabBrowser.removeTab(newTab);
closeTab(newTab);
loader.unload();
test.done();
}
Expand Down
Loading

0 comments on commit bb75129

Please sign in to comment.