|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 The xterm.js authors. All rights reserved. |
| 3 | + * @license MIT |
| 4 | + */ |
| 5 | + |
| 6 | +import { assert } from 'chai'; |
| 7 | +import { openTerminal, launchBrowser, writeSync, getBrowserType } from '../../../out-test/api/TestUtils'; |
| 8 | +import { Browser, BrowserContext, Page } from '@playwright/test'; |
| 9 | +import { beforeEach } from 'mocha'; |
| 10 | + |
| 11 | +const APP = 'http://127.0.0.1:3001/test'; |
| 12 | + |
| 13 | +let browser: Browser; |
| 14 | +let context: BrowserContext; |
| 15 | +let page: Page; |
| 16 | +const width = 800; |
| 17 | +const height = 600; |
| 18 | + |
| 19 | +describe('ClipboardAddon', () => { |
| 20 | + before(async function (): Promise<any> { |
| 21 | + browser = await launchBrowser({ |
| 22 | + // Enable clipboard access in firefox, mainly for readText |
| 23 | + firefoxUserPrefs: { |
| 24 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 25 | + 'dom.events.testing.asyncClipboard': true, |
| 26 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 27 | + 'dom.events.asyncClipboard.readText': true |
| 28 | + } |
| 29 | + }); |
| 30 | + context = await browser.newContext(); |
| 31 | + if (getBrowserType().name() !== 'webkit') { |
| 32 | + // Enable clipboard access in chromium without user gesture |
| 33 | + context.grantPermissions(['clipboard-read', 'clipboard-write']); |
| 34 | + } |
| 35 | + page = await context.newPage(); |
| 36 | + await page.setViewportSize({ width, height }); |
| 37 | + await page.goto(APP); |
| 38 | + await openTerminal(page); |
| 39 | + await page.evaluate(` |
| 40 | + window.clipboardAddon = new ClipboardAddon(); |
| 41 | + window.term.loadAddon(window.clipboardAddon); |
| 42 | + `); |
| 43 | + }); |
| 44 | + |
| 45 | + after(() => { |
| 46 | + browser.close(); |
| 47 | + }); |
| 48 | + |
| 49 | + beforeEach(async () => { |
| 50 | + await page.evaluate(`window.term.reset()`); |
| 51 | + }); |
| 52 | + |
| 53 | + const testDataEncoded = 'aGVsbG8gd29ybGQ='; |
| 54 | + const testDataDecoded = 'hello world'; |
| 55 | + |
| 56 | + describe('write data', async function (): Promise<any> { |
| 57 | + it('simple string', async () => { |
| 58 | + await writeSync(page, `\x1b]52;c;${testDataEncoded}\x07`); |
| 59 | + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), testDataDecoded); |
| 60 | + }); |
| 61 | + it('invalid base64 string', async () => { |
| 62 | + await writeSync(page, `\x1b]52;c;${testDataEncoded}invalid\x07`); |
| 63 | + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); |
| 64 | + }); |
| 65 | + it('empty string', async () => { |
| 66 | + await writeSync(page, `\x1b]52;c;${testDataEncoded}\x07`); |
| 67 | + await writeSync(page, `\x1b]52;c;\x07`); |
| 68 | + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('read data', async function (): Promise<any> { |
| 73 | + it('simple string', async () => { |
| 74 | + await page.evaluate(` |
| 75 | + window.data = []; |
| 76 | + window.term.onData(e => data.push(e)); |
| 77 | + `); |
| 78 | + await page.evaluate(() => window.navigator.clipboard.writeText('hello world')); |
| 79 | + await writeSync(page, `\x1b]52;c;?\x07`); |
| 80 | + assert.deepEqual(await page.evaluate('window.data'), [`\x1b]52;c;${testDataEncoded}\x07`]); |
| 81 | + }); |
| 82 | + it('clear clipboard', async () => { |
| 83 | + await writeSync(page, `\x1b]52;c;!\x07`); |
| 84 | + await writeSync(page, `\x1b]52;c;?\x07`); |
| 85 | + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments