Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ public override void DoPreview<T>(T dataSource)

// WebView2.NavigateToString() limitation
// See https://learn.microsoft.com/dotnet/api/microsoft.web.webview2.core.corewebview2.navigatetostring?view=webview2-dotnet-1.0.864.35#remarks
// While testing the limit, it turned out it is ~1.5MB, so to be on a safe side we go for 1.5m bytes
if (markdownHTML.Length > 1_500_000)
// While testing the limit, it turned out it is ~1.5MB of UTF-8 encoded content, so to be on the safe side we check the UTF-8 byte count.
// Using character count (string.Length) is not sufficient because multi-byte UTF-8 characters (e.g. CJK) can cause the byte size to exceed the limit even when the character count is below it.
if (System.Text.Encoding.UTF8.GetByteCount(markdownHTML) > 1_500_000)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the requested coverage in MarkdownPreviewHandlerTest.cs:

  • multi-byte UTF-8 content under 1.5M chars but over 2MB -> temp-file navigation
  • content under both thresholds -> NavigateToString
  • ASCII content over 1.5M chars -> temp-file navigation

The tests verify the navigation path by checking whether _localFileURI is populated and, for temp-file cases, that the WebView source points to the generated file. I also rebuilt the handler/test projects with MSBuild and ran PreviewPaneUnitTests via vstest.console.exe (14/14 passing).

{
string filename = _webView2UserDataFolder + "\\" + Guid.NewGuid().ToString() + ".html";
File.WriteAllText(filename, markdownHTML);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;

Expand All @@ -16,6 +19,9 @@ namespace MarkdownPreviewHandlerUnitTests
[STATestClass]
public class MarkdownPreviewHandlerTest
{
private const int NavigateToStringUtf8LimitInBytes = 1_500_000;
private const int OversizedUtf8FileThresholdInBytes = 2_000_000;

// A long timeout is needed. WebView2 can take a long time to load the first time in some CI systems.
private static readonly int HardTimeoutInMilliseconds = 60000;
private static readonly int SleepTimeInMilliseconds = 200;
Expand Down Expand Up @@ -161,5 +167,141 @@ public void MarkdownPreviewHandlerControlUpdateInfobarSettingsWhenDoPreviewIsCal
Assert.AreEqual(true, ((RichTextBox)markdownPreviewHandlerControl.Controls[1]).Multiline);
}
}

[TestMethod]
public void MarkdownPreviewHandlerControlUsesTempFileNavigationWhenUtf8ByteCountExceedsThresholdWithMultiByteCharacters()
{
string content = new string('漢', 700_000);

Assert.IsTrue(content.Length < NavigateToStringUtf8LimitInBytes);
Assert.IsTrue(Encoding.UTF8.GetByteCount(content) > OversizedUtf8FileThresholdInBytes);

string filePath = CreateMarkdownFile(content);

try
{
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
{
markdownPreviewHandlerControl.DoPreview(filePath);

WaitForBrowserControl(markdownPreviewHandlerControl);

AssertUsesTempFileNavigation(markdownPreviewHandlerControl);
}
}
finally
{
DeleteFileIfExists(filePath);
}
}

[TestMethod]
public void MarkdownPreviewHandlerControlUsesNavigateToStringWhenContentIsWithinCharacterAndUtf8ByteThresholds()
{
string content = new string('a', 10_000);

Assert.IsTrue(content.Length < NavigateToStringUtf8LimitInBytes);
Assert.IsTrue(Encoding.UTF8.GetByteCount(content) < NavigateToStringUtf8LimitInBytes);

string filePath = CreateMarkdownFile(content);

try
{
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
{
markdownPreviewHandlerControl.DoPreview(filePath);

WaitForBrowserControl(markdownPreviewHandlerControl);

AssertUsesNavigateToString(markdownPreviewHandlerControl);
}
}
finally
{
DeleteFileIfExists(filePath);
}
}

[TestMethod]
public void MarkdownPreviewHandlerControlUsesTempFileNavigationWhenAsciiContentExceedsCharacterThreshold()
{
string content = new string('a', 1_600_000);

Assert.IsTrue(content.Length > NavigateToStringUtf8LimitInBytes);
Assert.IsTrue(Encoding.UTF8.GetByteCount(content) > NavigateToStringUtf8LimitInBytes);

string filePath = CreateMarkdownFile(content);

try
{
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
{
markdownPreviewHandlerControl.DoPreview(filePath);

WaitForBrowserControl(markdownPreviewHandlerControl);

AssertUsesTempFileNavigation(markdownPreviewHandlerControl);
}
}
finally
{
DeleteFileIfExists(filePath);
}
}

private static void WaitForBrowserControl(MarkdownPreviewHandlerControl markdownPreviewHandlerControl)
{
int beforeTick = Environment.TickCount;

while (markdownPreviewHandlerControl.Controls.Count == 0 && Environment.TickCount < beforeTick + HardTimeoutInMilliseconds)
{
Application.DoEvents();
Thread.Sleep(SleepTimeInMilliseconds);
}

Assert.AreEqual(1, markdownPreviewHandlerControl.Controls.Count);
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[0], typeof(WebView2));
}

private static void AssertUsesTempFileNavigation(MarkdownPreviewHandlerControl markdownPreviewHandlerControl)
{
Uri localFileUri = GetLocalFileUri(markdownPreviewHandlerControl);

Assert.IsNotNull(localFileUri);
Assert.IsTrue(File.Exists(localFileUri.LocalPath));
Assert.AreEqual(localFileUri, ((WebView2)markdownPreviewHandlerControl.Controls[0]).Source);
}

private static void AssertUsesNavigateToString(MarkdownPreviewHandlerControl markdownPreviewHandlerControl)
{
Assert.IsNull(GetLocalFileUri(markdownPreviewHandlerControl));
}

private static Uri GetLocalFileUri(MarkdownPreviewHandlerControl markdownPreviewHandlerControl)
{
FieldInfo localFileUriField = typeof(MarkdownPreviewHandlerControl).GetField("_localFileURI", BindingFlags.Instance | BindingFlags.NonPublic);

Assert.IsNotNull(localFileUriField);

return (Uri)localFileUriField.GetValue(markdownPreviewHandlerControl);
}

private static string CreateMarkdownFile(string content)
{
string generatedFilesDirectory = Path.Combine(AppContext.BaseDirectory, "HelperFiles", "Generated");
Directory.CreateDirectory(generatedFilesDirectory);

string filePath = Path.Combine(generatedFilesDirectory, $"{Guid.NewGuid():N}.md");
File.WriteAllText(filePath, content, Encoding.UTF8);
return filePath;
}

private static void DeleteFileIfExists(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
}
Loading