| 1 | #requires -Version 5.1 |
| 2 | param( |
| 3 | [Parameter(Mandatory)][string]$ProfilePath, |
| 4 | [Parameter(Mandatory)][string]$CredentialFile, |
| 5 | [Parameter(Mandatory)][string]$FixturePath, |
| 6 | [Parameter(Mandatory)][string]$EvidenceDirectory, |
| 7 | [switch]$SplitOnly, |
| 8 | [switch]$PerfOnly |
| 9 | ) |
| 10 | $ErrorActionPreference='Stop';$ProgressPreference='SilentlyContinue' |
| 11 | [Console]::OutputEncoding=[Text.UTF8Encoding]::new($false) |
| 12 | $utf8=[Text.UTF8Encoding]::new($false) |
| 13 | $copiedFixtureText=$utf8.GetString([Convert]::FromBase64String('Q2FnZSBuYXRpdmUgY29weSDkuK3mlofwn5mC')) |
| 14 | $binding=Get-Content -LiteralPath $ProfilePath -Raw|ConvertFrom-Json |
| 15 | $vm=Get-VM -Id ([guid]$binding.vm_id) |
| 16 | if($binding.version -ne 1 -or $vm.State -ne 'Running' -or @(Get-VMNetworkAdapter -VM $vm).Count -ne 0){throw 'The independently bound offline guest is not ready.'} |
| 17 | $credential=Import-Clixml -LiteralPath $CredentialFile |
| 18 | if(($credential.UserName -split '\\')[-1] -cne 'CageLabSetup'){throw 'The retained QA administrator identity does not match.'} |
| 19 | $credential=[pscredential]::new('CageLabSetup',$credential.Password) |
| 20 | $session=New-PSSession -VMId $vm.Id -Credential $credential |
| 21 | $owned=[Collections.Generic.List[Diagnostics.Process]]::new();$tabs=[Collections.Generic.List[string]]::new() |
| 22 | $results=[ordered]@{};$script:phase='starting' |
| 23 | function Bridge([string]$Operation){ |
| 24 | $s=[Diagnostics.ProcessStartInfo]::new();$s.FileName=$binding.relay_executable |
| 25 | $s.Arguments='--relay-profile "'+$ProfilePath+'" '+$Operation |
| 26 | $s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.RedirectStandardInput=$true;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$s.StandardOutputEncoding=$utf8 |
| 27 | $p=[Diagnostics.Process]::Start($s);$owned.Add($p);$null=$p.StandardError.ReadToEndAsync();return $p |
| 28 | } |
| 29 | function Send($p,$value){$bytes=$utf8.GetBytes((ConvertTo-Json -InputObject $value -Compress -Depth 12)+"`n");$p.StandardInput.BaseStream.Write($bytes,0,$bytes.Length);$p.StandardInput.BaseStream.Flush()} |
| 30 | function Read($p){$pending=$p.StandardOutput.ReadLineAsync();if(!$pending.Wait(45000) -or !$pending.Result){throw ('The owned browser response did not complete: '+$script:phase)};return $pending.Result|ConvertFrom-Json} |
| 31 | function Call([string]$Path,$Payload=@{},[string]$Method='POST'){Send $browser @{method=$Method;path=$Path;input=$Payload};$value=Read $browser;if($value.error){throw ($script:phase+': '+$value.error)};return $value} |
| 32 | function State {return Invoke-Command -Session $session -ArgumentList $guestDirectory -ScriptBlock {param($dir);$file=Join-Path $dir 'fixture-state.json';if(Test-Path -LiteralPath $file){foreach($report in (Get-Content -LiteralPath $file -Raw|ConvertFrom-Json)){$report}}}} |
| 33 | function Frame([string]$Name){ |
| 34 | $f=Call '/frame' @{} 'GET';if($f.width -lt 1 -or $f.height -lt 1 -or !$f.target){throw 'The native frame receipt is invalid.'} |
| 35 | Add-Type -AssemblyName System.Drawing |
| 36 | $image=[Drawing.Bitmap]::new($f.width,$f.height);$g=[Drawing.Graphics]::FromImage($image) |
| 37 | try { |
| 38 | $parts=@();if($f.jpeg -or $f.png){$parts+=@([pscustomobject]@{jpeg=$f.jpeg;png=$f.png;x=0;y=0;width=$f.width;height=$f.height})};$parts+=@($f.panes);$parts+=@($f.overlays) |
| 39 | foreach($part in $parts){if(!$part){continue};$encoded=if($part.png){$part.png}else{$part.jpeg};$stream=[IO.MemoryStream]::new([Convert]::FromBase64String($encoded));$piece=[Drawing.Image]::FromStream($stream);try{$g.DrawImage($piece,[Drawing.Rectangle]::new($part.x,$part.y,$part.width,$part.height))}finally{$piece.Dispose();$stream.Dispose()}} |
| 40 | $image.Save((Join-Path $EvidenceDirectory ($Name+'.png')),[Drawing.Imaging.ImageFormat]::Png) |
| 41 | } finally {$g.Dispose();$image.Dispose()} |
| 42 | return $f |
| 43 | } |
| 44 | function Pointer($Frame,[int]$X,[int]$Y,[int]$Mask){$null=Call '/input' @{kind='pointer';target=$Frame.target;x=$X;y=$Y;mask=$Mask}} |
| 45 | function Click($Frame,[double]$X,[double]$Y,[int]$Mask=1){Pointer $Frame ([int]($X*$Frame.scale)) ([int]($Y*$Frame.scale)) $Mask;Pointer $Frame ([int]($X*$Frame.scale)) ([int]($Y*$Frame.scale)) 0} |
| 46 | function Key($Frame,[uint32]$Symbol,[bool]$Down){$null=Call '/input' @{kind='key';target=$Frame.target;keysym=$Symbol;down=$Down}} |
| 47 | function Text($Frame,[string]$Value){foreach($char in $Value.ToCharArray()){Key $Frame ([uint32][char]$char) $true;Key $Frame ([uint32][char]$char) $false}} |
| 48 | function ReadAck([long]$Serial){while($true){$r=Read $clipboard;if($r.error){throw 'The GUI clipboard write failed.'};if($r.ack -eq $Serial){return}}} |
| 49 | function NativeKeys { |
| 50 | $probe=Bridge 'terminal';Send $probe @{action='open';workspace='home';shell='powershell';cols=160;rows=25};if(!(Read $probe).ready){throw 'The GUI native key probe is unavailable.'} |
| 51 | $marker='__BN_'+[guid]::NewGuid().ToString('N').Substring(0,8)+'=' |
| 52 | $code='Add-Type -TypeDefinition ''using System.Runtime.InteropServices; public static class BrowserKeyProbe { [DllImport("user32.dll")] public static extern short GetAsyncKeyState(int key); }'';$ctrl=if(([BrowserKeyProbe]::GetAsyncKeyState(17)-band 32768)-ne 0){"held"}else{"released"};$mouse=if(([BrowserKeyProbe]::GetAsyncKeyState(1)-band 32768)-ne 0){"held"}else{"released"};[Console]::WriteLine("'+$marker+'"+$ctrl+"_"+$mouse)' |
| 53 | $command='powershell.exe -NoProfile -EncodedCommand '+[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))+"`r" |
| 54 | Send $probe @{action='input';data=[Convert]::ToBase64String($utf8.GetBytes($command))};$output='' |
| 55 | while($true){$r=Read $probe;if($r.error -or $null -ne $r.exit){throw 'The GUI native key probe exited.'};if($r.data){$output+=$utf8.GetString([Convert]::FromBase64String($r.data));$match=[regex]::Match($output,[regex]::Escape($marker)+'(held|released)_(held|released)');if($match.Success){return @{ctrl=$match.Groups[1].Value;mouse=$match.Groups[2].Value}}}} |
| 56 | } |
| 57 | function NativeGeometry { |
| 58 | $probe=Bridge 'terminal';Send $probe @{action='open';workspace='home';shell='powershell';cols=160;rows=25};if(!(Read $probe).ready){throw 'The GUI geometry probe is unavailable.'} |
| 59 | $source=@' |
| 60 | using System;using System.Collections.Generic;using System.Runtime.InteropServices;using System.Text;using System.Diagnostics; |
| 61 | public static class BrowserGeometryProbe { |
| 62 | [StructLayout(LayoutKind.Sequential)]public struct Rect{public int Left,Top,Right,Bottom;} |
| 63 | [StructLayout(LayoutKind.Sequential)]public struct Gui{public uint Size,Flags;public IntPtr Active,Focus,Capture,Menu,Move,Caret;public Rect CaretRect;} |
| 64 | [DllImport("user32.dll")]static extern IntPtr GetForegroundWindow(); |
| 65 | [DllImport("user32.dll")]static extern uint GetWindowThreadProcessId(IntPtr h,out int p); |
| 66 | [DllImport("user32.dll")]static extern bool GetWindowRect(IntPtr h,out Rect r); |
| 67 | [StructLayout(LayoutKind.Sequential)]public struct Point{public int X,Y;} |
| 68 | [DllImport("user32.dll")]static extern bool GetClientRect(IntPtr h,out Rect r); |
| 69 | [DllImport("user32.dll")]static extern bool ClientToScreen(IntPtr h,ref Point p); |
| 70 | [DllImport("user32.dll")]static extern bool ScreenToClient(IntPtr h,ref Point p); |
| 71 | [DllImport("user32.dll")]static extern IntPtr ChildWindowFromPointEx(IntPtr h,Point p,uint flags); |
| 72 | [DllImport("user32.dll")]static extern uint GetDpiForWindow(IntPtr h); |
| 73 | [DllImport("user32.dll")]static extern bool GetGUIThreadInfo(uint thread,ref Gui info); |
| 74 | [DllImport("user32.dll")]static extern IntPtr SetThreadDpiAwarenessContext(IntPtr c); |
| 75 | [DllImport("user32.dll",CharSet=CharSet.Unicode)]static extern int GetClassName(IntPtr h,StringBuilder s,int n); |
| 76 | delegate bool EnumFn(IntPtr h,IntPtr p);[DllImport("user32.dll")]static extern bool EnumChildWindows(IntPtr h,EnumFn f,IntPtr p); |
| 77 | public static string Read(){var old=SetThreadDpiAwarenessContext(new IntPtr(-4));try{var h=GetForegroundWindow();int p;var thread=GetWindowThreadProcessId(h,out p);using(var process=Process.GetProcessById(p)){var name=process.ProcessName;if(name!="msedge"&&name!="chrome")return "unowned_foreground";}Rect w;GetWindowRect(h,out w);var a=new List<string>();a.Add(GetDpiForWindow(h)+","+w.Left+","+w.Top+","+(w.Right-w.Left)+","+(w.Bottom-w.Top));var info=new Gui{Size=(uint)Marshal.SizeOf(typeof(Gui))};if(GetGUIThreadInfo(thread,ref info)){var cl=new StringBuilder(128);GetClassName(info.Focus,cl,128);Rect fr;GetWindowRect(info.Focus,out fr);a.Add("focus:"+cl+","+fr.Left+","+fr.Top+","+(fr.Right-fr.Left)+","+(fr.Bottom-fr.Top));}Rect cr;GetClientRect(h,out cr);var center=new Point{X=(cr.Right-cr.Left)/2,Y=(cr.Bottom-cr.Top)/2};ClientToScreen(h,ref center);foreach(uint flags in new uint[]{0,1,7}){var parent=h;for(int depth=0;depth<12;depth++){var pt=center;ScreenToClient(parent,ref pt);var child=ChildWindowFromPointEx(parent,pt,flags);if(child==IntPtr.Zero||child==parent){a.Add("hit"+flags+":none");break;}var cl=new StringBuilder(128);GetClassName(child,cl,128);Rect r;GetWindowRect(child,out r);a.Add("hit"+flags+":"+cl+","+r.Left+","+r.Top+","+(r.Right-r.Left)+","+(r.Bottom-r.Top));parent=child;}}EnumChildWindows(h,(c,_)=>{var s=new StringBuilder(128);GetClassName(c,s,128);Rect r;if(s.ToString()=="Chrome_RenderWidgetHostHWND"&&GetWindowRect(c,out r))a.Add(r.Left+","+r.Top+","+(r.Right-r.Left)+","+(r.Bottom-r.Top));return true;},IntPtr.Zero);return string.Join(";",a);}finally{SetThreadDpiAwarenessContext(old);}} |
| 78 | } |
| 79 | '@ |
| 80 | $marker='__BG_'+[guid]::NewGuid().ToString('N').Substring(0,8)+'=' |
| 81 | $geometryFile=Join-Path $guestDirectory ('geometry-'+[guid]::NewGuid().ToString('N')+'.txt') |
| 82 | $code='Add-Type -TypeDefinition ([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("'+[Convert]::ToBase64String($utf8.GetBytes($source))+'")));[IO.File]::WriteAllText('''+$geometryFile+''',[BrowserGeometryProbe]::Read());[Console]::WriteLine("'+$marker+'done")' |
| 83 | Send $probe @{action='input';data=[Convert]::ToBase64String($utf8.GetBytes('powershell.exe -NoProfile -EncodedCommand '+[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))+"`r"))};$output='' |
| 84 | while($true){$r=Read $probe;if($r.error -or $null -ne $r.exit){throw 'The GUI geometry probe exited.'};if($r.data){$output+=$utf8.GetString([Convert]::FromBase64String($r.data));if($output.Contains($marker+'done')){return Invoke-Command -Session $session -ArgumentList $geometryFile -ScriptBlock {param($path);Get-Content -LiteralPath $path -Raw}}}} |
| 85 | } |
| 86 | try { |
| 87 | $null=[IO.Directory]::CreateDirectory($EvidenceDirectory) |
| 88 | $guestDirectory=Invoke-Command -Session $session -ArgumentList $vm.Id,$binding.gui_user_sid -ScriptBlock { |
| 89 | param($expected,$sid) |
| 90 | if([guid](Get-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters').VirtualMachineId -ne $expected){throw 'guest_identity_mismatch'} |
| 91 | $guiHome=(Get-ItemProperty -LiteralPath ('HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\'+$sid)).ProfileImagePath |
| 92 | $directory=Join-Path $guiHome ('AppData\Local\Temp\CageBrowserAcceptance-'+[guid]::NewGuid().ToString('N'));$null=[IO.Directory]::CreateDirectory($directory);return $directory |
| 93 | } |
| 94 | $copiedFixtureText+=' '+[IO.Path]::GetFileName($guestDirectory) |
| 95 | $copiedFixtureBytes=[Convert]::ToBase64String($utf8.GetBytes($copiedFixtureText)) |
| 96 | Copy-Item -LiteralPath $FixturePath -Destination (Join-Path $guestDirectory 'fixture.mjs') -ToSession $session |
| 97 | $script:phase='gui_status';$statusProcess=Bridge 'status';$status=Read $statusProcess |
| 98 | if(!$status.available -or !$status.browser.installed -or !$status.browser.network_configured){throw 'The actual GUI browser dependencies or trusted network receipt are unavailable.'} |
| 99 | $script:phase='fixture_terminal';$terminal=Bridge 'terminal';Send $terminal @{action='open';workspace='home';shell='powershell';cols=160;rows=30};if(!(Read $terminal).ready){throw 'The GUI fixture terminal is unavailable.'} |
| 100 | $guestNode='C:\Program Files\Cage Guest Bridge\'+$vm.Id.ToString('D')+'\node.exe' |
| 101 | $launch='& '''+$guestNode+''' '''+(Join-Path $guestDirectory 'fixture.mjs')+''' '''+$guestDirectory+'''' |
| 102 | $command='powershell.exe -NoProfile -EncodedCommand '+[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($launch))+"`r" |
| 103 | Send $terminal @{action='input';data=[Convert]::ToBase64String($utf8.GetBytes($command))} |
| 104 | $output='';while($output -notmatch 'CAGE_NATIVE_FIXTURE_READY'){ $r=Read $terminal;if($r.error -or $null -ne $r.exit){throw 'The GUI fixture server exited.'};if($r.data){$output+=$utf8.GetString([Convert]::FromBase64String($r.data))}} |
| 105 | $port=Invoke-Command -Session $session -ArgumentList $guestDirectory -ScriptBlock {param($dir);[int](Get-Content -LiteralPath (Join-Path $dir 'fixture-port') -Raw)} |
| 106 | $browser=Bridge 'browser';$clipboard=Bridge 'stream';$null=Read $clipboard |
| 107 | # The production clipboard stream starts guest observation after the first |
| 108 | # host write ACK. Exercise that lifecycle instead of waiting on an unseeded reader. |
| 109 | Send $clipboard @{serial=1;text=('Cage fixture baseline '+[IO.Path]::GetFileName($guestDirectory))};ReadAck 1 |
| 110 | if($PerfOnly){ |
| 111 | $script:phase='native_capture_performance';$tab=Call '/new' @{url=('http://127.0.0.1:'+$port+'/perf')};$tabs.Add($tab.id);Start-Sleep -Milliseconds 300 |
| 112 | $frame=Frame 'perf-01-page';Key $frame 65307 $true;Key $frame 65307 $false;$frame=Frame 'perf-02-native-ready' |
| 113 | function Dimensions($f){if($f.unchanged){return};$encoded=if($f.jpeg){$f.jpeg}else{$f.png};$memory=[IO.MemoryStream]::new([Convert]::FromBase64String($encoded));$picture=[Drawing.Image]::FromStream($memory);try{if($picture.Width -ne $f.width -or $picture.Height -ne $f.height){throw 'The actual encoded frame does not match its native HiDPI dimensions.'}}finally{$picture.Dispose();$memory.Dispose()}} |
| 114 | Dimensions $frame;$before=(Call '/health' @{} 'GET').capture;if(!$before){throw 'The actual capture counters are unavailable.'} |
| 115 | $static=[Collections.Generic.List[double]]::new();$unchanged=0 |
| 116 | for($i=0;$i -lt 16;$i++){$watch=[Diagnostics.Stopwatch]::StartNew();$next=Call '/frame' @{after=$frame.sequence} 'GET';$watch.Stop();$static.Add($watch.Elapsed.TotalMilliseconds);if($next.unchanged){$unchanged++}else{Dimensions $next;$frame=$next};Start-Sleep -Milliseconds 80} |
| 117 | $still=(Call '/health' @{} 'GET').capture;$scroll=[Collections.Generic.List[double]]::new() |
| 118 | for($i=0;$i -lt 20;$i++){Pointer $frame ([int](100*$frame.scale)) ([int](600*$frame.scale)) 16;Pointer $frame ([int](100*$frame.scale)) ([int](600*$frame.scale)) 0;$watch=[Diagnostics.Stopwatch]::StartNew();$next=Call '/frame' @{after=$frame.sequence} 'GET';$watch.Stop();$scroll.Add($watch.Elapsed.TotalMilliseconds);if(!$next.unchanged){Dimensions $next;$frame=$next};Start-Sleep -Milliseconds 80} |
| 119 | $scrolled=(Call '/health' @{} 'GET').capture;if(!(@(State)|Where-Object {$_.type -eq 'scroll' -and $_.y -gt 0})){throw 'The actual native wheel did not scroll the fixture.'} |
| 120 | $summary=[ordered]@{static_polls=16;static_unchanged=$unchanged;static_screenshots=$still.screenshots-$before.screenshots;static_damage_events=$still.damage_frames-$before.damage_frames;static_latency_ms=@($static);scroll_polls=20;scroll_screenshots=$scrolled.screenshots-$still.screenshots;scroll_damage_events=$scrolled.damage_frames-$still.damage_frames;scroll_latency_ms=@($scroll);actual_native_pixels=@($frame.width,$frame.height);completed=$true} |
| 121 | [IO.File]::WriteAllText((Join-Path $EvidenceDirectory 'capture-performance.json'),($summary|ConvertTo-Json -Depth 8),$utf8);[IO.File]::WriteAllText((Join-Path $EvidenceDirectory 'perf-events.json'),(@(State)|ConvertTo-Json -Depth 8),$utf8) |
| 122 | $summary|ConvertTo-Json -Compress;return |
| 123 | } |
| 124 | if(!$SplitOnly){ |
| 125 | $script:phase='native_frame';$tab=Call '/new' @{url=('http://127.0.0.1:'+$port+'/one');incognito=$true};$tabs.Add($tab.id);Start-Sleep -Milliseconds 400 |
| 126 | $frame=Frame '01-page';$results.native_frame=$true |
| 127 | $script:phase='native_text';Click $frame 100 130;Text $frame 'native-text';Start-Sleep -Milliseconds 200 |
| 128 | if(!(@(State)|Where-Object {$_.type -eq 'text' -and $_.value -eq 'native-text'})){throw 'Native key input did not reach the fixture textarea.'};$results.native_text=$true |
| 129 | $frame=Frame '02-text';$script:phase='native_find';Key $frame 65507 $true;Key $frame 102 $true;Key $frame 102 $false;Key $frame 65507 $false;Start-Sleep -Milliseconds 200 |
| 130 | $find=Frame '03-native-find';if(@($find.overlays).Count -eq 0){throw 'The native Find UI was not captured.'};Text $find 'cage-native-find-needle';Start-Sleep -Milliseconds 100 |
| 131 | if(@(State|Where-Object {$_.type -eq 'text'})[-1].value -ne 'native-text'){throw 'Find input reached the webpage instead of native Chrome UI.'};$results.native_find=$true |
| 132 | $find=Frame '04-find-query';Key $find 65307 $true;Key $find 65307 $false |
| 133 | $frame=Frame '05-find-closed';$script:phase='native_context';Click $frame 100 130 4;Start-Sleep -Milliseconds 150 |
| 134 | $menu=Frame '06-native-context';if(@($menu.overlays).Count -eq 0){throw 'The native context menu was not captured.'};$results.native_context_menu=$true;Key $menu 65307 $true;Key $menu 65307 $false |
| 135 | $frame=Frame '07-context-closed';$script:phase='page_copy';Click $frame 100 215 |
| 136 | do {$r=Read $clipboard}while($r.source -ne 'guest' -or $r.text -cne $copiedFixtureText) |
| 137 | if([Convert]::ToBase64String($utf8.GetBytes($r.text)) -cne $copiedFixtureBytes){throw 'The actual clipboard text bytes differ from the fixture copy.'} |
| 138 | if(!(@(State)|Where-Object type -eq copied)){throw 'The actual fixture copy button did not complete.'};$results.page_copy_reaches_os_clipboard=$true |
| 139 | $script:phase='image_paste';$frame=Frame '08-before-png';Click $frame 100 295 |
| 140 | $bitmap=[Drawing.Bitmap]::new(17,9);$graphics=[Drawing.Graphics]::FromImage($bitmap);$graphics.Clear([Drawing.Color]::Coral);$bytes=[IO.MemoryStream]::new();$bitmap.Save($bytes,[Drawing.Imaging.ImageFormat]::Png);$png=$bytes.ToArray();$bytes.Dispose();$graphics.Dispose();$bitmap.Dispose() |
| 141 | $started=Call '/image-start' @{id=$tab.id;length=$png.Length};$null=Call '/image-chunk' @{transfer=$started.transfer;sequence=0;data=[Convert]::ToBase64String($png)};$null=Call '/image-finish' @{transfer=$started.transfer;sequence=1};Start-Sleep -Milliseconds 250 |
| 142 | if(!(@(State)|Where-Object {$_.type -eq 'png' -and $_.width -eq 17 -and $_.height -eq 9})){throw 'The native OS PNG did not reach the fixture paste event.'};$results.native_png_paste=$true;$null=Frame '09-png-pasted' |
| 143 | $script:phase='native_permission';$frame=Frame '10-before-permission';Click $frame 330 215;Start-Sleep -Milliseconds 250 |
| 144 | $permission=Frame '11-native-permission';if(@($permission.overlays).Count -eq 0){throw 'The native permission prompt was not captured.'};$results.native_permission_prompt=$true;Key $permission 65307 $true;Key $permission 65307 $false |
| 145 | $script:phase='native_private_tabs';$frame=Frame '11a-private-one';Click $frame ($frame.width/$frame.scale-120) 36 |
| 146 | $privateSecond=$null;for($i=0;$i-lt 40;$i++){$snapshot=Call '/tabs' @{} 'GET';$privateSecond=@($snapshot.tabs|Where-Object {$_.incognito -and $_.url -like '*/private-two'})[0];if($privateSecond){break};Start-Sleep -Milliseconds 50} |
| 147 | if(!$privateSecond){throw 'The native private popup was not registered.'};$tabs.Add($privateSecond.id) |
| 148 | $privateFrame=Frame '11b-private-two';if($privateFrame.id -ne $privateSecond.id){throw 'The private popup did not become the actual active page.'} |
| 149 | Key $privateFrame 65507 $true;Key $privateFrame 65289 $true;Key $privateFrame 65289 $false;Key $privateFrame 65507 $false;Start-Sleep -Milliseconds 200 |
| 150 | $cycled=Frame '11c-native-ctrl-tab';if($cycled.id -ne $tab.id){throw 'Native Ctrl+Tab did not synchronize the known private document.'};$results.native_ctrl_tab=$true |
| 151 | $null=Call '/close' @{id=$tab.id};$snapshot=Call '/tabs' @{} 'GET';if(!(@($snapshot.tabs)|Where-Object id -eq $privateSecond.id)){throw 'Closing a private tab also closed its sibling.'};$results.private_sibling_retained=$true |
| 152 | } |
| 153 | # Split follows the common browser contract: two ordinary tabs in one |
| 154 | # context. Only these newly created fixture tabs are used and closed. |
| 155 | $script:phase='split';$left=Call '/new' @{url=('http://127.0.0.1:'+$port+'/split-one')};$tabs.Add($left.id) |
| 156 | $second=Call '/new' @{url=('http://127.0.0.1:'+$port+'/split-two')};$tabs.Add($second.id);$null=Call '/split' @{id=$left.id;left=$left.id;right=$second.id};Start-Sleep -Milliseconds 250 |
| 157 | $split=Frame '12-split';if(@($split.panes).Count -ne 2){throw 'The real split frame does not contain both panes.'} |
| 158 | Pointer $split ([int]($split.panes[1].x+100*$split.scale)) ([int](130*$split.scale)) 1;Pointer $split ([int]($split.panes[1].x+100*$split.scale)) ([int](130*$split.scale)) 0;Text $split 'split-right';Start-Sleep -Milliseconds 250 |
| 159 | if(!(@(State)|Where-Object {$_.page -eq '/split-two' -and $_.type -eq 'text' -and $_.value -eq 'split-right'})){$null=Frame '13-split-failed';[IO.File]::WriteAllText((Join-Path $EvidenceDirectory 'split-events.json'),(@(State)|ConvertTo-Json -Depth 8),$utf8);[IO.File]::WriteAllText((Join-Path $EvidenceDirectory 'split-native-geometry.txt'),(NativeGeometry),$utf8);throw 'The native split input did not reach the right pane.'};$results.native_split_input=$true;$null=Frame '13-split-input' |
| 160 | $null=Call '/split' @{} |
| 161 | $script:phase='native_disconnect';$baseline=NativeKeys;if($baseline.ctrl -ne 'released' -or $baseline.mouse -ne 'released'){throw 'The GUI already contains held input before this isolated disconnect test.'} |
| 162 | $primary=$browser;$viewerA=Bridge 'browser';$viewerB=Bridge 'browser' |
| 163 | $browser=$viewerA;$leaseFrame=Frame '14-before-disconnect';Key $leaseFrame 65507 $true;Pointer $leaseFrame ([int](100*$leaseFrame.scale)) ([int](130*$leaseFrame.scale)) 1 |
| 164 | $browser=$viewerB;Key $leaseFrame 65507 $true |
| 165 | $viewerA.StandardInput.Close();if(!$viewerA.WaitForExit(3000)){$viewerA.Kill()};Start-Sleep -Milliseconds 250 |
| 166 | $native=NativeKeys;if($native.ctrl -ne 'held' -or $native.mouse -ne 'released'){throw 'Disconnect did not release its own mouse while preserving the other viewer Ctrl.'};$results.disconnect_preserves_other_viewer=$true |
| 167 | $viewerB.StandardInput.Close();if(!$viewerB.WaitForExit(3000)){$viewerB.Kill()};Start-Sleep -Milliseconds 250 |
| 168 | $native=NativeKeys;if($native.ctrl -ne 'released' -or $native.mouse -ne 'released'){throw 'The last viewer disconnect left native input held.'};$results.disconnect_native_keys_released=$true |
| 169 | $browser=$primary;$results.completed=$true |
| 170 | [IO.File]::WriteAllText((Join-Path $EvidenceDirectory 'browser-result.json'),($results|ConvertTo-Json),$utf8) |
| 171 | $results|ConvertTo-Json -Compress |
| 172 | } catch { |
| 173 | try{[IO.File]::WriteAllText((Join-Path $EvidenceDirectory 'failure-native-geometry.txt'),(NativeGeometry),$utf8)}catch{} |
| 174 | $results.completed=$false;$results.failed_phase=$script:phase;$results.reason=$_.Exception.Message |
| 175 | if(Test-Path -LiteralPath $EvidenceDirectory){[IO.File]::WriteAllText((Join-Path $EvidenceDirectory 'browser-result.json'),($results|ConvertTo-Json),$utf8)} |
| 176 | $results|ConvertTo-Json -Compress |
| 177 | throw |
| 178 | } finally { |
| 179 | if($primary){$browser=$primary} |
| 180 | if($browser -and $frame){try{$cleanup=Call '/frame' @{} 'GET';foreach($symbol in @(65507,65505,65513)){Key $cleanup $symbol $false};Pointer $cleanup 1 1 0}catch{}} |
| 181 | if($browser){foreach($id in $tabs){try{$null=Call '/close' @{id=$id}}catch{}}} |
| 182 | foreach($p in $owned){try{$p.StandardInput.Close();if(!$p.WaitForExit(1000)){$p.Kill()};$p.Dispose()}catch{}} |
| 183 | if($session){Remove-PSSession $session} |
| 184 | } |