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 = { |
| 18 | term: new MockObject({options: {}}), |
| 19 | fontManager: new MockObject(), |
| 20 | fitAddon: new MockObject(), |
| 21 | }; |
| 22 | const testParams = {}; |
| 23 | for (const prop in this.mocks) { |
| 24 | testParams[prop] = this.mocks[prop].proxy; |
| 25 | } |
| 26 | |
| 27 | this.terminal = new XtermTerminal({ |
| 28 | storage: new lib.Storage.Memory(), |
| 29 | profileId: 'test', |
| 30 | enableWebGL: true, |
| 31 | testParams: /** @type {!XtermTerminalTestParams} */(testParams), |
| 32 | }); |
| 33 | }); |
| 34 | |
| 35 | describe('updateFont_()', function() { |
| 36 | it('updates font', async function() { |
| 37 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 38 | assert.deepEqual( |
| 39 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 40 | [['font one']]); |
| 41 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 42 | assert.isNotNull(this.terminal.pendingFont_); |
| 43 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 44 | |
| 45 | await updateFontPromise; |
| 46 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one'); |
| 47 | assert.isNull(this.terminal.pendingFont_); |
| 48 | await sleep(0); |
| 49 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), [[]]); |
| 50 | }); |
| 51 | |
| 52 | it('refresh font when the font is the same', async function() { |
| 53 | this.mocks.term.baseObj.options.fontFamily = 'font one'; |
| 54 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 55 | assert.deepEqual( |
| 56 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 57 | [['font one']]); |
| 58 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one'); |
| 59 | assert.isNotNull(this.terminal.pendingFont_); |
| 60 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 61 | |
| 62 | await updateFontPromise; |
| 63 | // Note the extra space at the end. |
| 64 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one '); |
| 65 | assert.isNull(this.terminal.pendingFont_); |
| 66 | await sleep(0); |
| 67 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), [[]]); |
| 68 | }); |
| 69 | |
| 70 | it('aborts if pendingFont_ was changed', async function() { |
| 71 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 72 | assert.deepEqual( |
| 73 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 74 | [['font one']]); |
| 75 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 76 | assert.isNotNull(this.terminal.pendingFont_); |
| 77 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 78 | |
| 79 | this.terminal.pendingFont_ = 'font two'; |
| 80 | |
| 81 | await updateFontPromise; |
| 82 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 83 | assert.equal(this.terminal.pendingFont_, 'font two'); |
| 84 | await sleep(0); |
| 85 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 86 | }); |
| 87 | }); |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame^] | 88 | |
| 89 | it('customKeyEventHandler_', async function() { |
| 90 | const mockHandler = new MockFunction(); |
| 91 | const fakeEvent = { |
| 92 | type: 'keydown', |
| 93 | keyCode: 65, |
| 94 | ctrlKey: true, |
| 95 | }; |
| 96 | this.terminal.keyDownHandlers_.set(encodeKeyCombo(Modifier.Ctrl, 65), |
| 97 | mockHandler.proxy); |
| 98 | assert.isFalse(this.terminal.customKeyEventHandler_(fakeEvent)); |
| 99 | const history = mockHandler.popHistory(); |
| 100 | assert.equal(history.length, 1); |
| 101 | assert.equal(history[0][0], fakeEvent); |
| 102 | |
| 103 | assert.isFalse(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 104 | type: 'keypress'})); |
| 105 | assert.isEmpty(mockHandler.popHistory()); |
| 106 | |
| 107 | assert.isTrue(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 108 | shiftKey: true})); |
| 109 | assert.isEmpty(mockHandler.popHistory()); |
| 110 | |
| 111 | assert.isTrue(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 112 | keyCode: 66})); |
| 113 | assert.isEmpty(mockHandler.popHistory()); |
| 114 | |
| 115 | assert.isTrue(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 116 | ctrlKey: false})); |
| 117 | assert.isEmpty(mockHandler.popHistory()); |
| 118 | }); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 119 | }); |
| 120 | }); |