textAreaState.test.ts 14.7 KB
Newer Older
A
Alex Dima 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  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 assert = require('assert');
import {Position} from 'vs/editor/common/core/position';
import {Range} from 'vs/editor/common/core/range';
import {EndOfLinePreference, IEditorRange, IRange, IEditorPosition} from 'vs/editor/common/editorCommon';
11
import {ISimpleModel, TextAreaState, IENarratorTextAreaState} from 'vs/editor/common/controller/textAreaState';
A
Alex Dima 已提交
12 13 14 15 16
import {MockTextAreaWrapper} from 'vs/editor/test/common/mocks/mockTextAreaWrapper';

suite('TextAreaState', () => {

	function assertTextAreaState(actual:TextAreaState, value:string, selectionStart:number, selectionEnd:number, isInOverwriteMode:boolean, selectionToken:number): void {
17
		let desired = new IENarratorTextAreaState(null, value, selectionStart, selectionEnd, isInOverwriteMode, selectionToken);
A
Alex Dima 已提交
18 19 20 21 22 23 24 25 26
		assert.ok(desired.equals(actual), desired.toString() + ' == ' + actual.toString());
	}

	test('fromTextArea', () => {
		let textArea = new MockTextAreaWrapper();
		textArea._value = 'Hello world!';
		textArea._selectionStart = 1;
		textArea._selectionEnd = 12;
		textArea._isInOverwriteMode = false;
27
		let actual = IENarratorTextAreaState.EMPTY.fromTextArea(textArea);
A
Alex Dima 已提交
28

A
Alex Dima 已提交
29
		assertTextAreaState(actual, 'Hello world!', 1, 12, false, 0);
A
Alex Dima 已提交
30 31 32
		assert.equal(actual.getValue(), 'Hello world!');
		assert.equal(actual.getSelectionStart(), 1);

A
Alex Dima 已提交
33 34
		actual = actual.resetSelection();
		assertTextAreaState(actual, 'Hello world!', 12, 12, false, 0);
A
Alex Dima 已提交
35 36 37 38 39 40 41 42 43 44 45

		textArea.dispose();
	});

	test('applyToTextArea', () => {
		let textArea = new MockTextAreaWrapper();
		textArea._value = 'Hello world!';
		textArea._selectionStart = 1;
		textArea._selectionEnd = 12;
		textArea._isInOverwriteMode = false;

46 47
		let state = new IENarratorTextAreaState(null, 'Hi world!', 2, 2, false, 0);
		state.applyToTextArea('test', textArea, false);
A
Alex Dima 已提交
48 49 50 51 52

		assert.equal(textArea._value, 'Hi world!');
		assert.equal(textArea._selectionStart, 9);
		assert.equal(textArea._selectionEnd, 9);

53 54
		state = new IENarratorTextAreaState(null, 'Hi world!', 3, 3, false, 0);
		state.applyToTextArea('test', textArea, false);
A
Alex Dima 已提交
55 56 57 58 59

		assert.equal(textArea._value, 'Hi world!');
		assert.equal(textArea._selectionStart, 9);
		assert.equal(textArea._selectionEnd, 9);

60 61
		state = new IENarratorTextAreaState(null, 'Hi world!', 0, 2, false, 0);
		state.applyToTextArea('test', textArea, true);
A
Alex Dima 已提交
62 63 64 65 66 67 68 69

		assert.equal(textArea._value, 'Hi world!');
		assert.equal(textArea._selectionStart, 0);
		assert.equal(textArea._selectionEnd, 2);

		textArea.dispose();
	});

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	function testDeduceInput(prevState:TextAreaState, value:string, selectionStart:number, selectionEnd:number, isInOverwriteMode: boolean, expected:string, expectedCharReplaceCnt: number): void {
		let textArea = new MockTextAreaWrapper();
		textArea._value = value;
		textArea._selectionStart = selectionStart;
		textArea._selectionEnd = selectionEnd;
		textArea._isInOverwriteMode = isInOverwriteMode;

		let newState = (prevState || IENarratorTextAreaState.EMPTY).fromTextArea(textArea);

		let actual = newState.deduceInput();

		assert.equal(actual.text, expected);
		assert.equal(actual.replaceCharCnt, expectedCharReplaceCnt);

		textArea.dispose();
	}

	test('deduceInput - Japanese typing sennsei and accepting', () => {
		// manual test:
		// - choose keyboard layout: Japanese -> Hiragama
		// - type sennsei
		// - accept with Enter
		// - expected: せんせい

		// s
		// PREVIOUS STATE: [ <>, selectionStart: 0, selectionEnd: 0, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <s>, selectionStart: 0, selectionEnd: 1, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, '', 0, 0, false, 0),
			'',
			0, 1, false,
			'', 0
		);

		// e
		// PREVIOUS STATE: [ <s>, selectionStart: 0, selectionEnd: 1, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <せ>, selectionStart: 0, selectionEnd: 1, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, '', 0, 1, false, 0),
			'',
			0, 1, false,
			'', 1
		);

		// n
		// PREVIOUS STATE: [ <せ>, selectionStart: 0, selectionEnd: 1, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <せn>, selectionStart: 0, selectionEnd: 2, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, '', 0, 1, false, 0),
			'せn',
			0, 2, false,
			'せn', 1
		);

		// n
		// PREVIOUS STATE: [ <せn>, selectionStart: 0, selectionEnd: 2, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <せん>, selectionStart: 0, selectionEnd: 2, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, 'せn', 0, 2, false, 0),
			'せん',
			0, 2, false,
			'せん', 2
		);

		// s
		// PREVIOUS STATE: [ <せん>, selectionStart: 0, selectionEnd: 2, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <せんs>, selectionStart: 0, selectionEnd: 3, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, 'せん', 0, 2, false, 0),
			'せんs',
			0, 3, false,
			'せんs', 2
		);

		// e
		// PREVIOUS STATE: [ <せんs>, selectionStart: 0, selectionEnd: 3, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <せんせ>, selectionStart: 0, selectionEnd: 3, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, 'せんs', 0, 3, false, 0),
			'せんせ',
			0, 3, false,
			'せんせ', 3
		);

		// no-op? [was recorded]
		// PREVIOUS STATE: [ <せんせ>, selectionStart: 0, selectionEnd: 3, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <せんせ>, selectionStart: 0, selectionEnd: 3, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, 'せんせ', 0, 3, false, 0),
			'せんせ',
			0, 3, false,
			'せんせ', 3
		);

		// i
		// PREVIOUS STATE: [ <せんせ>, selectionStart: 0, selectionEnd: 3, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <せんせい>, selectionStart: 0, selectionEnd: 4, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, 'せんせ', 0, 3, false, 0),
			'せんせい',
			0, 4, false,
			'せんせい', 3
		);

		// ENTER (accept)
		// PREVIOUS STATE: [ <せんせい>, selectionStart: 0, selectionEnd: 4, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <せんせい>, selectionStart: 4, selectionEnd: 4, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, 'せんせい', 0, 4, false, 0),
			'せんせい',
			4, 4, false,
			'', 0
		);
	});

	test('deduceInput - Japanese typing sennsei and choosing different suggestion', () => {
		// manual test:
		// - choose keyboard layout: Japanese -> Hiragama
		// - type sennsei
		// - arrow down (choose next suggestion)
		// - accept with Enter
		// - expected: せんせい

		// sennsei
		// PREVIOUS STATE: [ <せんせい>, selectionStart: 0, selectionEnd: 4, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <せんせい>, selectionStart: 0, selectionEnd: 4, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, 'せんせい', 0, 4, false, 0),
			'せんせい',
			0, 4, false,
			'せんせい', 4
		);

		// arrow down
		// CURRENT STATE: [ <先生>, selectionStart: 0, selectionEnd: 2, isInOverwriteMode: false, selectionToken: 0]
		// PREVIOUS STATE: [ <せんせい>, selectionStart: 0, selectionEnd: 4, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, 'せんせい', 0, 4, false, 0),
			'先生',
			0, 2, false,
			'先生', 4
		);

		// ENTER (accept)
		// PREVIOUS STATE: [ <先生>, selectionStart: 0, selectionEnd: 2, isInOverwriteMode: false, selectionToken: 0]
		// CURRENT STATE: [ <先生>, selectionStart: 2, selectionEnd: 2, isInOverwriteMode: false, selectionToken: 0]
		testDeduceInput(
			new IENarratorTextAreaState(null, '先生', 0, 2, false, 0),
			'先生',
			2, 2, false,
			'', 0
		);
	});

A
Alex Dima 已提交
224
	test('extractNewText - no previous state with selection', () => {
225
		testDeduceInput(
A
Alex Dima 已提交
226 227 228
			null,
			'a',
			0, 1, false,
229
			'a', 0
A
Alex Dima 已提交
230 231 232 233
		);
	});

	test('extractNewText - no previous state without selection', () => {
234
		testDeduceInput(
A
Alex Dima 已提交
235 236 237
			null,
			'a',
			1, 1, false,
238
			'a', 0
A
Alex Dima 已提交
239 240 241 242
		);
	});

	test('extractNewText - typing does not cause a selection', () => {
243
		testDeduceInput(
244
			new IENarratorTextAreaState(null, '', 0, 0, false, 0),
A
Alex Dima 已提交
245 246
			'a',
			0, 1, false,
247
			'a', 0
A
Alex Dima 已提交
248 249 250 251
		);
	});

	test('extractNewText - had the textarea empty', () => {
252
		testDeduceInput(
253
			new IENarratorTextAreaState(null, '', 0, 0, false, 0),
A
Alex Dima 已提交
254 255
			'a',
			1, 1, false,
256
			'a', 0
A
Alex Dima 已提交
257 258 259 260
		);
	});

	test('extractNewText - had the entire line selected', () => {
261
		testDeduceInput(
262
			new IENarratorTextAreaState(null, 'Hello world!', 0, 12, false, 0),
A
Alex Dima 已提交
263 264
			'H',
			1, 1, false,
265
			'H', 0
A
Alex Dima 已提交
266 267 268 269
		);
	});

	test('extractNewText - had previous text 1', () => {
270
		testDeduceInput(
271
			new IENarratorTextAreaState(null, 'Hello world!', 12, 12, false, 0),
A
Alex Dima 已提交
272 273
			'Hello world!a',
			13, 13, false,
274
			'a', 0
A
Alex Dima 已提交
275 276 277 278
		);
	});

	test('extractNewText - had previous text 2', () => {
279
		testDeduceInput(
280
			new IENarratorTextAreaState(null, 'Hello world!', 0, 0, false, 0),
A
Alex Dima 已提交
281 282
			'aHello world!',
			1, 1, false,
283
			'a', 0
A
Alex Dima 已提交
284 285 286 287
		);
	});

	test('extractNewText - had previous text 3', () => {
288
		testDeduceInput(
289
			new IENarratorTextAreaState(null, 'Hello world!', 6, 11, false, 0),
A
Alex Dima 已提交
290 291
			'Hello other!',
			11, 11, false,
292
			'other', 0
A
Alex Dima 已提交
293 294 295 296
		);
	});

	test('extractNewText - IME', () => {
297
		testDeduceInput(
298
			new IENarratorTextAreaState(null, '', 0, 0, false, 0),
A
Alex Dima 已提交
299 300
			'これは',
			3, 3, false,
301
			'これは', 0
A
Alex Dima 已提交
302 303 304 305
		);
	});

	test('extractNewText - isInOverwriteMode', () => {
306
		testDeduceInput(
307
			new IENarratorTextAreaState(null, 'Hello world!', 0, 0, false, 0),
A
Alex Dima 已提交
308 309
			'Aello world!',
			1, 1, true,
310
			'A', 0
A
Alex Dima 已提交
311 312 313
		);
	});

A
Alex Dima 已提交
314
	test('extractMacReplacedText - does nothing if there is selection', () => {
315 316
		testDeduceInput(
			new IENarratorTextAreaState(null, 'Hello world!', 5, 5, false, 0),
A
Alex Dima 已提交
317 318
			'Hellö world!',
			4, 5, false,
319
			'ö', 0
A
Alex Dima 已提交
320 321 322 323
		);
	});

	test('extractMacReplacedText - does nothing if there is more than one extra char', () => {
324 325
		testDeduceInput(
			new IENarratorTextAreaState(null, 'Hello world!', 5, 5, false, 0),
A
Alex Dima 已提交
326
			'Hellöö world!',
327 328
			5, 5, false,
			'öö', 1
A
Alex Dima 已提交
329 330 331 332
		);
	});

	test('extractMacReplacedText - does nothing if there is more than one changed char', () => {
333 334
		testDeduceInput(
			new IENarratorTextAreaState(null, 'Hello world!', 5, 5, false, 0),
A
Alex Dima 已提交
335
			'Helöö world!',
336 337
			5, 5, false,
			'öö', 2
A
Alex Dima 已提交
338 339 340 341
		);
	});

	test('extractMacReplacedText', () => {
342 343
		testDeduceInput(
			new IENarratorTextAreaState(null, 'Hello world!', 5, 5, false, 0),
A
Alex Dima 已提交
344 345
			'Hellö world!',
			5, 5, false,
346
			'ö', 1
A
Alex Dima 已提交
347 348 349
		);
	});

A
Alex Dima 已提交
350 351
	function testFromEditorSelectionAndPreviousState(eol:string, lines:string[], range:Range, prevSelectionToken:number): TextAreaState {
		let model = new SimpleModel(lines, eol);
352
		let previousState = new IENarratorTextAreaState(null, '', 0, 0, false, prevSelectionToken);
A
Alex Dima 已提交
353
		return previousState.fromEditorSelection(model, range);
A
Alex Dima 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
	}

	test('fromEditorSelectionAndPreviousState - no selection on first line', () => {
		let actual = testFromEditorSelectionAndPreviousState('\n', [
			'Just a line',
			'And another line'
		], new Range(1,1,1,1), 0);
		assertTextAreaState(actual, 'Just a line', 0, 11, false, 1);
	});

	test('fromEditorSelectionAndPreviousState - no selection on second line', () => {
		let actual = testFromEditorSelectionAndPreviousState('\n', [
			'Just a line',
			'And another line',
			'And yet another line',
		], new Range(2,1,2,1), 0);
		assertTextAreaState(actual, 'And another line', 0, 16, false, 2);
	});

	test('fromEditorSelectionAndPreviousState - on a long line with selectionToken mismatch', () => {
		let aLongLine = 'a';
		for (let i = 0; i < 10; i++) {
			aLongLine = aLongLine + aLongLine;
		}
		let actual = testFromEditorSelectionAndPreviousState('\n', [
			'Just a line',
			aLongLine,
			'And yet another line',
		], new Range(2,500,2,500), 0);
		assertTextAreaState(actual, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa…aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 0, 201, false, 2);
	});

	test('fromEditorSelectionAndPreviousState - on a long line with same selectionToken', () => {
		let aLongLine = 'a';
		for (let i = 0; i < 10; i++) {
			aLongLine = aLongLine + aLongLine;
		}
		let actual = testFromEditorSelectionAndPreviousState('\n', [
			'Just a line',
			aLongLine,
			'And yet another line',
		], new Range(2,500,2,500), 2);
		assertTextAreaState(actual, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 100, 100, false, 2);
	});
});

class SimpleModel implements ISimpleModel {

	private _lines: string[];
	private _eol: string;

	constructor(lines:string[], eol:string) {
		this._lines = lines;
		this._eol = eol;
	}

	public getLineMaxColumn(lineNumber:number): number {
		return this._lines[lineNumber - 1].length + 1;
	}

	private _getEndOfLine(eol:EndOfLinePreference): string {
		switch (eol) {
			case EndOfLinePreference.LF:
				return '\n';
			case EndOfLinePreference.CRLF:
				return '\r\n';
			case EndOfLinePreference.TextDefined:
				return this._eol;
		}
		throw new Error('Unknown EOL preference');
	}

	public getValueInRange(range:IRange, eol:EndOfLinePreference): string {
		if (Range.isEmpty(range)) {
			return '';
		}

		if (range.startLineNumber === range.endLineNumber) {
			return this._lines[range.startLineNumber - 1].substring(range.startColumn - 1, range.endColumn - 1);
		}

		var lineEnding = this._getEndOfLine(eol),
			startLineIndex = range.startLineNumber - 1,
			endLineIndex = range.endLineNumber - 1,
			resultLines:string[] = [];

		resultLines.push(this._lines[startLineIndex].substring(range.startColumn - 1));
		for (var i = startLineIndex + 1; i < endLineIndex; i++) {
			resultLines.push(this._lines[i]);
		}
		resultLines.push(this._lines[endLineIndex].substring(0, range.endColumn - 1));

		return resultLines.join(lineEnding);
	}

	public getModelLineContent(lineNumber:number): string {
		return this._lines[lineNumber - 1];
	}

	public getLineCount(): number {
		return this._lines.length;
	}

	public convertViewPositionToModelPosition(viewLineNumber:number, viewColumn:number): IEditorPosition {
		return new Position(viewLineNumber, viewColumn);
	}
}