Skip to content

Commit

Permalink
add the test to check that the telemetry from razor is flowing through
Browse files Browse the repository at this point in the history
  • Loading branch information
akshita31 committed Mar 21, 2019
1 parent fc20243 commit 0995d5e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 129 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ out
.vscode-test
.razor
dist/
*.razor.json

install.*

Expand Down
68 changes: 0 additions & 68 deletions test/integrationTests/integrationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,72 +34,4 @@ export async function activateCSharpExtension(): Promise<ActivationResult | unde
}
}

export async function pollUntil(fn: () => boolean, timeoutMs: number) {
const pollInterval = 50;
let timeWaited = 0;
while (!fn()) {
if (timeWaited >= timeoutMs) {
throw new Error(`Timed out after ${timeoutMs}ms.`);
}

await new Promise(r => setTimeout(r, pollInterval));
timeWaited += pollInterval;
}
}

// In tests when we edit a document if our test expects to evaluate the output of that document
// after an edit then we'll need to wait for all those edits to flush through the system. Otherwise
// the edits remain in a cached version of the document resulting in our calls to `getText` failing.
export async function waitForDocumentUpdate(
documentUri: vscode.Uri,
isUpdated: (document: vscode.TextDocument) => boolean) {
const updatedDocument = await vscode.workspace.openTextDocument(documentUri);
let updateError: any;
let documentUpdated = false;
const checkUpdated = (document: vscode.TextDocument) => {
try {
documentUpdated = isUpdated(document);
} catch (error) {
updateError = error;
}
};

checkUpdated(updatedDocument);

const registration = vscode.workspace.onDidChangeTextDocument(args => {
if (documentUri === args.document.uri) {
checkUpdated(args.document);
}
});

try {
await pollUntil(() => updateError !== undefined || documentUpdated === true, 3000);
} finally {
registration.dispose();
}

if (updateError) {
throw updateError;
}

return updatedDocument;
}

export async function extensionActivated<T>(identifier: string) {
const extension = vscode.extensions.getExtension<T>(identifier);

if (!extension) {
throw new Error(`Could not find extension '${identifier}'`);
}

if (!extension.isActive) {
await extension.activate();
}

return extension;
}


export async function htmlLanguageFeaturesExtensionReady() {
await extensionActivated<any>('vscode.html-language-features');
}
89 changes: 28 additions & 61 deletions test/razorTests/completion.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,53 @@
import * as path from 'path';
import * as vscode from 'vscode';
import testAssetWorkspace from '../integrationTests/testAssets/testAssetWorkspace';
import { activateCSharpExtension, htmlLanguageFeaturesExtensionReady } from '../integrationTests/integrationHelpers';
import { activateCSharpExtension } from '../integrationTests/integrationHelpers';
import { htmlLanguageFeaturesExtensionReady } from "./testUtils";
import { EventStream } from '../../src/EventStream';
import { EventType } from '../../src/omnisharp/EventType';
import { TelemetryEvent } from 'microsoft.aspnetcore.razor.vscode/dist/HostEventStream';
import { Subscription } from 'rxjs';

//let doc: vscode.TextDocument;
let eventStream: EventStream;
let activationResolver: (value?: any) => void;
// tslint:disable-next-line:promise-must-complete
export const extensionActivated = new Promise(resolve => {
activationResolver = resolve;
});


let subscription: Subscription;

suite(`Completions ${testAssetWorkspace.description}`, () => {
suiteSetup(async function () {
eventStream = (await activateCSharpExtension()).eventStream;
eventStream.subscribe(event =>{
if (event.type == EventType.TelemetryEvent) {
let possibleRazorEvent = <TelemetryEvent>event;
if (possibleRazorEvent.eventName == "VSCode.Razor.DocumentOpened") {
activationResolver();
}
}
});

await htmlLanguageFeaturesExtensionReady();
await testAssetWorkspace.restore();
});

setup(async () => {
const filePath = path.join(testAssetWorkspace.projects[0].projectDirectoryPath, 'Pages', 'Index.cshtml');
await vscode.workspace.openTextDocument(filePath);
//eventStream = result.eventStream;
});

// teardown(async () => {
// await vscode.commands.executeCommand('workbench.action.revertAndCloseActiveEditor');
// await pollUntil(() => vscode.window.visibleTextEditors.length === 0, 1000);
// });

suiteTeardown(async () => {
await testAssetWorkspace.cleanupWorkspace();
});

// test('Can get HTML completions on document open', async () => {
// // This test relies on the Index.cshtml document containing at least 1 HTML tag in it.
// // For the purposes of this test it locates that tag and tries to get the Html completion
// // list from it.

// const content = doc.getText();
// const tagNameIndex = content.indexOf('<') + 1;
// const docPosition = doc.positionAt(tagNameIndex);
// const completions = await vscode.commands.executeCommand<vscode.CompletionList>(
// 'vscode.executeCompletionItemProvider',
// doc.uri,
// docPosition);
// const matchingCompletions = completions!.items
// .filter(item => (typeof item.insertText === 'string') && item.insertText === 'iframe')
// .map(item => item.insertText as string);

// assert.deepEqual(matchingCompletions, ['iframe']);
// });

// test('Can complete C# code blocks', async () => {
// const lastLine = new vscode.Position(doc.lineCount - 1, 0);
// await editor.edit(edit => edit.insert(lastLine, '@{}'));
// await waitForDocumentUpdate(doc.uri, document => document.getText().indexOf('@{}') >= 0);

// const completions = await vscode.commands.executeCommand<vscode.CompletionList>(
// 'vscode.executeCompletionItemProvider',
// doc.uri,
// new vscode.Position(doc.lineCount - 1, 2));
// const matchingCompletions = completions!.items
// .filter(item => (typeof item.insertText === 'string') && item.insertText.startsWith('DateTime'))
// .map(item => item.insertText as string);
teardown(() => {
if (subscription) {
subscription.unsubscribe();
}
});

// assert.deepEqual(matchingCompletions, ['DateTime', 'DateTimeKind', 'DateTimeOffset']);
// });
test("The DocumentOpened event is received when a document is opened", async () => {

test("The telemetry event is received", async() => {
let telemetryEventResolver: (value?: any) => void;
// tslint:disable-next-line:promise-must-complete
const extensionActivated = new Promise(resolve => {
telemetryEventResolver = resolve;
});

subscription = eventStream.subscribe(event =>{
if (event.type == EventType.TelemetryEvent) {
let possibleRazorEvent = <TelemetryEvent>event;
if (possibleRazorEvent.eventName == "VSCode.Razor.DocumentOpened") {
telemetryEventResolver();
}
}
});
const filePath = path.join(testAssetWorkspace.projects[0].projectDirectoryPath, 'Pages', 'Index.cshtml');
await vscode.workspace.openTextDocument(filePath);
await extensionActivated;
});
});
14 changes: 14 additions & 0 deletions test/razorTests/testUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from "vscode";

export async function htmlLanguageFeaturesExtensionReady() {
let extension = vscode.extensions.getExtension<any>('vscode.html-language-features');

if (!extension.isActive) {
await extension.activate();
}
}

0 comments on commit 0995d5e

Please sign in to comment.