Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | 'use strict'; |
michael@0 | 5 | |
michael@0 | 6 | module.metadata = { |
michael@0 | 7 | 'engines': { |
michael@0 | 8 | 'Firefox': '> 28' |
michael@0 | 9 | } |
michael@0 | 10 | }; |
michael@0 | 11 | |
michael@0 | 12 | const { Cu } = require('chrome'); |
michael@0 | 13 | const { Loader } = require('sdk/test/loader'); |
michael@0 | 14 | const { data } = require('sdk/self'); |
michael@0 | 15 | const { open, focus, close } = require('sdk/window/helpers'); |
michael@0 | 16 | const { setTimeout } = require('sdk/timers'); |
michael@0 | 17 | const { getMostRecentBrowserWindow } = require('sdk/window/utils'); |
michael@0 | 18 | const { partial } = require('sdk/lang/functional'); |
michael@0 | 19 | |
michael@0 | 20 | const openBrowserWindow = partial(open, null, {features: {toolbar: true}}); |
michael@0 | 21 | const openPrivateBrowserWindow = partial(open, null, |
michael@0 | 22 | {features: {toolbar: true, private: true}}); |
michael@0 | 23 | |
michael@0 | 24 | function getWidget(buttonId, window = getMostRecentBrowserWindow()) { |
michael@0 | 25 | const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {}); |
michael@0 | 26 | const { AREA_NAVBAR } = CustomizableUI; |
michael@0 | 27 | |
michael@0 | 28 | let widgets = CustomizableUI.getWidgetIdsInArea(AREA_NAVBAR). |
michael@0 | 29 | filter((id) => id.startsWith('toggle-button--') && id.endsWith(buttonId)); |
michael@0 | 30 | |
michael@0 | 31 | if (widgets.length === 0) |
michael@0 | 32 | throw new Error('Widget with id `' + id +'` not found.'); |
michael@0 | 33 | |
michael@0 | 34 | if (widgets.length > 1) |
michael@0 | 35 | throw new Error('Unexpected number of widgets: ' + widgets.length) |
michael@0 | 36 | |
michael@0 | 37 | return CustomizableUI.getWidget(widgets[0]).forWindow(window); |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | exports['test basic constructor validation'] = function(assert) { |
michael@0 | 41 | let loader = Loader(module); |
michael@0 | 42 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 43 | |
michael@0 | 44 | assert.throws( |
michael@0 | 45 | () => ToggleButton({}), |
michael@0 | 46 | /^The option/, |
michael@0 | 47 | 'throws on no option given'); |
michael@0 | 48 | |
michael@0 | 49 | // Test no label |
michael@0 | 50 | assert.throws( |
michael@0 | 51 | () => ToggleButton({ id: 'my-button', icon: './icon.png'}), |
michael@0 | 52 | /^The option "label"/, |
michael@0 | 53 | 'throws on no label given'); |
michael@0 | 54 | |
michael@0 | 55 | // Test no id |
michael@0 | 56 | assert.throws( |
michael@0 | 57 | () => ToggleButton({ label: 'my button', icon: './icon.png' }), |
michael@0 | 58 | /^The option "id"/, |
michael@0 | 59 | 'throws on no id given'); |
michael@0 | 60 | |
michael@0 | 61 | // Test no icon |
michael@0 | 62 | assert.throws( |
michael@0 | 63 | () => ToggleButton({ id: 'my-button', label: 'my button' }), |
michael@0 | 64 | /^The option "icon"/, |
michael@0 | 65 | 'throws on no icon given'); |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | // Test empty label |
michael@0 | 69 | assert.throws( |
michael@0 | 70 | () => ToggleButton({ id: 'my-button', label: '', icon: './icon.png' }), |
michael@0 | 71 | /^The option "label"/, |
michael@0 | 72 | 'throws on no valid label given'); |
michael@0 | 73 | |
michael@0 | 74 | // Test invalid id |
michael@0 | 75 | assert.throws( |
michael@0 | 76 | () => ToggleButton({ id: 'my button', label: 'my button', icon: './icon.png' }), |
michael@0 | 77 | /^The option "id"/, |
michael@0 | 78 | 'throws on no valid id given'); |
michael@0 | 79 | |
michael@0 | 80 | // Test empty id |
michael@0 | 81 | assert.throws( |
michael@0 | 82 | () => ToggleButton({ id: '', label: 'my button', icon: './icon.png' }), |
michael@0 | 83 | /^The option "id"/, |
michael@0 | 84 | 'throws on no valid id given'); |
michael@0 | 85 | |
michael@0 | 86 | // Test remote icon |
michael@0 | 87 | assert.throws( |
michael@0 | 88 | () => ToggleButton({ id: 'my-button', label: 'my button', icon: 'http://www.mozilla.org/favicon.ico'}), |
michael@0 | 89 | /^The option "icon"/, |
michael@0 | 90 | 'throws on no valid icon given'); |
michael@0 | 91 | |
michael@0 | 92 | // Test wrong icon: no absolute URI to local resource, neither relative './' |
michael@0 | 93 | assert.throws( |
michael@0 | 94 | () => ToggleButton({ id: 'my-button', label: 'my button', icon: 'icon.png'}), |
michael@0 | 95 | /^The option "icon"/, |
michael@0 | 96 | 'throws on no valid icon given'); |
michael@0 | 97 | |
michael@0 | 98 | // Test wrong icon: no absolute URI to local resource, neither relative './' |
michael@0 | 99 | assert.throws( |
michael@0 | 100 | () => ToggleButton({ id: 'my-button', label: 'my button', icon: 'foo and bar'}), |
michael@0 | 101 | /^The option "icon"/, |
michael@0 | 102 | 'throws on no valid icon given'); |
michael@0 | 103 | |
michael@0 | 104 | // Test wrong icon: '../' is not allowed |
michael@0 | 105 | assert.throws( |
michael@0 | 106 | () => ToggleButton({ id: 'my-button', label: 'my button', icon: '../icon.png'}), |
michael@0 | 107 | /^The option "icon"/, |
michael@0 | 108 | 'throws on no valid icon given'); |
michael@0 | 109 | |
michael@0 | 110 | // Test wrong checked |
michael@0 | 111 | assert.throws( |
michael@0 | 112 | () => ToggleButton({ |
michael@0 | 113 | id: 'my-button', label: 'my button', icon: './icon.png', checked: 'yes'}), |
michael@0 | 114 | /^The option "checked"/, |
michael@0 | 115 | 'throws on no valid checked value given'); |
michael@0 | 116 | |
michael@0 | 117 | loader.unload(); |
michael@0 | 118 | }; |
michael@0 | 119 | |
michael@0 | 120 | exports['test button added'] = function(assert) { |
michael@0 | 121 | let loader = Loader(module); |
michael@0 | 122 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 123 | |
michael@0 | 124 | let button = ToggleButton({ |
michael@0 | 125 | id: 'my-button-1', |
michael@0 | 126 | label: 'my button', |
michael@0 | 127 | icon: './icon.png' |
michael@0 | 128 | }); |
michael@0 | 129 | |
michael@0 | 130 | // check defaults |
michael@0 | 131 | assert.equal(button.checked, false, |
michael@0 | 132 | 'checked is set to default `false` value'); |
michael@0 | 133 | |
michael@0 | 134 | assert.equal(button.disabled, false, |
michael@0 | 135 | 'disabled is set to default `false` value'); |
michael@0 | 136 | |
michael@0 | 137 | let { node } = getWidget(button.id); |
michael@0 | 138 | |
michael@0 | 139 | assert.ok(!!node, 'The button is in the navbar'); |
michael@0 | 140 | |
michael@0 | 141 | assert.equal(button.label, node.getAttribute('label'), |
michael@0 | 142 | 'label is set'); |
michael@0 | 143 | |
michael@0 | 144 | assert.equal(button.label, node.getAttribute('tooltiptext'), |
michael@0 | 145 | 'tooltip is set'); |
michael@0 | 146 | |
michael@0 | 147 | assert.equal(data.url(button.icon.substr(2)), node.getAttribute('image'), |
michael@0 | 148 | 'icon is set'); |
michael@0 | 149 | |
michael@0 | 150 | loader.unload(); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | exports['test button added with resource URI'] = function(assert) { |
michael@0 | 154 | let loader = Loader(module); |
michael@0 | 155 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 156 | |
michael@0 | 157 | let button = ToggleButton({ |
michael@0 | 158 | id: 'my-button-1', |
michael@0 | 159 | label: 'my button', |
michael@0 | 160 | icon: data.url('icon.png') |
michael@0 | 161 | }); |
michael@0 | 162 | |
michael@0 | 163 | assert.equal(button.icon, data.url('icon.png'), |
michael@0 | 164 | 'icon is set'); |
michael@0 | 165 | |
michael@0 | 166 | let { node } = getWidget(button.id); |
michael@0 | 167 | |
michael@0 | 168 | assert.equal(button.icon, node.getAttribute('image'), |
michael@0 | 169 | 'icon on node is set'); |
michael@0 | 170 | |
michael@0 | 171 | loader.unload(); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | exports['test button duplicate id'] = function(assert) { |
michael@0 | 175 | let loader = Loader(module); |
michael@0 | 176 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 177 | |
michael@0 | 178 | let button = ToggleButton({ |
michael@0 | 179 | id: 'my-button-2', |
michael@0 | 180 | label: 'my button', |
michael@0 | 181 | icon: './icon.png' |
michael@0 | 182 | }); |
michael@0 | 183 | |
michael@0 | 184 | assert.throws(() => { |
michael@0 | 185 | let doppelganger = ToggleButton({ |
michael@0 | 186 | id: 'my-button-2', |
michael@0 | 187 | label: 'my button', |
michael@0 | 188 | icon: './icon.png' |
michael@0 | 189 | }); |
michael@0 | 190 | }, |
michael@0 | 191 | /^The ID/, |
michael@0 | 192 | 'No duplicates allowed'); |
michael@0 | 193 | |
michael@0 | 194 | loader.unload(); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | exports['test button multiple destroy'] = function(assert) { |
michael@0 | 198 | let loader = Loader(module); |
michael@0 | 199 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 200 | |
michael@0 | 201 | let button = ToggleButton({ |
michael@0 | 202 | id: 'my-button-2', |
michael@0 | 203 | label: 'my button', |
michael@0 | 204 | icon: './icon.png' |
michael@0 | 205 | }); |
michael@0 | 206 | |
michael@0 | 207 | button.destroy(); |
michael@0 | 208 | button.destroy(); |
michael@0 | 209 | button.destroy(); |
michael@0 | 210 | |
michael@0 | 211 | assert.pass('multiple destroy doesn\'t matter'); |
michael@0 | 212 | |
michael@0 | 213 | loader.unload(); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | exports['test button removed on dispose'] = function(assert, done) { |
michael@0 | 217 | const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {}); |
michael@0 | 218 | let loader = Loader(module); |
michael@0 | 219 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 220 | |
michael@0 | 221 | let widgetId; |
michael@0 | 222 | |
michael@0 | 223 | CustomizableUI.addListener({ |
michael@0 | 224 | onWidgetDestroyed: function(id) { |
michael@0 | 225 | if (id === widgetId) { |
michael@0 | 226 | CustomizableUI.removeListener(this); |
michael@0 | 227 | |
michael@0 | 228 | assert.pass('button properly removed'); |
michael@0 | 229 | loader.unload(); |
michael@0 | 230 | done(); |
michael@0 | 231 | } |
michael@0 | 232 | } |
michael@0 | 233 | }); |
michael@0 | 234 | |
michael@0 | 235 | let button = ToggleButton({ |
michael@0 | 236 | id: 'my-button-3', |
michael@0 | 237 | label: 'my button', |
michael@0 | 238 | icon: './icon.png' |
michael@0 | 239 | }); |
michael@0 | 240 | |
michael@0 | 241 | // Tried to use `getWidgetIdsInArea` but seems undefined, not sure if it |
michael@0 | 242 | // was removed or it's not in the UX build yet |
michael@0 | 243 | widgetId = getWidget(button.id).id; |
michael@0 | 244 | |
michael@0 | 245 | button.destroy(); |
michael@0 | 246 | }; |
michael@0 | 247 | |
michael@0 | 248 | exports['test button global state updated'] = function(assert) { |
michael@0 | 249 | let loader = Loader(module); |
michael@0 | 250 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 251 | |
michael@0 | 252 | let button = ToggleButton({ |
michael@0 | 253 | id: 'my-button-4', |
michael@0 | 254 | label: 'my button', |
michael@0 | 255 | icon: './icon.png' |
michael@0 | 256 | }); |
michael@0 | 257 | |
michael@0 | 258 | // Tried to use `getWidgetIdsInArea` but seems undefined, not sure if it |
michael@0 | 259 | // was removed or it's not in the UX build yet |
michael@0 | 260 | |
michael@0 | 261 | let { node, id: widgetId } = getWidget(button.id); |
michael@0 | 262 | |
michael@0 | 263 | // check read-only properties |
michael@0 | 264 | |
michael@0 | 265 | assert.throws(() => button.id = 'another-id', |
michael@0 | 266 | /^setting a property that has only a getter/, |
michael@0 | 267 | 'id cannot be set at runtime'); |
michael@0 | 268 | |
michael@0 | 269 | assert.equal(button.id, 'my-button-4', |
michael@0 | 270 | 'id is unchanged'); |
michael@0 | 271 | assert.equal(node.id, widgetId, |
michael@0 | 272 | 'node id is unchanged'); |
michael@0 | 273 | |
michael@0 | 274 | // check writable properties |
michael@0 | 275 | |
michael@0 | 276 | button.label = 'New label'; |
michael@0 | 277 | assert.equal(button.label, 'New label', |
michael@0 | 278 | 'label is updated'); |
michael@0 | 279 | assert.equal(node.getAttribute('label'), 'New label', |
michael@0 | 280 | 'node label is updated'); |
michael@0 | 281 | assert.equal(node.getAttribute('tooltiptext'), 'New label', |
michael@0 | 282 | 'node tooltip is updated'); |
michael@0 | 283 | |
michael@0 | 284 | button.icon = './new-icon.png'; |
michael@0 | 285 | assert.equal(button.icon, './new-icon.png', |
michael@0 | 286 | 'icon is updated'); |
michael@0 | 287 | assert.equal(node.getAttribute('image'), data.url('new-icon.png'), |
michael@0 | 288 | 'node image is updated'); |
michael@0 | 289 | |
michael@0 | 290 | button.disabled = true; |
michael@0 | 291 | assert.equal(button.disabled, true, |
michael@0 | 292 | 'disabled is updated'); |
michael@0 | 293 | assert.equal(node.getAttribute('disabled'), 'true', |
michael@0 | 294 | 'node disabled is updated'); |
michael@0 | 295 | |
michael@0 | 296 | // TODO: test validation on update |
michael@0 | 297 | |
michael@0 | 298 | loader.unload(); |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | exports['test button global state set and get with state method'] = function(assert) { |
michael@0 | 302 | let loader = Loader(module); |
michael@0 | 303 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 304 | |
michael@0 | 305 | let button = ToggleButton({ |
michael@0 | 306 | id: 'my-button-16', |
michael@0 | 307 | label: 'my button', |
michael@0 | 308 | icon: './icon.png' |
michael@0 | 309 | }); |
michael@0 | 310 | |
michael@0 | 311 | // read the button's state |
michael@0 | 312 | let state = button.state(button); |
michael@0 | 313 | |
michael@0 | 314 | assert.equal(state.label, 'my button', |
michael@0 | 315 | 'label is correct'); |
michael@0 | 316 | assert.equal(state.icon, './icon.png', |
michael@0 | 317 | 'icon is correct'); |
michael@0 | 318 | assert.equal(state.disabled, false, |
michael@0 | 319 | 'disabled is correct'); |
michael@0 | 320 | |
michael@0 | 321 | // set the new button's state |
michael@0 | 322 | button.state(button, { |
michael@0 | 323 | label: 'New label', |
michael@0 | 324 | icon: './new-icon.png', |
michael@0 | 325 | disabled: true |
michael@0 | 326 | }); |
michael@0 | 327 | |
michael@0 | 328 | assert.equal(button.label, 'New label', |
michael@0 | 329 | 'label is updated'); |
michael@0 | 330 | assert.equal(button.icon, './new-icon.png', |
michael@0 | 331 | 'icon is updated'); |
michael@0 | 332 | assert.equal(button.disabled, true, |
michael@0 | 333 | 'disabled is updated'); |
michael@0 | 334 | |
michael@0 | 335 | loader.unload(); |
michael@0 | 336 | }; |
michael@0 | 337 | |
michael@0 | 338 | exports['test button global state updated on multiple windows'] = function(assert, done) { |
michael@0 | 339 | let loader = Loader(module); |
michael@0 | 340 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 341 | |
michael@0 | 342 | let button = ToggleButton({ |
michael@0 | 343 | id: 'my-button-5', |
michael@0 | 344 | label: 'my button', |
michael@0 | 345 | icon: './icon.png' |
michael@0 | 346 | }); |
michael@0 | 347 | |
michael@0 | 348 | let nodes = [getWidget(button.id).node]; |
michael@0 | 349 | |
michael@0 | 350 | openBrowserWindow().then(window => { |
michael@0 | 351 | nodes.push(getWidget(button.id, window).node); |
michael@0 | 352 | |
michael@0 | 353 | button.label = 'New label'; |
michael@0 | 354 | button.icon = './new-icon.png'; |
michael@0 | 355 | button.disabled = true; |
michael@0 | 356 | |
michael@0 | 357 | for (let node of nodes) { |
michael@0 | 358 | assert.equal(node.getAttribute('label'), 'New label', |
michael@0 | 359 | 'node label is updated'); |
michael@0 | 360 | assert.equal(node.getAttribute('tooltiptext'), 'New label', |
michael@0 | 361 | 'node tooltip is updated'); |
michael@0 | 362 | |
michael@0 | 363 | assert.equal(button.icon, './new-icon.png', |
michael@0 | 364 | 'icon is updated'); |
michael@0 | 365 | assert.equal(node.getAttribute('image'), data.url('new-icon.png'), |
michael@0 | 366 | 'node image is updated'); |
michael@0 | 367 | |
michael@0 | 368 | assert.equal(button.disabled, true, |
michael@0 | 369 | 'disabled is updated'); |
michael@0 | 370 | assert.equal(node.getAttribute('disabled'), 'true', |
michael@0 | 371 | 'node disabled is updated'); |
michael@0 | 372 | }; |
michael@0 | 373 | |
michael@0 | 374 | return window; |
michael@0 | 375 | }). |
michael@0 | 376 | then(close). |
michael@0 | 377 | then(loader.unload). |
michael@0 | 378 | then(done, assert.fail); |
michael@0 | 379 | }; |
michael@0 | 380 | |
michael@0 | 381 | exports['test button window state'] = function(assert, done) { |
michael@0 | 382 | let loader = Loader(module); |
michael@0 | 383 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 384 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 385 | |
michael@0 | 386 | let button = ToggleButton({ |
michael@0 | 387 | id: 'my-button-6', |
michael@0 | 388 | label: 'my button', |
michael@0 | 389 | icon: './icon.png' |
michael@0 | 390 | }); |
michael@0 | 391 | |
michael@0 | 392 | let mainWindow = browserWindows.activeWindow; |
michael@0 | 393 | let nodes = [getWidget(button.id).node]; |
michael@0 | 394 | |
michael@0 | 395 | openBrowserWindow().then(focus).then(window => { |
michael@0 | 396 | nodes.push(getWidget(button.id, window).node); |
michael@0 | 397 | |
michael@0 | 398 | let { activeWindow } = browserWindows; |
michael@0 | 399 | |
michael@0 | 400 | button.state(activeWindow, { |
michael@0 | 401 | label: 'New label', |
michael@0 | 402 | icon: './new-icon.png', |
michael@0 | 403 | disabled: true |
michael@0 | 404 | }); |
michael@0 | 405 | |
michael@0 | 406 | // check the states |
michael@0 | 407 | |
michael@0 | 408 | assert.equal(button.label, 'my button', |
michael@0 | 409 | 'global label unchanged'); |
michael@0 | 410 | assert.equal(button.icon, './icon.png', |
michael@0 | 411 | 'global icon unchanged'); |
michael@0 | 412 | assert.equal(button.disabled, false, |
michael@0 | 413 | 'global disabled unchanged'); |
michael@0 | 414 | |
michael@0 | 415 | let state = button.state(mainWindow); |
michael@0 | 416 | |
michael@0 | 417 | assert.equal(state.label, 'my button', |
michael@0 | 418 | 'previous window label unchanged'); |
michael@0 | 419 | assert.equal(state.icon, './icon.png', |
michael@0 | 420 | 'previous window icon unchanged'); |
michael@0 | 421 | assert.equal(state.disabled, false, |
michael@0 | 422 | 'previous window disabled unchanged'); |
michael@0 | 423 | |
michael@0 | 424 | let state = button.state(activeWindow); |
michael@0 | 425 | |
michael@0 | 426 | assert.equal(state.label, 'New label', |
michael@0 | 427 | 'active window label updated'); |
michael@0 | 428 | assert.equal(state.icon, './new-icon.png', |
michael@0 | 429 | 'active window icon updated'); |
michael@0 | 430 | assert.equal(state.disabled, true, |
michael@0 | 431 | 'active disabled updated'); |
michael@0 | 432 | |
michael@0 | 433 | // change the global state, only the windows without a state are affected |
michael@0 | 434 | |
michael@0 | 435 | button.label = 'A good label'; |
michael@0 | 436 | |
michael@0 | 437 | assert.equal(button.label, 'A good label', |
michael@0 | 438 | 'global label updated'); |
michael@0 | 439 | assert.equal(button.state(mainWindow).label, 'A good label', |
michael@0 | 440 | 'previous window label updated'); |
michael@0 | 441 | assert.equal(button.state(activeWindow).label, 'New label', |
michael@0 | 442 | 'active window label unchanged'); |
michael@0 | 443 | |
michael@0 | 444 | // delete the window state will inherits the global state again |
michael@0 | 445 | |
michael@0 | 446 | button.state(activeWindow, null); |
michael@0 | 447 | |
michael@0 | 448 | assert.equal(button.state(activeWindow).label, 'A good label', |
michael@0 | 449 | 'active window label inherited'); |
michael@0 | 450 | |
michael@0 | 451 | // check the nodes properties |
michael@0 | 452 | let node = nodes[0]; |
michael@0 | 453 | let state = button.state(mainWindow); |
michael@0 | 454 | |
michael@0 | 455 | assert.equal(node.getAttribute('label'), state.label, |
michael@0 | 456 | 'node label is correct'); |
michael@0 | 457 | assert.equal(node.getAttribute('tooltiptext'), state.label, |
michael@0 | 458 | 'node tooltip is correct'); |
michael@0 | 459 | |
michael@0 | 460 | assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)), |
michael@0 | 461 | 'node image is correct'); |
michael@0 | 462 | assert.equal(node.hasAttribute('disabled'), state.disabled, |
michael@0 | 463 | 'disabled is correct'); |
michael@0 | 464 | |
michael@0 | 465 | let node = nodes[1]; |
michael@0 | 466 | let state = button.state(activeWindow); |
michael@0 | 467 | |
michael@0 | 468 | assert.equal(node.getAttribute('label'), state.label, |
michael@0 | 469 | 'node label is correct'); |
michael@0 | 470 | assert.equal(node.getAttribute('tooltiptext'), state.label, |
michael@0 | 471 | 'node tooltip is correct'); |
michael@0 | 472 | |
michael@0 | 473 | assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)), |
michael@0 | 474 | 'node image is correct'); |
michael@0 | 475 | assert.equal(node.hasAttribute('disabled'), state.disabled, |
michael@0 | 476 | 'disabled is correct'); |
michael@0 | 477 | |
michael@0 | 478 | return window; |
michael@0 | 479 | }). |
michael@0 | 480 | then(close). |
michael@0 | 481 | then(loader.unload). |
michael@0 | 482 | then(done, assert.fail); |
michael@0 | 483 | }; |
michael@0 | 484 | |
michael@0 | 485 | |
michael@0 | 486 | exports['test button tab state'] = function(assert, done) { |
michael@0 | 487 | let loader = Loader(module); |
michael@0 | 488 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 489 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 490 | let tabs = loader.require('sdk/tabs'); |
michael@0 | 491 | |
michael@0 | 492 | let button = ToggleButton({ |
michael@0 | 493 | id: 'my-button-7', |
michael@0 | 494 | label: 'my button', |
michael@0 | 495 | icon: './icon.png' |
michael@0 | 496 | }); |
michael@0 | 497 | |
michael@0 | 498 | let mainTab = tabs.activeTab; |
michael@0 | 499 | let node = getWidget(button.id).node; |
michael@0 | 500 | |
michael@0 | 501 | tabs.open({ |
michael@0 | 502 | url: 'about:blank', |
michael@0 | 503 | onActivate: function onActivate(tab) { |
michael@0 | 504 | tab.removeListener('activate', onActivate); |
michael@0 | 505 | |
michael@0 | 506 | let { activeWindow } = browserWindows; |
michael@0 | 507 | // set window state |
michael@0 | 508 | button.state(activeWindow, { |
michael@0 | 509 | label: 'Window label', |
michael@0 | 510 | icon: './window-icon.png' |
michael@0 | 511 | }); |
michael@0 | 512 | |
michael@0 | 513 | // set previous active tab state |
michael@0 | 514 | button.state(mainTab, { |
michael@0 | 515 | label: 'Tab label', |
michael@0 | 516 | icon: './tab-icon.png', |
michael@0 | 517 | }); |
michael@0 | 518 | |
michael@0 | 519 | // set current active tab state |
michael@0 | 520 | button.state(tab, { |
michael@0 | 521 | icon: './another-tab-icon.png', |
michael@0 | 522 | disabled: true |
michael@0 | 523 | }); |
michael@0 | 524 | |
michael@0 | 525 | // check the states |
michael@0 | 526 | |
michael@0 | 527 | Cu.schedulePreciseGC(() => { |
michael@0 | 528 | assert.equal(button.label, 'my button', |
michael@0 | 529 | 'global label unchanged'); |
michael@0 | 530 | assert.equal(button.icon, './icon.png', |
michael@0 | 531 | 'global icon unchanged'); |
michael@0 | 532 | assert.equal(button.disabled, false, |
michael@0 | 533 | 'global disabled unchanged'); |
michael@0 | 534 | |
michael@0 | 535 | let state = button.state(mainTab); |
michael@0 | 536 | |
michael@0 | 537 | assert.equal(state.label, 'Tab label', |
michael@0 | 538 | 'previous tab label updated'); |
michael@0 | 539 | assert.equal(state.icon, './tab-icon.png', |
michael@0 | 540 | 'previous tab icon updated'); |
michael@0 | 541 | assert.equal(state.disabled, false, |
michael@0 | 542 | 'previous tab disabled unchanged'); |
michael@0 | 543 | |
michael@0 | 544 | let state = button.state(tab); |
michael@0 | 545 | |
michael@0 | 546 | assert.equal(state.label, 'Window label', |
michael@0 | 547 | 'active tab inherited from window state'); |
michael@0 | 548 | assert.equal(state.icon, './another-tab-icon.png', |
michael@0 | 549 | 'active tab icon updated'); |
michael@0 | 550 | assert.equal(state.disabled, true, |
michael@0 | 551 | 'active disabled updated'); |
michael@0 | 552 | |
michael@0 | 553 | // change the global state |
michael@0 | 554 | button.icon = './good-icon.png'; |
michael@0 | 555 | |
michael@0 | 556 | // delete the tab state |
michael@0 | 557 | button.state(tab, null); |
michael@0 | 558 | |
michael@0 | 559 | assert.equal(button.icon, './good-icon.png', |
michael@0 | 560 | 'global icon updated'); |
michael@0 | 561 | assert.equal(button.state(mainTab).icon, './tab-icon.png', |
michael@0 | 562 | 'previous tab icon unchanged'); |
michael@0 | 563 | assert.equal(button.state(tab).icon, './window-icon.png', |
michael@0 | 564 | 'tab icon inherited from window'); |
michael@0 | 565 | |
michael@0 | 566 | // delete the window state |
michael@0 | 567 | button.state(activeWindow, null); |
michael@0 | 568 | |
michael@0 | 569 | assert.equal(button.state(tab).icon, './good-icon.png', |
michael@0 | 570 | 'tab icon inherited from global'); |
michael@0 | 571 | |
michael@0 | 572 | // check the node properties |
michael@0 | 573 | |
michael@0 | 574 | let state = button.state(tabs.activeTab); |
michael@0 | 575 | |
michael@0 | 576 | assert.equal(node.getAttribute('label'), state.label, |
michael@0 | 577 | 'node label is correct'); |
michael@0 | 578 | assert.equal(node.getAttribute('tooltiptext'), state.label, |
michael@0 | 579 | 'node tooltip is correct'); |
michael@0 | 580 | assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)), |
michael@0 | 581 | 'node image is correct'); |
michael@0 | 582 | assert.equal(node.hasAttribute('disabled'), state.disabled, |
michael@0 | 583 | 'disabled is correct'); |
michael@0 | 584 | |
michael@0 | 585 | tabs.once('activate', () => { |
michael@0 | 586 | // This is made in order to avoid to check the node before it |
michael@0 | 587 | // is updated, need a better check |
michael@0 | 588 | setTimeout(() => { |
michael@0 | 589 | let state = button.state(mainTab); |
michael@0 | 590 | |
michael@0 | 591 | assert.equal(node.getAttribute('label'), state.label, |
michael@0 | 592 | 'node label is correct'); |
michael@0 | 593 | assert.equal(node.getAttribute('tooltiptext'), state.label, |
michael@0 | 594 | 'node tooltip is correct'); |
michael@0 | 595 | assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)), |
michael@0 | 596 | 'node image is correct'); |
michael@0 | 597 | assert.equal(node.hasAttribute('disabled'), state.disabled, |
michael@0 | 598 | 'disabled is correct'); |
michael@0 | 599 | |
michael@0 | 600 | tab.close(() => { |
michael@0 | 601 | loader.unload(); |
michael@0 | 602 | done(); |
michael@0 | 603 | }); |
michael@0 | 604 | }, 500); |
michael@0 | 605 | }); |
michael@0 | 606 | |
michael@0 | 607 | mainTab.activate(); |
michael@0 | 608 | }); |
michael@0 | 609 | } |
michael@0 | 610 | }); |
michael@0 | 611 | |
michael@0 | 612 | }; |
michael@0 | 613 | |
michael@0 | 614 | exports['test button click'] = function(assert, done) { |
michael@0 | 615 | let loader = Loader(module); |
michael@0 | 616 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 617 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 618 | |
michael@0 | 619 | let labels = []; |
michael@0 | 620 | |
michael@0 | 621 | let button = ToggleButton({ |
michael@0 | 622 | id: 'my-button-8', |
michael@0 | 623 | label: 'my button', |
michael@0 | 624 | icon: './icon.png', |
michael@0 | 625 | onClick: ({label}) => labels.push(label) |
michael@0 | 626 | }); |
michael@0 | 627 | |
michael@0 | 628 | let mainWindow = browserWindows.activeWindow; |
michael@0 | 629 | let chromeWindow = getMostRecentBrowserWindow(); |
michael@0 | 630 | |
michael@0 | 631 | openBrowserWindow().then(focus).then(window => { |
michael@0 | 632 | button.state(mainWindow, { label: 'nothing' }); |
michael@0 | 633 | button.state(mainWindow.tabs.activeTab, { label: 'foo'}) |
michael@0 | 634 | button.state(browserWindows.activeWindow, { label: 'bar' }); |
michael@0 | 635 | |
michael@0 | 636 | button.click(); |
michael@0 | 637 | |
michael@0 | 638 | focus(chromeWindow).then(() => { |
michael@0 | 639 | button.click(); |
michael@0 | 640 | |
michael@0 | 641 | assert.deepEqual(labels, ['bar', 'foo'], |
michael@0 | 642 | 'button click works'); |
michael@0 | 643 | |
michael@0 | 644 | close(window). |
michael@0 | 645 | then(loader.unload). |
michael@0 | 646 | then(done, assert.fail); |
michael@0 | 647 | }); |
michael@0 | 648 | }).then(null, assert.fail); |
michael@0 | 649 | } |
michael@0 | 650 | |
michael@0 | 651 | exports['test button icon set'] = function(assert) { |
michael@0 | 652 | const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {}); |
michael@0 | 653 | let loader = Loader(module); |
michael@0 | 654 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 655 | |
michael@0 | 656 | // Test remote icon set |
michael@0 | 657 | assert.throws( |
michael@0 | 658 | () => ToggleButton({ |
michael@0 | 659 | id: 'my-button-10', |
michael@0 | 660 | label: 'my button', |
michael@0 | 661 | icon: { |
michael@0 | 662 | '16': 'http://www.mozilla.org/favicon.ico' |
michael@0 | 663 | } |
michael@0 | 664 | }), |
michael@0 | 665 | /^The option "icon"/, |
michael@0 | 666 | 'throws on no valid icon given'); |
michael@0 | 667 | |
michael@0 | 668 | let button = ToggleButton({ |
michael@0 | 669 | id: 'my-button-11', |
michael@0 | 670 | label: 'my button', |
michael@0 | 671 | icon: { |
michael@0 | 672 | '5': './icon5.png', |
michael@0 | 673 | '16': './icon16.png', |
michael@0 | 674 | '32': './icon32.png', |
michael@0 | 675 | '64': './icon64.png' |
michael@0 | 676 | } |
michael@0 | 677 | }); |
michael@0 | 678 | |
michael@0 | 679 | let { node, id: widgetId } = getWidget(button.id); |
michael@0 | 680 | let { devicePixelRatio } = node.ownerDocument.defaultView; |
michael@0 | 681 | |
michael@0 | 682 | let size = 16 * devicePixelRatio; |
michael@0 | 683 | |
michael@0 | 684 | assert.equal(node.getAttribute('image'), data.url(button.icon[size].substr(2)), |
michael@0 | 685 | 'the icon is set properly in navbar'); |
michael@0 | 686 | |
michael@0 | 687 | let size = 32 * devicePixelRatio; |
michael@0 | 688 | |
michael@0 | 689 | CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_PANEL); |
michael@0 | 690 | |
michael@0 | 691 | assert.equal(node.getAttribute('image'), data.url(button.icon[size].substr(2)), |
michael@0 | 692 | 'the icon is set properly in panel'); |
michael@0 | 693 | |
michael@0 | 694 | // Using `loader.unload` without move back the button to the original area |
michael@0 | 695 | // raises an error in the CustomizableUI. This is doesn't happen if the |
michael@0 | 696 | // button is moved manually from navbar to panel. I believe it has to do |
michael@0 | 697 | // with `addWidgetToArea` method, because even with a `timeout` the issue |
michael@0 | 698 | // persist. |
michael@0 | 699 | CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_NAVBAR); |
michael@0 | 700 | |
michael@0 | 701 | loader.unload(); |
michael@0 | 702 | } |
michael@0 | 703 | |
michael@0 | 704 | exports['test button icon se with only one option'] = function(assert) { |
michael@0 | 705 | const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {}); |
michael@0 | 706 | let loader = Loader(module); |
michael@0 | 707 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 708 | |
michael@0 | 709 | // Test remote icon set |
michael@0 | 710 | assert.throws( |
michael@0 | 711 | () => ToggleButton({ |
michael@0 | 712 | id: 'my-button-10', |
michael@0 | 713 | label: 'my button', |
michael@0 | 714 | icon: { |
michael@0 | 715 | '16': 'http://www.mozilla.org/favicon.ico' |
michael@0 | 716 | } |
michael@0 | 717 | }), |
michael@0 | 718 | /^The option "icon"/, |
michael@0 | 719 | 'throws on no valid icon given'); |
michael@0 | 720 | |
michael@0 | 721 | let button = ToggleButton({ |
michael@0 | 722 | id: 'my-button-11', |
michael@0 | 723 | label: 'my button', |
michael@0 | 724 | icon: { |
michael@0 | 725 | '5': './icon5.png' |
michael@0 | 726 | } |
michael@0 | 727 | }); |
michael@0 | 728 | |
michael@0 | 729 | let { node, id: widgetId } = getWidget(button.id); |
michael@0 | 730 | |
michael@0 | 731 | assert.equal(node.getAttribute('image'), data.url(button.icon['5'].substr(2)), |
michael@0 | 732 | 'the icon is set properly in navbar'); |
michael@0 | 733 | |
michael@0 | 734 | CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_PANEL); |
michael@0 | 735 | |
michael@0 | 736 | assert.equal(node.getAttribute('image'), data.url(button.icon['5'].substr(2)), |
michael@0 | 737 | 'the icon is set properly in panel'); |
michael@0 | 738 | |
michael@0 | 739 | // Using `loader.unload` without move back the button to the original area |
michael@0 | 740 | // raises an error in the CustomizableUI. This is doesn't happen if the |
michael@0 | 741 | // button is moved manually from navbar to panel. I believe it has to do |
michael@0 | 742 | // with `addWidgetToArea` method, because even with a `timeout` the issue |
michael@0 | 743 | // persist. |
michael@0 | 744 | CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_NAVBAR); |
michael@0 | 745 | |
michael@0 | 746 | loader.unload(); |
michael@0 | 747 | } |
michael@0 | 748 | |
michael@0 | 749 | exports['test button state validation'] = function(assert) { |
michael@0 | 750 | let loader = Loader(module); |
michael@0 | 751 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 752 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 753 | |
michael@0 | 754 | let button = ToggleButton({ |
michael@0 | 755 | id: 'my-button-12', |
michael@0 | 756 | label: 'my button', |
michael@0 | 757 | icon: './icon.png' |
michael@0 | 758 | }) |
michael@0 | 759 | |
michael@0 | 760 | let state = button.state(button); |
michael@0 | 761 | |
michael@0 | 762 | assert.throws( |
michael@0 | 763 | () => button.state(button, { icon: 'http://www.mozilla.org/favicon.ico' }), |
michael@0 | 764 | /^The option "icon"/, |
michael@0 | 765 | 'throws on remote icon given'); |
michael@0 | 766 | |
michael@0 | 767 | loader.unload(); |
michael@0 | 768 | }; |
michael@0 | 769 | |
michael@0 | 770 | exports['test button are not in private windows'] = function(assert, done) { |
michael@0 | 771 | let loader = Loader(module); |
michael@0 | 772 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 773 | let{ isPrivate } = loader.require('sdk/private-browsing'); |
michael@0 | 774 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 775 | |
michael@0 | 776 | let button = ToggleButton({ |
michael@0 | 777 | id: 'my-button-13', |
michael@0 | 778 | label: 'my button', |
michael@0 | 779 | icon: './icon.png' |
michael@0 | 780 | }); |
michael@0 | 781 | |
michael@0 | 782 | openPrivateBrowserWindow().then(window => { |
michael@0 | 783 | assert.ok(isPrivate(window), |
michael@0 | 784 | 'the new window is private'); |
michael@0 | 785 | |
michael@0 | 786 | let { node } = getWidget(button.id, window); |
michael@0 | 787 | |
michael@0 | 788 | assert.ok(!node || node.style.display === 'none', |
michael@0 | 789 | 'the button is not added / is not visible on private window'); |
michael@0 | 790 | |
michael@0 | 791 | return window; |
michael@0 | 792 | }). |
michael@0 | 793 | then(close). |
michael@0 | 794 | then(loader.unload). |
michael@0 | 795 | then(done, assert.fail) |
michael@0 | 796 | } |
michael@0 | 797 | |
michael@0 | 798 | exports['test button state are snapshot'] = function(assert) { |
michael@0 | 799 | let loader = Loader(module); |
michael@0 | 800 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 801 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 802 | let tabs = loader.require('sdk/tabs'); |
michael@0 | 803 | |
michael@0 | 804 | let button = ToggleButton({ |
michael@0 | 805 | id: 'my-button-14', |
michael@0 | 806 | label: 'my button', |
michael@0 | 807 | icon: './icon.png' |
michael@0 | 808 | }); |
michael@0 | 809 | |
michael@0 | 810 | let state = button.state(button); |
michael@0 | 811 | let windowState = button.state(browserWindows.activeWindow); |
michael@0 | 812 | let tabState = button.state(tabs.activeTab); |
michael@0 | 813 | |
michael@0 | 814 | assert.deepEqual(windowState, state, |
michael@0 | 815 | 'window state has the same properties of button state'); |
michael@0 | 816 | |
michael@0 | 817 | assert.deepEqual(tabState, state, |
michael@0 | 818 | 'tab state has the same properties of button state'); |
michael@0 | 819 | |
michael@0 | 820 | assert.notEqual(windowState, state, |
michael@0 | 821 | 'window state is not the same object of button state'); |
michael@0 | 822 | |
michael@0 | 823 | assert.notEqual(tabState, state, |
michael@0 | 824 | 'tab state is not the same object of button state'); |
michael@0 | 825 | |
michael@0 | 826 | assert.deepEqual(button.state(button), state, |
michael@0 | 827 | 'button state has the same content of previous button state'); |
michael@0 | 828 | |
michael@0 | 829 | assert.deepEqual(button.state(browserWindows.activeWindow), windowState, |
michael@0 | 830 | 'window state has the same content of previous window state'); |
michael@0 | 831 | |
michael@0 | 832 | assert.deepEqual(button.state(tabs.activeTab), tabState, |
michael@0 | 833 | 'tab state has the same content of previous tab state'); |
michael@0 | 834 | |
michael@0 | 835 | assert.notEqual(button.state(button), state, |
michael@0 | 836 | 'button state is not the same object of previous button state'); |
michael@0 | 837 | |
michael@0 | 838 | assert.notEqual(button.state(browserWindows.activeWindow), windowState, |
michael@0 | 839 | 'window state is not the same object of previous window state'); |
michael@0 | 840 | |
michael@0 | 841 | assert.notEqual(button.state(tabs.activeTab), tabState, |
michael@0 | 842 | 'tab state is not the same object of previous tab state'); |
michael@0 | 843 | |
michael@0 | 844 | loader.unload(); |
michael@0 | 845 | } |
michael@0 | 846 | |
michael@0 | 847 | exports['test button icon object is a snapshot'] = function(assert) { |
michael@0 | 848 | let loader = Loader(module); |
michael@0 | 849 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 850 | |
michael@0 | 851 | let icon = { |
michael@0 | 852 | '16': './foo.png' |
michael@0 | 853 | }; |
michael@0 | 854 | |
michael@0 | 855 | let button = ToggleButton({ |
michael@0 | 856 | id: 'my-button-17', |
michael@0 | 857 | label: 'my button', |
michael@0 | 858 | icon: icon |
michael@0 | 859 | }); |
michael@0 | 860 | |
michael@0 | 861 | assert.deepEqual(button.icon, icon, |
michael@0 | 862 | 'button.icon has the same properties of the object set in the constructor'); |
michael@0 | 863 | |
michael@0 | 864 | assert.notEqual(button.icon, icon, |
michael@0 | 865 | 'button.icon is not the same object of the object set in the constructor'); |
michael@0 | 866 | |
michael@0 | 867 | assert.throws( |
michael@0 | 868 | () => button.icon[16] = './bar.png', |
michael@0 | 869 | /16 is read-only/, |
michael@0 | 870 | 'properties of button.icon are ready-only' |
michael@0 | 871 | ); |
michael@0 | 872 | |
michael@0 | 873 | let newIcon = {'16': './bar.png'}; |
michael@0 | 874 | button.icon = newIcon; |
michael@0 | 875 | |
michael@0 | 876 | assert.deepEqual(button.icon, newIcon, |
michael@0 | 877 | 'button.icon has the same properties of the object set'); |
michael@0 | 878 | |
michael@0 | 879 | assert.notEqual(button.icon, newIcon, |
michael@0 | 880 | 'button.icon is not the same object of the object set'); |
michael@0 | 881 | |
michael@0 | 882 | loader.unload(); |
michael@0 | 883 | } |
michael@0 | 884 | |
michael@0 | 885 | exports['test button after destroy'] = function(assert) { |
michael@0 | 886 | let loader = Loader(module); |
michael@0 | 887 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 888 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 889 | let { activeTab } = loader.require('sdk/tabs'); |
michael@0 | 890 | |
michael@0 | 891 | let button = ToggleButton({ |
michael@0 | 892 | id: 'my-button-15', |
michael@0 | 893 | label: 'my button', |
michael@0 | 894 | icon: './icon.png', |
michael@0 | 895 | onClick: () => assert.fail('onClick should not be called') |
michael@0 | 896 | }); |
michael@0 | 897 | |
michael@0 | 898 | button.destroy(); |
michael@0 | 899 | |
michael@0 | 900 | assert.throws( |
michael@0 | 901 | () => button.click(), |
michael@0 | 902 | /^The state cannot be set or get/, |
michael@0 | 903 | 'button.click() not executed'); |
michael@0 | 904 | |
michael@0 | 905 | assert.throws( |
michael@0 | 906 | () => button.label, |
michael@0 | 907 | /^The state cannot be set or get/, |
michael@0 | 908 | 'button.label cannot be get after destroy'); |
michael@0 | 909 | |
michael@0 | 910 | assert.throws( |
michael@0 | 911 | () => button.label = 'my label', |
michael@0 | 912 | /^The state cannot be set or get/, |
michael@0 | 913 | 'button.label cannot be set after destroy'); |
michael@0 | 914 | |
michael@0 | 915 | assert.throws( |
michael@0 | 916 | () => { |
michael@0 | 917 | button.state(browserWindows.activeWindow, { |
michael@0 | 918 | label: 'window label' |
michael@0 | 919 | }); |
michael@0 | 920 | }, |
michael@0 | 921 | /^The state cannot be set or get/, |
michael@0 | 922 | 'window state label cannot be set after destroy'); |
michael@0 | 923 | |
michael@0 | 924 | assert.throws( |
michael@0 | 925 | () => button.state(browserWindows.activeWindow).label, |
michael@0 | 926 | /^The state cannot be set or get/, |
michael@0 | 927 | 'window state label cannot be get after destroy'); |
michael@0 | 928 | |
michael@0 | 929 | assert.throws( |
michael@0 | 930 | () => { |
michael@0 | 931 | button.state(activeTab, { |
michael@0 | 932 | label: 'tab label' |
michael@0 | 933 | }); |
michael@0 | 934 | }, |
michael@0 | 935 | /^The state cannot be set or get/, |
michael@0 | 936 | 'tab state label cannot be set after destroy'); |
michael@0 | 937 | |
michael@0 | 938 | assert.throws( |
michael@0 | 939 | () => button.state(activeTab).label, |
michael@0 | 940 | /^The state cannot be set or get/, |
michael@0 | 941 | 'window state label cannot se get after destroy'); |
michael@0 | 942 | |
michael@0 | 943 | loader.unload(); |
michael@0 | 944 | }; |
michael@0 | 945 | |
michael@0 | 946 | exports['test button checked'] = function(assert, done) { |
michael@0 | 947 | let loader = Loader(module); |
michael@0 | 948 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 949 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 950 | |
michael@0 | 951 | let events = []; |
michael@0 | 952 | |
michael@0 | 953 | let button = ToggleButton({ |
michael@0 | 954 | id: 'my-button-9', |
michael@0 | 955 | label: 'my button', |
michael@0 | 956 | icon: './icon.png', |
michael@0 | 957 | checked: true, |
michael@0 | 958 | onClick: ({label}) => events.push('clicked:' + label), |
michael@0 | 959 | onChange: state => events.push('changed:' + state.label + ':' + state.checked) |
michael@0 | 960 | }); |
michael@0 | 961 | |
michael@0 | 962 | let { node } = getWidget(button.id); |
michael@0 | 963 | |
michael@0 | 964 | assert.equal(node.getAttribute('type'), 'checkbox', |
michael@0 | 965 | 'node type is properly set'); |
michael@0 | 966 | |
michael@0 | 967 | let mainWindow = browserWindows.activeWindow; |
michael@0 | 968 | let chromeWindow = getMostRecentBrowserWindow(); |
michael@0 | 969 | |
michael@0 | 970 | openBrowserWindow().then(focus).then(window => { |
michael@0 | 971 | button.state(mainWindow, { label: 'nothing' }); |
michael@0 | 972 | button.state(mainWindow.tabs.activeTab, { label: 'foo'}) |
michael@0 | 973 | button.state(browserWindows.activeWindow, { label: 'bar' }); |
michael@0 | 974 | |
michael@0 | 975 | button.click(); |
michael@0 | 976 | button.click(); |
michael@0 | 977 | |
michael@0 | 978 | focus(chromeWindow).then(() => { |
michael@0 | 979 | button.click(); |
michael@0 | 980 | button.click(); |
michael@0 | 981 | |
michael@0 | 982 | assert.deepEqual(events, [ |
michael@0 | 983 | 'clicked:bar', 'changed:bar:false', 'clicked:bar', 'changed:bar:true', |
michael@0 | 984 | 'clicked:foo', 'changed:foo:false', 'clicked:foo', 'changed:foo:true' |
michael@0 | 985 | ], |
michael@0 | 986 | 'button change events works'); |
michael@0 | 987 | |
michael@0 | 988 | close(window). |
michael@0 | 989 | then(loader.unload). |
michael@0 | 990 | then(done, assert.fail); |
michael@0 | 991 | }) |
michael@0 | 992 | }).then(null, assert.fail); |
michael@0 | 993 | } |
michael@0 | 994 | |
michael@0 | 995 | exports['test button is checked on window level'] = function(assert, done) { |
michael@0 | 996 | let loader = Loader(module); |
michael@0 | 997 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 998 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 999 | let tabs = loader.require('sdk/tabs'); |
michael@0 | 1000 | |
michael@0 | 1001 | let button = ToggleButton({ |
michael@0 | 1002 | id: 'my-button-20', |
michael@0 | 1003 | label: 'my button', |
michael@0 | 1004 | icon: './icon.png' |
michael@0 | 1005 | }); |
michael@0 | 1006 | |
michael@0 | 1007 | let mainWindow = browserWindows.activeWindow; |
michael@0 | 1008 | let mainTab = tabs.activeTab; |
michael@0 | 1009 | |
michael@0 | 1010 | assert.equal(button.checked, false, |
michael@0 | 1011 | 'global state, checked is `false`.'); |
michael@0 | 1012 | assert.equal(button.state(mainTab).checked, false, |
michael@0 | 1013 | 'tab state, checked is `false`.'); |
michael@0 | 1014 | assert.equal(button.state(mainWindow).checked, false, |
michael@0 | 1015 | 'window state, checked is `false`.'); |
michael@0 | 1016 | |
michael@0 | 1017 | button.click(); |
michael@0 | 1018 | |
michael@0 | 1019 | tabs.open({ |
michael@0 | 1020 | url: 'about:blank', |
michael@0 | 1021 | onActivate: function onActivate(tab) { |
michael@0 | 1022 | tab.removeListener('activate', onActivate); |
michael@0 | 1023 | |
michael@0 | 1024 | assert.notEqual(mainTab, tab, |
michael@0 | 1025 | 'the current tab is not the same.'); |
michael@0 | 1026 | |
michael@0 | 1027 | assert.equal(button.checked, false, |
michael@0 | 1028 | 'global state, checked is `false`.'); |
michael@0 | 1029 | assert.equal(button.state(mainTab).checked, true, |
michael@0 | 1030 | 'previous tab state, checked is `true`.'); |
michael@0 | 1031 | assert.equal(button.state(tab).checked, true, |
michael@0 | 1032 | 'current tab state, checked is `true`.'); |
michael@0 | 1033 | assert.equal(button.state(mainWindow).checked, true, |
michael@0 | 1034 | 'window state, checked is `true`.'); |
michael@0 | 1035 | |
michael@0 | 1036 | openBrowserWindow().then(focus).then(window => { |
michael@0 | 1037 | let { activeWindow } = browserWindows; |
michael@0 | 1038 | let { activeTab } = activeWindow.tabs; |
michael@0 | 1039 | |
michael@0 | 1040 | assert.equal(button.checked, false, |
michael@0 | 1041 | 'global state, checked is `false`.'); |
michael@0 | 1042 | assert.equal(button.state(activeTab).checked, false, |
michael@0 | 1043 | 'tab state, checked is `false`.'); |
michael@0 | 1044 | |
michael@0 | 1045 | assert.equal(button.state(activeWindow).checked, false, |
michael@0 | 1046 | 'window state, checked is `false`.'); |
michael@0 | 1047 | |
michael@0 | 1048 | tab.close(()=> { |
michael@0 | 1049 | close(window). |
michael@0 | 1050 | then(loader.unload). |
michael@0 | 1051 | then(done, assert.fail); |
michael@0 | 1052 | }) |
michael@0 | 1053 | }). |
michael@0 | 1054 | then(null, assert.fail); |
michael@0 | 1055 | } |
michael@0 | 1056 | }); |
michael@0 | 1057 | |
michael@0 | 1058 | }; |
michael@0 | 1059 | |
michael@0 | 1060 | exports['test button click do not messing up states'] = function(assert) { |
michael@0 | 1061 | let loader = Loader(module); |
michael@0 | 1062 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 1063 | let { browserWindows } = loader.require('sdk/windows'); |
michael@0 | 1064 | |
michael@0 | 1065 | let button = ToggleButton({ |
michael@0 | 1066 | id: 'my-button-21', |
michael@0 | 1067 | label: 'my button', |
michael@0 | 1068 | icon: './icon.png' |
michael@0 | 1069 | }); |
michael@0 | 1070 | |
michael@0 | 1071 | let mainWindow = browserWindows.activeWindow; |
michael@0 | 1072 | let { activeTab } = mainWindow.tabs; |
michael@0 | 1073 | |
michael@0 | 1074 | button.state(mainWindow, { icon: './new-icon.png' }); |
michael@0 | 1075 | button.state(activeTab, { label: 'foo'}) |
michael@0 | 1076 | |
michael@0 | 1077 | assert.equal(button.state(mainWindow).label, 'my button', |
michael@0 | 1078 | 'label property for window state, properly derived from global state'); |
michael@0 | 1079 | |
michael@0 | 1080 | assert.equal(button.state(activeTab).icon, './new-icon.png', |
michael@0 | 1081 | 'icon property for tab state, properly derived from window state'); |
michael@0 | 1082 | |
michael@0 | 1083 | button.click(); |
michael@0 | 1084 | |
michael@0 | 1085 | button.label = 'bar'; |
michael@0 | 1086 | |
michael@0 | 1087 | assert.equal(button.state(mainWindow).label, 'bar', |
michael@0 | 1088 | 'label property for window state, properly derived from global state'); |
michael@0 | 1089 | |
michael@0 | 1090 | button.state(mainWindow, null); |
michael@0 | 1091 | |
michael@0 | 1092 | assert.equal(button.state(activeTab).icon, './icon.png', |
michael@0 | 1093 | 'icon property for tab state, properly derived from window state'); |
michael@0 | 1094 | |
michael@0 | 1095 | loader.unload(); |
michael@0 | 1096 | } |
michael@0 | 1097 | |
michael@0 | 1098 | exports['test buttons can have anchored panels'] = function(assert, done) { |
michael@0 | 1099 | let loader = Loader(module); |
michael@0 | 1100 | let { ToggleButton } = loader.require('sdk/ui'); |
michael@0 | 1101 | let { Panel } = loader.require('sdk/panel'); |
michael@0 | 1102 | let { identify } = loader.require('sdk/ui/id'); |
michael@0 | 1103 | let { getActiveView } = loader.require('sdk/view/core'); |
michael@0 | 1104 | |
michael@0 | 1105 | let b1 = ToggleButton({ |
michael@0 | 1106 | id: 'my-button-22', |
michael@0 | 1107 | label: 'my button', |
michael@0 | 1108 | icon: './icon.png', |
michael@0 | 1109 | onChange: ({checked}) => checked && panel.show() |
michael@0 | 1110 | }); |
michael@0 | 1111 | |
michael@0 | 1112 | let b2 = ToggleButton({ |
michael@0 | 1113 | id: 'my-button-23', |
michael@0 | 1114 | label: 'my button', |
michael@0 | 1115 | icon: './icon.png', |
michael@0 | 1116 | onChange: ({checked}) => checked && panel.show({position: b2}) |
michael@0 | 1117 | }); |
michael@0 | 1118 | |
michael@0 | 1119 | let panel = Panel({ |
michael@0 | 1120 | position: b1 |
michael@0 | 1121 | }); |
michael@0 | 1122 | |
michael@0 | 1123 | let { document } = getMostRecentBrowserWindow(); |
michael@0 | 1124 | let b1Node = document.getElementById(identify(b1)); |
michael@0 | 1125 | let b2Node = document.getElementById(identify(b2)); |
michael@0 | 1126 | let panelNode = getActiveView(panel); |
michael@0 | 1127 | |
michael@0 | 1128 | panel.once('show', () => { |
michael@0 | 1129 | assert.ok(b1.state('window').checked, |
michael@0 | 1130 | 'button is checked'); |
michael@0 | 1131 | |
michael@0 | 1132 | assert.equal(panelNode.getAttribute('type'), 'arrow', |
michael@0 | 1133 | 'the panel is a arrow type'); |
michael@0 | 1134 | |
michael@0 | 1135 | assert.strictEqual(b1Node, panelNode.anchorNode, |
michael@0 | 1136 | 'the panel is anchored properly to the button given in costructor'); |
michael@0 | 1137 | |
michael@0 | 1138 | panel.hide(); |
michael@0 | 1139 | |
michael@0 | 1140 | panel.once('show', () => { |
michael@0 | 1141 | assert.ok(b2.state('window').checked, |
michael@0 | 1142 | 'button is checked'); |
michael@0 | 1143 | |
michael@0 | 1144 | assert.equal(panelNode.getAttribute('type'), 'arrow', |
michael@0 | 1145 | 'the panel is a arrow type'); |
michael@0 | 1146 | |
michael@0 | 1147 | // test also that the button passed in `show` method, takes the precedence |
michael@0 | 1148 | // over the button set in panel's constructor. |
michael@0 | 1149 | assert.strictEqual(b2Node, panelNode.anchorNode, |
michael@0 | 1150 | 'the panel is anchored properly to the button passed to show method'); |
michael@0 | 1151 | |
michael@0 | 1152 | loader.unload(); |
michael@0 | 1153 | |
michael@0 | 1154 | done(); |
michael@0 | 1155 | }); |
michael@0 | 1156 | |
michael@0 | 1157 | b2.click(); |
michael@0 | 1158 | }); |
michael@0 | 1159 | |
michael@0 | 1160 | b1.click(); |
michael@0 | 1161 | } |
michael@0 | 1162 | |
michael@0 | 1163 | require('sdk/test').run(exports); |