|
| 1 | +# End-to-end check of cancellable close requests (#65) from Flutter on |
| 2 | +# Windows: flutter_window_title_bar_example's "Keep open" cancels every close |
| 3 | +# request through WindowCloseRequestedEvent. The window's own close button and |
| 4 | +# the system menu's Close are clicked for real: cancelled, the window stays; |
| 5 | +# allowed, it closes and the app exits normally. Owner-guarded clicks on the |
| 6 | +# example's own window and menu only, no keyboard input. |
| 7 | +$ErrorActionPreference = 'Stop' |
| 8 | +. "$PSScriptRoot\env.ps1" |
| 9 | +. "$PSScriptRoot\winput.ps1" |
| 10 | +. "$PSScriptRoot\guiapp.ps1" |
| 11 | +Add-Type @" |
| 12 | +using System; |
| 13 | +using System.Runtime.InteropServices; |
| 14 | +public static class CloseWin { |
| 15 | + [StructLayout(LayoutKind.Sequential)] public struct RECT { public int L, T, R, B; } |
| 16 | + [DllImport("dwmapi.dll")] static extern int DwmGetWindowAttribute(IntPtr h, int a, out RECT r, int size); |
| 17 | + [DllImport("user32.dll")] public static extern bool IsWindow(IntPtr h); |
| 18 | + // DWMWA_CAPTION_BUTTON_BOUNDS, relative to the window: minimize, maximize, close. |
| 19 | + public static int[] CaptionButtons(IntPtr h) { |
| 20 | + RECT r; if (DwmGetWindowAttribute(h, 5, out r, Marshal.SizeOf(typeof(RECT))) != 0) return null; |
| 21 | + return new[] { r.L, r.T, r.R, r.B }; |
| 22 | + } |
| 23 | +} |
| 24 | +"@ |
| 25 | + |
| 26 | +Start-Result "$RemoteScratch\flutter_window_title_bar_close_test.result.txt" |
| 27 | +$exe = $env:TITLE_BAR_EXAMPLE_EXE |
| 28 | +if (-not $exe) { |
| 29 | + $exe = Get-FlutterExe "$RemoteWorkspace\examples\flutter_window_title_bar_example" 'window_title_bar_example' |
| 30 | +} |
| 31 | +Assert-Idle |
| 32 | +$app = Start-GuiApp $exe |
| 33 | +# Opened now, or ExitCode stays empty for a Start-Process -PassThru process. |
| 34 | +$null = $app.Proc.Handle |
| 35 | +# The probe fails (a traceback on stderr) until the VM service is up, and |
| 36 | +# PowerShell 5.1 turns native stderr into a terminating error under Stop. |
| 37 | +function Get-ViewsQuietly { |
| 38 | + $saved = $ErrorActionPreference |
| 39 | + $ErrorActionPreference = 'Continue' |
| 40 | + try { @(Get-Views $app 2>$null) } catch { @() } finally { $ErrorActionPreference = $saved } |
| 41 | +} |
| 42 | +function Get-State { |
| 43 | + $script:win = Get-Win $app 'nativeapi*' |
| 44 | + if (-not $script:win) { throw 'The example window is missing' } |
| 45 | + $script:view = Get-ViewsQuietly | Select-Object -First 1 |
| 46 | + if (-not $script:view) { throw 'The example has no Flutter view to probe' } |
| 47 | +} |
| 48 | +function Get-Cancelled { |
| 49 | + Get-State |
| 50 | + $t = @($view.texts | % { $_[0] } | ? { $_ -like 'Cancelled closes: *' }) | Select-Object -First 1 |
| 51 | + if (-not $t) { throw 'The cancelled-closes counter is missing' } |
| 52 | + [int]$t.Split(':')[1].Trim() |
| 53 | +} |
| 54 | +function Click-CloseButton { |
| 55 | + Get-State |
| 56 | + $b = [CloseWin]::CaptionButtons([IntPtr]$win.Hwnd) |
| 57 | + if (-not $b) { throw 'No caption button bounds' } |
| 58 | + $third = [int](($b[2] - $b[0]) / 3) |
| 59 | + $pt = @(($win.Left + $b[2] - [int]($third / 2)), ($win.Top + [int](($b[1] + $b[3]) / 2))) |
| 60 | + Invoke-Click $app $pt 400 |
| 61 | + Pause 1.5 |
| 62 | +} |
| 63 | + |
| 64 | +try { |
| 65 | + $deadline = (Get-Date).AddSeconds(40) |
| 66 | + while (@(Get-ViewsQuietly).Count -eq 0 -and (Get-Date) -lt $deadline) { Pause 0.5 } |
| 67 | + Get-State |
| 68 | + Invoke-Activate $app $win |
| 69 | + Invoke-Click $app (ConvertTo-Screen $win (Get-TextCenter $view 'Keep open')) 400 |
| 70 | + Pause 0.8 |
| 71 | + Check 'the counter starts at 0' ((Get-Cancelled) -eq 0) |
| 72 | + |
| 73 | + Click-CloseButton |
| 74 | + Check 'with Keep open, the close button leaves the window open' ([CloseWin]::IsWindow([IntPtr]$win.Hwnd) -and -not $app.Proc.HasExited) |
| 75 | + Check 'and the example cancelled one request' ((Get-Cancelled) -eq 1) |
| 76 | + |
| 77 | + # The system menu's Close (the last of its fixed items), from the strip. |
| 78 | + Invoke-RightClick $app (ConvertTo-Screen $win (Get-TextCenter $view 'Drag this strip to move the window')) |
| 79 | + if (-not (Wait-Menu $app)) { throw 'No system menu opened' } |
| 80 | + $menu = @(Get-OpenMenus $app)[0] |
| 81 | + if ($menu.Items.Count -ne 6) { throw "Expected the 6 system menu items, got $($menu.Items.Count)" } |
| 82 | + $pt = Get-RectCenter $menu.Items[5].Rect |
| 83 | + Move-Cursor $pt 400 |
| 84 | + Pause 0.2 |
| 85 | + Assert-Owner $app.Proc.Id $pt[0] $pt[1] |
| 86 | + [WInput]::Click($pt[0], $pt[1]) |
| 87 | + Pause 1.5 |
| 88 | + Check 'with Keep open, the system menu Close leaves the window open' ([CloseWin]::IsWindow([IntPtr]$win.Hwnd) -and -not $app.Proc.HasExited) |
| 89 | + Check 'and the example cancelled a second request' ((Get-Cancelled) -eq 2) |
| 90 | + |
| 91 | + Invoke-Click $app (ConvertTo-Screen $win (Get-TextCenter $view 'Allow')) 400 |
| 92 | + Pause 0.8 |
| 93 | + Click-CloseButton |
| 94 | + $exited = $app.Proc.WaitForExit(10000) |
| 95 | + Check 'with Allow, the close button closes the window and the app exits' $exited |
| 96 | + if ($exited) { Check 'with exit code 0' ($app.Proc.ExitCode -eq 0) "exit code $($app.Proc.ExitCode)" } |
| 97 | +} catch { |
| 98 | + Check 'the scenario ran to the end' $false "$($_.Exception.Message) at line $($_.InvocationInfo.ScriptLineNumber)" |
| 99 | +} finally { |
| 100 | + if (-not $app.Proc.HasExited) { Stop-GuiApp $app | Out-Null } |
| 101 | +} |
| 102 | +Say "$(if ($script:Failures) { 'FAILED' } else { 'OK' })" |
0 commit comments