Skip to content

Commit

Permalink
Merge pull request mozilla#793 from erikvold/784224-2
Browse files Browse the repository at this point in the history
Fix Bug 784224: Page-mod tests now work on Fennec r=@ochameau(cherry picked from commit 133c457)

Conflicts:
	lib/sdk/tabs/utils.js
	test/test-page-mod.js
  • Loading branch information
erikvold committed Mar 15, 2013
1 parent b2e31f9 commit 0afaa2b
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 96 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
5 changes: 4 additions & 1 deletion lib/sdk/tabs/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ function Options(options) {

return validateOptions(options, {
url: { is: ["string"] },
inBackground: { is: ["undefined", "boolean"] },
inBackground: {
map: function(v) !!v,
is: ["undefined", "boolean"]
},
isPinned: { is: ["undefined", "boolean"] },
isPrivate: { is: ["undefined", "boolean"] },
onOpen: { is: ["undefined", "function"] },
Expand Down
28 changes: 26 additions & 2 deletions lib/sdk/tabs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,39 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

const { getTabForContentWindow } = require('./utils');
module.metadata = {
'stability': 'unstable'
};


// NOTE: This file should only export Tab instances


const { getTabForContentWindow, getTabForBrowser: getRawTabForBrowser } = require('./utils');
const { Tab } = require('./tab');
const { rawTabNS } = 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 = rawTabNS(rawTab).tab;
if (tab) {
return tab;
}
return null;
}
exports.getTabForRawTab = getTabForRawTab;

function getTabForBrowser(browser) {
return getTabForRawTab(getRawTabForBrowser(browser));
}
exports.getTabForBrowser = getTabForBrowser;
1 change: 1 addition & 0 deletions lib/sdk/tabs/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ let { ns } = require('../core/namespace');

exports.tabsNS = ns();
exports.tabNS = ns();
exports.rawTabNS = ns();
56 changes: 52 additions & 4 deletions lib/sdk/tabs/tab-fennec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,40 @@

const { Cc, Ci } = require('chrome');
const { Class } = require('../core/heritage');
const { tabNS } = require('./namespace');
const { tabNS, rawTabNS } = require('./namespace');
const { EventTarget } = require('../event/target');
const { activateTab, getTabTitle, setTabTitle, closeTab, getTabURL, getContentWindowForTab,
getTabForBrowser,
setTabURL, getOwnerWindow, getTabContentType, getTabId } = require('./utils');
const { emit } = require('../event/core');
const { getOwnerWindow: getPBOwnerWindow } = require('../private-browsing/window/utils');
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);
rawTabNS(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 @@ -145,6 +158,41 @@ 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;
rawTabNS(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 rawTab = getTabForBrowser(event.target);
if (tabNS(this).tab !== rawTab)
return;

emit(this, EVENTS.close.name, this);
cleanupTab(this);
};

getPBOwnerWindow.define(Tab, function(tab) {
return getContentWindowForTab(tabNS(tab).tab);
});
23 changes: 15 additions & 8 deletions lib/sdk/tabs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function getTabs(window) {
exports.getTabs = getTabs;

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

Expand Down Expand Up @@ -113,10 +113,13 @@ function openTab(window, url, options) {
isPrivate: options.isPrivate || false
});
}
let tab = window.gBrowser.addTab(url);
if (!options.inBackground)
activateTab(tab);
return tab;

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

Expand Down Expand Up @@ -203,16 +206,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 @@ -229,9 +236,9 @@ 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(window);
}

return null;
Expand Down
56 changes: 6 additions & 50 deletions lib/sdk/windows/tabs-fennec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ 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, getTabForBrowser: getRawTabForBrowser } = require('../tabs/utils');
const { Options } = require('../tabs/common');
const { getTabForBrowser, getTabForRawTab } = require('../tabs/helpers');
const { on, once, off, emit } = require('../event/core');
const { method } = require('../lang/functional');
const { EVENTS } = require('../tabs/events');
Expand Down Expand Up @@ -41,9 +42,6 @@ const Tabs = Class({

// TabSelect
window.BrowserApp.deck.addEventListener(EVENTS.activate.dom, onTabSelect, false);

// TabClose
window.BrowserApp.deck.addEventListener(EVENTS.close.dom, onTabClose, false);
},
get activeTab() {
return getTabForRawTab(getSelectedTab(tabsNS(this).window));
Expand Down Expand Up @@ -92,7 +90,6 @@ function tabsUnloader(event, window) {
return;
window.BrowserApp.deck.removeEventListener(EVENTS.open.dom, onTabOpen, false);
window.BrowserApp.deck.removeEventListener(EVENTS.activate.dom, onTabSelect, false);
window.BrowserApp.deck.removeEventListener(EVENTS.close.dom, onTabClose, false);
}

// unload handler
Expand All @@ -112,20 +109,6 @@ function removeTab(tab) {
return 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;
}

// TabOpen
function onTabOpen(event) {
let browser = event.target;
Expand All @@ -140,19 +123,13 @@ function onTabOpen(event) {

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));
tab.once('close', onTabClose);
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 @@ -168,28 +145,7 @@ function onTabSelect(event) {
};

// TabClose
function onTabClose(event) {
let tab = getTabForBrowser(event.target);
function onTabClose(tab) {
removeTab(tab);

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

function getTabForRawTab(rawTab) {
for each (let tab in gTabs) {
if (tabNS(tab).tab === rawTab)
return tab;
}
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 0afaa2b

Please sign in to comment.