Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 1 | // Copyright 2022 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /** |
| 6 | * @fileoverview Unit tests for terminal_emulator.js. |
| 7 | */ |
| 8 | |
| 9 | import {sleep} from './terminal_common.js'; |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 10 | import {encodeKeyCombo, Modifier, XtermTerminal, XtermTerminalTestParams} |
| 11 | from './terminal_emulator.js'; |
| 12 | import {MockFunction, MockObject} from './terminal_test_mocks.js'; |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 13 | |
| 14 | describe('terminal_emulator_tests.js', function() { |
| 15 | describe('XtermTerminal', function() { |
| 16 | beforeEach(async function() { |
| 17 | this.mocks = { |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 18 | term: new MockObject({ |
| 19 | options: {}, |
| 20 | parser: { |
| 21 | registerOscHandler: () => {}, |
| 22 | }, |
| 23 | }), |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 24 | fontManager: new MockObject(), |
| 25 | fitAddon: new MockObject(), |
| 26 | }; |
| 27 | const testParams = {}; |
| 28 | for (const prop in this.mocks) { |
| 29 | testParams[prop] = this.mocks[prop].proxy; |
| 30 | } |
| 31 | |
| 32 | this.terminal = new XtermTerminal({ |
| 33 | storage: new lib.Storage.Memory(), |
| 34 | profileId: 'test', |
| 35 | enableWebGL: true, |
| 36 | testParams: /** @type {!XtermTerminalTestParams} */(testParams), |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | describe('updateFont_()', function() { |
| 41 | it('updates font', async function() { |
| 42 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 43 | assert.deepEqual( |
| 44 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 45 | [['font one']]); |
| 46 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 47 | assert.isNotNull(this.terminal.pendingFont_); |
| 48 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 49 | |
| 50 | await updateFontPromise; |
| 51 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one'); |
| 52 | assert.isNull(this.terminal.pendingFont_); |
| 53 | await sleep(0); |
| 54 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), [[]]); |
| 55 | }); |
| 56 | |
| 57 | it('refresh font when the font is the same', async function() { |
| 58 | this.mocks.term.baseObj.options.fontFamily = 'font one'; |
| 59 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 60 | assert.deepEqual( |
| 61 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 62 | [['font one']]); |
| 63 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one'); |
| 64 | assert.isNotNull(this.terminal.pendingFont_); |
| 65 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 66 | |
| 67 | await updateFontPromise; |
| 68 | // Note the extra space at the end. |
| 69 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one '); |
| 70 | assert.isNull(this.terminal.pendingFont_); |
| 71 | await sleep(0); |
| 72 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), [[]]); |
| 73 | }); |
| 74 | |
| 75 | it('aborts if pendingFont_ was changed', async function() { |
| 76 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 77 | assert.deepEqual( |
| 78 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 79 | [['font one']]); |
| 80 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 81 | assert.isNotNull(this.terminal.pendingFont_); |
| 82 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 83 | |
| 84 | this.terminal.pendingFont_ = 'font two'; |
| 85 | |
| 86 | await updateFontPromise; |
| 87 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 88 | assert.equal(this.terminal.pendingFont_, 'font two'); |
| 89 | await sleep(0); |
| 90 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 91 | }); |
| 92 | }); |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 93 | |
| 94 | it('customKeyEventHandler_', async function() { |
| 95 | const mockHandler = new MockFunction(); |
| 96 | const fakeEvent = { |
| 97 | type: 'keydown', |
| 98 | keyCode: 65, |
| 99 | ctrlKey: true, |
| 100 | }; |
| 101 | this.terminal.keyDownHandlers_.set(encodeKeyCombo(Modifier.Ctrl, 65), |
| 102 | mockHandler.proxy); |
| 103 | assert.isFalse(this.terminal.customKeyEventHandler_(fakeEvent)); |
| 104 | const history = mockHandler.popHistory(); |
| 105 | assert.equal(history.length, 1); |
| 106 | assert.equal(history[0][0], fakeEvent); |
| 107 | |
| 108 | assert.isFalse(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 109 | type: 'keypress'})); |
| 110 | assert.isEmpty(mockHandler.popHistory()); |
| 111 | |
| 112 | assert.isTrue(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 113 | shiftKey: true})); |
| 114 | assert.isEmpty(mockHandler.popHistory()); |
| 115 | |
| 116 | assert.isTrue(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 117 | keyCode: 66})); |
| 118 | assert.isEmpty(mockHandler.popHistory()); |
| 119 | |
| 120 | assert.isTrue(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 121 | ctrlKey: false})); |
| 122 | assert.isEmpty(mockHandler.popHistory()); |
| 123 | }); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 124 | }); |
| 125 | }); |