private async Task TestInput() { // Start a process to test against using (var process = Process.Start("notepad.exe")) { // Make sure it's started Assert.NotNull(process); // Wait until the process started it's message pump (listening for input) process.WaitForInputIdle(); User32Api.SetWindowText(process.MainWindowHandle, "TestInput"); // Find the belonging window var notepadWindow = await WindowsEnumerator.EnumerateWindowsAsync() .Where(interopWindow => { User32Api.GetWindowThreadProcessId(interopWindow.Handle, out var processId); return(processId == process.Id); }) .FirstOrDefaultAsync(); Assert.NotNull(notepadWindow); // Send input var sentInputs = KeyboardInputGenerator.KeyPresses(VirtualKeyCode.KeyR, VirtualKeyCode.KeyO, VirtualKeyCode.KeyB, VirtualKeyCode.KeyI, VirtualKeyCode.KeyN); // Test if we indead sent 10 inputs (5 x down & up) Assert.Equal((uint)10, sentInputs); // Kill the process process.Kill(); } }
public async Task Enumerate_Take10_Async() { var result = await WindowsEnumerator.EnumerateWindowsAsync().Take(10).Count(window => window != null); if (result != 10) { throw new Exception($"Expected 10, actual {result}"); } }
public async Task EnumerateAll_Async() { var result = await WindowsEnumerator.EnumerateWindowsAsync().All(window => window != null).ToTask(); if (!result) { throw new ArgumentNullException(); } }
private async Task EnumerateWindowsAsync_Find() { var textValue = Guid.NewGuid().ToString(); var form = new Form { Text = textValue, TopLevel = true }; form.Show(); // Important, otherwise Windows doesn't have time to display the window! Application.DoEvents(); await Task.Delay(400); var window = await WindowsEnumerator.EnumerateWindowsAsync().Where(info => info.GetCaption().Contains(textValue)).FirstOrDefaultAsync(); Assert.NotNull(window); }
private async Task EnumerateWindowsAsync() { var windows = await WindowsEnumerator.EnumerateWindowsAsync().ToList().ToTask().ConfigureAwait(false); Assert.True(windows.Count > 0); }