editorStacksModel.test.ts 53.3 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 312
	test('Groups - Rename Group', function () {
		const model = create();
		const events = modelListener(model);

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

		model.moveGroup(group1, 1);
		assert.equal(events.moved[0], group1);
		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);
	});

334 335 336
	test('Groups - Event Aggregation', function () {
		const model = create();

337
		let groupEvents: IStacksModelChangeEvent[] = [];
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 364 365 366 367
		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);
	});

368 369 370 371 372 373 374 375 376 377 378 379
	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);
	});

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	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 已提交
399
		model.closeGroups();
400 401 402 403 404

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

405 406 407 408 409 410 411 412 413 414 415 416
	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 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	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);
	});

443 444
	test('Stack - One Editor', function () {
		const model = create();
B
Benjamin Pasero 已提交
445
		const group = model.openGroup('group');
446 447
		const events = groupListener(group);

448
		assert.equal(group.count, 0);
449 450 451 452 453 454
		assert.equal(group.getEditors(true).length, 0);

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

455
		assert.equal(group.count, 1);
456 457 458 459 460 461 462 463 464 465
		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);
466
		assert.equal(group.count, 0);
467 468 469 470 471 472 473 474
		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 });

475
		assert.equal(group.count, 1);
476 477 478 479 480 481 482 483 484 485
		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);
486
		assert.equal(group.count, 0);
487 488 489 490 491
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[1], input2);

		group.closeEditor(input2);
492
		assert.equal(group.count, 0);
493 494 495 496 497 498 499 500
		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 });

501
		assert.equal(group.count, 1);
502 503 504 505 506 507 508 509 510 511
		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);
512
		assert.equal(group.count, 0);
513 514 515 516 517 518 519 520
		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);
521
		assert.equal(group.count, 0);
522 523 524 525 526 527 528 529
		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);

530
		assert.equal(group.count, 1);
531 532 533 534 535 536 537 538 539 540
		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);
541
		assert.equal(group.count, 0);
542 543 544 545 546
		assert.equal(group.getEditors(true).length, 0);
		assert.equal(group.activeEditor, void 0);
		assert.equal(events.closed[3], input4);
	});

B
Benjamin Pasero 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560
	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 });

561
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
		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);
582 583 584 585 586

		group.closeAllEditors();

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

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	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();
604
		group.openEditor(input4, { pinned: false, active: true }); // this should cause the preview editor to move after input3
605 606 607 608

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

609
	test('Stack - Multiple Editors - Pinned and Active (DEFAULT_OPEN_EDITOR_DIRECTION = Direction.LEFT)', function () {
610 611 612 613 614 615 616
		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 已提交
617
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'left' }});
618 619 620 621

		let inst = new InstantiationService(services);

		const model = inst.createInstance(EditorStacksModel);
622 623

		const group = model.openGroup('group');
624
		const events = groupListener(group);
625 626 627 628 629 630 631 632 633 634 635 636 637

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

B
Benjamin Pasero 已提交
639
		model.closeGroups();
640 641 642

		assert.equal(events.closed.length, 3);
		assert.equal(group.count, 0);
643 644
	});

B
Benjamin Pasero 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657
	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 });

658
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
		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);
	});

677 678
	test('Stack - Multiple Editors - Preview gets overwritten', function () {
		const model = create();
B
Benjamin Pasero 已提交
679
		const group = model.openGroup('group');
680 681 682 683 684 685 686 687 688 689 690
		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

691
		assert.equal(group.count, 1);
692 693 694 695 696 697 698 699 700 701 702
		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 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

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

B
Benjamin Pasero 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758
	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);
759
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
760 761 762 763 764 765 766 767

		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);
768
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
769 770 771 772 773 774 775 776

		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);
777
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
778 779 780 781

		group.unpin(input2);

		assert.equal(group.activeEditor, input3);
782
		assert.equal(group.count, 2); // 2 previews got merged into one
B
Benjamin Pasero 已提交
783 784 785
		assert.equal(group.getEditors()[0], input2);
		assert.equal(group.getEditors()[1], input3);
		assert.equal(events.closed[0], input1);
786
		assert.equal(group.count, 2);
B
Benjamin Pasero 已提交
787 788 789 790

		group.unpin(input3);

		assert.equal(group.activeEditor, input3);
791
		assert.equal(group.count, 1); // pinning replaced the preview
B
Benjamin Pasero 已提交
792 793
		assert.equal(group.getEditors()[0], input3);
		assert.equal(events.closed[1], input2);
794
		assert.equal(group.count, 1);
B
Benjamin Pasero 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
	});

	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);
816
		assert.equal(group.count, 5);
B
Benjamin Pasero 已提交
817 818 819 820

		group.closeEditor(input5);
		assert.equal(group.activeEditor, input4);
		assert.equal(events.activated[5], input4);
821
		assert.equal(group.count, 4);
B
Benjamin Pasero 已提交
822 823 824 825 826 827

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

		assert.equal(group.activeEditor, input1);
828
		assert.equal(group.count, 3);
B
Benjamin Pasero 已提交
829 830 831 832

		group.closeEditor(input1);

		assert.equal(group.activeEditor, input3);
833
		assert.equal(group.count, 2);
B
Benjamin Pasero 已提交
834 835 836 837 838

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

		assert.equal(group.activeEditor, input3);
839
		assert.equal(group.count, 1);
B
Benjamin Pasero 已提交
840 841 842 843

		group.closeEditor(input3);

		assert.ok(!group.activeEditor);
844
		assert.equal(group.count, 0);
B
Benjamin Pasero 已提交
845 846
	});

B
Benjamin Pasero 已提交
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
	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 已提交
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 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	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 已提交
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 971
	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 已提交
972

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

1029 1030 1031 1032 1033
	test('Stack - Multiple Editors - real user example', function () {
		const model = create();
		const group = model.openGroup('group');

		// [] -> /index.html/
B
Benjamin Pasero 已提交
1034
		let indexHtml = input('index.html');
1035 1036 1037 1038 1039 1040 1041
		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 已提交
1042
		let styleCss = input('style.css');
1043 1044 1045 1046 1047 1048 1049
		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 已提交
1050
		let testJs = input('test.js');
1051 1052 1053 1054 1055 1056 1057 1058 1059
		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);

1060
		// [/style.css/, test.js] -> [test.js, /index.html/]
B
Benjamin Pasero 已提交
1061
		indexHtml = input('index.html');
1062 1063 1064 1065 1066
		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);
1067 1068
		assert.equal(group.getEditors()[0], testJs);
		assert.equal(group.getEditors()[1], indexHtml);
1069 1070 1071
		assert.equal(group.count, 2);

		// make test.js active
B
Benjamin Pasero 已提交
1072
		testJs = input('test.js');
1073 1074 1075 1076 1077
		group.setActive(testJs);
		assert.equal(group.activeEditor, testJs);
		assert.equal(group.isActive(testJs), true);
		assert.equal(group.count, 2);

1078
		// [test.js, /indexHtml/] -> [test.js, index.html]
B
Benjamin Pasero 已提交
1079
		indexHtml = input('index.html');
1080 1081 1082 1083 1084
		group.pin(indexHtml);
		assert.equal(group.isPinned(indexHtml), true);
		assert.equal(group.isPreview(indexHtml), false);
		assert.equal(group.activeEditor, testJs);

1085
		// [test.js, index.html] -> [test.js, file.ts, index.html]
1086 1087 1088 1089 1090 1091 1092
		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);

1093
		// [test.js, index.html, file.ts] -> [test.js, /file.ts/, index.html]
1094 1095 1096 1097 1098 1099
		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);

1100
		// [test.js, /file.ts/, index.html] -> [test.js, /other.ts/, index.html]
1101 1102 1103 1104
		const otherTs = input('other.ts');
		group.openEditor(otherTs, { active: true });
		assert.equal(group.count, 3);
		assert.equal(group.activeEditor, otherTs);
B
Benjamin Pasero 已提交
1105
		assert.ok(group.getEditors()[0].matches(testJs));
1106
		assert.equal(group.getEditors()[1], otherTs);
B
Benjamin Pasero 已提交
1107
		assert.ok(group.getEditors()[2].matches(indexHtml));
1108 1109

		// make index.html active
B
Benjamin Pasero 已提交
1110
		indexHtml = input('index.html');
1111 1112 1113
		group.setActive(indexHtml);
		assert.equal(group.activeEditor, indexHtml);

1114
		// [test.js, /other.ts/, index.html] -> [test.js, /other.ts/]
1115 1116 1117
		group.closeEditor(indexHtml);
		assert.equal(group.count, 2);
		assert.equal(group.activeEditor, otherTs);
B
Benjamin Pasero 已提交
1118
		assert.ok(group.getEditors()[0].matches(testJs));
1119 1120 1121 1122 1123 1124
		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 已提交
1125
		assert.ok(group.getEditors()[0].matches(testJs));
1126 1127 1128 1129 1130

		// [test.js] -> /test.js/
		group.unpin(testJs);
		assert.equal(group.count, 1);
		assert.equal(group.activeEditor, testJs);
B
Benjamin Pasero 已提交
1131
		assert.ok(group.getEditors()[0].matches(testJs));
1132 1133 1134 1135 1136 1137 1138 1139 1140
		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);
	});
1141 1142 1143 1144 1145

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

		services.set(IStorageService, new TestStorageService());
B
Benjamin Pasero 已提交
1146
		services.set(IWorkspaceContextService, new TestContextService());
1147 1148
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);
1149
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1150
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1151
		services.set(IConfigurationService, config);
1152 1153 1154 1155 1156

		let inst = new InstantiationService(services);

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

B
Benjamin Pasero 已提交
1157
		let model: EditorStacksModel = inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
1158
		let group = model.openGroup('group');
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175

		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 已提交
1176 1177 1178

		group = model.activeGroup;

1179 1180 1181 1182 1183 1184
		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 已提交
1185 1186 1187 1188 1189

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

		services.set(IStorageService, new TestStorageService());
B
Benjamin Pasero 已提交
1190
		services.set(IWorkspaceContextService, new TestContextService());
B
Benjamin Pasero 已提交
1191 1192
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);
1193
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1194
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1195
		services.set(IConfigurationService, config);
B
Benjamin Pasero 已提交
1196 1197 1198 1199 1200

		let inst = new InstantiationService(services);

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

B
Benjamin Pasero 已提交
1201
		let model: EditorStacksModel = inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 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

		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 已提交
1272
		services.set(IWorkspaceContextService, new TestContextService());
B
Benjamin Pasero 已提交
1273 1274
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);
1275
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1276
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1277
		services.set(IConfigurationService, config);
B
Benjamin Pasero 已提交
1278 1279 1280 1281 1282

		let inst = new InstantiationService(services);

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

B
Benjamin Pasero 已提交
1283
		let model: EditorStacksModel = inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
1284 1285 1286 1287

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

		const serializableInput1 = input();
B
Benjamin Pasero 已提交
1288
		const nonSerializableInput2 = input('3', true);
B
Benjamin Pasero 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
		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 已提交
1322
		services.set(IWorkspaceContextService, new TestContextService());
B
Benjamin Pasero 已提交
1323 1324
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);
1325
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1326
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1327
		services.set(IConfigurationService, config);
B
Benjamin Pasero 已提交
1328 1329 1330 1331 1332

		let inst = new InstantiationService(services);

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

B
Benjamin Pasero 已提交
1333
		let model: EditorStacksModel = inst.createInstance(EditorStacksModel);
B
Benjamin Pasero 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358

		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 已提交
1359 1360 1361 1362 1363 1364 1365 1366

	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);
1367
		const config = new TestConfigurationService();
B
Benjamin Pasero 已提交
1368
		config.setUserConfiguration('workbench', { editor: { openPositioning: 'right' }});
1369
		services.set(IConfigurationService, config);
B
Benjamin Pasero 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395

		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);
	});
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421

	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 已提交
1422
		let previous = model.previous();
1423 1424 1425
		assert.equal(previous.group, group2);
		assert.equal(previous.editor, input6);

1426
		model.setActive(<EditorGroup>previous.group);
I
isidor 已提交
1427
		(<EditorGroup>previous.group).setActive(<EditorInput>previous.editor);
1428

B
Benjamin Pasero 已提交
1429
		let next = model.next();
1430 1431 1432 1433 1434 1435
		assert.equal(next.group, group1);
		assert.equal(next.editor, input1);

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

B
Benjamin Pasero 已提交
1436
		next = model.next();
1437 1438 1439
		assert.equal(next.group, group2);
		assert.equal(next.editor, input4);

1440
		model.setActive(<EditorGroup>next.group);
I
isidor 已提交
1441
		(<EditorGroup>next.group).setActive(<EditorInput>next.editor);
1442

B
Benjamin Pasero 已提交
1443
		previous = model.previous();
1444 1445 1446
		assert.equal(previous.group, group1);
		assert.equal(previous.editor, input3);
	});
1447

1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	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));

1500
		group1.openEditor(input3, { pinned: true });
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510

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

		model.closeGroups();

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

1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 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
	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);

1605
		group1.openEditor(diffInput, { pinned: true, active: true });
1606 1607 1608 1609 1610 1611 1612 1613
		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);
	});
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623

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

1624
		group1.openEditor(input1, { pinned: true, active: true });
1625 1626 1627
		group2.openEditor(input2, { pinned: true, active: true });

		let dirtyCounter = 0;
1628
		model.onEditorDirty(() => {
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
			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);
	});
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688

	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 已提交
1689
});