| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
import { expect } from 'chai'; |
| 11 |
|
| 12 |
import { every } from '@lumino/algorithm'; |
| 13 |
|
| 14 |
import { |
| 15 |
IMessageHandler, |
| 16 |
IMessageHook, |
| 17 |
Message, |
| 18 |
MessageLoop |
| 19 |
} from '@lumino/messaging'; |
| 20 |
|
| 21 |
import { SplitLayout, Widget } from '@lumino/widgets'; |
| 22 |
|
| 23 |
const renderer: SplitLayout.IRenderer = { |
| 24 |
createHandle: () => document.createElement('div') |
| 25 |
}; |
| 26 |
|
| 27 |
class LogSplitLayout extends SplitLayout { |
| 28 |
methods: string[] = []; |
| 29 |
|
| 30 |
protected init(): void { |
| 31 |
super.init(); |
| 32 |
this.methods.push('init'); |
| 33 |
} |
| 34 |
|
| 35 |
protected attachWidget(index: number, widget: Widget): void { |
| 36 |
super.attachWidget(index, widget); |
| 37 |
this.methods.push('attachWidget'); |
| 38 |
} |
| 39 |
|
| 40 |
protected moveWidget( |
| 41 |
fromIndex: number, |
| 42 |
toIndex: number, |
| 43 |
widget: Widget |
| 44 |
): void { |
| 45 |
super.moveWidget(fromIndex, toIndex, widget); |
| 46 |
this.methods.push('moveWidget'); |
| 47 |
} |
| 48 |
|
| 49 |
protected detachWidget(index: number, widget: Widget): void { |
| 50 |
super.detachWidget(index, widget); |
| 51 |
this.methods.push('detachWidget'); |
| 52 |
} |
| 53 |
|
| 54 |
protected onAfterShow(msg: Message): void { |
| 55 |
super.onAfterShow(msg); |
| 56 |
this.methods.push('onAfterShow'); |
| 57 |
} |
| 58 |
|
| 59 |
protected onAfterAttach(msg: Message): void { |
| 60 |
super.onAfterAttach(msg); |
| 61 |
this.methods.push('onAfterAttach'); |
| 62 |
} |
| 63 |
|
| 64 |
protected onChildShown(msg: Widget.ChildMessage): void { |
| 65 |
super.onChildShown(msg); |
| 66 |
this.methods.push('onChildShown'); |
| 67 |
} |
| 68 |
|
| 69 |
protected onChildHidden(msg: Widget.ChildMessage): void { |
| 70 |
super.onChildHidden(msg); |
| 71 |
this.methods.push('onChildHidden'); |
| 72 |
} |
| 73 |
|
| 74 |
protected onResize(msg: Widget.ResizeMessage): void { |
| 75 |
super.onResize(msg); |
| 76 |
this.methods.push('onResize'); |
| 77 |
} |
| 78 |
|
| 79 |
protected onUpdateRequest(msg: Message): void { |
| 80 |
super.onUpdateRequest(msg); |
| 81 |
this.methods.push('onUpdateRequest'); |
| 82 |
} |
| 83 |
|
| 84 |
protected onFitRequest(msg: Message): void { |
| 85 |
super.onFitRequest(msg); |
| 86 |
this.methods.push('onFitRequest'); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
class LogHook implements IMessageHook { |
| 91 |
messages: string[] = []; |
| 92 |
|
| 93 |
messageHook(target: IMessageHandler, msg: Message): boolean { |
| 94 |
this.messages.push(msg.type); |
| 95 |
return true; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
describe('@lumino/widgets', () => { |
| 100 |
describe('SplitLayout', () => { |
| 101 |
describe('#constructor()', () => { |
| 102 |
it('should accept a renderer', () => { |
| 103 |
let layout = new SplitLayout({ renderer }); |
| 104 |
expect(layout).to.be.an.instanceof(SplitLayout); |
| 105 |
}); |
| 106 |
}); |
| 107 |
|
| 108 |
describe('#orientation', () => { |
| 109 |
it('should get the layout orientation for the split layout', () => { |
| 110 |
let layout = new SplitLayout({ renderer }); |
| 111 |
expect(layout.orientation).to.equal('horizontal'); |
| 112 |
}); |
| 113 |
|
| 114 |
it('should set the layout orientation for the split layout', () => { |
| 115 |
let layout = new SplitLayout({ renderer }); |
| 116 |
layout.orientation = 'vertical'; |
| 117 |
expect(layout.orientation).to.equal('vertical'); |
| 118 |
}); |
| 119 |
|
| 120 |
it('should set the orientation attribute of the parent widget', () => { |
| 121 |
let parent = new Widget(); |
| 122 |
let layout = new SplitLayout({ renderer }); |
| 123 |
parent.layout = layout; |
| 124 |
layout.orientation = 'vertical'; |
| 125 |
expect(parent.node.getAttribute('data-orientation')).to.equal( |
| 126 |
'vertical' |
| 127 |
); |
| 128 |
layout.orientation = 'horizontal'; |
| 129 |
expect(parent.node.getAttribute('data-orientation')).to.equal( |
| 130 |
'horizontal' |
| 131 |
); |
| 132 |
}); |
| 133 |
|
| 134 |
it('should post a fit request to the parent widget', () => { |
| 135 |
let layout = new LogSplitLayout({ renderer }); |
| 136 |
let parent = new Widget(); |
| 137 |
parent.layout = layout; |
| 138 |
layout.orientation = 'vertical'; |
| 139 |
MessageLoop.flush(); |
| 140 |
expect(layout.methods).to.contain('onFitRequest', 'unexpected failure'); |
| 141 |
}); |
| 142 |
|
| 143 |
it('should be a no-op if the value does not change', () => { |
| 144 |
let layout = new LogSplitLayout({ renderer }); |
| 145 |
let parent = new Widget(); |
| 146 |
parent.layout = layout; |
| 147 |
layout.orientation = 'horizontal'; |
| 148 |
MessageLoop.flush(); |
| 149 |
expect(layout.methods).to.not.contain('onFitRequest'); |
| 150 |
}); |
| 151 |
}); |
| 152 |
|
| 153 |
describe('#spacing', () => { |
| 154 |
it('should get the inter-element spacing for the split layout', () => { |
| 155 |
let layout = new SplitLayout({ renderer }); |
| 156 |
expect(layout.spacing).to.equal(4); |
| 157 |
}); |
| 158 |
|
| 159 |
it('should set the inter-element spacing for the split layout', () => { |
| 160 |
let layout = new SplitLayout({ renderer }); |
| 161 |
layout.spacing = 10; |
| 162 |
expect(layout.spacing).to.equal(10); |
| 163 |
}); |
| 164 |
|
| 165 |
it('should post a fit request to the parent widget', () => { |
| 166 |
let layout = new LogSplitLayout({ renderer }); |
| 167 |
let parent = new Widget(); |
| 168 |
parent.layout = layout; |
| 169 |
layout.spacing = 10; |
| 170 |
MessageLoop.flush(); |
| 171 |
expect(layout.methods).to.contain('onFitRequest'); |
| 172 |
}); |
| 173 |
|
| 174 |
it('should be a no-op if the value does not change', () => { |
| 175 |
let layout = new LogSplitLayout({ renderer }); |
| 176 |
let parent = new Widget(); |
| 177 |
parent.layout = layout; |
| 178 |
layout.spacing = 4; |
| 179 |
MessageLoop.flush(); |
| 180 |
expect(layout.methods).to.not.contain('onFitRequest'); |
| 181 |
}); |
| 182 |
}); |
| 183 |
|
| 184 |
describe('#renderer', () => { |
| 185 |
it('should get the renderer for the layout', () => { |
| 186 |
let layout = new SplitLayout({ renderer }); |
| 187 |
expect(layout.renderer).to.equal(renderer); |
| 188 |
}); |
| 189 |
}); |
| 190 |
|
| 191 |
describe('#handles', () => { |
| 192 |
it('should be a read-only sequence of the split handles in the layout', () => { |
| 193 |
let layout = new SplitLayout({ renderer }); |
| 194 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 195 |
widgets.forEach(w => { |
| 196 |
layout.addWidget(w); |
| 197 |
}); |
| 198 |
expect(every(layout.handles, h => h instanceof HTMLElement)); |
| 199 |
}); |
| 200 |
}); |
| 201 |
|
| 202 |
describe('#relativeSizes()', () => { |
| 203 |
it('should get the current sizes of the widgets in the layout', () => { |
| 204 |
let layout = new SplitLayout({ renderer }); |
| 205 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 206 |
let parent = new Widget(); |
| 207 |
parent.layout = layout; |
| 208 |
widgets.forEach(w => { |
| 209 |
layout.addWidget(w); |
| 210 |
}); |
| 211 |
let sizes = layout.relativeSizes(); |
| 212 |
expect(sizes).to.deep.equal([1 / 3, 1 / 3, 1 / 3]); |
| 213 |
parent.dispose(); |
| 214 |
}); |
| 215 |
}); |
| 216 |
|
| 217 |
describe('#setRelativeSizes()', () => { |
| 218 |
it('should set the desired sizes for the widgets in the panel', () => { |
| 219 |
let layout = new SplitLayout({ renderer }); |
| 220 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 221 |
let parent = new Widget(); |
| 222 |
parent.layout = layout; |
| 223 |
widgets.forEach(w => { |
| 224 |
layout.addWidget(w); |
| 225 |
}); |
| 226 |
layout.setRelativeSizes([10, 10, 10]); |
| 227 |
let sizes = layout.relativeSizes(); |
| 228 |
expect(sizes).to.deep.equal([10 / 30, 10 / 30, 10 / 30]); |
| 229 |
parent.dispose(); |
| 230 |
}); |
| 231 |
|
| 232 |
it('should ignore extra values', () => { |
| 233 |
let layout = new SplitLayout({ renderer }); |
| 234 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 235 |
let parent = new Widget(); |
| 236 |
parent.layout = layout; |
| 237 |
widgets.forEach(w => { |
| 238 |
layout.addWidget(w); |
| 239 |
}); |
| 240 |
layout.setRelativeSizes([10, 15, 20, 20]); |
| 241 |
let sizes = layout.relativeSizes(); |
| 242 |
expect(sizes).to.deep.equal([10 / 45, 15 / 45, 20 / 45]); |
| 243 |
parent.dispose(); |
| 244 |
}); |
| 245 |
}); |
| 246 |
|
| 247 |
describe('#moveHandle()', () => { |
| 248 |
it('should set the offset position of a split handle', () => { |
| 249 |
let parent = new Widget(); |
| 250 |
let layout = new SplitLayout({ renderer }); |
| 251 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 252 |
widgets.forEach(w => { |
| 253 |
layout.addWidget(w); |
| 254 |
}); |
| 255 |
widgets.forEach(w => { |
| 256 |
w.node.style.minHeight = '100px'; |
| 257 |
}); |
| 258 |
widgets.forEach(w => { |
| 259 |
w.node.style.minWidth = '100px'; |
| 260 |
}); |
| 261 |
parent.layout = layout; |
| 262 |
Widget.attach(parent, document.body); |
| 263 |
MessageLoop.flush(); |
| 264 |
let handle = layout.handles[1]; |
| 265 |
let left = handle.offsetLeft; |
| 266 |
layout.moveHandle(1, left + 20); |
| 267 |
MessageLoop.flush(); |
| 268 |
expect(handle.offsetLeft).to.not.equal(left); |
| 269 |
}); |
| 270 |
}); |
| 271 |
|
| 272 |
describe('#init()', () => { |
| 273 |
it('should set the orientation attribute of the parent widget', () => { |
| 274 |
let parent = new Widget(); |
| 275 |
let layout = new LogSplitLayout({ renderer }); |
| 276 |
parent.layout = layout; |
| 277 |
expect(layout.methods).to.contain('init'); |
| 278 |
expect(parent.node.getAttribute('data-orientation')).to.equal( |
| 279 |
'horizontal' |
| 280 |
); |
| 281 |
}); |
| 282 |
|
| 283 |
it('should attach all widgets to the DOM', () => { |
| 284 |
let parent = new Widget(); |
| 285 |
Widget.attach(parent, document.body); |
| 286 |
let layout = new LogSplitLayout({ renderer }); |
| 287 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 288 |
widgets.forEach(w => { |
| 289 |
layout.addWidget(w); |
| 290 |
}); |
| 291 |
parent.layout = layout; |
| 292 |
expect(every(widgets, w => w.parent === parent)).to.equal(true); |
| 293 |
expect(every(widgets, w => w.isAttached)).to.equal(true); |
| 294 |
parent.dispose(); |
| 295 |
}); |
| 296 |
}); |
| 297 |
|
| 298 |
describe('#attachWidget()', () => { |
| 299 |
it("should attach a widget to the parent's DOM node", () => { |
| 300 |
let layout = new LogSplitLayout({ renderer }); |
| 301 |
let parent = new Widget(); |
| 302 |
parent.layout = layout; |
| 303 |
let widget = new Widget(); |
| 304 |
layout.addWidget(widget); |
| 305 |
expect(layout.methods).to.contain('attachWidget'); |
| 306 |
expect(parent.node.contains(widget.node)).to.equal(true); |
| 307 |
expect(layout.handles.length).to.equal(1); |
| 308 |
}); |
| 309 |
|
| 310 |
it('should send before/after attach messages if the parent is attached', () => { |
| 311 |
let layout = new LogSplitLayout({ renderer }); |
| 312 |
let parent = new Widget(); |
| 313 |
let widget = new Widget(); |
| 314 |
let hook = new LogHook(); |
| 315 |
MessageLoop.installMessageHook(widget, hook); |
| 316 |
parent.layout = layout; |
| 317 |
Widget.attach(parent, document.body); |
| 318 |
layout.addWidget(widget); |
| 319 |
expect(hook.messages).to.contain('before-attach'); |
| 320 |
expect(hook.messages).to.contain('after-attach'); |
| 321 |
}); |
| 322 |
|
| 323 |
it('should post a layout request for the parent widget', () => { |
| 324 |
let layout = new LogSplitLayout({ renderer }); |
| 325 |
let parent = new Widget(); |
| 326 |
parent.layout = layout; |
| 327 |
let widget = new Widget(); |
| 328 |
Widget.attach(parent, document.body); |
| 329 |
layout.addWidget(widget); |
| 330 |
MessageLoop.flush(); |
| 331 |
expect(layout.methods).to.contain('onFitRequest'); |
| 332 |
}); |
| 333 |
}); |
| 334 |
|
| 335 |
describe('#moveWidget()', () => { |
| 336 |
it("should move a widget in the parent's DOM node", () => { |
| 337 |
let layout = new LogSplitLayout({ renderer }); |
| 338 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 339 |
let parent = new Widget(); |
| 340 |
parent.layout = layout; |
| 341 |
widgets.forEach(w => { |
| 342 |
layout.addWidget(w); |
| 343 |
}); |
| 344 |
let widget = widgets[0]; |
| 345 |
let handle = layout.handles[0]; |
| 346 |
layout.insertWidget(2, widget); |
| 347 |
expect(layout.methods).to.contain('moveWidget'); |
| 348 |
expect(layout.handles[2]).to.equal(handle); |
| 349 |
expect(layout.widgets[2]).to.equal(widget); |
| 350 |
}); |
| 351 |
|
| 352 |
it('should post a a layout request to the parent', () => { |
| 353 |
let layout = new LogSplitLayout({ renderer }); |
| 354 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 355 |
let parent = new Widget(); |
| 356 |
parent.layout = layout; |
| 357 |
widgets.forEach(w => { |
| 358 |
layout.addWidget(w); |
| 359 |
}); |
| 360 |
let widget = widgets[0]; |
| 361 |
layout.insertWidget(2, widget); |
| 362 |
MessageLoop.flush(); |
| 363 |
expect(layout.methods).to.contain('onFitRequest'); |
| 364 |
}); |
| 365 |
}); |
| 366 |
|
| 367 |
describe('#detachWidget()', () => { |
| 368 |
it("should detach a widget from the parent's DOM node", () => { |
| 369 |
let layout = new LogSplitLayout({ renderer }); |
| 370 |
let widget = new Widget(); |
| 371 |
let parent = new Widget(); |
| 372 |
parent.layout = layout; |
| 373 |
layout.addWidget(widget); |
| 374 |
layout.removeWidget(widget); |
| 375 |
expect(layout.methods).to.contain('detachWidget'); |
| 376 |
expect(parent.node.contains(widget.node)).to.equal(false); |
| 377 |
parent.dispose(); |
| 378 |
}); |
| 379 |
|
| 380 |
it('should send before/after detach message if the parent is attached', () => { |
| 381 |
let layout = new LogSplitLayout({ renderer }); |
| 382 |
let parent = new Widget(); |
| 383 |
let widget = new Widget(); |
| 384 |
let hook = new LogHook(); |
| 385 |
MessageLoop.installMessageHook(widget, hook); |
| 386 |
parent.layout = layout; |
| 387 |
layout.addWidget(widget); |
| 388 |
Widget.attach(parent, document.body); |
| 389 |
layout.removeWidget(widget); |
| 390 |
expect(layout.methods).to.contain('detachWidget'); |
| 391 |
expect(hook.messages).to.contain('before-detach'); |
| 392 |
expect(hook.messages).to.contain('after-detach'); |
| 393 |
parent.dispose(); |
| 394 |
}); |
| 395 |
|
| 396 |
it('should post a a layout request to the parent', () => { |
| 397 |
let layout = new LogSplitLayout({ renderer }); |
| 398 |
let widget = new Widget(); |
| 399 |
let parent = new Widget(); |
| 400 |
parent.layout = layout; |
| 401 |
layout.addWidget(widget); |
| 402 |
Widget.attach(parent, document.body); |
| 403 |
layout.removeWidget(widget); |
| 404 |
MessageLoop.flush(); |
| 405 |
expect(layout.methods).to.contain('onFitRequest'); |
| 406 |
parent.dispose(); |
| 407 |
}); |
| 408 |
}); |
| 409 |
|
| 410 |
describe('#onAfterShow()', () => { |
| 411 |
it('should post an update to the parent', () => { |
| 412 |
let layout = new LogSplitLayout({ renderer }); |
| 413 |
let parent = new Widget(); |
| 414 |
parent.layout = layout; |
| 415 |
parent.hide(); |
| 416 |
Widget.attach(parent, document.body); |
| 417 |
parent.show(); |
| 418 |
MessageLoop.flush(); |
| 419 |
expect(layout.methods).to.contain('onAfterShow'); |
| 420 |
expect(layout.methods).to.contain('onUpdateRequest'); |
| 421 |
parent.dispose(); |
| 422 |
}); |
| 423 |
}); |
| 424 |
|
| 425 |
describe('#onAfterAttach()', () => { |
| 426 |
it('should post a layout request to the parent', () => { |
| 427 |
let layout = new LogSplitLayout({ renderer }); |
| 428 |
let parent = new Widget(); |
| 429 |
parent.layout = layout; |
| 430 |
Widget.attach(parent, document.body); |
| 431 |
MessageLoop.flush(); |
| 432 |
expect(layout.methods).to.contain('onAfterAttach'); |
| 433 |
expect(layout.methods).to.contain('onFitRequest'); |
| 434 |
parent.dispose(); |
| 435 |
}); |
| 436 |
}); |
| 437 |
|
| 438 |
describe('#onChildShown()', () => { |
| 439 |
it('should post a fit request to the parent', () => { |
| 440 |
let parent = new Widget(); |
| 441 |
let layout = new LogSplitLayout({ renderer }); |
| 442 |
parent.layout = layout; |
| 443 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 444 |
widgets[0].hide(); |
| 445 |
widgets.forEach(w => { |
| 446 |
layout.addWidget(w); |
| 447 |
}); |
| 448 |
Widget.attach(parent, document.body); |
| 449 |
widgets[0].show(); |
| 450 |
MessageLoop.flush(); |
| 451 |
expect(layout.methods).to.contain('onChildShown'); |
| 452 |
expect(layout.methods).to.contain('onFitRequest'); |
| 453 |
parent.dispose(); |
| 454 |
}); |
| 455 |
}); |
| 456 |
|
| 457 |
describe('#onChildHidden()', () => { |
| 458 |
it('should post a fit request to the parent', () => { |
| 459 |
let parent = new Widget(); |
| 460 |
let layout = new LogSplitLayout({ renderer }); |
| 461 |
parent.layout = layout; |
| 462 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 463 |
widgets.forEach(w => { |
| 464 |
layout.addWidget(w); |
| 465 |
}); |
| 466 |
Widget.attach(parent, document.body); |
| 467 |
widgets[0].hide(); |
| 468 |
MessageLoop.flush(); |
| 469 |
expect(layout.methods).to.contain('onChildHidden'); |
| 470 |
expect(layout.methods).to.contain('onFitRequest'); |
| 471 |
parent.dispose(); |
| 472 |
}); |
| 473 |
}); |
| 474 |
|
| 475 |
describe('#onResize', () => { |
| 476 |
it('should be called when a resize event is sent to the parent', () => { |
| 477 |
let parent = new Widget(); |
| 478 |
let layout = new LogSplitLayout({ renderer }); |
| 479 |
parent.layout = layout; |
| 480 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 481 |
widgets.forEach(w => { |
| 482 |
layout.addWidget(w); |
| 483 |
}); |
| 484 |
Widget.attach(parent, document.body); |
| 485 |
MessageLoop.sendMessage(parent, Widget.ResizeMessage.UnknownSize); |
| 486 |
expect(layout.methods).to.contain('onResize'); |
| 487 |
parent.dispose(); |
| 488 |
}); |
| 489 |
}); |
| 490 |
|
| 491 |
describe('.getStretch()', () => { |
| 492 |
it('should get the split layout stretch factor for the given widget', () => { |
| 493 |
let widget = new Widget(); |
| 494 |
expect(SplitLayout.getStretch(widget)).to.equal(0); |
| 495 |
}); |
| 496 |
}); |
| 497 |
|
| 498 |
describe('.setStretch()', () => { |
| 499 |
it('should set the split layout stretch factor for the given widget', () => { |
| 500 |
let widget = new Widget(); |
| 501 |
SplitLayout.setStretch(widget, 10); |
| 502 |
expect(SplitLayout.getStretch(widget)).to.equal(10); |
| 503 |
}); |
| 504 |
|
| 505 |
it('should post a fit request to the parent', () => { |
| 506 |
let parent = new Widget(); |
| 507 |
let widget = new Widget(); |
| 508 |
let layout = new LogSplitLayout({ renderer }); |
| 509 |
parent.layout = layout; |
| 510 |
layout.addWidget(widget); |
| 511 |
SplitLayout.setStretch(widget, 10); |
| 512 |
MessageLoop.flush(); |
| 513 |
expect(layout.methods).to.contain('onFitRequest'); |
| 514 |
}); |
| 515 |
}); |
| 516 |
}); |
| 517 |
}); |