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'; |
| 10 | import {XtermTerminal, XtermTerminalTestParams} from './terminal_emulator.js'; |
| 11 | import {MockObject} from './terminal_test_mocks.js'; |
| 12 | |
| 13 | describe('terminal_emulator_tests.js', function() { |
| 14 | describe('XtermTerminal', function() { |
| 15 | beforeEach(async function() { |
| 16 | this.mocks = { |
| 17 | term: new MockObject({options: {}}), |
| 18 | fontManager: new MockObject(), |
| 19 | fitAddon: new MockObject(), |
| 20 | }; |
| 21 | const testParams = {}; |
| 22 | for (const prop in this.mocks) { |
| 23 | testParams[prop] = this.mocks[prop].proxy; |
| 24 | } |
| 25 | |
| 26 | this.terminal = new XtermTerminal({ |
| 27 | storage: new lib.Storage.Memory(), |
| 28 | profileId: 'test', |
| 29 | enableWebGL: true, |
| 30 | testParams: /** @type {!XtermTerminalTestParams} */(testParams), |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | describe('updateFont_()', function() { |
| 35 | it('updates font', async function() { |
| 36 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 37 | assert.deepEqual( |
| 38 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 39 | [['font one']]); |
| 40 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 41 | assert.isNotNull(this.terminal.pendingFont_); |
| 42 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 43 | |
| 44 | await updateFontPromise; |
| 45 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one'); |
| 46 | assert.isNull(this.terminal.pendingFont_); |
| 47 | await sleep(0); |
| 48 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), [[]]); |
| 49 | }); |
| 50 | |
| 51 | it('refresh font when the font is the same', async function() { |
| 52 | this.mocks.term.baseObj.options.fontFamily = 'font one'; |
| 53 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 54 | assert.deepEqual( |
| 55 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 56 | [['font one']]); |
| 57 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one'); |
| 58 | assert.isNotNull(this.terminal.pendingFont_); |
| 59 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 60 | |
| 61 | await updateFontPromise; |
| 62 | // Note the extra space at the end. |
| 63 | assert.equal(this.mocks.term.baseObj.options.fontFamily, 'font one '); |
| 64 | assert.isNull(this.terminal.pendingFont_); |
| 65 | await sleep(0); |
| 66 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), [[]]); |
| 67 | }); |
| 68 | |
| 69 | it('aborts if pendingFont_ was changed', async function() { |
| 70 | const updateFontPromise = this.terminal.updateFont_('font one'); |
| 71 | assert.deepEqual( |
| 72 | await this.mocks.fontManager.whenCalled('loadFont'), |
| 73 | [['font one']]); |
| 74 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 75 | assert.isNotNull(this.terminal.pendingFont_); |
| 76 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 77 | |
| 78 | this.terminal.pendingFont_ = 'font two'; |
| 79 | |
| 80 | await updateFontPromise; |
| 81 | assert.equal(this.mocks.term.baseObj.options.fontFamily, undefined); |
| 82 | assert.equal(this.terminal.pendingFont_, 'font two'); |
| 83 | await sleep(0); |
| 84 | assert.deepEqual(this.mocks.fitAddon.getMethodHistory('fit'), []); |
| 85 | }); |
| 86 | }); |
| 87 | }); |
| 88 | }); |