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 | }, |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame^] | 23 | _core: { |
| 24 | _renderService: { |
| 25 | dimensions: { |
| 26 | actualCellWidth: 10.5, |
| 27 | actualCellHeight: 20.5, |
| 28 | }, |
| 29 | }, |
| 30 | }, |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 31 | }), |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 32 | fontManager: new MockObject(), |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 33 | }; |
| 34 | const testParams = {}; |
| 35 | for (const prop in this.mocks) { |
| 36 | testParams[prop] = this.mocks[prop].proxy; |
| 37 | } |
| 38 | |
| 39 | this.terminal = new XtermTerminal({ |
| 40 | storage: new lib.Storage.Memory(), |
| 41 | profileId: 'test', |
| 42 | enableWebGL: true, |
| 43 | testParams: /** @type {!XtermTerminalTestParams} */(testParams), |
| 44 | }); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame^] | 45 | |
| 46 | // Some hacking because we don't run the decorate() function. Maybe we |
| 47 | // should just run it. |
| 48 | this.terminal.container_ = /** @type {!Element} */({ |
| 49 | offsetWidth: 1000, |
| 50 | offsetHeight: 500, |
| 51 | }); |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 52 | this.terminal.inited_ = true; |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 53 | }); |
| 54 | |
| 55 | describe('updateFont_()', function() { |
| 56 | it('updates font', async function() { |
| 57 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 58 | assert.deepEqual( |
| 59 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 60 | [['font one']]); |
| 61 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 62 | assert.isNotNull(this.terminal.pendingFont_); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame^] | 63 | assert.equal(this.mocks.term.popMethodHistory('resize').length, 0); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 64 | |
| 65 | await updateFontPromise; |
| 66 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one'); |
| 67 | assert.isNull(this.terminal.pendingFont_); |
| 68 | await sleep(0); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame^] | 69 | assert.equal(this.mocks.term.popMethodHistory('resize').length, 1); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 70 | }); |
| 71 | |
| 72 | it('refresh font when the font is the same', async function() { |
| 73 | this.mocks.term.baseObj.options.fontFamily = 'font one'; |
| 74 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 75 | assert.deepEqual( |
| 76 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 77 | [['font one']]); |
| 78 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one'); |
| 79 | assert.isNotNull(this.terminal.pendingFont_); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame^] | 80 | assert.equal(this.mocks.term.popMethodHistory('resize').length, 0); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 81 | |
| 82 | await updateFontPromise; |
| 83 | // Note the extra space at the end. |
| 84 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one '); |
| 85 | assert.isNull(this.terminal.pendingFont_); |
| 86 | await sleep(0); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame^] | 87 | assert.equal(this.mocks.term.popMethodHistory('resize').length, 1); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 88 | }); |
| 89 | |
| 90 | it('aborts if pendingFont_ was changed', async function() { |
| 91 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 92 | assert.deepEqual( |
| 93 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 94 | [['font one']]); |
| 95 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 96 | assert.isNotNull(this.terminal.pendingFont_); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame^] | 97 | assert.equal(this.mocks.term.popMethodHistory('resize').length, 0); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 98 | |
| 99 | this.terminal.pendingFont_ = 'font two'; |
| 100 | |
| 101 | await updateFontPromise; |
| 102 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 103 | assert.equal(this.terminal.pendingFont_, 'font two'); |
| 104 | await sleep(0); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame^] | 105 | assert.equal(this.mocks.term.popMethodHistory('resize').length, 0); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 106 | }); |
| 107 | }); |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 108 | |
| 109 | it('customKeyEventHandler_', async function() { |
| 110 | const mockHandler = new MockFunction(); |
| 111 | const fakeEvent = { |
| 112 | type: 'keydown', |
| 113 | keyCode: 65, |
| 114 | ctrlKey: true, |
| 115 | }; |
| 116 | this.terminal.keyDownHandlers_.set(encodeKeyCombo(Modifier.Ctrl, 65), |
| 117 | mockHandler.proxy); |
| 118 | assert.isFalse(this.terminal.customKeyEventHandler_(fakeEvent)); |
| 119 | const history = mockHandler.popHistory(); |
| 120 | assert.equal(history.length, 1); |
| 121 | assert.equal(history[0][0], fakeEvent); |
| 122 | |
| 123 | assert.isFalse(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 124 | type: 'keypress'})); |
| 125 | assert.isEmpty(mockHandler.popHistory()); |
| 126 | |
| 127 | assert.isTrue(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 128 | shiftKey: true})); |
| 129 | assert.isEmpty(mockHandler.popHistory()); |
| 130 | |
| 131 | assert.isTrue(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 132 | keyCode: 66})); |
| 133 | assert.isEmpty(mockHandler.popHistory()); |
| 134 | |
| 135 | assert.isTrue(this.terminal.customKeyEventHandler_({...fakeEvent, |
| 136 | ctrlKey: false})); |
| 137 | assert.isEmpty(mockHandler.popHistory()); |
| 138 | }); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 139 | }); |
| 140 | }); |