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

Commit

Permalink
Merge pull request #338 from Gozala/bug/simple-addon-page@644595
Browse files Browse the repository at this point in the history
Bug 644595 - Implement very minimal support of add-on page API. r=@ochameau
  • Loading branch information
Gozala committed Feb 12, 2012
2 parents 03d0556 + e4db2a3 commit 8d34075
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/addon-kit/data/index.html
@@ -0,0 +1,12 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<html>
<head>
<title>Add-on Page</title>
</head>
<body>
<p>This is an add-on page test!</p>
</body>
</html>
33 changes: 33 additions & 0 deletions packages/addon-kit/lib/addon-page.js
@@ -0,0 +1,33 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict';

const { uriPrefix, name } = require('@packaging');
const { WindowTracker, isBrowser } = require('api-utils/window-utils');
const { add, remove } = require('api-utils/array');
const { getTabs, closeTab } = require('api-utils/tabs/utils');

// Note: This is an URL that will be returned by calling
// `require('self').data.url('index.html')` from the add-on modules.
// We could not use this expression as in this module it would have
// returned "addon-kit/data/index.html" instead.
const addonURL = uriPrefix + name + '/data/index.html';

WindowTracker({
onTrack: function onTrack(window) {
if (isBrowser(window))
add(window.XULBrowserWindow.inContentWhitelist, addonURL);
},
onUntrack: function onUntrack(window) {
getTabs(window).
filter(function(tab) tab.linkedBrowser.currentURI.spec === addonURL).
forEach(function(tab) {
// Note: `onUntrack` will be called for all windows on add-on unloads,
// so we want to clean them up from these URLs.
remove(window.XULBrowserWindow.inContentWhitelist, addonURL);
closeTab(tab);
});
}
});
51 changes: 51 additions & 0 deletions packages/addon-kit/tests/test-addon-page.js
@@ -0,0 +1,51 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict';

const { isTabOpen, activateTab, openTab, closeTab } = require('api-utils/tabs/utils');
const windows = require('api-utils/window-utils');
const { Loader } = require('./helpers');
const { setTimeout } = require('api-utils/timer');

let uri = require('self').data.url('index.html');

function isChromeVisible(window)
window.document.documentElement.getAttribute('disablechrome') !== 'true'

exports['test that add-on page has no chrome'] = function(assert, done) {
let loader = Loader(module);
loader.require('addon-kit/addon-page');

let window = windows.activeBrowserWindow;
let tab = openTab(window, uri);

assert.ok(isChromeVisible(window), 'chrome is visible for non addon page');

// need to do this in another turn to make sure event listener
// that sets property has time to do that.
setTimeout(function() {
activateTab(tab);

assert.ok(!isChromeVisible(window), 'chrome is not visible for addon page');

closeTab(tab);
assert.ok(isChromeVisible(window), 'chrome is visible again');
loader.unload();
done();
});
};

exports['test that add-on pages is closed on unload'] = function(assert) {
let loader = Loader(module);
loader.require('addon-kit/addon-page');

let tab = openTab(windows.activeBrowserWindow, uri);
loader.unload();

assert.ok(isTabOpen(tab), 'add-on page tabs are closed on unload');
};


require('test').run(exports);
5 changes: 5 additions & 0 deletions packages/api-utils/lib/tabs/utils.js
Expand Up @@ -43,6 +43,11 @@ function openTab(window, url) {
} }
exports.openTab = openTab; exports.openTab = openTab;


function isTabOpen(tab) {
return !!tab.linkedBrowser;
}
exports.isTabOpen = isTabOpen;

function closeTab(tab) { function closeTab(tab) {
return getOwnerWindow(tab).gBrowser.removeTab(tab); return getOwnerWindow(tab).gBrowser.removeTab(tab);
} }
Expand Down

0 comments on commit 8d34075

Please sign in to comment.