提交 565f971f 编写于 作者: M Matt Bierner

Add basic test for webview communication

上级 a529621b
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'mocha';
import * as assert from 'assert';
import * as vscode from 'vscode';
const webviewId = 'myWebview';
suite('Webview tests', () => {
test('webview communication', async () => {
const webview = vscode.window.createWebviewPanel(webviewId, 'title', { viewColumn: vscode.ViewColumn.One }, { enableScripts: true });
webview.webview.html = createHtmlDocumentWithBody(/*html*/`
<script>
const vscode = acquireVsCodeApi();
window.addEventListener('message', (message) => {
vscode.postMessage({ value: message.data.value + 1 });
});
</script>`);
const p = new Promise<any>(resolve => {
webview.webview.onDidReceiveMessage(message => {
resolve(message);
});
});
webview.webview.postMessage({ value: 1 });
const response = await p;
assert.strictEqual(response.value, 2);
});
});
function createHtmlDocumentWithBody(body: string): string {
return /*html*/`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
${body}
</body>
</html>`;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册