This commit is contained in:
Diego Colombo 2022-06-09 12:56:51 +01:00 коммит произвёл Diego Colombo
Родитель 7e0d21e19c
Коммит 0dd1ce1d48
7 изменённых файлов: 101 добавлений и 8 удалений

Просмотреть файл

@ -0,0 +1,32 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Threading.Tasks;
using Microsoft.Playwright;
namespace Microsoft.TryDotNet.IntegrationTests;
public class DotNetOnline
{
private readonly IPage _page;
public DotNetOnline(IPage page)
{
_page = page;
}
public Task FocusAsync()
{
return _page.EvaluateAsync("() => { dotnetOnline.focus(); }");
}
public Task ExecuteAsync()
{
return _page.EvaluateAsync("() => { dotnetOnline.execute(); }");
}
public Task SetCodeAsync(string code)
{
return _page.EvaluateAsync("(code) => { dotnetOnline.setCode(code); }",code);
}
}

Просмотреть файл

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Playwright;
using Xunit;
namespace Microsoft.TryDotNet.IntegrationTests;
@ -18,7 +19,7 @@ public class LearnIntegrationTests : PlaywrightTestBase, IClassFixture<LearnFixt
Learn = learn;
}
[Fact(Skip = "later")]
[Fact]
public async Task loads_trydotnet()
{
var page = await Playwright.Browser!.NewPageAsync();
@ -34,6 +35,13 @@ public class LearnIntegrationTests : PlaywrightTestBase, IClassFixture<LearnFixt
var pageUri = new Uri(QueryHelpers.AddQueryString(new Uri(learnRoot,"DocsHost.html").ToString(), param!));
await page.GotoAsync(pageUri.ToString());
await page.WaitForLoadStateAsync(LoadState.NetworkIdle);
var dotnetOnline = new DotNetOnline(page);
await dotnetOnline.FocusAsync();
await dotnetOnline.SetCodeAsync("Console.WriteLine(123);");
await dotnetOnline.ExecuteAsync();
throw new NotImplementedException();
}
}

Просмотреть файл

@ -111,6 +111,4 @@ window.dispatchEvent(new MessageEvent(""message"", { data: request }));
return messages;
}
}

Просмотреть файл

@ -18,7 +18,7 @@ public class CommandExecutionTests
var c = applicationBuilderFactory.CreateDefaultClient();
var code = @"{
var requests = @"{
""commands"": [
{
""commandType"": ""OpenProject"",
@ -57,7 +57,7 @@ public class CommandExecutionTests
]
}";
var requestBody = JsonContent.Create(JsonDocument.Parse(code).RootElement);
var requestBody = JsonContent.Create(JsonDocument.Parse(requests).RootElement);
var response = await c.PostAsync("commands", requestBody);
@ -74,4 +74,55 @@ public class CommandExecutionTests
assemblyProduced!.Assembly
.Value.Should().NotBeNullOrWhiteSpace();
}
[Fact]
public async Task can_open_document_with_user_code_in_region()
{
await using var applicationBuilderFactory = new WebApplicationFactory<Program>();
var c = applicationBuilderFactory.CreateDefaultClient();
var requests = @"{
""commands"": [
{
""commandType"": ""OpenProject"",
""command"": {
""project"": {
""files"": [
{
""relativeFilePath"": ""program.cs"",
""content"": ""using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Globalization;\nusing System.Text.RegularExpressions;\n\nnamespace Program\n{\n class Program\n {\n static void Main(string[] args)\n {\n #region controller\n\nConsole.WriteLine(123); #endregion\n }\n }\n}""
}
]
}
},
""token"": ""595d327c-b14f-5ad7-7da0-2579cbfa9961::22||6""
},
{
""commandType"": ""OpenDocument"",
""command"": {
""relativeFilePath"": ""./program.cs"",
""regionName"": ""controller""
},
""token"": ""595d327c-b14f-5ad7-7da0-2579cbfa9961::22||7""
}
]
}";
var requestBody = JsonContent.Create(JsonDocument.Parse(requests).RootElement);
var response = await c.PostAsync("commands", requestBody);
var responseJson = JsonDocument.Parse(await response.Content.ReadAsStringAsync(CancellationToken.None)).RootElement;
var events = responseJson.GetProperty("events").EnumerateArray().Select(KernelEventEnvelope.Deserialize).Select(ee => ee.Event).ToList();
using var _ = new AssertionScope();
response.EnsureSuccessStatusCode();
var documentOpened = events.OfType<DocumentOpened>().SingleOrDefault();
documentOpened.Should().NotBeNull();
documentOpened!.Content.Should().Contain("Console.WriteLine(123);");
}
}

Просмотреть файл

@ -5,6 +5,7 @@ function setup(global: any) {
if (container) {
const interactive = new DotNetOnline(container);
if (global) {
console.log("Setting up dotnetOnline on global");
global.dotnetOnline = interactive;
}
}

Просмотреть файл

@ -91,7 +91,8 @@ export class DotNetOnline implements InteractiveComponent {
}
public focus() {
window.postMessage({ type: 'focusEditor' }, hostOrigin);
let settings = getUrls();
this.editor.contentWindow?.postMessage({ type: 'focusEditor' }, settings.trydotnetOrigin);
return Promise.resolve();
}
@ -175,7 +176,7 @@ export class DotNetOnline implements InteractiveComponent {
private async getEditorReady(type: string) {
if (this.trydotnet) {
let settings = getUrls();
//window.postMessage({ type, editorId: "0" }, hostOrigin);
window.postMessage({ type, editorId: "0" }, hostOrigin);
const configuration: Configuration = { hostOrigin, trydotnetOrigin: settings.trydotnetOrigin, enableLogging: true };
const content = scaffoldingMethod.replace('____', '');
const fileName: string = 'program.cs';

Просмотреть файл

@ -268,7 +268,9 @@ export class TryDotNetEditor {
this.getEditor().disableLanguageService();
this.getEditor().disableTextChangedEvents();
this._currentDocument = await dotnetInteractive.submitCommandAndGetResult<dotnetInteractive.DocumentOpened>(
this.getKernel(), command, dotnetInteractive.DocumentOpenedType); //?
this.getKernel(),
command,
dotnetInteractive.DocumentOpenedType); //?
this.getEditor().setCode(this._currentDocument.content);
this.getEditor().enableTextChangedEvents();
this.getEditor().enableLanguageService();