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

expose unique tab ids to the high-level tabs module #595

Merged
merged 2 commits into from Dec 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/sdk/tabs/tab-fennec.js
Expand Up @@ -8,7 +8,7 @@ const { Class } = require('../core/heritage');
const { tabNS } = require('./namespace'); const { tabNS } = require('./namespace');
const { EventTarget } = require('../event/target'); const { EventTarget } = require('../event/target');
const { activateTab, getTabTitle, setTabTitle, closeTab, getTabURL, const { activateTab, getTabTitle, setTabTitle, closeTab, getTabURL,
setTabURL, getOwnerWindow, getTabContentType } = require('./utils'); setTabURL, getOwnerWindow, getTabContentType, getTabId } = require('./utils');
const { emit } = require('../event/core'); const { emit } = require('../event/core');
const { when: unload } = require('../system/unload'); const { when: unload } = require('../system/unload');


Expand Down Expand Up @@ -63,6 +63,10 @@ const Tab = Class({
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAtCAYAAAA5reyyAAAAJElEQVRoge3BAQEAAACCIP+vbkhAAQAAAAAAAAAAAAAAAADXBjhtAAGQ0AF/AAAAAElFTkSuQmCC'; return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAtCAYAAAA5reyyAAAAJElEQVRoge3BAQEAAACCIP+vbkhAAQAAAAAAAAAAAAAAAADXBjhtAAGQ0AF/AAAAAElFTkSuQmCC';
}, },


get id() {
return getTabId(tabNS(this).tab);
},

/** /**
* The index of the tab relative to other tabs in the application window. * The index of the tab relative to other tabs in the application window.
* Changing this property will change order of the actual position of the tab. * Changing this property will change order of the actual position of the tab.
Expand Down
7 changes: 6 additions & 1 deletion lib/sdk/tabs/tab-firefox.js
Expand Up @@ -10,7 +10,7 @@ const { EVENTS } = require("./events");
const { getThumbnailURIForWindow } = require("../content/thumbnail"); const { getThumbnailURIForWindow } = require("../content/thumbnail");
const { getFaviconURIForLocation } = require("../io/data"); const { getFaviconURIForLocation } = require("../io/data");
const { activateTab, getOwnerWindow, getBrowserForTab, getTabTitle, setTabTitle, const { activateTab, getOwnerWindow, getBrowserForTab, getTabTitle, setTabTitle,
getTabURL, setTabURL, getTabContentType } = require('./utils'); getTabURL, setTabURL, getTabContentType, getTabId } = require('./utils');


// Array of the inner instances of all the wrapped tabs. // Array of the inner instances of all the wrapped tabs.
const TABS = []; const TABS = [];
Expand Down Expand Up @@ -94,6 +94,11 @@ const TabTrait = Trait.compose(EventEmitter, {
*/ */
get _contentWindow() this._browser.contentWindow, get _contentWindow() this._browser.contentWindow,


/**
* Unique id for the tab, actually maps to tab.linkedPanel but with some munging.
*/
get id() getTabId(this._tab),

/** /**
* The title of the page currently loaded in the tab. * The title of the page currently loaded in the tab.
* Changing this property changes an actual title. * Changing this property changes an actual title.
Expand Down
9 changes: 9 additions & 0 deletions lib/sdk/tabs/utils.js
Expand Up @@ -150,6 +150,15 @@ function getBrowserForTab(tab) {
} }
exports.getBrowserForTab = getBrowserForTab; exports.getBrowserForTab = getBrowserForTab;


function getTabId(tab) {
if (tab.browser) // fennec
return tab.id

return String.split(tab.linkedPanel, 'panel').pop();
}
exports.getTabId = getTabId;


function getTabTitle(tab) { function getTabTitle(tab) {
return getBrowserForTab(tab).contentDocument.title || tab.label || ""; return getBrowserForTab(tab).contentDocument.title || tab.label || "";
} }
Expand Down
42 changes: 42 additions & 0 deletions test/tabs/test-fennec-tabs.js
Expand Up @@ -125,6 +125,7 @@ exports.testTabProperties = function(test) {
test.assertEqual(tab.style, null, "style of the new tab matches"); test.assertEqual(tab.style, null, "style of the new tab matches");
test.assertEqual(tab.index, tabsLen, "index of the new tab matches"); test.assertEqual(tab.index, tabsLen, "index of the new tab matches");
test.assertNotEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches"); test.assertNotEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches");
test.assertNotEqual(tab.id, null, "a tab object always has an id property");


tab.close(function() { tab.close(function() {
loader.unload(); loader.unload();
Expand Down Expand Up @@ -619,3 +620,44 @@ exports.testActiveTab_getter_alt = function(test) {
} }
}); });
}; };

exports.testUniqueTabIds = function(test) {
test.waitUntilDone();
var tabs = require('sdk/tabs');
var tabIds = {};
var steps = [
function (index) {
tabs.open({
url: "data:text/html;charset=utf-8,foo",
onOpen: function(tab) {
tabIds['tab1'] = tab.id;
next(index);
}
});
},
function (index) {
tabs.open({
url: "data:text/html;charset=utf-8,bar",
onOpen: function(tab) {
tabIds['tab2'] = tab.id;
next(index);
}
});
},
function (index) {
test.assertNotEqual(tabIds.tab1, tabIds.tab2, "Tab ids should be unique.");
test.done();
}
];

function next(index) {
if (index === steps.length) {
return;
}
let fn = steps[index];
index++;
fn(index);
}

next(0);
}
48 changes: 48 additions & 0 deletions test/tabs/test-firefox-tabs.js
Expand Up @@ -177,6 +177,7 @@ exports.testTabProperties = function(test) {
test.assertEqual(tab.style, null, "style of the new tab matches"); test.assertEqual(tab.style, null, "style of the new tab matches");
test.assertEqual(tab.index, 1, "index of the new tab matches"); test.assertEqual(tab.index, 1, "index of the new tab matches");
test.assertNotEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches"); test.assertNotEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches");
test.assertNotEqual(tab.id, null, "a tab object always has an id property.");
closeBrowserWindow(window, function() test.done()); closeBrowserWindow(window, function() test.done());
} }
}); });
Expand Down Expand Up @@ -913,6 +914,53 @@ exports['test ready event on new window tab'] = function(test) {


let window = openBrowserWindow(function(){}, uri); let window = openBrowserWindow(function(){}, uri);
}; };

exports['test unique tab ids'] = function(test) {
test.waitUntilDone();

var windows = require('windows').browserWindows,
tabIds = {}, win1, win2;

let steps = [
function (index) {
win1 = windows.open({
url: "data:text/html;charset=utf-8,foo",
onOpen: function(window) {
tabIds['tab1'] = window.tabs.activeTab.id;
next(index);
}
});
},
function (index) {
win2 = windows.open({
url: "data:text/html;charset=utf-8,foo",
onOpen: function(window) {
tabIds['tab2'] = window.tabs.activeTab.id;
next(index);
}
});
},
function (index) {
test.assertNotEqual(tabIds.tab1, tabIds.tab2, "Tab ids should be unique.");
win1.close();
win2.close();
test.done();
}
];

function next(index) {
if (index === steps.length) {
return;
}
let fn = steps[index];
index++
fn(index);
}

// run!
next(0);
}

/******************* helpers *********************/ /******************* helpers *********************/


// Helper for getting the active window // Helper for getting the active window
Expand Down