editorStacksModel.test.ts 54.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, EditorGroup} from 'vs/workbench/common/editor/editorStacksModel';
10
import {EditorInput, IFileEditorInput, IEditorIdentifier, IEditorGroup, IStacksModelChangeEvent} from 'vs/workbench/common/editor';
11
import URI from 'vs/base/common/uri';
12
import {TestStorageService, TestConfigurationService, TestLifecycleService, TestContextService, TestWorkspace, TestConfiguration} from 'vs/workbench/test/common/servicesTestUtils';
13
import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService';
14
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
15 16
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
import {IStorageService} from 'vs/platform/storage/common/storage';
17
import {ILifecycleService} from 'vs/platform/lifecycle/common/lifecycle';
B
Benjamin Pasero 已提交
18
import {IWorkspaceContextService} from 'vs/workbench/services/workspace/common/contextService';
19 20
import {IEditorRegistry, Extensions as EditorExtensions, IEditorInputFactory} from 'vs/workbench/browser/parts/editor/baseEditor';
import {Registry} from 'vs/platform/platform';
21
import {Position, Direction} from 'vs/platform/editor/common/editor';
22
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
23
import {DiffEditorInput} from 'vs/workbench/common/editor/diffEditorInput';
B
Benjamin Pasero 已提交
24

B
Benjamin Pasero 已提交
25
function create(): EditorStacksModel {
26 27
	let services = new ServiceCollection();
	services.set(IStorageService, new TestStorageService());
28
	services.set(ILifecycleService, new TestLifecycleService());
B
Benjamin Pasero 已提交
29
	services.set(IWorkspaceContextService, new TestContextService());
30

31
	const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
32
	config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
33 34
	services.set(IConfigurationService, config);

35 36 37
	let inst = new InstantiationService(services);

	return inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
38 39
}

40
interface ModelEvents {
B
Benjamin Pasero 已提交
41 42 43
	opened: IEditorGroup[];
	activated: IEditorGroup[];
	closed: IEditorGroup[];
B
Benjamin Pasero 已提交
44
	moved: IEditorGroup[];
45
	renamed: IEditorGroup[];
46
	disposed: IEditorIdentifier[];
47
	changed: IStacksModelChangeEvent[];
B
Benjamin Pasero 已提交
48 49
}

50 51 52 53 54 55
interface GroupEvents {
	opened: EditorInput[];
	activated: EditorInput[];
	closed: EditorInput[];
	pinned: EditorInput[];
	unpinned: EditorInput[];
B
Benjamin Pasero 已提交
56
	moved: EditorInput[];
57 58
}

59
function modelListener(model: EditorStacksModel): ModelEvents {
60
	const modelEvents = {
B
Benjamin Pasero 已提交
61 62
		opened: [],
		activated: [],
B
Benjamin Pasero 已提交
63
		closed: [],
64
		moved: [],
65
		renamed: [],
66 67
		disposed: [],
		changed: []
B
Benjamin Pasero 已提交
68 69
	};

70 71 72
	model.onGroupOpened(g => modelEvents.opened.push(g));
	model.onGroupActivated(g => modelEvents.activated.push(g));
	model.onGroupClosed(g => modelEvents.closed.push(g));
B
Benjamin Pasero 已提交
73
	model.onGroupMoved(g => modelEvents.moved.push(g));
74
	model.onGroupRenamed(g => modelEvents.renamed.push(g));
75
	model.onEditorDisposed(e => modelEvents.disposed.push(e));
76
	model.onModelChanged(e => modelEvents.changed.push(e));
77 78 79 80

	return modelEvents;
}

81
function groupListener(group: EditorGroup): GroupEvents {
82 83 84 85 86
	const groupEvents = {
		opened: [],
		closed: [],
		activated: [],
		pinned: [],
B
Benjamin Pasero 已提交
87 88
		unpinned: [],
		moved: []
89 90 91
	};

	group.onEditorOpened(e => groupEvents.opened.push(e));
92
	group.onEditorClosed(e => groupEvents.closed.push(e.editor));
93 94 95
	group.onEditorActivated(e => groupEvents.activated.push(e));
	group.onEditorPinned(e => groupEvents.pinned.push(e));
	group.onEditorUnpinned(e => groupEvents.unpinned.push(e));
B
Benjamin Pasero 已提交
96
	group.onEditorMoved(e => groupEvents.moved.push(e));
B
Benjamin Pasero 已提交
97 98 99 100

	return groupEvents;
}

B
Benjamin Pasero 已提交
101
let index = 0;
102
class TestEditorInput extends EditorInput {
103 104 105
	constructor(public id: string) {
		super();
	}
106
	public getTypeId() { return 'testEditorInput'; }
B
Benjamin Pasero 已提交
107
	public resolve() { return null; }
108 109

	public matches(other: TestEditorInput): boolean {
110
		return other && this.id === other.id && other instanceof TestEditorInput;
111
	}
112 113 114 115

	public setDirty(): void {
		this._onDidChangeDirty.fire();
	}
116 117 118 119 120 121
}

class NonSerializableTestEditorInput extends EditorInput {
	constructor(public id: string) {
		super();
	}
122
	public getTypeId() { return 'testEditorInput-nonSerializable'; }
123
	public resolve() { return null; }
B
Benjamin Pasero 已提交
124 125

	public matches(other: TestEditorInput): boolean {
126
		return other && this.id === other.id && other instanceof NonSerializableTestEditorInput;
B
Benjamin Pasero 已提交
127
	}
128 129
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
class TestFileEditorInput extends EditorInput implements IFileEditorInput {

	constructor(public id: string, private resource: URI) {
		super();
	}
	public getTypeId() { return 'testFileEditorInput'; }
	public resolve() { return null; }

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

	public setResource(r: URI): void {
	}

	public setMime(mime: string) {
	}

	public setEncoding(encoding: string) {
	}

	public getEncoding(): string {
		return null;
	}

	public setPreferredEncoding(encoding: string) {
	}

	public getResource(): URI {
		return this.resource;
	}

	public getMime(): string {
		return null;
	}
}

function input(id = String(index++), nonSerializable?: boolean, resource?: URI): EditorInput {
	if (resource) {
		return new TestFileEditorInput(id, resource);
	}

172
	return nonSerializable ? new NonSerializableTestEditorInput(id) : new TestEditorInput(id);
173 174
}

175 176
interface ISerializedTestInput {
	id: string;
177 178
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
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 已提交
201 202
suite('Editor Stacks Model', () => {

203
	teardown(() => {
204
		index = 1;
B
Benjamin Pasero 已提交
205 206
	});

B
Benjamin Pasero 已提交
207
	test('Groups - Basic', function () {
B
Benjamin Pasero 已提交
208
		const model = create();
209
		const events = modelListener(model);
B
Benjamin Pasero 已提交
210 211 212 213

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

B
Benjamin Pasero 已提交
214
		const first = model.openGroup('first');
B
Benjamin Pasero 已提交
215 216 217 218
		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 已提交
219
		assert.equal(model.groups[0], first);
B
Benjamin Pasero 已提交
220 221 222 223 224 225

		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 已提交
226
		assert.equal(model.groups[1], second);
B
Benjamin Pasero 已提交
227 228 229 230 231 232

		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 已提交
233
		assert.equal(model.groups[2], third);
B
Benjamin Pasero 已提交
234 235 236 237 238

		model.closeGroup(first);
		assert.equal(events.closed[0], first);
		assert.equal(model.groups.length, 2);
		assert.equal(model.activeGroup, third);
B
Benjamin Pasero 已提交
239 240
		assert.equal(model.groups[0], second);
		assert.equal(model.groups[1], third);
B
Benjamin Pasero 已提交
241 242 243 244 245 246

		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 已提交
247
		assert.equal(model.groups[0], second);
B
Benjamin Pasero 已提交
248 249 250 251 252

		const fourth = model.openGroup('fourth');
		assert.equal(fourth, model.activeGroup);
		model.closeGroup(fourth);
		assert.equal(second, model.activeGroup);
253 254 255 256 257 258 259 260 261 262 263

		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);

B
Benjamin Pasero 已提交
264
		model.closeGroups();
265 266

		assert.equal(model.groups.length, 0);
B
Benjamin Pasero 已提交
267
	});
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	test('Groups - Close Group sends move event for others to the right', function () {
		const model = create();
		const events = modelListener(model);

		const first = model.openGroup('first');
		model.openGroup('second');
		const third = model.openGroup('third');

		model.closeGroup(first);
		assert.equal(events.moved.length, 2);

		model.closeGroup(third);
		assert.equal(events.moved.length, 2);
	});

B
Benjamin Pasero 已提交
284 285 286 287
	test('Groups - Move Groups', function () {
		const model = create();
		const events = modelListener(model);

288 289 290 291 292 293 294 295 296
		model.openGroup('first');
		const group2 = model.openGroup('second');

		model.renameGroup(group2, 'renamed');

		assert.equal(group2.label, 'renamed');
		assert.equal(group2, events.renamed[0]);
	});

B
Benjamin Pasero 已提交
297 298 299 300 301 302 303 304 305 306 307 308
	test('Groups - Position of Group', function () {
		const model = create();

		const group1 = model.openGroup('first');
		const group2 = model.openGroup('second');
		const group3 = model.openGroup('third');

		assert.equal(Position.LEFT, model.positionOfGroup(group1));
		assert.equal(Position.CENTER, model.positionOfGroup(group2));
		assert.equal(Position.RIGHT, model.positionOfGroup(group3));
	});

309 310 311
	test('Groups - Rename Group', function () {
		const model = create();

B
Benjamin Pasero 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
		const group1 = model.openGroup('first');
		const group2 = model.openGroup('second');

		model.moveGroup(group1, 1);
		assert.equal(model.groups[0], group2);
		assert.equal(model.groups[1], group1);

		model.moveGroup(group1, 0);
		assert.equal(model.groups[0], group1);
		assert.equal(model.groups[1], group2);

		const group3 = model.openGroup('third');

		model.moveGroup(group1, 2);

		assert.equal(model.groups[0], group2);
		assert.equal(model.groups[1], group3);
		assert.equal(model.groups[2], group1);
	});

B
Benjamin Pasero 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	test('Groups - Move Group sends move events for all moved groups', function () {
		const model = create();
		let events = modelListener(model);

		let group1 = model.openGroup('first');
		let group2 = model.openGroup('second');
		let group3 = model.openGroup('third');

		model.moveGroup(group1, 1);
		assert.equal(events.moved.length, 2);

		model.closeGroups();

		events = modelListener(model);
		group1 = model.openGroup('first');
		group2 = model.openGroup('second');
		group3 = model.openGroup('third');

		model.moveGroup(group1, 2);
		assert.equal(events.moved.length, 3);

		model.closeGroups();

		events = modelListener(model);
		group1 = model.openGroup('first');
		group2 = model.openGroup('second');
		group3 = model.openGroup('third');

		model.moveGroup(group3, 1);
		assert.equal(events.moved.length, 2);
	});

364 365 366
	test('Groups - Event Aggregation', function () {
		const model = create();

367
		let groupEvents: IStacksModelChangeEvent[] = [];
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
		let count = groupEvents.length;
		model.onModelChanged(group => {
			groupEvents.push(group);
		});

		const first = model.openGroup('first');
		assert.ok(groupEvents.length > count);
		count = groupEvents.length;

		const second = model.openGroup('second');
		assert.ok(groupEvents.length > count);
		count = groupEvents.length;

		model.renameGroup(second, 'renamed');
		assert.ok(groupEvents.length > count);
		count = groupEvents.length;

		const input1 = input();
		first.openEditor(input1);
		assert.ok(groupEvents.length > count);
		count = groupEvents.length;

		first.closeEditor(input1);
		assert.ok(groupEvents.length > count);
		count = groupEvents.length;

		model.closeGroup(second);
		assert.ok(groupEvents.length > count);
	});

398 399 400 401 402 403 404 405 406 407 408 409
	test('Groups - Open group but do not set active', function () {
		const model = create();
		const events = modelListener(model);

		const group1 = model.openGroup('first');
		model.openGroup('second', false);

		assert.equal(model.activeGroup, group1);
		assert.equal(events.activated.length, 1);
		assert.equal(events.activated[0], group1);
	});

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	test('Groups - Group Identifiers', function () {
		const model = create();

		const group1 = model.openGroup('first');
		const group2 = model.openGroup('second');
		const group3 = model.openGroup('third');

		assert.equal(model.getGroup(group1.id), group1);
		assert.equal(model.getGroup(group2.id), group2);
		assert.equal(model.getGroup(group3.id), group3);

		model.closeGroup(group2);
		assert.equal(model.getGroup(group2.id), null);

		model.moveGroup(group1, 1);

		assert.equal(model.getGroup(group1.id), group1);
		assert.equal(model.getGroup(group3.id), group3);

B
Benjamin Pasero 已提交
429
		model.closeGroups();
430 431 432 433 434

		assert.equal(model.getGroup(group1.id), null);
		assert.equal(model.getGroup(group3.id), null);
	});

435 436 437 438 439 440 441 442 443 444 445 446
	test('Groups - Open Group at Index', function () {
		const model = create();

		const group1 = model.openGroup('first', false, 2);
		const group2 = model.openGroup('second', false, 1);
		const group3 = model.openGroup('third', false, 0);

		assert.equal(model.groups[0], group3);
		assert.equal(model.groups[1], group2);
		assert.equal(model.groups[2], group1);
	});

B
Benjamin Pasero 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
	test('Groups - Close All Groups except active', function () {
		const model = create();

		model.openGroup('first');
		model.openGroup('second');
		const group3 = model.openGroup('third');

		model.closeGroups(model.activeGroup);

		assert.equal(model.groups.length, 1);
		assert.equal(model.activeGroup, group3);
	});

	test('Groups - Close All Groups except inactive', function () {
		const model = create();

		model.openGroup('first');
		const group2 = model.openGroup('second');
		model.openGroup('third');

		model.closeGroups(group2);

		assert.equal(model.groups.length, 1);
		assert.equal(model.activeGroup, group2);
	});

473 474
	test('Stack - One Editor', function () {
		const model = create();
B
Benjamin Pasero 已提交
475
		const group = model.openGroup('group');
476 477
		const events = groupListener(group);

478
		assert.equal(group.count, 0);
479 480 481 482 483 484
		assert.equal(group.getEditors(true).length, 0);

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

485
		assert.equal(group.count, 1);
486 487 488 489 490 491 492 493 494 495
		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);
496
		assert.equal(group.count, 0);
497 498 499 500 501 502 503 504
		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 });

505
		assert.equal(group.count, 1);
506 507 508 509 510 511 512 513 514 515
		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);
516
		assert.equal(group.count, 0);
517 518 519 520 521
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[1], input2);

		group.closeEditor(input2);
522
		assert.equal(group.count, 0);
523 524 525 526 527 528 529 530
		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 });

531
		assert.equal(group.count, 1);
532 533 534 535 536 537 538 539 540 541
		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);
542
		assert.equal(group.count, 0);
543 544 545 546 547 548 549 550
		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);
551
		assert.equal(group.count, 0);
552 553 554 555 556 557 558 559
		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);

560
		assert.equal(group.count, 1);
561 562 563 564 565 566 567 568 569 570
		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);
571
		assert.equal(group.count, 0);
572 573 574 575 576
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[3], input4);
	});

B
Benjamin Pasero 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590
	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 });

591
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
		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);
612 613 614 615 616

		group.closeAllEditors();

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

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
	test('Stack - Multiple Editors - Preview editor moves to the side of the active one', function () {
		const model = create();
		const group = model.openGroup('group');

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

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

		assert.equal(input3, group.getEditors()[2]);

		const input4 = input();
634
		group.openEditor(input4, { pinned: false, active: true }); // this should cause the preview editor to move after input3
635 636 637 638

		assert.equal(input4, group.getEditors()[2]);
	});

639
	test('Stack - Multiple Editors - Pinned and Active (DEFAULT_OPEN_EDITOR_DIRECTION = Direction.LEFT)', function () {
640 641 642 643 644 645 646
		let services = new ServiceCollection();
		services.set(IStorageService, new TestStorageService());
		services.set(ILifecycleService, new TestLifecycleService());
		services.set(IWorkspaceContextService, new TestContextService());

		const config = new TestConfigurationService();
		services.set(IConfigurationService, config);
B
Benjamin Pasero 已提交
647
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'left' }});
648 649 650 651

		let inst = new InstantiationService(services);

		const model = inst.createInstance(EditorStacksModel);
652 653

		const group = model.openGroup('group');
654
		const events = groupListener(group);
655 656 657 658 659 660 661 662 663 664 665 666 667

		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);
668

B
Benjamin Pasero 已提交
669
		model.closeGroups();
670 671 672

		assert.equal(events.closed.length, 3);
		assert.equal(group.count, 0);
673 674
	});

B
Benjamin Pasero 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687
	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 });

688
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
		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);
	});

707 708
	test('Stack - Multiple Editors - Preview gets overwritten', function () {
		const model = create();
B
Benjamin Pasero 已提交
709
		const group = model.openGroup('group');
710 711 712 713 714 715 716 717 718 719 720
		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

721
		assert.equal(group.count, 1);
722 723 724 725 726 727 728 729 730 731 732
		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 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

		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);
773
	});
B
Benjamin Pasero 已提交
774

B
Benjamin Pasero 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788
	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);
789
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
790 791 792 793 794 795 796 797

		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);
798
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
799 800 801 802 803 804 805 806

		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);
807
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
808 809 810 811

		group.unpin(input2);

		assert.equal(group.activeEditor, input3);
812
		assert.equal(group.count, 2); // 2 previews got merged into one
B
Benjamin Pasero 已提交
813 814 815
		assert.equal(group.getEditors()[0], input2);
		assert.equal(group.getEditors()[1], input3);
		assert.equal(events.closed[0], input1);
816
		assert.equal(group.count, 2);
B
Benjamin Pasero 已提交
817 818 819 820

		group.unpin(input3);

		assert.equal(group.activeEditor, input3);
821
		assert.equal(group.count, 1); // pinning replaced the preview
B
Benjamin Pasero 已提交
822 823
		assert.equal(group.getEditors()[0], input3);
		assert.equal(events.closed[1], input2);
824
		assert.equal(group.count, 1);
B
Benjamin Pasero 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
	});

	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);
846
		assert.equal(group.count, 5);
B
Benjamin Pasero 已提交
847 848 849 850

		group.closeEditor(input5);
		assert.equal(group.activeEditor, input4);
		assert.equal(events.activated[5], input4);
851
		assert.equal(group.count, 4);
B
Benjamin Pasero 已提交
852 853 854 855 856 857

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

		assert.equal(group.activeEditor, input1);
858
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
859 860 861 862

		group.closeEditor(input1);

		assert.equal(group.activeEditor, input3);
863
		assert.equal(group.count, 2);
B
Benjamin Pasero 已提交
864 865 866 867 868

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

		assert.equal(group.activeEditor, input3);
869
		assert.equal(group.count, 1);
B
Benjamin Pasero 已提交
870 871 872 873

		group.closeEditor(input3);

		assert.ok(!group.activeEditor);
874
		assert.equal(group.count, 0);
B
Benjamin Pasero 已提交
875 876
	});

B
Benjamin Pasero 已提交
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 914 915 916 917 918 919 920
	test('Stack - Multiple Editors - move editor', 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.moveEditor(input1, 1);

		assert.equal(events.moved[0], input1);
		assert.equal(group.getEditors()[0], input2);
		assert.equal(group.getEditors()[1], input1);

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

		group.moveEditor(input4, 0);

		assert.equal(events.moved[1], input4);
		assert.equal(group.getEditors()[0], input4);
		assert.equal(group.getEditors()[1], input2);
		assert.equal(group.getEditors()[2], input1);
		assert.equal(group.getEditors()[3], input3);
		assert.equal(group.getEditors()[4], input5);

		group.moveEditor(input4, 3);
		group.moveEditor(input2, 1);

		assert.equal(group.getEditors()[0], input1);
		assert.equal(group.getEditors()[1], input2);
		assert.equal(group.getEditors()[2], input3);
		assert.equal(group.getEditors()[3], input4);
		assert.equal(group.getEditors()[4], input5);
	});

B
Benjamin Pasero 已提交
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 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
	test('Stack - Multiple Editors - move editor across groups', function () {
		const model = create();

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

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

		group1.openEditor(g1_input1, { active: true, pinned: true });
		group1.openEditor(g1_input2, { active: true, pinned: true });
		group2.openEditor(g2_input1, { active: true, pinned: true });

		// A move across groups is a close in the one group and an open in the other group at a specific index
		group2.closeEditor(g2_input1);
		group1.openEditor(g2_input1, { active: true, pinned: true, index: 1 });

		assert.equal(group1.count, 3);
		assert.equal(group1.getEditors()[0], g1_input1);
		assert.equal(group1.getEditors()[1], g2_input1);
		assert.equal(group1.getEditors()[2], g1_input2);
	});

	test('Stack - Multiple Editors - move editor across groups (input already exists in group 1)', function () {
		const model = create();

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

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

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

		// A move across groups is a close in the one group and an open in the other group at a specific index
		group2.closeEditor(g2_input1);
		group1.openEditor(g2_input1, { active: true, pinned: true, index: 0 });

		assert.equal(group1.count, 3);
		assert.equal(group1.getEditors()[0], g1_input2);
		assert.equal(group1.getEditors()[1], g1_input1);
		assert.equal(group1.getEditors()[2], g1_input3);
	});

B
Benjamin Pasero 已提交
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
	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 已提交
1002

B
Benjamin Pasero 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	test('Stack - Multiple Editors - Close Others, Close Left, Close Right', function () {
		const model = create();
		const group = model.openGroup('group');

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

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

		// Close Others
		group.closeEditors(group.activeEditor);
		assert.equal(group.activeEditor, input5);
		assert.equal(group.count, 1);

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

		// Close Left
		assert.equal(group.activeEditor, input3);
		group.closeEditors(group.activeEditor, Direction.LEFT);
		assert.equal(group.activeEditor, input3);
		assert.equal(group.count, 3);
		assert.equal(group.getEditors()[0], input3);
		assert.equal(group.getEditors()[1], input4);
		assert.equal(group.getEditors()[2], input5);

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

		// Close Right
		assert.equal(group.activeEditor, input3);
		group.closeEditors(group.activeEditor, Direction.RIGHT);
		assert.equal(group.activeEditor, input3);
		assert.equal(group.count, 3);
		assert.equal(group.getEditors()[0], input1);
		assert.equal(group.getEditors()[1], input2);
		assert.equal(group.getEditors()[2], input3);
	});

1059 1060 1061 1062 1063
	test('Stack - Multiple Editors - real user example', function () {
		const model = create();
		const group = model.openGroup('group');

		// [] -> /index.html/
B
Benjamin Pasero 已提交
1064
		let indexHtml = input('index.html');
1065 1066 1067 1068 1069 1070 1071
		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/
B
Benjamin Pasero 已提交
1072
		let styleCss = input('style.css');
1073 1074 1075 1076 1077 1078 1079
		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]
B
Benjamin Pasero 已提交
1080
		let testJs = input('test.js');
1081 1082 1083 1084 1085 1086 1087 1088 1089
		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);

1090
		// [/style.css/, test.js] -> [test.js, /index.html/]
B
Benjamin Pasero 已提交
1091
		indexHtml = input('index.html');
1092 1093 1094 1095 1096
		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);
1097 1098
		assert.equal(group.getEditors()[0], testJs);
		assert.equal(group.getEditors()[1], indexHtml);
1099 1100 1101
		assert.equal(group.count, 2);

		// make test.js active
B
Benjamin Pasero 已提交
1102
		testJs = input('test.js');
1103 1104 1105 1106 1107
		group.setActive(testJs);
		assert.equal(group.activeEditor, testJs);
		assert.equal(group.isActive(testJs), true);
		assert.equal(group.count, 2);

1108
		// [test.js, /indexHtml/] -> [test.js, index.html]
B
Benjamin Pasero 已提交
1109
		indexHtml = input('index.html');
1110 1111 1112 1113 1114
		group.pin(indexHtml);
		assert.equal(group.isPinned(indexHtml), true);
		assert.equal(group.isPreview(indexHtml), false);
		assert.equal(group.activeEditor, testJs);

1115
		// [test.js, index.html] -> [test.js, file.ts, index.html]
1116 1117 1118 1119 1120 1121 1122
		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);

1123
		// [test.js, index.html, file.ts] -> [test.js, /file.ts/, index.html]
1124 1125 1126 1127 1128 1129
		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);

1130
		// [test.js, /file.ts/, index.html] -> [test.js, /other.ts/, index.html]
1131 1132 1133 1134
		const otherTs = input('other.ts');
		group.openEditor(otherTs, { active: true });
		assert.equal(group.count, 3);
		assert.equal(group.activeEditor, otherTs);
B
Benjamin Pasero 已提交
1135
		assert.ok(group.getEditors()[0].matches(testJs));
1136
		assert.equal(group.getEditors()[1], otherTs);
B
Benjamin Pasero 已提交
1137
		assert.ok(group.getEditors()[2].matches(indexHtml));
1138 1139

		// make index.html active
B
Benjamin Pasero 已提交
1140
		indexHtml = input('index.html');
1141 1142 1143
		group.setActive(indexHtml);
		assert.equal(group.activeEditor, indexHtml);

1144
		// [test.js, /other.ts/, index.html] -> [test.js, /other.ts/]
1145 1146 1147
		group.closeEditor(indexHtml);
		assert.equal(group.count, 2);
		assert.equal(group.activeEditor, otherTs);
B
Benjamin Pasero 已提交
1148
		assert.ok(group.getEditors()[0].matches(testJs));
1149 1150 1151 1152 1153 1154
		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);
B
Benjamin Pasero 已提交
1155
		assert.ok(group.getEditors()[0].matches(testJs));
1156 1157 1158 1159 1160

		// [test.js] -> /test.js/
		group.unpin(testJs);
		assert.equal(group.count, 1);
		assert.equal(group.activeEditor, testJs);
B
Benjamin Pasero 已提交
1161
		assert.ok(group.getEditors()[0].matches(testJs));
1162 1163 1164 1165 1166 1167 1168 1169 1170
		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);
	});
1171 1172 1173 1174 1175

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

		services.set(IStorageService, new TestStorageService());
B
Benjamin Pasero 已提交
1176
		services.set(IWorkspaceContextService, new TestContextService());
1177 1178
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);
1179
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1180
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1181
		services.set(IConfigurationService, config);
1182 1183 1184 1185 1186

		let inst = new InstantiationService(services);

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

B
Benjamin Pasero 已提交
1187
		let model: EditorStacksModel = inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
1188
		let group = model.openGroup('group');
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205

		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 已提交
1206 1207 1208

		group = model.activeGroup;

1209 1210 1211 1212 1213 1214
		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 已提交
1215 1216 1217 1218 1219

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

		services.set(IStorageService, new TestStorageService());
B
Benjamin Pasero 已提交
1220
		services.set(IWorkspaceContextService, new TestContextService());
B
Benjamin Pasero 已提交
1221 1222
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);
1223
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1224
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1225
		services.set(IConfigurationService, config);
B
Benjamin Pasero 已提交
1226 1227 1228 1229 1230

		let inst = new InstantiationService(services);

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

B
Benjamin Pasero 已提交
1231
		let model: EditorStacksModel = inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301

		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());
B
Benjamin Pasero 已提交
1302
		services.set(IWorkspaceContextService, new TestContextService());
B
Benjamin Pasero 已提交
1303 1304
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);
1305
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1306
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1307
		services.set(IConfigurationService, config);
B
Benjamin Pasero 已提交
1308 1309 1310 1311 1312

		let inst = new InstantiationService(services);

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

B
Benjamin Pasero 已提交
1313
		let model: EditorStacksModel = inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
1314 1315 1316 1317

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

		const serializableInput1 = input();
B
Benjamin Pasero 已提交
1318
		const nonSerializableInput2 = input('3', true);
B
Benjamin Pasero 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
		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());
B
Benjamin Pasero 已提交
1352
		services.set(IWorkspaceContextService, new TestContextService());
B
Benjamin Pasero 已提交
1353 1354
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);
1355
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1356
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1357
		services.set(IConfigurationService, config);
B
Benjamin Pasero 已提交
1358 1359 1360 1361 1362

		let inst = new InstantiationService(services);

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

B
Benjamin Pasero 已提交
1363
		let model: EditorStacksModel = inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

		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);
	});
B
Benjamin Pasero 已提交
1389 1390 1391 1392 1393 1394 1395 1396

	test('Stack - Multiple groups, multiple editors - persist (ignore persisted when editors to open on startup)', function () {
		let services = new ServiceCollection();

		services.set(IStorageService, new TestStorageService());
		services.set(IWorkspaceContextService, new TestContextService(TestWorkspace, TestConfiguration, { filesToCreate: [true] }));
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);
1397
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1398
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1399
		services.set(IConfigurationService, config);
B
Benjamin Pasero 已提交
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425

		let inst = new InstantiationService(services);

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

		let model: EditorStacksModel = 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 NOT load from storage
		model = inst.createInstance(EditorStacksModel);

		assert.equal(model.groups.length, 0);
	});
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451

	test('Stack - Multiple Editors - Navigation (across groups)', function () {
		const model = create();

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

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

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

		const input4 = input();
		const input5 = input();
		const input6 = input();

		group2.openEditor(input4, { pinned: true, active: true });
		group2.openEditor(input5, { pinned: true, active: true });
		group2.openEditor(input6, { pinned: true, active: true });

		model.setActive(group1);
		group1.setActive(input1);

B
Benjamin Pasero 已提交
1452
		let previous = model.previous();
1453 1454 1455
		assert.equal(previous.group, group2);
		assert.equal(previous.editor, input6);

1456
		model.setActive(<EditorGroup>previous.group);
I
isidor 已提交
1457
		(<EditorGroup>previous.group).setActive(<EditorInput>previous.editor);
1458

B
Benjamin Pasero 已提交
1459
		let next = model.next();
1460 1461 1462 1463 1464 1465
		assert.equal(next.group, group1);
		assert.equal(next.editor, input1);

		model.setActive(group1);
		group1.setActive(input3);

B
Benjamin Pasero 已提交
1466
		next = model.next();
1467 1468 1469
		assert.equal(next.group, group2);
		assert.equal(next.editor, input4);

1470
		model.setActive(<EditorGroup>next.group);
I
isidor 已提交
1471
		(<EditorGroup>next.group).setActive(<EditorInput>next.editor);
1472

B
Benjamin Pasero 已提交
1473
		previous = model.previous();
1474 1475 1476
		assert.equal(previous.group, group1);
		assert.equal(previous.editor, input3);
	});
1477

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
	test('Stack - Multiple Editors - Resources', function () {
		const model = create();

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

		assert.ok(!model.isOpen(URI.file('/hello/world.txt')));

		const input1Resource = URI.file('/hello/world.txt');
		const input1 = input(void 0, false, input1Resource);
		group1.openEditor(input1);

		assert.ok(model.isOpen(input1Resource));
		assert.ok(group1.contains(input1Resource));

		group2.openEditor(input1);
		group1.closeEditor(input1);

		assert.ok(model.isOpen(input1Resource));
		assert.ok(!group1.contains(input1Resource));
		assert.ok(group2.contains(input1Resource));

		const input1ResourceClone = URI.file('/hello/world.txt');
		const input1Clone = input(void 0, false, input1ResourceClone);
		group1.openEditor(input1Clone);

		assert.ok(group1.contains(input1Resource));

		group2.closeEditor(input1);

		assert.ok(model.isOpen(input1Resource));
		assert.ok(group1.contains(input1Resource));
		assert.ok(!group2.contains(input1Resource));

		group1.closeEditor(input1Clone);

		assert.ok(!model.isOpen(input1Resource));
		assert.ok(!group1.contains(input1Resource));

		group1.openEditor(input1);

		const input2Resource = URI.file('/hello/world_other.txt');
		const input2 = input(void 0, false, input2Resource);

		const input3Resource = URI.file('/hello/world_different.txt');
		const input3 = input(void 0, false, input3Resource);

		group1.openEditor(input2);

		assert.ok(model.isOpen(input2Resource));
		assert.ok(!model.isOpen(input1Resource));

1530
		group1.openEditor(input3, { pinned: true });
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540

		assert.ok(model.isOpen(input2Resource));
		assert.ok(model.isOpen(input3Resource));

		model.closeGroups();

		assert.ok(!model.isOpen(input2Resource));
		assert.ok(!model.isOpen(input3Resource));
	});

1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
	test('Stack - Multiple Editors - Editor Dispose', function () {
		const model = create();
		const events = modelListener(model);

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

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

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

		group2.openEditor(input1, { pinned: true, active: true });
		group2.openEditor(input2, { pinned: true, active: true });

		input1.dispose();

		assert.equal(events.disposed.length, 2);
		assert.ok(events.disposed[0].editor.matches(input1));
		assert.ok(events.disposed[1].editor.matches(input1));

		input3.dispose();
		assert.equal(events.disposed.length, 3);
		assert.ok(events.disposed[2].editor.matches(input3));

		const input4 = input();
		const input5 = input();

		group1.openEditor(input4, { pinned: false, active: true });
		group1.openEditor(input5, { pinned: false, active: true });

		input4.dispose();
		assert.equal(events.disposed.length, 3);

		model.closeGroup(group2);

		input2.dispose();
		assert.equal(events.disposed.length, 4);
	});

	test('Stack - Multiple Editors - Editor Disposed on Close', function () {
		const model = create();

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

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

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

		group1.closeEditor(input3);

		assert.equal(input3.isDisposed(), true);

		group2.openEditor(input2, { pinned: true, active: true });
		group2.openEditor(input3, { pinned: true, active: true });
		group2.openEditor(input4, { pinned: true, active: true });

		group1.closeEditor(input2);

		assert.equal(input2.isDisposed(), false);

		group2.closeEditor(input2);

		assert.equal(input2.isDisposed(), true);

		group1.closeAllEditors();

		assert.equal(input4.isDisposed(), false);

		model.closeGroups();

		assert.equal(input4.isDisposed(), true);
	});

	test('Stack - Multiple Editors - Editor Disposed on Close (Diff Editor)', function () {
		const model = create();

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

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

		const diffInput = new DiffEditorInput('name', 'description', input1, input2);

1635
		group1.openEditor(diffInput, { pinned: true, active: true });
1636 1637 1638 1639 1640 1641 1642 1643
		group1.openEditor(input1, { pinned: true, active: true });

		group1.closeEditor(diffInput);

		assert.equal(diffInput.isDisposed(), true);
		assert.equal(input2.isDisposed(), true);
		assert.equal(input1.isDisposed(), false);
	});
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653

	test('Stack - Multiple Editors - Editor Emits Dirty', function () {
		const model = create();

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

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

1654
		group1.openEditor(input1, { pinned: true, active: true });
1655 1656 1657
		group2.openEditor(input2, { pinned: true, active: true });

		let dirtyCounter = 0;
1658
		model.onEditorDirty(() => {
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
			dirtyCounter++;
		});

		(<TestEditorInput>input1).setDirty();

		assert.equal(dirtyCounter, 1);

		(<TestEditorInput>input2).setDirty();

		assert.equal(dirtyCounter, 2);

		group2.closeAllEditors();

		(<TestEditorInput>input2).setDirty();

		assert.equal(dirtyCounter, 2);

		model.closeGroups();

		(<TestEditorInput>input1).setDirty();

		assert.equal(dirtyCounter, 2);
	});
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718

	test('Groups - Model change events (structural vs state)', function () {
		const model = create();
		const events = modelListener(model);

		const group1 = model.openGroup('first');

		assert.equal(events.changed[0].group, group1);
		assert.equal(events.changed[0].structural, true); // open group

		assert.equal(events.changed[1].group, group1);
		assert.ok(!events.changed[1].structural); // set active

		const input1 = input();

		group1.openEditor(input1, { pinned: true, active: true });

		assert.equal(events.changed[2].group, group1);
		assert.equal(events.changed[2].structural, true); // open editor
		assert.equal(events.changed[2].editor, input1);

		assert.equal(events.changed[3].group, group1);
		assert.ok(!events.changed[3].structural); // set active
		assert.equal(events.changed[3].editor, input1);

		group1.unpin(input1);

		assert.equal(events.changed[4].group, group1);
		assert.ok(!events.changed[4].structural); // unpin
		assert.equal(events.changed[4].editor, input1);

		group1.closeAllEditors();

		assert.equal(events.changed[5].group, group1);
		assert.ok(events.changed[5].structural); // close
		assert.equal(events.changed[5].editor, input1);
	});
B
Benjamin Pasero 已提交
1719
});