Skip to content

Commit

Permalink
Optimized code format
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhaopeng Wang (from Dev Box) committed Feb 14, 2025
1 parent 785086a commit f6b1765
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 65 deletions.
56 changes: 21 additions & 35 deletions src/common/UITestAutomation/ModuleConfigData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.PowerToys.UITest
{
Expand All @@ -21,7 +18,7 @@ public enum PowerToysModule
public enum PowerToysModuleWindow
{
None,
PowerToys,
PowerToysSettings,
FancyZone,
KeyboardManagerKeys,
KeyboardManagerShortcuts,
Expand All @@ -36,52 +33,41 @@ public class ModuleConfigData
// Singleton instance of ModuleConfigData
private static readonly Lazy<ModuleConfigData> MInstance = new Lazy<ModuleConfigData>(() => new ModuleConfigData());

public static ModuleConfigData Instance
{
get
{
return MInstance.Value;
}
}
public static ModuleConfigData Instance => MInstance.Value;

// Dictionary to hold module window names
public Dictionary<PowerToysModuleWindow, ModuleWindowData> ModuleWindowName { get; private set; }
public Dictionary<PowerToysModuleWindow, ModuleWindowData> ModuleWindowName { get; }

// Dictionary to hold module paths
private Dictionary<PowerToysModule, string> ModulePath { get; set; }
private Dictionary<PowerToysModule, string> ModulePath { get; }

// Private constructor to initialize module data
private ModuleConfigData()
{
ModuleWindowName = new Dictionary<PowerToysModuleWindow, ModuleWindowData>();
ModuleWindowName[PowerToysModuleWindow.FancyZone] = new ModuleWindowData("PowerToys", "PowerToys Settings");
ModuleWindowName[PowerToysModuleWindow.FancyZone] = new ModuleWindowData("Fancyzone", "FancyZones Layout");
ModuleWindowName[PowerToysModuleWindow.KeyboardManagerKeys] = new ModuleWindowData("KeyboardManagerKeys", "Remap keys");
ModuleWindowName[PowerToysModuleWindow.KeyboardManagerShortcuts] = new ModuleWindowData("KeyboardManagerShortcuts", "Remap shortcuts");
ModuleWindowName[PowerToysModuleWindow.Hosts] = new ModuleWindowData("Hosts", "Hosts File Editor");

ModulePath = new Dictionary<PowerToysModule, string>();
ModulePath[PowerToysModule.FancyZone] = @"\..\..\..\PowerToys.FancyZones.exe";
ModulePath[PowerToysModule.Hosts] = @"\..\..\..\WinUI3Apps\PowerToys.Hosts.exe";
ModuleWindowName = new Dictionary<PowerToysModuleWindow, ModuleWindowData>
{
[PowerToysModuleWindow.PowerToysSettings] = new ModuleWindowData("PowerToys", "PowerToys Settings"),
[PowerToysModuleWindow.FancyZone] = new ModuleWindowData("Fancyzone", "FancyZones Layout"),
[PowerToysModuleWindow.KeyboardManagerKeys] = new ModuleWindowData("KeyboardManagerKeys", "Remap keys"),
[PowerToysModuleWindow.KeyboardManagerShortcuts] = new ModuleWindowData("KeyboardManagerShortcuts", "Remap shortcuts"),
[PowerToysModuleWindow.Hosts] = new ModuleWindowData("Hosts", "Hosts File Editor"),
};

ModulePath = new Dictionary<PowerToysModule, string>
{
[PowerToysModule.FancyZone] = @"\..\..\..\PowerToys.FancyZones.exe",
[PowerToysModule.Hosts] = @"\..\..\..\WinUI3Apps\PowerToys.Hosts.exe",
};
}

// Method to get the path of a module
public string GetModulePath(PowerToysModule scope)
{
return ModulePath[scope];
}
public string GetModulePath(PowerToysModule scope) => ModulePath[scope];

// Method to get the URL of the Windows Application Driver
public string GetWindowsApplicationDriverUrl()
{
return WindowsApplicationDriverUrl;
}
public string GetWindowsApplicationDriverUrl() => WindowsApplicationDriverUrl;

// Method to get the window data of a module
public ModuleWindowData GetModuleWindowData(PowerToysModuleWindow scope)
{
return ModuleWindowName[scope];
}
public ModuleWindowData GetModuleWindowData(PowerToysModuleWindow scope) => ModuleWindowName[scope];
}

// Struct to hold module window data
Expand Down
24 changes: 8 additions & 16 deletions src/common/UITestAutomation/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public Session(WindowsDriver<WindowsElement> root, WindowsDriver<WindowsElement>

// Method to find an element by a given selector
public T FindElement<T>(By by)
where T : Element, new()
where T : Element, new()
{
var item = WindowsDriver.FindElement(by.ToSeleniumBy());
Assert.IsNotNull(item, "Can`t find this element");
Assert.IsNotNull(item, "Can't find this element");
return NewElement<T>(item);
}

Expand All @@ -49,7 +49,7 @@ public T FindElementByName<T>(string name)
where T : Element, new()
{
var item = WindowsDriver.FindElementByName(name);
Assert.IsNotNull(item, "Can`t find this element");
Assert.IsNotNull(item, "Can't find this element");
return NewElement<T>(item);
}

Expand All @@ -58,21 +58,14 @@ public T FindElementByName<T>(string name)
where T : Element, new()
{
var items = WindowsDriver.FindElementsByName(name);
Assert.IsNotNull(items, "Can`t find this element");
List<T> res = new List<T>();
foreach (var item in items)
{
var element = NewElement<T>(item);
res.Add(element);
}

var resReadOnlyCollection = new ReadOnlyCollection<T>(res);
return resReadOnlyCollection;
Assert.IsNotNull(items, "Can't find this element");
var res = items.Select(NewElement<T>).ToList();
return new ReadOnlyCollection<T>(res);
}

// Method to create a new element of type T
private T NewElement<T>(WindowsElement element)
where T : Element, new()
where T : Element, new()
{
T newElement = new T();
newElement.SetSession(WindowsDriver);
Expand All @@ -85,12 +78,11 @@ private T NewElement<T>(WindowsElement element)
{
Thread.Sleep(4000);
string windowName = ModuleConfigData.Instance.GetModuleWindowData(module).WindowName;
string appName = ModuleConfigData.Instance.GetModuleWindowData(module).ModuleName;

if (Root != null)
{
var window = Root.FindElementByName(windowName);
Assert.IsNotNull(window, windowName + " not found");
Assert.IsNotNull(window, $"{windowName} not found");

var windowHandle = new nint(int.Parse(window.GetAttribute("NativeWindowHandle")));
SetForegroundWindow(windowHandle);
Expand Down
24 changes: 10 additions & 14 deletions src/common/UITestAutomation/UITestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions;
using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext;
using static Microsoft.PowerToys.UITest.ModuleConfigData;
using static Microsoft.PowerToys.UITest.UITestBase;

namespace Microsoft.PowerToys.UITest
{
Expand All @@ -27,7 +23,7 @@ public class UITestBase
public Session Session { get; set; }

// Instance of TestInit class
private TestInit testInit = new TestInit();
private readonly TestInit testInit = new TestInit();

// Default constructor
public UITestBase()
Expand Down Expand Up @@ -71,15 +67,17 @@ public TestInit()
var desktopCapabilities = new AppiumOptions();
desktopCapabilities.AddAdditionalCapability("app", "Root");
Root = new WindowsDriver<WindowsElement>(new Uri(ModuleConfigData.Instance.GetWindowsApplicationDriverUrl()), desktopCapabilities);

Driver = null;
}

// Method to initialize the test
[UnconditionalSuppressMessage("SingleFile", "IL3000:Avoid accessing Assembly file path when publishing as a single file", Justification = "<Pending>")]
public void Init()
{
appDriver = Process.Start(new ProcessStartInfo() { FileName = "C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe", Verb = "runas" });
appDriver = Process.Start(new ProcessStartInfo
{
FileName = "C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe",
Verb = "runas",
});

// Launch the executable
string? path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Expand All @@ -88,7 +86,7 @@ public void Init()

Assert.IsNotNull(Driver, "Session not initialized");

// Set implicit timeout to make element search to retry every 500 ms
// Set implicit timeout to make element search retry every 500 ms
Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);
}

Expand All @@ -101,13 +99,14 @@ public void UnInit()
}
catch
{
// Handle exceptions if needed
}
}

// Create a new application and take control of it
public void StartExe(string appName, string windowName, string appPath)
{
AppiumOptions opts = new AppiumOptions();
var opts = new AppiumOptions();
opts.AddAdditionalCapability("app", appPath);
Driver = new WindowsDriver<WindowsElement>(new Uri(ModuleConfigData.Instance.GetWindowsApplicationDriverUrl()), opts);
}
Expand All @@ -119,10 +118,7 @@ public void SetScope(PowerToysModule scope)
}

// Method to get the root driver
public WindowsDriver<WindowsElement> GetRoot()
{
return Root;
}
public WindowsDriver<WindowsElement> GetRoot() => Root;

// Method to get the driver
public WindowsDriver<WindowsElement> GetDriver()
Expand Down

1 comment on commit f6b1765

@github-actions
Copy link

Choose a reason for hiding this comment

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

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (2)

Fancyzone
MInstance

These words are not needed and should be removed ahk AMPROPERTY AMPROPSETID Breadcrumb CDEF comdef ddf devenum DEVMON DEVSOURCE DGR DIIRFLAG dshow DVH DVHD DVSD DVSL EData ERole fdw FILEINFOSIG Filtergraph Filterx HCERTSTORE IKs iljxck IYUV KSPROPERTY lcb ldx lld LONGLONG LTRB majortype makecab MEDIASUBTYPE mediatype mfplat mic mjpg Msimg msiquery ORAW outpin overlaywindow PAUDIO PINDIR Pnp ppmt previouscamera PROPBAG propvarutil reencode reencoded REFGUID REGFILTER REGFILTERPINS REGPINTYPES regsvr shmem sizeread stl strsafe strutil subquery SYNCMFT TMPVAR vcdl vdi vid VIDCAP VIDEOINFOHEADER vih webcam wistd WVC

To accept these unrecognized words as correct and remove the previously acknowledged and now absent words, you could run the following commands

... in a clone of the [email protected]:microsoft/PowerToys.git repository
on the zhaopengwang/UITestAutomation branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.24/apply.pl' |
perl - 'https://github.com/microsoft/PowerToys/actions/runs/13327187957/attempts/1'
If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.