michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { isTabOpen, activateTab, openTab, michael@0: closeTab, getTabURL, getWindowHoldingTab } = require('sdk/tabs/utils'); michael@0: const windows = require('sdk/deprecated/window-utils'); michael@0: const { LoaderWithHookedConsole } = require('sdk/test/loader'); michael@0: const { setTimeout } = require('sdk/timers'); michael@0: const app = require("sdk/system/xul-app"); michael@0: const tabs = require('sdk/tabs'); michael@0: const isAustralis = "gCustomizeMode" in windows.activeBrowserWindow; michael@0: const { set: setPref, get: getPref } = require("sdk/preferences/service"); michael@0: const { PrefsTarget } = require("sdk/preferences/event-target"); michael@0: const { defer } = require('sdk/core/promise'); michael@0: michael@0: const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings"; michael@0: michael@0: let uri = require('sdk/self').data.url('index.html'); michael@0: michael@0: function closeTabPromise(tab) { michael@0: let { promise, resolve } = defer(); michael@0: let url = getTabURL(tab); michael@0: michael@0: tabs.on('close', function onCloseTab(t) { michael@0: if (t.url == url) { michael@0: tabs.removeListener('close', onCloseTab); michael@0: setTimeout(_ => resolve(tab)) michael@0: } michael@0: }); michael@0: closeTab(tab); michael@0: michael@0: return promise; michael@0: } michael@0: michael@0: function isChromeVisible(window) { michael@0: let x = window.document.documentElement.getAttribute('disablechrome') michael@0: return x !== 'true'; michael@0: } michael@0: michael@0: // Once Bug 903018 is resolved, just move the application testing to michael@0: // module.metadata.engines michael@0: if (app.is('Firefox')) { michael@0: michael@0: exports['test add-on page deprecation message'] = function(assert, done) { michael@0: let { loader, messages } = LoaderWithHookedConsole(module); michael@0: michael@0: loader.require('sdk/preferences/event-target').PrefsTarget({ michael@0: branchName: "devtools.errorconsole." michael@0: }).on("deprecation_warnings", function() { michael@0: if (!getPref(DEPRECATE_PREF, false)) { michael@0: return undefined; michael@0: } michael@0: michael@0: loader.require('sdk/addon-page'); michael@0: michael@0: assert.equal(messages.length, 1, "only one error is dispatched"); michael@0: assert.equal(messages[0].type, "error", "the console message is an error"); michael@0: michael@0: let msg = messages[0].msg; michael@0: assert.ok(msg.indexOf("DEPRECATED") === 0, michael@0: "The message is deprecation message"); michael@0: michael@0: loader.unload(); michael@0: done(); michael@0: return undefined; michael@0: }); michael@0: setPref(DEPRECATE_PREF, false); michael@0: setPref(DEPRECATE_PREF, true); michael@0: }; michael@0: michael@0: exports['test that add-on page has no chrome'] = function(assert, done) { michael@0: let { loader } = LoaderWithHookedConsole(module); michael@0: loader.require('sdk/addon-page'); michael@0: michael@0: let window = windows.activeBrowserWindow; michael@0: let tab = openTab(window, uri); michael@0: michael@0: assert.ok(isChromeVisible(window), 'chrome is visible for non addon page'); michael@0: michael@0: // need to do this in another turn to make sure event listener michael@0: // that sets property has time to do that. michael@0: setTimeout(function() { michael@0: activateTab(tab); michael@0: michael@0: assert.equal(isChromeVisible(window), app.is('Fennec') || isAustralis, michael@0: 'chrome is not visible for addon page'); michael@0: michael@0: closeTabPromise(tab).then(function() { michael@0: assert.ok(isChromeVisible(window), 'chrome is visible again'); michael@0: loader.unload(); michael@0: assert.ok(!isTabOpen(tab), 'add-on page tab is closed on unload'); michael@0: done(); michael@0: }).then(null, assert.fail); michael@0: }); michael@0: }; michael@0: michael@0: exports['test that add-on page with hash has no chrome'] = function(assert, done) { michael@0: let { loader } = LoaderWithHookedConsole(module); michael@0: loader.require('sdk/addon-page'); michael@0: michael@0: let window = windows.activeBrowserWindow; michael@0: let tab = openTab(window, uri + "#foo"); michael@0: michael@0: assert.ok(isChromeVisible(window), 'chrome is visible for non addon page'); michael@0: michael@0: // need to do this in another turn to make sure event listener michael@0: // that sets property has time to do that. michael@0: setTimeout(function() { michael@0: activateTab(tab); michael@0: michael@0: assert.equal(isChromeVisible(window), app.is('Fennec') || isAustralis, michael@0: 'chrome is not visible for addon page'); michael@0: michael@0: closeTabPromise(tab).then(function() { michael@0: assert.ok(isChromeVisible(window), 'chrome is visible again'); michael@0: loader.unload(); michael@0: assert.ok(!isTabOpen(tab), 'add-on page tab is closed on unload'); michael@0: done(); michael@0: }).then(null, assert.fail); michael@0: }); michael@0: }; michael@0: michael@0: exports['test that add-on page with querystring has no chrome'] = function(assert, done) { michael@0: let { loader } = LoaderWithHookedConsole(module); michael@0: loader.require('sdk/addon-page'); michael@0: michael@0: let window = windows.activeBrowserWindow; michael@0: let tab = openTab(window, uri + '?foo=bar'); michael@0: michael@0: assert.ok(isChromeVisible(window), 'chrome is visible for non addon page'); michael@0: michael@0: // need to do this in another turn to make sure event listener michael@0: // that sets property has time to do that. michael@0: setTimeout(function() { michael@0: activateTab(tab); michael@0: michael@0: assert.equal(isChromeVisible(window), app.is('Fennec') || isAustralis, michael@0: 'chrome is not visible for addon page'); michael@0: michael@0: closeTabPromise(tab).then(function() { michael@0: assert.ok(isChromeVisible(window), 'chrome is visible again'); michael@0: loader.unload(); michael@0: assert.ok(!isTabOpen(tab), 'add-on page tab is closed on unload'); michael@0: done(); michael@0: }).then(null, assert.fail); michael@0: }); michael@0: }; michael@0: michael@0: exports['test that add-on page with hash and querystring has no chrome'] = function(assert, done) { michael@0: let { loader } = LoaderWithHookedConsole(module); michael@0: loader.require('sdk/addon-page'); michael@0: michael@0: let window = windows.activeBrowserWindow; michael@0: let tab = openTab(window, uri + '#foo?foo=bar'); michael@0: michael@0: assert.ok(isChromeVisible(window), 'chrome is visible for non addon page'); michael@0: michael@0: // need to do this in another turn to make sure event listener michael@0: // that sets property has time to do that. michael@0: setTimeout(function() { michael@0: activateTab(tab); michael@0: michael@0: assert.equal(isChromeVisible(window), app.is('Fennec') || isAustralis, michael@0: 'chrome is not visible for addon page'); michael@0: michael@0: closeTabPromise(tab).then(function() { michael@0: assert.ok(isChromeVisible(window), 'chrome is visible again'); michael@0: loader.unload(); michael@0: assert.ok(!isTabOpen(tab), 'add-on page tab is closed on unload'); michael@0: done(); michael@0: }).then(null, assert.fail); michael@0: }); michael@0: }; michael@0: michael@0: exports['test that malformed uri is not an addon-page'] = function(assert, done) { michael@0: let { loader } = LoaderWithHookedConsole(module); michael@0: loader.require('sdk/addon-page'); michael@0: michael@0: let window = windows.activeBrowserWindow; michael@0: let tab = openTab(window, uri + 'anguage'); michael@0: michael@0: // need to do this in another turn to make sure event listener michael@0: // that sets property has time to do that. michael@0: setTimeout(function() { michael@0: activateTab(tab); michael@0: michael@0: assert.ok(isChromeVisible(window), 'chrome is visible for malformed uri'); michael@0: michael@0: closeTabPromise(tab).then(function() { michael@0: loader.unload(); michael@0: done(); michael@0: }).then(null, assert.fail); michael@0: }); michael@0: }; michael@0: michael@0: } else { michael@0: exports['test unsupported'] = (assert) => assert.pass('This application is unsupported.'); michael@0: } michael@0: michael@0: require('sdk/test/runner').runTestsFromModule(module);