123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038 |
- /* 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';
- module.metadata = {
- 'engines': {
- 'Firefox': '> 28'
- }
- };
- const { Cu } = require('chrome');
- const { Loader } = require('sdk/test/loader');
- const { data } = require('sdk/self');
- const { open, focus, close } = require('sdk/window/helpers');
- const { setTimeout } = require('sdk/timers');
- const { getMostRecentBrowserWindow } = require('sdk/window/utils');
- const { partial } = require('sdk/lang/functional');
- const openBrowserWindow = partial(open, null, {features: {toolbar: true}});
- const openPrivateBrowserWindow = partial(open, null,
- {features: {toolbar: true, private: true}});
- function getWidget(buttonId, window = getMostRecentBrowserWindow()) {
- const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
- const { AREA_NAVBAR } = CustomizableUI;
- let widgets = CustomizableUI.getWidgetIdsInArea(AREA_NAVBAR).
- filter((id) => id.startsWith('button--') && id.endsWith(buttonId));
- if (widgets.length === 0)
- throw new Error('Widget with id `' + id +'` not found.');
- if (widgets.length > 1)
- throw new Error('Unexpected number of widgets: ' + widgets.length)
- return CustomizableUI.getWidget(widgets[0]).forWindow(window);
- };
- exports['test basic constructor validation'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- assert.throws(
- () => ToggleButton({}),
- /^The option/,
- 'throws on no option given');
- // Test no label
- assert.throws(
- () => ToggleButton({ id: 'my-button', icon: './icon.png'}),
- /^The option "label"/,
- 'throws on no label given');
- // Test no id
- assert.throws(
- () => ToggleButton({ label: 'my button', icon: './icon.png' }),
- /^The option "id"/,
- 'throws on no id given');
- // Test no icon
- assert.throws(
- () => ToggleButton({ id: 'my-button', label: 'my button' }),
- /^The option "icon"/,
- 'throws on no icon given');
- // Test empty label
- assert.throws(
- () => ToggleButton({ id: 'my-button', label: '', icon: './icon.png' }),
- /^The option "label"/,
- 'throws on no valid label given');
- // Test invalid id
- assert.throws(
- () => ToggleButton({ id: 'my button', label: 'my button', icon: './icon.png' }),
- /^The option "id"/,
- 'throws on no valid id given');
- // Test empty id
- assert.throws(
- () => ToggleButton({ id: '', label: 'my button', icon: './icon.png' }),
- /^The option "id"/,
- 'throws on no valid id given');
- // Test remote icon
- assert.throws(
- () => ToggleButton({ id: 'my-button', label: 'my button', icon: 'http://www.mozilla.org/favicon.ico'}),
- /^The option "icon"/,
- 'throws on no valid icon given');
- // Test wrong icon: no absolute URI to local resource, neither relative './'
- assert.throws(
- () => ToggleButton({ id: 'my-button', label: 'my button', icon: 'icon.png'}),
- /^The option "icon"/,
- 'throws on no valid icon given');
- // Test wrong icon: no absolute URI to local resource, neither relative './'
- assert.throws(
- () => ToggleButton({ id: 'my-button', label: 'my button', icon: 'foo and bar'}),
- /^The option "icon"/,
- 'throws on no valid icon given');
- // Test wrong icon: '../' is not allowed
- assert.throws(
- () => ToggleButton({ id: 'my-button', label: 'my button', icon: '../icon.png'}),
- /^The option "icon"/,
- 'throws on no valid icon given');
- // Test wrong checked
- assert.throws(
- () => ToggleButton({
- id: 'my-button', label: 'my button', icon: './icon.png', checked: 'yes'}),
- /^The option "checked"/,
- 'throws on no valid checked value given');
- loader.unload();
- };
- exports['test button added'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let button = ToggleButton({
- id: 'my-button-1',
- label: 'my button',
- icon: './icon.png'
- });
- // check defaults
- assert.equal(button.checked, false,
- 'checked is set to default `false` value');
- assert.equal(button.disabled, false,
- 'disabled is set to default `false` value');
- let { node } = getWidget(button.id);
- assert.ok(!!node, 'The button is in the navbar');
- assert.equal(button.label, node.getAttribute('label'),
- 'label is set');
- assert.equal(button.label, node.getAttribute('tooltiptext'),
- 'tooltip is set');
- assert.equal(data.url(button.icon.substr(2)), node.getAttribute('image'),
- 'icon is set');
- loader.unload();
- }
- exports['test button added with resource URI'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let button = ToggleButton({
- id: 'my-button-1',
- label: 'my button',
- icon: data.url('icon.png')
- });
- assert.equal(button.icon, data.url('icon.png'),
- 'icon is set');
- let { node } = getWidget(button.id);
- assert.equal(button.icon, node.getAttribute('image'),
- 'icon on node is set');
- loader.unload();
- }
- exports['test button duplicate id'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let button = ToggleButton({
- id: 'my-button-2',
- label: 'my button',
- icon: './icon.png'
- });
- assert.throws(() => {
- let doppelganger = ToggleButton({
- id: 'my-button-2',
- label: 'my button',
- icon: './icon.png'
- });
- },
- /^The ID/,
- 'No duplicates allowed');
- loader.unload();
- }
- exports['test button multiple destroy'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let button = ToggleButton({
- id: 'my-button-2',
- label: 'my button',
- icon: './icon.png'
- });
- button.destroy();
- button.destroy();
- button.destroy();
- assert.pass('multiple destroy doesn\'t matter');
- loader.unload();
- }
- exports['test button removed on dispose'] = function(assert, done) {
- const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let widgetId;
- CustomizableUI.addListener({
- onWidgetDestroyed: function(id) {
- if (id === widgetId) {
- CustomizableUI.removeListener(this);
- assert.pass('button properly removed');
- loader.unload();
- done();
- }
- }
- });
- let button = ToggleButton({
- id: 'my-button-3',
- label: 'my button',
- icon: './icon.png'
- });
- // Tried to use `getWidgetIdsInArea` but seems undefined, not sure if it
- // was removed or it's not in the UX build yet
- widgetId = getWidget(button.id).id;
- button.destroy();
- };
- exports['test button global state updated'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let button = ToggleButton({
- id: 'my-button-4',
- label: 'my button',
- icon: './icon.png'
- });
- // Tried to use `getWidgetIdsInArea` but seems undefined, not sure if it
- // was removed or it's not in the UX build yet
- let { node, id: widgetId } = getWidget(button.id);
- // check read-only properties
- assert.throws(() => button.id = 'another-id',
- /^setting a property that has only a getter/,
- 'id cannot be set at runtime');
- assert.equal(button.id, 'my-button-4',
- 'id is unchanged');
- assert.equal(node.id, widgetId,
- 'node id is unchanged');
- // check writable properties
- button.label = 'New label';
- assert.equal(button.label, 'New label',
- 'label is updated');
- assert.equal(node.getAttribute('label'), 'New label',
- 'node label is updated');
- assert.equal(node.getAttribute('tooltiptext'), 'New label',
- 'node tooltip is updated');
- button.icon = './new-icon.png';
- assert.equal(button.icon, './new-icon.png',
- 'icon is updated');
- assert.equal(node.getAttribute('image'), data.url('new-icon.png'),
- 'node image is updated');
- button.disabled = true;
- assert.equal(button.disabled, true,
- 'disabled is updated');
- assert.equal(node.getAttribute('disabled'), 'true',
- 'node disabled is updated');
- // TODO: test validation on update
- loader.unload();
- }
- exports['test button global state updated on multiple windows'] = function(assert, done) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let button = ToggleButton({
- id: 'my-button-5',
- label: 'my button',
- icon: './icon.png'
- });
- let nodes = [getWidget(button.id).node];
- openBrowserWindow().then(window => {
- nodes.push(getWidget(button.id, window).node);
- button.label = 'New label';
- button.icon = './new-icon.png';
- button.disabled = true;
- for (let node of nodes) {
- assert.equal(node.getAttribute('label'), 'New label',
- 'node label is updated');
- assert.equal(node.getAttribute('tooltiptext'), 'New label',
- 'node tooltip is updated');
- assert.equal(button.icon, './new-icon.png',
- 'icon is updated');
- assert.equal(node.getAttribute('image'), data.url('new-icon.png'),
- 'node image is updated');
- assert.equal(button.disabled, true,
- 'disabled is updated');
- assert.equal(node.getAttribute('disabled'), 'true',
- 'node disabled is updated');
- };
- return window;
- }).
- then(close).
- then(loader.unload).
- then(done, assert.fail);
- };
- exports['test button window state'] = function(assert, done) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let { browserWindows } = loader.require('sdk/windows');
- let button = ToggleButton({
- id: 'my-button-6',
- label: 'my button',
- icon: './icon.png'
- });
- let mainWindow = browserWindows.activeWindow;
- let nodes = [getWidget(button.id).node];
- openBrowserWindow().then(focus).then(window => {
- nodes.push(getWidget(button.id, window).node);
- let { activeWindow } = browserWindows;
- button.state(activeWindow, {
- label: 'New label',
- icon: './new-icon.png',
- disabled: true
- });
- // check the states
- assert.equal(button.label, 'my button',
- 'global label unchanged');
- assert.equal(button.icon, './icon.png',
- 'global icon unchanged');
- assert.equal(button.disabled, false,
- 'global disabled unchanged');
- let state = button.state(mainWindow);
- assert.equal(state.label, 'my button',
- 'previous window label unchanged');
- assert.equal(state.icon, './icon.png',
- 'previous window icon unchanged');
- assert.equal(state.disabled, false,
- 'previous window disabled unchanged');
- let state = button.state(activeWindow);
- assert.equal(state.label, 'New label',
- 'active window label updated');
- assert.equal(state.icon, './new-icon.png',
- 'active window icon updated');
- assert.equal(state.disabled, true,
- 'active disabled updated');
- // change the global state, only the windows without a state are affected
- button.label = 'A good label';
- assert.equal(button.label, 'A good label',
- 'global label updated');
- assert.equal(button.state(mainWindow).label, 'A good label',
- 'previous window label updated');
- assert.equal(button.state(activeWindow).label, 'New label',
- 'active window label unchanged');
- // delete the window state will inherits the global state again
- button.state(activeWindow, null);
- assert.equal(button.state(activeWindow).label, 'A good label',
- 'active window label inherited');
- // check the nodes properties
- let node = nodes[0];
- let state = button.state(mainWindow);
- assert.equal(node.getAttribute('label'), state.label,
- 'node label is correct');
- assert.equal(node.getAttribute('tooltiptext'), state.label,
- 'node tooltip is correct');
- assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
- 'node image is correct');
- assert.equal(node.hasAttribute('disabled'), state.disabled,
- 'disabled is correct');
- let node = nodes[1];
- let state = button.state(activeWindow);
- assert.equal(node.getAttribute('label'), state.label,
- 'node label is correct');
- assert.equal(node.getAttribute('tooltiptext'), state.label,
- 'node tooltip is correct');
- assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
- 'node image is correct');
- assert.equal(node.hasAttribute('disabled'), state.disabled,
- 'disabled is correct');
- return window;
- }).
- then(close).
- then(loader.unload).
- then(done, assert.fail);
- };
- exports['test button tab state'] = function(assert, done) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let { browserWindows } = loader.require('sdk/windows');
- let tabs = loader.require('sdk/tabs');
- let button = ToggleButton({
- id: 'my-button-7',
- label: 'my button',
- icon: './icon.png'
- });
- let mainTab = tabs.activeTab;
- let node = getWidget(button.id).node;
- tabs.open({
- url: 'about:blank',
- onActivate: function onActivate(tab) {
- tab.removeListener('activate', onActivate);
- let { activeWindow } = browserWindows;
- // set window state
- button.state(activeWindow, {
- label: 'Window label',
- icon: './window-icon.png'
- });
- // set previous active tab state
- button.state(mainTab, {
- label: 'Tab label',
- icon: './tab-icon.png',
- });
- // set current active tab state
- button.state(tab, {
- icon: './another-tab-icon.png',
- disabled: true
- });
- // check the states
- Cu.schedulePreciseGC(() => {
- assert.equal(button.label, 'my button',
- 'global label unchanged');
- assert.equal(button.icon, './icon.png',
- 'global icon unchanged');
- assert.equal(button.disabled, false,
- 'global disabled unchanged');
- let state = button.state(mainTab);
- assert.equal(state.label, 'Tab label',
- 'previous tab label updated');
- assert.equal(state.icon, './tab-icon.png',
- 'previous tab icon updated');
- assert.equal(state.disabled, false,
- 'previous tab disabled unchanged');
- let state = button.state(tab);
- assert.equal(state.label, 'Window label',
- 'active tab inherited from window state');
- assert.equal(state.icon, './another-tab-icon.png',
- 'active tab icon updated');
- assert.equal(state.disabled, true,
- 'active disabled updated');
- // change the global state
- button.icon = './good-icon.png';
- // delete the tab state
- button.state(tab, null);
- assert.equal(button.icon, './good-icon.png',
- 'global icon updated');
- assert.equal(button.state(mainTab).icon, './tab-icon.png',
- 'previous tab icon unchanged');
- assert.equal(button.state(tab).icon, './window-icon.png',
- 'tab icon inherited from window');
- // delete the window state
- button.state(activeWindow, null);
- assert.equal(button.state(tab).icon, './good-icon.png',
- 'tab icon inherited from global');
- // check the node properties
- let state = button.state(tabs.activeTab);
- assert.equal(node.getAttribute('label'), state.label,
- 'node label is correct');
- assert.equal(node.getAttribute('tooltiptext'), state.label,
- 'node tooltip is correct');
- assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
- 'node image is correct');
- assert.equal(node.hasAttribute('disabled'), state.disabled,
- 'disabled is correct');
- tabs.once('activate', () => {
- // This is made in order to avoid to check the node before it
- // is updated, need a better check
- setTimeout(() => {
- let state = button.state(mainTab);
- assert.equal(node.getAttribute('label'), state.label,
- 'node label is correct');
- assert.equal(node.getAttribute('tooltiptext'), state.label,
- 'node tooltip is correct');
- assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
- 'node image is correct');
- assert.equal(node.hasAttribute('disabled'), state.disabled,
- 'disabled is correct');
- tab.close(() => {
- loader.unload();
- done();
- });
- }, 500);
- });
- mainTab.activate();
- });
- }
- });
- };
- exports['test button click'] = function(assert, done) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let { browserWindows } = loader.require('sdk/windows');
- let labels = [];
- let button = ToggleButton({
- id: 'my-button-8',
- label: 'my button',
- icon: './icon.png',
- onClick: ({label}) => labels.push(label)
- });
- let mainWindow = browserWindows.activeWindow;
- let chromeWindow = getMostRecentBrowserWindow();
- openBrowserWindow().then(focus).then(window => {
- button.state(mainWindow, { label: 'nothing' });
- button.state(mainWindow.tabs.activeTab, { label: 'foo'})
- button.state(browserWindows.activeWindow, { label: 'bar' });
- button.click();
- focus(chromeWindow).then(() => {
- button.click();
- assert.deepEqual(labels, ['bar', 'foo'],
- 'button click works');
- close(window).
- then(loader.unload).
- then(done, assert.fail);
- });
- }).then(null, assert.fail);
- }
- exports['test button icon set'] = function(assert) {
- const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- // Test remote icon set
- assert.throws(
- () => ToggleButton({
- id: 'my-button-10',
- label: 'my button',
- icon: {
- '16': 'http://www.mozilla.org/favicon.ico'
- }
- }),
- /^The option "icon"/,
- 'throws on no valid icon given');
- let button = ToggleButton({
- id: 'my-button-11',
- label: 'my button',
- icon: {
- '5': './icon5.png',
- '16': './icon16.png',
- '32': './icon32.png',
- '64': './icon64.png'
- }
- });
- let { node, id: widgetId } = getWidget(button.id);
- let { devicePixelRatio } = node.ownerDocument.defaultView;
- let size = 16 * devicePixelRatio;
- assert.equal(node.getAttribute('image'), data.url(button.icon[size].substr(2)),
- 'the icon is set properly in navbar');
- let size = 32 * devicePixelRatio;
- CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_PANEL);
- assert.equal(node.getAttribute('image'), data.url(button.icon[size].substr(2)),
- 'the icon is set properly in panel');
- // Using `loader.unload` without move back the button to the original area
- // raises an error in the CustomizableUI. This is doesn't happen if the
- // button is moved manually from navbar to panel. I believe it has to do
- // with `addWidgetToArea` method, because even with a `timeout` the issue
- // persist.
- CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_NAVBAR);
- loader.unload();
- }
- exports['test button icon se with only one option'] = function(assert) {
- const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- // Test remote icon set
- assert.throws(
- () => ToggleButton({
- id: 'my-button-10',
- label: 'my button',
- icon: {
- '16': 'http://www.mozilla.org/favicon.ico'
- }
- }),
- /^The option "icon"/,
- 'throws on no valid icon given');
- let button = ToggleButton({
- id: 'my-button-11',
- label: 'my button',
- icon: {
- '5': './icon5.png'
- }
- });
- let { node, id: widgetId } = getWidget(button.id);
- assert.equal(node.getAttribute('image'), data.url(button.icon['5'].substr(2)),
- 'the icon is set properly in navbar');
- CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_PANEL);
- assert.equal(node.getAttribute('image'), data.url(button.icon['5'].substr(2)),
- 'the icon is set properly in panel');
- // Using `loader.unload` without move back the button to the original area
- // raises an error in the CustomizableUI. This is doesn't happen if the
- // button is moved manually from navbar to panel. I believe it has to do
- // with `addWidgetToArea` method, because even with a `timeout` the issue
- // persist.
- CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_NAVBAR);
- loader.unload();
- }
- exports['test button state validation'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let { browserWindows } = loader.require('sdk/windows');
- let button = ToggleButton({
- id: 'my-button-12',
- label: 'my button',
- icon: './icon.png'
- })
- let state = button.state(button);
- assert.throws(
- () => button.state(button, { icon: 'http://www.mozilla.org/favicon.ico' }),
- /^The option "icon"/,
- 'throws on remote icon given');
- loader.unload();
- };
- exports['test button are not in private windows'] = function(assert, done) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let{ isPrivate } = loader.require('sdk/private-browsing');
- let { browserWindows } = loader.require('sdk/windows');
- let button = ToggleButton({
- id: 'my-button-13',
- label: 'my button',
- icon: './icon.png'
- });
- openPrivateBrowserWindow().then(window => {
- assert.ok(isPrivate(window),
- 'the new window is private');
- let { node } = getWidget(button.id, window);
- assert.ok(!node || node.style.display === 'none',
- 'the button is not added / is not visible on private window');
- return window;
- }).
- then(close).
- then(loader.unload).
- then(done, assert.fail)
- }
- exports['test button state are snapshot'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let { browserWindows } = loader.require('sdk/windows');
- let tabs = loader.require('sdk/tabs');
- let button = ToggleButton({
- id: 'my-button-14',
- label: 'my button',
- icon: './icon.png'
- });
- let state = button.state(button);
- let windowState = button.state(browserWindows.activeWindow);
- let tabState = button.state(tabs.activeTab);
- assert.deepEqual(windowState, state,
- 'window state has the same properties of button state');
- assert.deepEqual(tabState, state,
- 'tab state has the same properties of button state');
- assert.notEqual(windowState, state,
- 'window state is not the same object of button state');
- assert.notEqual(tabState, state,
- 'tab state is not the same object of button state');
- assert.deepEqual(button.state(button), state,
- 'button state has the same content of previous button state');
- assert.deepEqual(button.state(browserWindows.activeWindow), windowState,
- 'window state has the same content of previous window state');
- assert.deepEqual(button.state(tabs.activeTab), tabState,
- 'tab state has the same content of previous tab state');
- assert.notEqual(button.state(button), state,
- 'button state is not the same object of previous button state');
- assert.notEqual(button.state(browserWindows.activeWindow), windowState,
- 'window state is not the same object of previous window state');
- assert.notEqual(button.state(tabs.activeTab), tabState,
- 'tab state is not the same object of previous tab state');
- loader.unload();
- }
- exports['test button after destroy'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let { browserWindows } = loader.require('sdk/windows');
- let { activeTab } = loader.require('sdk/tabs');
- let button = ToggleButton({
- id: 'my-button-15',
- label: 'my button',
- icon: './icon.png',
- onClick: () => assert.fail('onClick should not be called')
- });
- button.destroy();
- assert.throws(
- () => button.click(),
- /^The state cannot be set or get/,
- 'button.click() not executed');
- assert.throws(
- () => button.label,
- /^The state cannot be set or get/,
- 'button.label cannot be get after destroy');
- assert.throws(
- () => button.label = 'my label',
- /^The state cannot be set or get/,
- 'button.label cannot be set after destroy');
- assert.throws(
- () => {
- button.state(browserWindows.activeWindow, {
- label: 'window label'
- });
- },
- /^The state cannot be set or get/,
- 'window state label cannot be set after destroy');
- assert.throws(
- () => button.state(browserWindows.activeWindow).label,
- /^The state cannot be set or get/,
- 'window state label cannot be get after destroy');
- assert.throws(
- () => {
- button.state(activeTab, {
- label: 'tab label'
- });
- },
- /^The state cannot be set or get/,
- 'tab state label cannot be set after destroy');
- assert.throws(
- () => button.state(activeTab).label,
- /^The state cannot be set or get/,
- 'window state label cannot se get after destroy');
- loader.unload();
- };
- exports['test button checked'] = function(assert, done) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let { browserWindows } = loader.require('sdk/windows');
- let events = [];
- let button = ToggleButton({
- id: 'my-button-9',
- label: 'my button',
- icon: './icon.png',
- checked: true,
- onClick: ({label}) => events.push('clicked:' + label),
- onChange: state => events.push('changed:' + state.label + ':' + state.checked)
- });
- let { node } = getWidget(button.id);
- assert.equal(node.getAttribute('type'), 'checkbox',
- 'node type is properly set');
- let mainWindow = browserWindows.activeWindow;
- let chromeWindow = getMostRecentBrowserWindow();
- openBrowserWindow().then(focus).then(window => {
- button.state(mainWindow, { label: 'nothing' });
- button.state(mainWindow.tabs.activeTab, { label: 'foo'})
- button.state(browserWindows.activeWindow, { label: 'bar' });
- button.click();
- button.click();
- focus(chromeWindow).then(() => {
- button.click();
- button.click();
- assert.deepEqual(events, [
- 'clicked:bar', 'changed:bar:false', 'clicked:bar', 'changed:bar:true',
- 'clicked:foo', 'changed:foo:false', 'clicked:foo', 'changed:foo:true'
- ],
- 'button change events works');
- close(window).
- then(loader.unload).
- then(done, assert.fail);
- })
- }).then(null, assert.fail);
- }
- exports['test button is checked on window level'] = function(assert, done) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let { browserWindows } = loader.require('sdk/windows');
- let tabs = loader.require('sdk/tabs');
- let button = ToggleButton({
- id: 'my-button-20',
- label: 'my button',
- icon: './icon.png'
- });
- let mainWindow = browserWindows.activeWindow;
- let mainTab = tabs.activeTab;
- assert.equal(button.checked, false,
- 'global state, checked is `false`.');
- assert.equal(button.state(mainTab).checked, false,
- 'tab state, checked is `false`.');
- assert.equal(button.state(mainWindow).checked, false,
- 'window state, checked is `false`.');
- button.click();
- tabs.open({
- url: 'about:blank',
- onActivate: function onActivate(tab) {
- tab.removeListener('activate', onActivate);
- assert.notEqual(mainTab, tab,
- 'the current tab is not the same.');
- assert.equal(button.checked, false,
- 'global state, checked is `false`.');
- assert.equal(button.state(mainTab).checked, true,
- 'previous tab state, checked is `true`.');
- assert.equal(button.state(tab).checked, true,
- 'current tab state, checked is `true`.');
- assert.equal(button.state(mainWindow).checked, true,
- 'window state, checked is `true`.');
- openBrowserWindow().then(focus).then(window => {
- let { activeWindow } = browserWindows;
- let { activeTab } = activeWindow.tabs;
- assert.equal(button.checked, false,
- 'global state, checked is `false`.');
- assert.equal(button.state(activeTab).checked, false,
- 'tab state, checked is `false`.');
- assert.equal(button.state(activeWindow).checked, false,
- 'window state, checked is `false`.');
- tab.close(()=> {
- close(window).
- then(loader.unload).
- then(done, assert.fail);
- })
- }).
- then(null, assert.fail);
- }
- });
- };
- exports['test button click do not messing up states'] = function(assert) {
- let loader = Loader(module);
- let { ToggleButton } = loader.require('sdk/ui');
- let { browserWindows } = loader.require('sdk/windows');
- let button = ToggleButton({
- id: 'my-button-21',
- label: 'my button',
- icon: './icon.png'
- });
- let mainWindow = browserWindows.activeWindow;
- let { activeTab } = mainWindow.tabs;
- button.state(mainWindow, { icon: './new-icon.png' });
- button.state(activeTab, { label: 'foo'})
- assert.equal(button.state(mainWindow).label, 'my button',
- 'label property for window state, properly derived from global state');
- assert.equal(button.state(activeTab).icon, './new-icon.png',
- 'icon property for tab state, properly derived from window state');
- button.click();
- button.label = 'bar';
- assert.equal(button.state(mainWindow).label, 'bar',
- 'label property for window state, properly derived from global state');
- button.state(mainWindow, null);
- assert.equal(button.state(activeTab).icon, './icon.png',
- 'icon property for tab state, properly derived from window state');
- loader.unload();
- }
- // If the module doesn't support the app we're being run in, require() will
- // throw. In that case, remove all tests above from exports, and add one dummy
- // test that passes.
- try {
- require('sdk/ui/button/toggle');
- }
- catch (err) {
- if (!/^Unsupported Application/.test(err.message))
- throw err;
- module.exports = {
- 'test Unsupported Application': assert => assert.pass(err.message)
- }
- }
- require('sdk/test').run(exports);
|