From 9df9caca64b21f5402d90bd9ebe0182389e80de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Thu, 14 Nov 2024 12:28:19 +0100 Subject: [PATCH 1/3] Implement change themes in Appium actions on Windows --- .../Issues/ThemeChange.xaml.cs | 2 +- .../Tests/Issues/ThemeChange.cs | 2 +- .../Actions/AppiumWindowsThemeChangeAction.cs | 67 +++++++++++++++++++ .../src/UITest.Appium/AppiumWindowsApp.cs | 1 + .../src/UITest.Appium/HelperExtensions.cs | 4 +- 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/TestUtils/src/UITest.Appium/Actions/AppiumWindowsThemeChangeAction.cs diff --git a/src/Controls/tests/TestCases.HostApp/Issues/ThemeChange.xaml.cs b/src/Controls/tests/TestCases.HostApp/Issues/ThemeChange.xaml.cs index 85c4f5d5a304..61becd8d9c4c 100644 --- a/src/Controls/tests/TestCases.HostApp/Issues/ThemeChange.xaml.cs +++ b/src/Controls/tests/TestCases.HostApp/Issues/ThemeChange.xaml.cs @@ -1,6 +1,6 @@ namespace Maui.Controls.Sample.Issues { - [Issue(IssueTracker.None, 2, "UI theme change during the runtime", PlatformAffected.Android | PlatformAffected.iOS)] + [Issue(IssueTracker.None, 2, "UI theme change during the runtime", PlatformAffected.Android | PlatformAffected.iOS | PlatformAffected.UWP)] public partial class ThemeChange : ContentPage { public ThemeChange() diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/ThemeChange.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/ThemeChange.cs index be0ba5dc1137..70035f383f74 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/ThemeChange.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/ThemeChange.cs @@ -1,4 +1,4 @@ -#if ANDROID || IOS +#if ANDROID || IOS || TEST_FAILS_ON_WINDOWS // Using AppThemeBinding and changing theme not working on Windows using NUnit.Framework; using UITest.Appium; using UITest.Core; diff --git a/src/TestUtils/src/UITest.Appium/Actions/AppiumWindowsThemeChangeAction.cs b/src/TestUtils/src/UITest.Appium/Actions/AppiumWindowsThemeChangeAction.cs new file mode 100644 index 000000000000..6a1530081522 --- /dev/null +++ b/src/TestUtils/src/UITest.Appium/Actions/AppiumWindowsThemeChangeAction.cs @@ -0,0 +1,67 @@ +using System.Diagnostics; +using UITest.Core; + +namespace UITest.Appium +{ + public class AppiumWindowsThemeChangeAction : ICommandExecutionGroup + { + const string SetLightTheme = "setLightTheme"; + const string SetDarkTheme = "setDarkTheme"; + + readonly List _commands = new() + { + SetLightTheme, + SetDarkTheme + }; + + public CommandResponse Execute(string commandName, IDictionary parameters) + { + if (commandName == SetLightTheme) + { + ExecuteCommand($"start C://Windows/Resources/Themes/aero.theme"); + return CommandResponse.SuccessEmptyResponse; + } + else if (commandName == SetDarkTheme) + { + ExecuteCommand($"start C://Windows/Resources/Themes/dark.theme"); + return CommandResponse.SuccessEmptyResponse; + } + + return CommandResponse.FailedEmptyResponse; + } + + public bool IsCommandSupported(string commandName) + { + return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase); + } + + private static void ExecuteCommand(string command) + { + var shell = GetShell(); + var shellArgument = GetShellArgument(shell, command); + + var processInfo = new ProcessStartInfo(shell, shellArgument) + { + CreateNoWindow = true, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true + }; + + var process = new Process { StartInfo = processInfo }; + + process.Start(); + process.WaitForExit(); + } + + private static string GetShell() + { + return "cmd.exe"; + } + + private static string GetShellArgument(string shell, string command) + { + return $"/C {command}"; + } + } +} diff --git a/src/TestUtils/src/UITest.Appium/AppiumWindowsApp.cs b/src/TestUtils/src/UITest.Appium/AppiumWindowsApp.cs index 4f5b84630ff0..49e8e33caecb 100644 --- a/src/TestUtils/src/UITest.Appium/AppiumWindowsApp.cs +++ b/src/TestUtils/src/UITest.Appium/AppiumWindowsApp.cs @@ -11,6 +11,7 @@ public AppiumWindowsApp(Uri remoteAddress, IConfig config) : base(new WindowsDriver(remoteAddress, GetOptions(config)), config) { _commandExecutor.AddCommandGroup(new AppiumWindowsStepperActions(this)); + _commandExecutor.AddCommandGroup(new AppiumWindowsThemeChangeAction()); } public override ApplicationState AppState diff --git a/src/TestUtils/src/UITest.Appium/HelperExtensions.cs b/src/TestUtils/src/UITest.Appium/HelperExtensions.cs index f900fb8b38c5..7b18feb3fb3e 100644 --- a/src/TestUtils/src/UITest.Appium/HelperExtensions.cs +++ b/src/TestUtils/src/UITest.Appium/HelperExtensions.cs @@ -1451,7 +1451,7 @@ public static TestDevice GetTestDevice(this IApp app) /// Represents the main gateway to interact with an app. public static void SetLightTheme(this IApp app) { - if (app is not AppiumAndroidApp && app is not AppiumIOSApp) + if (app is not AppiumAndroidApp && app is not AppiumIOSApp && app is not AppiumWindowsApp) { throw new InvalidOperationException($"SetLightTheme is not supported"); } @@ -1465,7 +1465,7 @@ public static void SetLightTheme(this IApp app) /// Represents the main gateway to interact with an app. public static void SetDarkTheme(this IApp app) { - if (app is not AppiumAndroidApp && app is not AppiumIOSApp) + if (app is not AppiumAndroidApp && app is not AppiumIOSApp && app is not AppiumWindowsApp) { throw new InvalidOperationException($"SetDarkTheme is not supported"); } From 97056afd9501aef93a1e5b7d2bee4ec222cbca49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Fri, 15 Nov 2024 11:08:53 +0100 Subject: [PATCH 2/3] Fix tests --- src/TestUtils/src/UITest.Appium/HelperExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TestUtils/src/UITest.Appium/HelperExtensions.cs b/src/TestUtils/src/UITest.Appium/HelperExtensions.cs index 7b18feb3fb3e..0f3e76d76e30 100644 --- a/src/TestUtils/src/UITest.Appium/HelperExtensions.cs +++ b/src/TestUtils/src/UITest.Appium/HelperExtensions.cs @@ -1451,7 +1451,7 @@ public static TestDevice GetTestDevice(this IApp app) /// Represents the main gateway to interact with an app. public static void SetLightTheme(this IApp app) { - if (app is not AppiumAndroidApp && app is not AppiumIOSApp && app is not AppiumWindowsApp) + if (app is AppiumCatalystApp) { throw new InvalidOperationException($"SetLightTheme is not supported"); } @@ -1465,7 +1465,7 @@ public static void SetLightTheme(this IApp app) /// Represents the main gateway to interact with an app. public static void SetDarkTheme(this IApp app) { - if (app is not AppiumAndroidApp && app is not AppiumIOSApp && app is not AppiumWindowsApp) + if (app is AppiumCatalystApp) { throw new InvalidOperationException($"SetDarkTheme is not supported"); } From 518f261a656866cd2c10d682cb6fbef77a5ae00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Mon, 18 Nov 2024 02:25:42 +0100 Subject: [PATCH 3/3] More changes --- .../tests/TestCases.Shared.Tests/Tests/Issues/ThemeChange.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/ThemeChange.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/ThemeChange.cs index 70035f383f74..ed58e53309b3 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/ThemeChange.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/ThemeChange.cs @@ -1,4 +1,4 @@ -#if ANDROID || IOS || TEST_FAILS_ON_WINDOWS // Using AppThemeBinding and changing theme not working on Windows +#if ANDROID || IOS // Using AppThemeBinding and changing theme not working on Windows using NUnit.Framework; using UITest.Appium; using UITest.Core;