Skip to content

Commit 1d13779

Browse files
authored
[UITest] Add UI Automation for advanced paste (#40803)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request * Clipboard History - [x] Open Settings and Enable clipboard history (if not enabled already). Open Advanced Paste window with hotkey, click Clipboard history and try deleting some entry. Check OS clipboard history (Win+V), and confirm that the same entry no longer exist. - [x] Open Advanced Paste window with hotkey, click Clipboard history, and click any entry (but first). Observe that entry is put on top of clipboard history. Check OS clipboard history (Win+V), and confirm that the same entry is on top of the clipboard. - [x] Open Settings and Disable clipboard history. Open Advanced Paste window with hotkey and observe that Clipboard history button is disabled. * Disable Advanced Paste, try different Advanced Paste hotkeys and confirm that it's disabled and nothing happens. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [ ] Closes: #40681 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed --------- Signed-off-by: Shawn Yuan <[email protected]>
1 parent 37bd24d commit 1d13779

File tree

1 file changed

+243
-6
lines changed

1 file changed

+243
-6
lines changed

src/modules/AdvancedPaste/UITest-AdvancedPaste/AdvancedPasteUITest.cs

Lines changed: 243 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
67
using System.Diagnostics;
78
using System.Drawing;
89
using System.Globalization;
@@ -11,7 +12,6 @@
1112
using System.Runtime.InteropServices;
1213
using System.Threading;
1314
using System.Windows.Forms;
14-
1515
using Microsoft.AdvancedPaste.UITests.Helper;
1616
using Microsoft.CodeCoverage.Core.Reports.Coverage;
1717
using Microsoft.PowerToys.UITest;
@@ -55,7 +55,7 @@ static AdvancedPasteUITest()
5555
}
5656

5757
public AdvancedPasteUITest()
58-
: base(PowerToysModule.PowerToysSettings, size: WindowSize.Small)
58+
: base(PowerToysModule.PowerToysSettings, size: WindowSize.Large_Vertical)
5959
{
6060
Type currentTestType = typeof(AdvancedPasteUITest);
6161
string? dirName = Path.GetDirectoryName(currentTestType.Assembly.Location);
@@ -254,13 +254,250 @@ public void TestCasePasteAsJSONCase3()
254254

255255
/*
256256
* Clipboard History
257-
- [] Open Settings and Enable clipboard history (if not enabled already). Open Advanced Paste window with hotkey, click Clipboard history and try deleting some entry. Check OS clipboard history (Win+V), and confirm that the same entry no longer exist.
258-
- [] Open Advanced Paste window with hotkey, click Clipboard history, and click any entry (but first). Observe that entry is put on top of clipboard history. Check OS clipboard history (Win+V), and confirm that the same entry is on top of the clipboard.
259-
- [] Open Settings and Disable clipboard history. Open Advanced Paste window with hotkey and observe that Clipboard history button is disabled.
257+
- [x] Open Settings and Enable clipboard history (if not enabled already). Open Advanced Paste window with hotkey, click Clipboard history and try deleting some entry. Check OS clipboard history (Win+V), and confirm that the same entry no longer exist.
258+
- [x] Open Advanced Paste window with hotkey, click Clipboard history, and click any entry (but first). Observe that entry is put on top of clipboard history. Check OS clipboard history (Win+V), and confirm that the same entry is on top of the clipboard.
259+
- [x] Open Settings and Disable clipboard history. Open Advanced Paste window with hotkey and observe that Clipboard history button is disabled.
260260
* Disable Advanced Paste, try different Advanced Paste hotkeys and confirm that it's disabled and nothing happens.
261261
*/
262-
private void TestCaseClipboardHistory()
262+
[TestMethod]
263+
[TestCategory("AdvancedPasteUITest")]
264+
[TestCategory("TestCaseClipboardHistoryDeleteTest")]
265+
public void TestCaseClipboardHistoryDeleteTest()
263266
{
267+
RestartScopeExe();
268+
Thread.Sleep(1500);
269+
270+
// Find the PowerToys Settings window
271+
var settingsWindow = Find<Window>("PowerToys Settings", global: true);
272+
Assert.IsNotNull(settingsWindow, "Failed to open PowerToys Settings window");
273+
274+
if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0)
275+
{
276+
// Expand Advanced list-group if needed
277+
Find<NavigationViewItem>("System Tools").Click();
278+
}
279+
280+
Find<NavigationViewItem>("Advanced Paste").Click();
281+
282+
Find<ToggleSwitch>("Clipboard history").Toggle(true);
283+
284+
Session.CloseMainWindow();
285+
286+
// clear system clipboard
287+
ClearSystemClipboardHistory();
288+
289+
// set test content to clipboard
290+
const string textForTesting = "Test text";
291+
SetClipboardTextInSTAMode(textForTesting);
292+
293+
// Open Advanced Paste window with hotkey, click Clipboard history and try deleting some entry.
294+
this.SendKeys(Key.Win, Key.Shift, Key.V);
295+
Thread.Sleep(1500);
296+
297+
var apWind = this.Find<Window>("Advanced Paste", global: true);
298+
apWind.Find<PowerToys.UITest.Button>("Clipboard history").Click();
299+
300+
var textGroup = apWind.Find<Group>(textForTesting);
301+
Assert.IsNotNull(textGroup, "Cannot find the test string from advanced paste clipboard history.");
302+
303+
textGroup.Find<PowerToys.UITest.Button>("More options").Click();
304+
apWind.Find<TextBlock>("Delete").Click();
305+
306+
// Check OS clipboard history (Win+V), and confirm that the same entry no longer exist.
307+
this.SendKeys(Key.Win, Key.V);
308+
309+
Thread.Sleep(1500);
310+
311+
var clipboardWindow = this.Find<Window>("Windows Input Experience", global: true);
312+
Assert.IsNotNull(clipboardWindow, "Cannot find system clipboard window.");
313+
314+
var nothingText = clipboardWindow.Find<Group>("Nothing here, You'll see your clipboard history here once you've copied something.");
315+
Assert.IsNotNull(nothingText, "System clipboard is not empty, which should be yes.");
316+
}
317+
318+
[TestMethod]
319+
[TestCategory("AdvancedPasteUITest")]
320+
[TestCategory("TestCaseClipboardHistorySelectTest")]
321+
public void TestCaseClipboardHistorySelectTest()
322+
{
323+
RestartScopeExe();
324+
Thread.Sleep(1500);
325+
326+
// Find the PowerToys Settings window
327+
var settingsWindow = Find<Window>("PowerToys Settings", global: true);
328+
Assert.IsNotNull(settingsWindow, "Failed to open PowerToys Settings window");
329+
330+
if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0)
331+
{
332+
// Expand Advanced list-group if needed
333+
Find<NavigationViewItem>("System Tools").Click();
334+
}
335+
336+
Find<NavigationViewItem>("Advanced Paste").Click();
337+
338+
Find<ToggleSwitch>("Clipboard history").Toggle(true);
339+
340+
Session.CloseMainWindow();
341+
342+
// clear system clipboard
343+
ClearSystemClipboardHistory();
344+
345+
// set test content to clipboard
346+
string[] textForTesting = { "Test text1", "Test text2", "Test text3", "Test text4", "Test text5", "Test text6", };
347+
foreach (var str in textForTesting)
348+
{
349+
SetClipboardTextInSTAMode(str);
350+
Thread.Sleep(1000);
351+
}
352+
353+
// Open Advanced Paste window with hotkey
354+
this.SendKeys(Key.Win, Key.Shift, Key.V);
355+
Thread.Sleep(1500);
356+
357+
var apWind = this.Find<Window>("Advanced Paste", global: true);
358+
apWind.Find<PowerToys.UITest.Button>("Clipboard history").Click();
359+
360+
// click the 3rd item
361+
var textGroup = apWind.Find<Group>(textForTesting[0]);
362+
Assert.IsNotNull(textGroup, "Cannot find the test string from advanced paste clipboard history.");
363+
textGroup.Click();
364+
365+
// Check OS clipboard history (Win+V)
366+
this.SendKeys(Key.Win, Key.V);
367+
368+
Thread.Sleep(1500);
369+
370+
var clipboardWindow = this.Find<Window>("Windows Input Experience", global: true);
371+
Assert.IsNotNull(clipboardWindow, "Cannot find system clipboard window.");
372+
373+
var txtFound = clipboardWindow.Find<Element>(textForTesting[0]);
374+
Assert.IsNotNull(txtFound, "Cannot find textblock");
375+
}
376+
377+
// [x] Open Settings and Disable clipboard history.Open Advanced Paste window with hotkey and observe that Clipboard history button is disabled.
378+
[TestMethod]
379+
[TestCategory("AdvancedPasteUITest")]
380+
[TestCategory("TestCaseClipboardHistoryDisableTest")]
381+
public void TestCaseClipboardHistoryDisableTest()
382+
{
383+
RestartScopeExe();
384+
Thread.Sleep(1500);
385+
386+
// Find the PowerToys Settings window
387+
var settingsWindow = Find<Window>("PowerToys Settings", global: true);
388+
Assert.IsNotNull(settingsWindow, "Failed to open PowerToys Settings window");
389+
390+
if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0)
391+
{
392+
// Expand Advanced list-group if needed
393+
Find<NavigationViewItem>("System Tools").Click();
394+
}
395+
396+
Find<NavigationViewItem>("Advanced Paste").Click();
397+
398+
Find<ToggleSwitch>("Clipboard history").Toggle(false);
399+
400+
Session.CloseMainWindow();
401+
402+
// set test content to clipboard
403+
const string textForTesting = "Test text";
404+
SetClipboardTextInSTAMode(textForTesting);
405+
406+
// Open Advanced Paste window with hotkey, click Clipboard history and try deleting some entry.
407+
this.SendKeys(Key.Win, Key.Shift, Key.V);
408+
Thread.Sleep(1500);
409+
410+
var apWind = this.Find<Window>("Advanced Paste", global: true);
411+
412+
// Click the button (which should still exist but be disabled)
413+
apWind.Find<PowerToys.UITest.Button>("Clipboard history").Click();
414+
415+
// Verify that the clipboard content doesn't appear
416+
// Use a short timeout to avoid a long wait when the element doesn't exist
417+
Assert.IsFalse(
418+
Has<Group>(textForTesting),
419+
"Clipboard content should not appear when clipboard history is disabled");
420+
}
421+
422+
// Disable Advanced Paste, try different Advanced Paste hotkeys and confirm that it's disabled and nothing happens.
423+
[TestMethod]
424+
[TestCategory("AdvancedPasteUITest")]
425+
[TestCategory("TestCaseDisableAdvancedPaste")]
426+
public void TestCaseDisableAdvancedPaste()
427+
{
428+
RestartScopeExe();
429+
Thread.Sleep(1500);
430+
431+
// Find the PowerToys Settings window
432+
var settingsWindow = Find<Window>("PowerToys Settings", global: true);
433+
Assert.IsNotNull(settingsWindow, "Failed to open PowerToys Settings window");
434+
435+
if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0)
436+
{
437+
// Expand System Tools if needed
438+
Find<NavigationViewItem>("System Tools").Click();
439+
}
440+
441+
Find<NavigationViewItem>("Advanced Paste").Click();
442+
443+
// Disable Advanced Paste module
444+
var moduleToggle = Find<ToggleSwitch>("Enable Advanced Paste");
445+
moduleToggle.Toggle(false);
446+
447+
Session.CloseMainWindow();
448+
449+
// Prepare some text to test with
450+
const string textForTesting = "Test text for disabled module";
451+
SetClipboardTextInSTAMode(textForTesting);
452+
453+
// Try main Advanced Paste hotkey
454+
this.SendKeys(Key.Win, Key.Shift, Key.V);
455+
Thread.Sleep(500);
456+
457+
// Verify Advanced Paste window does not appear
458+
Assert.IsFalse(
459+
Has<Window>("Advanced Paste", global: true),
460+
"Advanced Paste window should not appear when the module is disabled");
461+
462+
// Re-enable Advanced Paste for other tests
463+
RestartScopeExe();
464+
Thread.Sleep(1500);
465+
466+
settingsWindow = Find<Window>("PowerToys Settings", global: true);
467+
468+
if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0)
469+
{
470+
Find<NavigationViewItem>("System Tools").Click();
471+
}
472+
473+
Find<NavigationViewItem>("Advanced Paste").Click();
474+
Find<ToggleSwitch>("Enable Advanced Paste").Toggle(true);
475+
476+
Session.CloseMainWindow();
477+
}
478+
479+
private void ClearSystemClipboardHistory()
480+
{
481+
this.SendKeys(Key.Win, Key.V);
482+
483+
Thread.Sleep(1500);
484+
485+
var clipboardWindow = this.Find<Window>("Windows Input Experience", global: true);
486+
Assert.IsNotNull(clipboardWindow, "Cannot find system clipboard window.");
487+
488+
clipboardWindow.Find<PowerToys.UITest.Button>("Clear all except pinned items").Click();
489+
}
490+
491+
private void SetClipboardTextInSTAMode(string text)
492+
{
493+
var thread = new Thread(() =>
494+
{
495+
System.Windows.Forms.Clipboard.SetText(text);
496+
});
497+
498+
thread.SetApartmentState(ApartmentState.STA);
499+
thread.Start();
500+
thread.Join();
264501
}
265502

266503
private void ContentCopyAndPasteDirectly(string fileName, bool isRTF = false)

0 commit comments

Comments
 (0)