-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Added unit tests for low-coverage System.Windows.Forms classes #14911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sathish-087
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
Sathish-087:Fix_Issue_13442
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
249 changes: 249 additions & 0 deletions
249
src/test/unit/System.Windows.Forms/System/Windows/Forms/HtmlHistoryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System.Text; | ||
| using Windows.Win32.Web.MsHtml; | ||
|
|
||
| namespace System.Windows.Forms.Tests; | ||
|
|
||
| [Collection("Sequential")] // workaround for WebBrowser control corrupting memory when run on multiple UI threads | ||
| public class HtmlHistoryTests | ||
| { | ||
| private const string HtmlPage1 = "<html><body>page1</body></html>"; | ||
| private const string HtmlPage2 = "<html><body>page2</body></html>"; | ||
|
|
||
| [WinFormsFact] | ||
| public async Task HtmlHistory_Length_Get_ReturnsExpected() | ||
| { | ||
| using Control parent = new(); | ||
| using WebBrowser control = new() | ||
| { | ||
| Parent = parent | ||
| }; | ||
|
|
||
| HtmlDocument document = await GetDocument(control, HtmlPage1); | ||
| document.Window.Should().NotBeNull(); | ||
| using HtmlHistory? history = document.Window.History; | ||
|
|
||
| history.Should().NotBeNull(); | ||
| // IE/WebBrowser reports engine-specific history lengths (often 0 for a single load). | ||
| // Exercise the getter and only require a non-negative value. | ||
| history!.Length.Should().BeGreaterThanOrEqualTo(0); | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public async Task HtmlHistory_DomHistory_Get_ReturnsExpected() | ||
| { | ||
| using Control parent = new(); | ||
| using WebBrowser control = new() | ||
| { | ||
| Parent = parent | ||
| }; | ||
|
|
||
| HtmlDocument document = await GetDocument(control, HtmlPage1); | ||
| document.Window.Should().NotBeNull(); | ||
| using HtmlHistory? history = document.Window.History; | ||
|
|
||
| history.Should().NotBeNull(); | ||
| object domHistory = history!.DomHistory; | ||
|
|
||
| domHistory.Should().NotBeNull(); | ||
| domHistory.Should().BeSameAs(history.DomHistory); | ||
| domHistory.GetType().IsCOMObject.Should().BeTrue(); | ||
| domHistory.Should().BeAssignableTo<IOmHistory.Interface>(); | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public async Task HtmlHistory_Back_Negative_ThrowsArgumentOutOfRangeException() | ||
| { | ||
| using Control parent = new(); | ||
| using WebBrowser control = new() | ||
| { | ||
| Parent = parent | ||
| }; | ||
|
|
||
| HtmlDocument document = await GetDocument(control, HtmlPage1); | ||
| document.Window.Should().NotBeNull(); | ||
| using HtmlHistory? history = document.Window.History; | ||
|
|
||
| history.Should().NotBeNull(); | ||
| Action action = () => history!.Back(-1); | ||
| action.Should().Throw<ArgumentOutOfRangeException>() | ||
| .And.ParamName.Should().Be("numberBack"); | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public async Task HtmlHistory_Forward_Negative_ThrowsArgumentOutOfRangeException() | ||
| { | ||
| using Control parent = new(); | ||
| using WebBrowser control = new() | ||
| { | ||
| Parent = parent | ||
| }; | ||
|
|
||
| HtmlDocument document = await GetDocument(control, HtmlPage1); | ||
| document.Window.Should().NotBeNull(); | ||
| using HtmlHistory? history = document.Window.History; | ||
|
|
||
| history.Should().NotBeNull(); | ||
| Action action = () => history!.Forward(-1); | ||
| action.Should().Throw<ArgumentOutOfRangeException>() | ||
| .And.ParamName.Should().Be("numberForward"); | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public async Task HtmlHistory_Back_And_Forward_Zero_DoNotThrow() | ||
| { | ||
| using Control parent = new(); | ||
| using WebBrowser control = new() | ||
| { | ||
| Parent = parent | ||
| }; | ||
|
|
||
| HtmlDocument document = await GetDocument(control, HtmlPage1); | ||
| document.Window.Should().NotBeNull(); | ||
| using HtmlHistory? history = document.Window.History; | ||
|
|
||
| history.Should().NotBeNull(); | ||
|
|
||
| // Zero is a no-op (guarded by number > 0) and must not call into COM go(). | ||
| Action back = () => history!.Back(0); | ||
| Action forward = () => history!.Forward(0); | ||
|
|
||
| back.Should().NotThrow(); | ||
| forward.Should().NotThrow(); | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public async Task HtmlHistory_Back_And_Forward_Positive_InvokeGo() | ||
| { | ||
| using Control parent = new(); | ||
| using WebBrowser control = new() | ||
| { | ||
| Parent = parent | ||
| }; | ||
|
|
||
| // Keep both temp files alive for the life of the test. Disposing them after | ||
| // Navigate can leave history entries pointing at deleted paths and collapse the stack. | ||
| using TempFile file1 = CreateTempFile(HtmlPage1); | ||
| using TempFile file2 = CreateTempFile(HtmlPage2); | ||
|
|
||
| await NavigateToPathAsync(control, file1.Path); | ||
| await NavigateToPathAsync(control, file2.Path); | ||
|
|
||
| // Positive Back/Forward must enter the number > 0 branch that calls IOmHistory.go. | ||
| // Do not assert Length — IE reports engine-specific values (often 1 after two loads). | ||
| // Do not wait for DocumentCompleted — go() may not raise it when the stack is thin. | ||
| control.Document.Should().NotBeNull(); | ||
| control.Document!.Window.Should().NotBeNull(); | ||
|
|
||
| using (HtmlHistory? history = control.Document.Window!.History) | ||
| { | ||
| history.Should().NotBeNull(); | ||
| Action back = () => history!.Back(1); | ||
| back.Should().NotThrow(); | ||
| } | ||
|
|
||
| // History wrapper is per-get; obtain a fresh instance after Back. | ||
| control.Document.Window.Should().NotBeNull(); | ||
| using (HtmlHistory? history = control.Document.Window!.History) | ||
| { | ||
| history.Should().NotBeNull(); | ||
| Action forward = () => history!.Forward(1); | ||
| forward.Should().NotThrow(); | ||
| } | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public async Task HtmlHistory_Go_Overloads_DoNotThrow() | ||
| { | ||
| using Control parent = new(); | ||
| using WebBrowser control = new() | ||
| { | ||
| Parent = parent | ||
| }; | ||
|
|
||
| HtmlDocument document = await GetDocument(control, HtmlPage1); | ||
| document.Window.Should().NotBeNull(); | ||
| using HtmlHistory? history = document.Window.History; | ||
|
|
||
| history.Should().NotBeNull(); | ||
|
|
||
| // Relative position always forwards to COM go(); 0 is the safe relative position. | ||
| Action goRelative = () => history!.Go(0); | ||
| // String overload intentionally accepts values that may not be fully qualified Uris. | ||
| Action goString = () => history!.Go("about:blank"); | ||
| // Uri overload is a thin wrapper over the string overload. | ||
| Action goUri = () => history!.Go(new Uri("about:blank")); | ||
|
|
||
| goRelative.Should().NotThrow(); | ||
| goString.Should().NotThrow(); | ||
| goUri.Should().NotThrow(); | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public async Task HtmlHistory_Dispose_IsIdempotentAndMembersThrow() | ||
| { | ||
| using Control parent = new(); | ||
| using WebBrowser control = new() | ||
| { | ||
| Parent = parent | ||
| }; | ||
|
|
||
| HtmlDocument document = await GetDocument(control, HtmlPage1); | ||
| document.Window.Should().NotBeNull(); | ||
| HtmlHistory? history = document.Window.History; | ||
|
|
||
| history.Should().NotBeNull(); | ||
| history!.Dispose(); | ||
|
|
||
| // Dispose is safe to call more than once. | ||
| Action disposeAgain = history.Dispose; | ||
| disposeAgain.Should().NotThrow(); | ||
|
|
||
| // Members route through NativeOmHistory, which throws once disposed. | ||
| // Back/Forward(1) also cover the disposed path past the negative check. | ||
| Action getLength = () => _ = history.Length; | ||
| Action getDomHistory = () => _ = history.DomHistory; | ||
| Action back = () => history.Back(1); | ||
| Action forward = () => history.Forward(1); | ||
| Action go = () => history.Go(0); | ||
|
|
||
| getLength.Should().Throw<ObjectDisposedException>(); | ||
| getDomHistory.Should().Throw<ObjectDisposedException>(); | ||
| back.Should().Throw<ObjectDisposedException>(); | ||
| forward.Should().Throw<ObjectDisposedException>(); | ||
| go.Should().Throw<ObjectDisposedException>(); | ||
| } | ||
|
|
||
| private static async Task<HtmlDocument> GetDocument(WebBrowser control, string html) | ||
| { | ||
| using TempFile file = CreateTempFile(html); | ||
| await NavigateToPathAsync(control, file.Path); | ||
| return control.Document!; | ||
| } | ||
|
|
||
| private static async Task NavigateToPathAsync(WebBrowser control, string path) | ||
| { | ||
| TaskCompletionSource<bool> source = new(); | ||
| void Handler(object? sender, WebBrowserDocumentCompletedEventArgs e) => source.TrySetResult(true); | ||
| control.DocumentCompleted += Handler; | ||
| try | ||
| { | ||
| await Task.Run(() => control.Navigate(path)); | ||
| Assert.True(await source.Task); | ||
|
Comment on lines
+235
to
+236
|
||
| } | ||
| finally | ||
| { | ||
| control.DocumentCompleted -= Handler; | ||
| } | ||
| } | ||
|
|
||
| private static TempFile CreateTempFile(string html) | ||
| { | ||
| byte[] data = Encoding.UTF8.GetBytes(html); | ||
| return TempFile.Create(data); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would make our test cases more comprehensive if we could also cover some edge-case navigation scenarios here—such as
Go(-1)or cases where the index falls outside the range of available history.