editorStacksModel.test.ts 31.0 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import * as assert from 'assert';
9
import {EditorStacksModel, IEditorStacksModel, IEditorGroup, setAllowEmptyGroups, setOpenEditorDirection, Direction} from 'vs/workbench/common/editor/editorStacksModel';
10
import {EditorInput} from 'vs/workbench/common/editor';
11
import {TestStorageService, TestLifecycleService} from 'vs/workbench/test/common/servicesTestUtils';
12 13 14
import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService';
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
import {IStorageService} from 'vs/platform/storage/common/storage';
15 16 17 18
import {ILifecycleService} from 'vs/platform/lifecycle/common/lifecycle';
import {IEditorRegistry, Extensions as EditorExtensions, IEditorInputFactory} from 'vs/workbench/browser/parts/editor/baseEditor';
import {Registry} from 'vs/platform/platform';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
B
Benjamin Pasero 已提交
19 20

function create(): IEditorStacksModel {
21 22
	let services = new ServiceCollection();
	services.set(IStorageService, new TestStorageService());
23
	services.set(ILifecycleService, new TestLifecycleService());
24 25 26 27

	let inst = new InstantiationService(services);

	return inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
28 29
}

30
interface ModelEvents {
B
Benjamin Pasero 已提交
31 32 33 34 35
	opened: IEditorGroup[];
	activated: IEditorGroup[];
	closed: IEditorGroup[];
}

36 37 38 39 40 41 42 43 44 45
interface GroupEvents {
	opened: EditorInput[];
	activated: EditorInput[];
	closed: EditorInput[];
	pinned: EditorInput[];
	unpinned: EditorInput[];
}

function modelListener(model: IEditorStacksModel): ModelEvents {
	const modelEvents = {
B
Benjamin Pasero 已提交
46 47 48 49 50
		opened: [],
		activated: [],
		closed: []
	};

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	model.onGroupOpened(g => modelEvents.opened.push(g));
	model.onGroupActivated(g => modelEvents.activated.push(g));
	model.onGroupClosed(g => modelEvents.closed.push(g));

	return modelEvents;
}

function groupListener(group: IEditorGroup): GroupEvents {
	const groupEvents = {
		opened: [],
		closed: [],
		activated: [],
		pinned: [],
		unpinned: []
	};

	group.onEditorOpened(e => groupEvents.opened.push(e));
	group.onEditorClosed(e => groupEvents.closed.push(e));
	group.onEditorActivated(e => groupEvents.activated.push(e));
	group.onEditorPinned(e => groupEvents.pinned.push(e));
	group.onEditorUnpinned(e => groupEvents.unpinned.push(e));
B
Benjamin Pasero 已提交
72 73 74 75

	return groupEvents;
}

B
Benjamin Pasero 已提交
76
let index = 0;
77
class TestEditorInput extends EditorInput {
78 79 80
	constructor(public id: string) {
		super();
	}
81
	public getId() { return 'testEditorInput'; }
B
Benjamin Pasero 已提交
82
	public resolve() { return null; }
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

	public matches(other: TestEditorInput): boolean {
		return this.id === other.id;
	}
}

class NonSerializableTestEditorInput extends EditorInput {
	constructor(public id: string) {
		super();
	}
	public getId() { return 'testEditorInput-nonSerializable'; }
	public resolve() { return null; }
}

function input(id = String(index++), nonSerializable?: boolean): EditorInput {
	return nonSerializable ? new NonSerializableTestEditorInput(id) : new TestEditorInput(id);
99 100
}

101 102
interface ISerializedTestInput {
	id: string;
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
class TestEditorInputFactory implements IEditorInputFactory {

	constructor() { }

	public serialize(editorInput: EditorInput): string {
		let testEditorInput = <TestEditorInput>editorInput;
		let testInput: ISerializedTestInput = {
			id: testEditorInput.id
		};

		return JSON.stringify(testInput);
	}

	public deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): EditorInput {
		let testInput: ISerializedTestInput = JSON.parse(serializedEditorInput);

		return new TestEditorInput(testInput.id);
	}
}

(<IEditorRegistry>Registry.as(EditorExtensions.Editors)).registerEditorInputFactory('testEditorInput', TestEditorInputFactory);

B
Benjamin Pasero 已提交
127 128
suite('Editor Stacks Model', () => {

129
	teardown(() => {
130 131
		index = 1;
		setOpenEditorDirection(Direction.RIGHT);
132
		setAllowEmptyGroups(false);
B
Benjamin Pasero 已提交
133 134
	});

B
Benjamin Pasero 已提交
135 136
	test('Groups', function () {
		const model = create();
137
		const events = modelListener(model);
B
Benjamin Pasero 已提交
138 139 140 141

		assert.equal(model.groups.length, 0);
		assert.ok(!model.activeGroup);

B
Benjamin Pasero 已提交
142
		const first = model.openGroup('first');
B
Benjamin Pasero 已提交
143 144 145 146
		assert.equal(events.opened[0], first);
		assert.equal(events.activated[0], first);
		assert.equal(model.activeGroup, first);
		assert.equal(model.groups.length, 1);
B
Benjamin Pasero 已提交
147
		assert.equal(model.groups[0], first);
B
Benjamin Pasero 已提交
148 149 150 151 152 153

		const second = model.openGroup('second');
		assert.equal(events.opened[1], second);
		assert.equal(events.activated[1], second);
		assert.equal(model.activeGroup, second);
		assert.equal(model.groups.length, 2);
B
Benjamin Pasero 已提交
154
		assert.equal(model.groups[1], second);
B
Benjamin Pasero 已提交
155 156 157 158 159 160

		const third = model.openGroup('third');
		assert.equal(events.opened[2], third);
		assert.equal(events.activated[2], third);
		assert.equal(model.activeGroup, third);
		assert.equal(model.groups.length, 3);
B
Benjamin Pasero 已提交
161
		assert.equal(model.groups[2], third);
B
Benjamin Pasero 已提交
162 163 164 165 166

		model.closeGroup(first);
		assert.equal(events.closed[0], first);
		assert.equal(model.groups.length, 2);
		assert.equal(model.activeGroup, third);
B
Benjamin Pasero 已提交
167 168
		assert.equal(model.groups[0], second);
		assert.equal(model.groups[1], third);
B
Benjamin Pasero 已提交
169 170 171 172 173 174

		model.closeGroup(third);
		assert.equal(events.closed[1], third);
		assert.equal(events.activated[3], second);
		assert.equal(model.activeGroup, second);
		assert.equal(model.groups.length, 1);
B
Benjamin Pasero 已提交
175
		assert.equal(model.groups[0], second);
B
Benjamin Pasero 已提交
176 177 178 179 180

		const fourth = model.openGroup('fourth');
		assert.equal(fourth, model.activeGroup);
		model.closeGroup(fourth);
		assert.equal(second, model.activeGroup);
181 182 183 184 185 186 187 188 189 190 191 192 193 194

		model.closeGroup(second);
		assert.equal(model.groups.length, 0);

		model.openGroup('first');
		model.openGroup('second');
		model.openGroup('third');
		model.openGroup('fourth');

		assert.equal(model.groups.length, 4);

		model.closeAllGroups();

		assert.equal(model.groups.length, 0);
B
Benjamin Pasero 已提交
195
	});
196 197 198

	test('Stack - One Editor', function () {
		const model = create();
B
Benjamin Pasero 已提交
199
		const group = model.openGroup('group');
200 201
		const events = groupListener(group);

202
		assert.equal(group.count, 0);
203 204 205 206 207 208
		assert.equal(group.getEditors(true).length, 0);

		// Active && Pinned
		const input1 = input();
		group.openEditor(input1, { active: true, pinned: true });

209
		assert.equal(group.count, 1);
210 211 212 213 214 215 216 217 218 219
		assert.equal(group.getEditors(true).length, 1);
		assert.equal(group.activeEditor, input1);
		assert.equal(group.isActive(input1), true);
		assert.equal(group.isPreview(input1), false);
		assert.equal(group.isPinned(input1), true);

		assert.equal(events.opened[0], input1);
		assert.equal(events.activated[0], input1);

		group.closeEditor(input1);
220
		assert.equal(group.count, 0);
221 222 223 224 225 226 227 228
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[0], input1);

		// Active && Preview
		const input2 = input();
		group.openEditor(input2, { active: true, pinned: false });

229
		assert.equal(group.count, 1);
230 231 232 233 234 235 236 237 238 239
		assert.equal(group.getEditors(true).length, 1);
		assert.equal(group.activeEditor, input2);
		assert.equal(group.isActive(input2), true);
		assert.equal(group.isPreview(input2), true);
		assert.equal(group.isPinned(input2), false);

		assert.equal(events.opened[1], input2);
		assert.equal(events.activated[1], input2);

		group.closeEditor(input2);
240
		assert.equal(group.count, 0);
241 242 243 244 245
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[1], input2);

		group.closeEditor(input2);
246
		assert.equal(group.count, 0);
247 248 249 250 251 252 253 254
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[1], input2);

		// Nonactive && Pinned => gets active because its first editor
		const input3 = input();
		group.openEditor(input3, { active: false, pinned: true });

255
		assert.equal(group.count, 1);
256 257 258 259 260 261 262 263 264 265
		assert.equal(group.getEditors(true).length, 1);
		assert.equal(group.activeEditor, input3);
		assert.equal(group.isActive(input3), true);
		assert.equal(group.isPreview(input3), false);
		assert.equal(group.isPinned(input3), true);

		assert.equal(events.opened[2], input3);
		assert.equal(events.activated[2], input3);

		group.closeEditor(input3);
266
		assert.equal(group.count, 0);
267 268 269 270 271 272 273 274
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[2], input3);

		assert.equal(events.opened[2], input3);
		assert.equal(events.activated[2], input3);

		group.closeEditor(input3);
275
		assert.equal(group.count, 0);
276 277 278 279 280 281 282 283
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[2], input3);

		// Nonactive && Preview => gets active because its first editor
		const input4 = input();
		group.openEditor(input4);

284
		assert.equal(group.count, 1);
285 286 287 288 289 290 291 292 293 294
		assert.equal(group.getEditors(true).length, 1);
		assert.equal(group.activeEditor, input4);
		assert.equal(group.isActive(input4), true);
		assert.equal(group.isPreview(input4), true);
		assert.equal(group.isPinned(input4), false);

		assert.equal(events.opened[3], input4);
		assert.equal(events.activated[3], input4);

		group.closeEditor(input4);
295
		assert.equal(group.count, 0);
296 297 298 299 300
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[3], input4);
	});

B
Benjamin Pasero 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314
	test('Stack - Multiple Editors - Pinned and Active', function () {
		const model = create();
		const group = model.openGroup('group');
		const events = groupListener(group);

		const input1 = input();
		const input2 = input();
		const input3 = input();

		// Pinned and Active
		group.openEditor(input1, { pinned: true, active: true });
		group.openEditor(input2, { pinned: true, active: true });
		group.openEditor(input3, { pinned: true, active: true });

315
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
		assert.equal(group.getEditors(true).length, 3);
		assert.equal(group.activeEditor, input3);
		assert.equal(group.isActive(input1), false);
		assert.equal(group.isPinned(input1), true);
		assert.equal(group.isPreview(input1), false);
		assert.equal(group.isActive(input2), false);
		assert.equal(group.isPinned(input2), true);
		assert.equal(group.isPreview(input2), false);
		assert.equal(group.isActive(input3), true);
		assert.equal(group.isPinned(input3), true);
		assert.equal(group.isPreview(input3), false);

		assert.equal(events.opened[0], input1);
		assert.equal(events.opened[1], input2);
		assert.equal(events.opened[2], input3);

		const mru = group.getEditors(true);
		assert.equal(mru[0], input3);
		assert.equal(mru[1], input2);
		assert.equal(mru[2], input1);
336 337 338 339 340

		group.closeAllEditors();

		assert.equal(events.closed.length, 3);
		assert.equal(group.count, 0);
B
Benjamin Pasero 已提交
341 342
	});

343 344 345 346 347
	test('Stack - Multiple Editors - Pinned and Active (DEFAULT_OPEN_EDITOR_DIRECTION = Direction.LEFT)', function () {
		setOpenEditorDirection(Direction.LEFT);

		const model = create();
		const group = model.openGroup('group');
348
		const events = groupListener(group);
349 350 351 352 353 354 355 356 357 358 359 360 361

		const input1 = input();
		const input2 = input();
		const input3 = input();

		// Pinned and Active
		group.openEditor(input1, { pinned: true, active: true });
		group.openEditor(input2, { pinned: true, active: true });
		group.openEditor(input3, { pinned: true, active: true });

		assert.equal(group.getEditors()[0], input3);
		assert.equal(group.getEditors()[1], input2);
		assert.equal(group.getEditors()[2], input1);
362 363 364 365 366

		model.closeAllGroups();

		assert.equal(events.closed.length, 3);
		assert.equal(group.count, 0);
367 368
	});

B
Benjamin Pasero 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381
	test('Stack - Multiple Editors - Pinned and Not Active', function () {
		const model = create();
		const group = model.openGroup('group');

		const input1 = input();
		const input2 = input();
		const input3 = input();

		// Pinned and Active
		group.openEditor(input1, { pinned: true });
		group.openEditor(input2, { pinned: true });
		group.openEditor(input3, { pinned: true });

382
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
		assert.equal(group.getEditors(true).length, 3);
		assert.equal(group.activeEditor, input1);
		assert.equal(group.isActive(input1), true);
		assert.equal(group.isPinned(input1), true);
		assert.equal(group.isPreview(input1), false);
		assert.equal(group.isActive(input2), false);
		assert.equal(group.isPinned(input2), true);
		assert.equal(group.isPreview(input2), false);
		assert.equal(group.isActive(input3), false);
		assert.equal(group.isPinned(input3), true);
		assert.equal(group.isPreview(input3), false);

		const mru = group.getEditors(true);
		assert.equal(mru[0], input1);
		assert.equal(mru[1], input2);
		assert.equal(mru[2], input3);
	});

401 402
	test('Stack - Multiple Editors - Preview gets overwritten', function () {
		const model = create();
B
Benjamin Pasero 已提交
403
		const group = model.openGroup('group');
404 405 406 407 408 409 410 411 412 413 414
		const events = groupListener(group);

		const input1 = input();
		const input2 = input();
		const input3 = input();

		// Non active, preview
		group.openEditor(input1); // becomes active, preview
		group.openEditor(input2); // overwrites preview
		group.openEditor(input3); // overwrites preview

415
		assert.equal(group.count, 1);
416 417 418 419 420 421 422 423 424 425 426
		assert.equal(group.getEditors(true).length, 1);
		assert.equal(group.activeEditor, input3);
		assert.equal(group.isActive(input3), true);
		assert.equal(group.isPinned(input3), false);
		assert.equal(group.isPreview(input3), true);

		assert.equal(events.opened[0], input1);
		assert.equal(events.opened[1], input2);
		assert.equal(events.opened[2], input3);
		assert.equal(events.closed[0], input1);
		assert.equal(events.closed[1], input2);
B
Benjamin Pasero 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

		const mru = group.getEditors(true);
		assert.equal(mru[0], input3);
		assert.equal(mru.length, 1);
	});

	test('Stack - Multiple Editors - set active', function () {
		const model = create();
		const group = model.openGroup('group');
		const events = groupListener(group);

		const input1 = input();
		const input2 = input();
		const input3 = input();

		group.openEditor(input1, { pinned: true, active: true });
		group.openEditor(input2, { pinned: true, active: true });
		group.openEditor(input3, { pinned: false, active: true });

		assert.equal(group.activeEditor, input3);

		let mru = group.getEditors(true);
		assert.equal(mru[0], input3);
		assert.equal(mru[1], input2);
		assert.equal(mru[2], input1);

		group.setActive(input3);
		assert.equal(events.activated.length, 3);

		group.setActive(input1);
		assert.equal(events.activated[3], input1);
		assert.equal(group.activeEditor, input1);
		assert.equal(group.isActive(input1), true);
		assert.equal(group.isActive(input2), false);
		assert.equal(group.isActive(input3), false);

		mru = group.getEditors(true);
		assert.equal(mru[0], input1);
		assert.equal(mru[1], input3);
		assert.equal(mru[2], input2);
467
	});
B
Benjamin Pasero 已提交
468

B
Benjamin Pasero 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482
	test('Stack - Multiple Editors - pin and unpin', function () {
		const model = create();
		const group = model.openGroup('group');
		const events = groupListener(group);

		const input1 = input();
		const input2 = input();
		const input3 = input();

		group.openEditor(input1, { pinned: true, active: true });
		group.openEditor(input2, { pinned: true, active: true });
		group.openEditor(input3, { pinned: false, active: true });

		assert.equal(group.activeEditor, input3);
483
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
484 485 486 487 488 489 490 491

		group.pin(input3);

		assert.equal(group.activeEditor, input3);
		assert.equal(group.isPinned(input3), true);
		assert.equal(group.isPreview(input3), false);
		assert.equal(group.isActive(input3), true);
		assert.equal(events.pinned[0], input3);
492
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
493 494 495 496 497 498 499 500

		group.unpin(input1);

		assert.equal(group.activeEditor, input3);
		assert.equal(group.isPinned(input1), false);
		assert.equal(group.isPreview(input1), true);
		assert.equal(group.isActive(input1), false);
		assert.equal(events.unpinned[0], input1);
501
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
502 503 504 505

		group.unpin(input2);

		assert.equal(group.activeEditor, input3);
506
		assert.equal(group.count, 2); // 2 previews got merged into one
B
Benjamin Pasero 已提交
507 508 509
		assert.equal(group.getEditors()[0], input2);
		assert.equal(group.getEditors()[1], input3);
		assert.equal(events.closed[0], input1);
510
		assert.equal(group.count, 2);
B
Benjamin Pasero 已提交
511 512 513 514

		group.unpin(input3);

		assert.equal(group.activeEditor, input3);
515
		assert.equal(group.count, 1); // pinning replaced the preview
B
Benjamin Pasero 已提交
516 517
		assert.equal(group.getEditors()[0], input3);
		assert.equal(events.closed[1], input2);
518
		assert.equal(group.count, 1);
B
Benjamin Pasero 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	});

	test('Stack - Multiple Editors - closing picks next from MRU list', function () {
		const model = create();
		const group = model.openGroup('group');
		const events = groupListener(group);

		const input1 = input();
		const input2 = input();
		const input3 = input();
		const input4 = input();
		const input5 = input();

		group.openEditor(input1, { pinned: true, active: true });
		group.openEditor(input2, { pinned: true, active: true });
		group.openEditor(input3, { pinned: true, active: true });
		group.openEditor(input4, { pinned: true, active: true });
		group.openEditor(input5, { pinned: true, active: true });

		assert.equal(group.activeEditor, input5);
		assert.equal(group.getEditors(true)[0], input5);
540
		assert.equal(group.count, 5);
B
Benjamin Pasero 已提交
541 542 543 544

		group.closeEditor(input5);
		assert.equal(group.activeEditor, input4);
		assert.equal(events.activated[5], input4);
545
		assert.equal(group.count, 4);
B
Benjamin Pasero 已提交
546 547 548 549 550 551

		group.setActive(input1);
		group.setActive(input4);
		group.closeEditor(input4);

		assert.equal(group.activeEditor, input1);
552
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
553 554 555 556

		group.closeEditor(input1);

		assert.equal(group.activeEditor, input3);
557
		assert.equal(group.count, 2);
B
Benjamin Pasero 已提交
558 559 560 561 562

		group.setActive(input2);
		group.closeEditor(input2);

		assert.equal(group.activeEditor, input3);
563
		assert.equal(group.count, 1);
B
Benjamin Pasero 已提交
564 565 566 567

		group.closeEditor(input3);

		assert.ok(!group.activeEditor);
568
		assert.equal(group.count, 0);
B
Benjamin Pasero 已提交
569 570
	});

B
Benjamin Pasero 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
	test('Stack - Multiple Editors - Pinned & Non Active', function () {
		const model = create();
		const group = model.openGroup('group');


		const input1 = input();
		group.openEditor(input1);
		assert.equal(group.activeEditor, input1);
		assert.equal(group.previewEditor, input1);
		assert.equal(group.getEditors()[0], input1);
		assert.equal(group.count, 1);

		const input2 = input();
		group.openEditor(input2, { pinned: true, active: false });
		assert.equal(group.activeEditor, input1);
		assert.equal(group.previewEditor, input1);
		assert.equal(group.getEditors()[0], input1);
		assert.equal(group.getEditors()[1], input2);
		assert.equal(group.count, 2);

		const input3 = input();
		group.openEditor(input3, { pinned: true, active: false });
		assert.equal(group.activeEditor, input1);
		assert.equal(group.previewEditor, input1);
		assert.equal(group.getEditors()[0], input1);
		assert.equal(group.getEditors()[1], input3);
		assert.equal(group.getEditors()[2], input2);
		assert.equal(group.isPinned(input1), false);
		assert.equal(group.isPinned(input2), true);
		assert.equal(group.isPinned(input3), true);
		assert.equal(group.count, 3);
	});
B
Benjamin Pasero 已提交
603

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	test('Stack - Multiple Editors - real user example', function () {
		const model = create();
		const group = model.openGroup('group');

		// [] -> /index.html/
		const indexHtml = input('index.html');
		group.openEditor(indexHtml);
		assert.equal(group.activeEditor, indexHtml);
		assert.equal(group.previewEditor, indexHtml);
		assert.equal(group.getEditors()[0], indexHtml);
		assert.equal(group.count, 1);

		// /index.html/ -> /style.css/
		const styleCss = input('style.css');
		group.openEditor(styleCss);
		assert.equal(group.activeEditor, styleCss);
		assert.equal(group.previewEditor, styleCss);
		assert.equal(group.getEditors()[0], styleCss);
		assert.equal(group.count, 1);

		// /style.css/ -> [/style.css/, test.js]
		const testJs = input('test.js');
		group.openEditor(testJs, { active: true, pinned: true });
		assert.equal(group.previewEditor, styleCss);
		assert.equal(group.activeEditor, testJs);
		assert.equal(group.isPreview(styleCss), true);
		assert.equal(group.isPinned(testJs), true);
		assert.equal(group.getEditors()[0], styleCss);
		assert.equal(group.getEditors()[1], testJs);
		assert.equal(group.count, 2);

		// [/style.css/, test.js] -> [/indexHtml/, test.js]
		group.openEditor(indexHtml, { active: true });
		assert.equal(group.activeEditor, indexHtml);
		assert.equal(group.previewEditor, indexHtml);
		assert.equal(group.isPreview(indexHtml), true);
		assert.equal(group.isPinned(testJs), true);
		assert.equal(group.getEditors()[0], indexHtml);
		assert.equal(group.getEditors()[1], testJs);
		assert.equal(group.count, 2);

		// make test.js active
		group.setActive(testJs);
		assert.equal(group.activeEditor, testJs);
		assert.equal(group.isActive(testJs), true);
		assert.equal(group.count, 2);

		// [/indexHtml/, test.js] -> [indexHtml, test.js]
		group.pin(indexHtml);
		assert.equal(group.isPinned(indexHtml), true);
		assert.equal(group.isPreview(indexHtml), false);
		assert.equal(group.activeEditor, testJs);

		// [indexHtml, test.js] -> [indexHtml, test.js, file.ts]
		const fileTs = input('file.ts');
		group.openEditor(fileTs, { active: true, pinned: true });
		assert.equal(group.isPinned(fileTs), true);
		assert.equal(group.isPreview(fileTs), false);
		assert.equal(group.count, 3);
		assert.equal(group.activeEditor, fileTs);

		// [indexHtml, test.js, file.ts] -> [indexHtml, test.js, /file.ts/]
		group.unpin(fileTs);
		assert.equal(group.count, 3);
		assert.equal(group.isPinned(fileTs), false);
		assert.equal(group.isPreview(fileTs), true);
		assert.equal(group.activeEditor, fileTs);

		// [indexHtml, test.js, /file.ts/] -> [indexHtml, test.js, /other.ts/]
		const otherTs = input('other.ts');
		group.openEditor(otherTs, { active: true });
		assert.equal(group.count, 3);
		assert.equal(group.activeEditor, otherTs);
		assert.equal(group.getEditors()[0], indexHtml);
		assert.equal(group.getEditors()[1], testJs);
		assert.equal(group.getEditors()[2], otherTs);

		// make index.html active
		group.setActive(indexHtml);
		assert.equal(group.activeEditor, indexHtml);

		// [indexHtml, test.js, /file.ts/] -> [test.js, /other.ts/]
		group.closeEditor(indexHtml);
		assert.equal(group.count, 2);
		assert.equal(group.activeEditor, otherTs);
		assert.equal(group.getEditors()[0], testJs);
		assert.equal(group.getEditors()[1], otherTs);

		// [test.js, /other.ts/] -> [test.js]
		group.closeEditor(otherTs);
		assert.equal(group.count, 1);
		assert.equal(group.activeEditor, testJs);
		assert.equal(group.getEditors()[0], testJs);

		// [test.js] -> /test.js/
		group.unpin(testJs);
		assert.equal(group.count, 1);
		assert.equal(group.activeEditor, testJs);
		assert.equal(group.getEditors()[0], testJs);
		assert.equal(group.isPinned(testJs), false);
		assert.equal(group.isPreview(testJs), true);

		// /test.js/ -> []
		group.closeEditor(testJs);
		assert.equal(group.count, 0);
		assert.equal(group.activeEditor, null);
		assert.equal(group.previewEditor, null);
	});
712 713 714 715 716 717 718 719 720 721 722 723

	test('Stack - Single Group, Single Editor - persist', function () {
		let services = new ServiceCollection();

		services.set(IStorageService, new TestStorageService());
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);

		let inst = new InstantiationService(services);

		(<IEditorRegistry>Registry.as(EditorExtensions.Editors)).setInstantiationService(inst);

B
Benjamin Pasero 已提交
724 725
		let model: IEditorStacksModel = inst.createInstance(EditorStacksModel);
		let group = model.openGroup('group');
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

		const input1 = input();
		group.openEditor(input1);

		assert.equal(model.groups.length, 1);
		assert.equal(group.count, 1);
		assert.equal(group.activeEditor.matches(input1), true);
		assert.equal(group.previewEditor.matches(input1), true);
		assert.equal(group.label, 'group');
		assert.equal(group.isActive(input1), true);

		lifecycle.fireShutdown();

		// Create model again - should load from storage
		model = inst.createInstance(EditorStacksModel);

		assert.equal(model.groups.length, 1);
B
Benjamin Pasero 已提交
743 744 745

		group = model.activeGroup;

746 747 748 749 750 751
		assert.equal(group.count, 1);
		assert.equal(group.activeEditor.matches(input1), true);
		assert.equal(group.previewEditor.matches(input1), true);
		assert.equal(group.label, 'group');
		assert.equal(group.isActive(input1), true);
	});
B
Benjamin Pasero 已提交
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913

	test('Stack - Multiple Groups, Multiple editors - persist', function () {
		let services = new ServiceCollection();

		services.set(IStorageService, new TestStorageService());
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);

		let inst = new InstantiationService(services);

		(<IEditorRegistry>Registry.as(EditorExtensions.Editors)).setInstantiationService(inst);

		let model: IEditorStacksModel = inst.createInstance(EditorStacksModel);

		let group1 = model.openGroup('group1');

		const g1_input1 = input();
		const g1_input2 = input();
		const g1_input3 = input();

		group1.openEditor(g1_input1, { active: true, pinned: true });
		group1.openEditor(g1_input2, { active: true, pinned: false });
		group1.openEditor(g1_input3, { active: false, pinned: true });

		let group2 = model.openGroup('group2');

		const g2_input1 = input();
		const g2_input2 = input();
		const g2_input3 = input();

		group2.openEditor(g2_input1, { active: true, pinned: true });
		group2.openEditor(g2_input2, { active: false, pinned: false });
		group2.openEditor(g2_input3, { active: false, pinned: true });

		assert.equal(model.groups.length, 2);
		assert.equal(group1.count, 3);
		assert.equal(group2.count, 3);
		assert.equal(group1.activeEditor.matches(g1_input2), true);
		assert.equal(group2.activeEditor.matches(g2_input1), true);
		assert.equal(group1.previewEditor.matches(g1_input2), true);
		assert.equal(group2.previewEditor.matches(g2_input2), true);
		assert.equal(group1.label, 'group1');
		assert.equal(group2.label, 'group2');

		assert.equal(group1.getEditors(true)[0].matches(g1_input2), true);
		assert.equal(group1.getEditors(true)[1].matches(g1_input1), true);
		assert.equal(group1.getEditors(true)[2].matches(g1_input3), true);

		assert.equal(group2.getEditors(true)[0].matches(g2_input1), true);
		assert.equal(group2.getEditors(true)[1].matches(g2_input2), true);
		assert.equal(group2.getEditors(true)[2].matches(g2_input3), true);

		lifecycle.fireShutdown();

		// Create model again - should load from storage
		model = inst.createInstance(EditorStacksModel);

		group1 = model.groups[0];
		group2 = model.groups[1];

		assert.equal(model.groups.length, 2);
		assert.equal(group1.count, 3);
		assert.equal(group2.count, 3);
		assert.equal(group1.activeEditor.matches(g1_input2), true);
		assert.equal(group2.activeEditor.matches(g2_input1), true);
		assert.equal(group1.previewEditor.matches(g1_input2), true);
		assert.equal(group2.previewEditor.matches(g2_input2), true);
		assert.equal(group1.label, 'group1');
		assert.equal(group2.label, 'group2');

		assert.equal(group1.getEditors(true)[0].matches(g1_input2), true);
		assert.equal(group1.getEditors(true)[1].matches(g1_input1), true);
		assert.equal(group1.getEditors(true)[2].matches(g1_input3), true);

		assert.equal(group2.getEditors(true)[0].matches(g2_input1), true);
		assert.equal(group2.getEditors(true)[1].matches(g2_input2), true);
		assert.equal(group2.getEditors(true)[2].matches(g2_input3), true);
	});

	test('Stack - Single group, multiple editors - persist (some not persistable)', function () {
		let services = new ServiceCollection();

		services.set(IStorageService, new TestStorageService());
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);

		let inst = new InstantiationService(services);

		(<IEditorRegistry>Registry.as(EditorExtensions.Editors)).setInstantiationService(inst);

		let model: IEditorStacksModel = inst.createInstance(EditorStacksModel);

		let group = model.openGroup('group1');

		const serializableInput1 = input();
		const nonSerializableInput2 = input('2', true);
		const serializableInput2 = input();

		group.openEditor(serializableInput1, { active: true, pinned: true });
		group.openEditor(nonSerializableInput2, { active: true, pinned: false });
		group.openEditor(serializableInput2, { active: false, pinned: true });

		assert.equal(group.count, 3);
		assert.equal(group.activeEditor.matches(nonSerializableInput2), true);
		assert.equal(group.previewEditor.matches(nonSerializableInput2), true);

		assert.equal(group.getEditors(true)[0].matches(nonSerializableInput2), true);
		assert.equal(group.getEditors(true)[1].matches(serializableInput1), true);
		assert.equal(group.getEditors(true)[2].matches(serializableInput2), true);

		lifecycle.fireShutdown();

		// Create model again - should load from storage
		model = inst.createInstance(EditorStacksModel);

		group = model.groups[0];

		assert.equal(group.count, 2);
		assert.equal(group.activeEditor.matches(serializableInput1), true);
		assert.equal(group.previewEditor, null);

		assert.equal(group.getEditors(true)[0].matches(serializableInput1), true);
		assert.equal(group.getEditors(true)[1].matches(serializableInput2), true);
	});

	test('Stack - Multiple groups, multiple editors - persist (some not persistable, causes empty group)', function () {
		let services = new ServiceCollection();

		services.set(IStorageService, new TestStorageService());
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);

		let inst = new InstantiationService(services);

		(<IEditorRegistry>Registry.as(EditorExtensions.Editors)).setInstantiationService(inst);

		let model: IEditorStacksModel = inst.createInstance(EditorStacksModel);

		let group1 = model.openGroup('group1');
		let group2 = model.openGroup('group1');

		const serializableInput1 = input();
		const serializableInput2 = input();
		const nonSerializableInput = input('2', true);

		group1.openEditor(serializableInput1, { pinned: true });
		group1.openEditor(serializableInput2);

		group2.openEditor(nonSerializableInput);

		lifecycle.fireShutdown();

		// Create model again - should load from storage
		model = inst.createInstance(EditorStacksModel);

		group1 = model.groups[0];

		assert.equal(model.groups.length, 1);
		assert.equal(group1.count, 2);
		assert.equal(group1.getEditors()[0].matches(serializableInput1), true);
		assert.equal(group1.getEditors()[1].matches(serializableInput2), true);
	});
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955

	test('Stack - Multiple groups, multiple editors - persist (some not persistable, causes empty group, allow empty groups)', function () {
		setAllowEmptyGroups(true);

		let services = new ServiceCollection();

		services.set(IStorageService, new TestStorageService());
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);

		let inst = new InstantiationService(services);

		(<IEditorRegistry>Registry.as(EditorExtensions.Editors)).setInstantiationService(inst);

		let model: IEditorStacksModel = inst.createInstance(EditorStacksModel);

		let group1 = model.openGroup('group1');
		let group2 = model.openGroup('group1');

		const serializableInput1 = input();
		const serializableInput2 = input();
		const nonSerializableInput = input('2', true);

		group1.openEditor(serializableInput1, { pinned: true });
		group1.openEditor(serializableInput2);

		group2.openEditor(nonSerializableInput);

		lifecycle.fireShutdown();

		// Create model again - should load from storage
		model = inst.createInstance(EditorStacksModel);

		group1 = model.groups[0];
		group2 = model.groups[1];

		assert.equal(model.groups.length, 2);
		assert.equal(group1.count, 2);
		assert.equal(group2.count, 0);
		assert.equal(group1.getEditors()[0].matches(serializableInput1), true);
		assert.equal(group1.getEditors()[1].matches(serializableInput2), true);
	});
B
Benjamin Pasero 已提交
956
});