Skip to content

Commit ae5129b

Browse files
committed
Add test for GetMainMenu/GetRoot/Maximize
1 parent 14c6a63 commit ae5129b

File tree

7 files changed

+212
-49
lines changed

7 files changed

+212
-49
lines changed

NetAutoGUI.Windows/WindowExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ public static Win32UIElement GetRoot(this Window window)
4242
{
4343
throw new NotSupportedException("WPF window doesn't support GetRoot()");
4444
}
45+
46+
if (className.StartsWith("SDL_"))
47+
{
48+
throw new NotSupportedException("SDL window doesn't support GetRoot()");
49+
}
50+
51+
if (className.StartsWith("Chrome_"))
52+
{
53+
throw new NotSupportedException("Chrome window doesn't support GetRoot()");
54+
}
55+
56+
if (className.StartsWith("UnityWndClass_"))
57+
{
58+
throw new NotSupportedException("Chrome window doesn't support GetRoot()");
59+
}
60+
61+
if (className.StartsWith("Windows.UI.Core.CoreWindow") ||
62+
className.StartsWith("ApplicationFrameWindow"))
63+
{
64+
throw new NotSupportedException("UWP window doesn't support GetRoot()");
65+
}
4566
return new Win32UIElement(window.Id);
4667
}
4768

NetAutoGUI.Windows/WindowsWindowController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ public class WindowsWindowController : IWindowController
88
{
99
public Rectangle GetBoundary(Window window)
1010
{
11-
User32.GetWindowRect(window.Id.ToHWND(), out RECT rect);
11+
if (!User32.GetWindowRect(window.Id.ToHWND(), out RECT rect))
12+
{
13+
Win32Error.ThrowLastError();
14+
}
1215
return new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
1316
}
1417

Tests/NetAutoGUI.Windows.IntegrationTests/WindowExtensionsTests/ActivateShould.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void Work_Correctly()
1818
try
1919
{
2020
Window? win = process.WaitForWindowByTitle("WinFormsAppForTest1");
21-
User32.ShowWindow(new HWND(new IntPtr(win.Id)), ShowWindowCommand.SW_MINIMIZE);
21+
User32.ShowWindow(new HWND(new IntPtr(win.Id)), ShowWindowCommand.SW_MINIMIZE); //Hide the window first.
2222
var ocr = new PaddleOCREngine();
2323
ocr.DetectText(GUI.Screenshot.Screenshot().Data).Text.Should().NotContain("Zack666");
2424
win?.Activate();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Diagnostics;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace NetAutoGUI.Windows.UnitTests.WindowExtensionsTests;
6+
7+
public class GetMainMenuShould
8+
{
9+
[Fact]
10+
public void ReturnMenu_WhenHavingMainMenu()
11+
{
12+
GUIWindows.Initialize();
13+
Process process = GUI.Application.LaunchApplication("notepad.exe");
14+
try
15+
{
16+
Window? win = process.WaitForWindowLikeTitle("*");
17+
((object)win.GetMainMenu()).Should().NotBeNull();
18+
}
19+
finally
20+
{
21+
process.Kill();
22+
}
23+
}
24+
25+
[Fact]
26+
public void ThrowException_WhenNoMainMenu()
27+
{
28+
GUIWindows.Initialize();
29+
string solutionRoot = TestHelpers.GetSolutionRootDirectory();
30+
string pathOfWinFormsAppForTest1 = TestHelpers.FindFile(solutionRoot, "WinFormsAppForTest1.exe");
31+
Process process = GUI.Application.LaunchApplication(pathOfWinFormsAppForTest1);
32+
try
33+
{
34+
Window? win = process.WaitForWindowByTitle("WinFormsAppForTest1");
35+
Action action = () => win.GetMainMenu();
36+
action.Should().Throw<NotSupportedException>();
37+
}
38+
finally
39+
{
40+
process.Kill();
41+
}
42+
}
43+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Diagnostics;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace NetAutoGUI.Windows.UnitTests.WindowExtensionsTests;
6+
7+
public class GetRootShould
8+
{
9+
[Fact]
10+
public void ThrowException_WhenRunChrome()
11+
{
12+
GUIWindows.Initialize();
13+
Process process = GUI.Application.LaunchApplication("Chrome");
14+
try
15+
{
16+
Window? win = process.WaitForWindowLikeTitle("*");
17+
Action action = () => win.GetRoot();
18+
action.Should().Throw<NotSupportedException>();
19+
}
20+
finally
21+
{
22+
process.Kill();
23+
}
24+
}
25+
26+
[Fact]
27+
public void ThrowException_WhenRunWPFApp()
28+
{
29+
GUIWindows.Initialize();
30+
string solutionRoot = TestHelpers.GetSolutionRootDirectory();
31+
string pathOfWinFormsAppForTest1 = TestHelpers.FindFile(solutionRoot, "WpfAppForTest1.exe");
32+
Process process = GUI.Application.LaunchApplication(pathOfWinFormsAppForTest1);
33+
try
34+
{
35+
Window? win = process.WaitForWindowByTitle("MainWindow");
36+
Action action = () => win.GetRoot();
37+
action.Should().Throw<NotSupportedException>();
38+
}
39+
finally
40+
{
41+
process.Kill();
42+
}
43+
}
44+
45+
[Fact]
46+
public void ThrowException_WhenRunWinFormApp()
47+
{
48+
GUIWindows.Initialize();
49+
string solutionRoot = TestHelpers.GetSolutionRootDirectory();
50+
string pathOfWinFormsAppForTest1 = TestHelpers.FindFile(solutionRoot, "WinFormsAppForTest1.exe");
51+
Process process = GUI.Application.LaunchApplication(pathOfWinFormsAppForTest1);
52+
try
53+
{
54+
Window? win = process.WaitForWindowByTitle("WinFormsAppForTest1");
55+
win.GetRoot().Should().NotBeNull();
56+
}
57+
finally
58+
{
59+
process.Kill();
60+
}
61+
}
62+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Diagnostics;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace NetAutoGUI.Windows.UnitTests.WindowExtensionsTests;
6+
7+
public class MaximizeShould
8+
{
9+
[Fact]
10+
public void Work_Correctly()
11+
{
12+
GUIWindows.Initialize();
13+
string solutionRoot = TestHelpers.GetSolutionRootDirectory();
14+
string pathOfWinFormsAppForTest1 = TestHelpers.FindFile(solutionRoot, "WinFormsAppForTest1.exe");
15+
Process process = GUI.Application.LaunchApplication(pathOfWinFormsAppForTest1);
16+
try
17+
{
18+
Window? win = process.WaitForWindowByTitle("WinFormsAppForTest1");
19+
var originalBoundary = win.Boundary;
20+
win.Maximize();
21+
GUI.Pause(1);
22+
var newBoundary = win.Boundary;
23+
newBoundary.Area.Should().BeGreaterThan(originalBoundary.Area);
24+
25+
newBoundary.X.Should().BeLessThanOrEqualTo(0);
26+
newBoundary.Y.Should().BeLessThanOrEqualTo(0);
27+
newBoundary.Width.Should().BeGreaterThan(originalBoundary.Width);
28+
newBoundary.Height.Should().BeGreaterThan(originalBoundary.Height);
29+
}
30+
finally
31+
{
32+
process.Kill();
33+
}
34+
}
35+
}

Tests/WinFormsAppForTest1/FormMain.Designer.cs

Lines changed: 46 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)