diff --git a/lib/bus.ps1 b/lib/bus.ps1 index 787e950..9584089 100644 --- a/lib/bus.ps1 +++ b/lib/bus.ps1 @@ -106,6 +106,14 @@ class BusDevice : IODevice { [string] identifier () { return 'Bus[' + $this.index + '].Device' } + + [int] EnumCount () { + return $this.remote.GetOutputCount() + } + + [PSObject] EnumDevice ([int]$eIndex) { + return $this.remote.GetOutputDevice($eIndex) + } } function Make_Buses ([Object]$remote) { diff --git a/lib/io.ps1 b/lib/io.ps1 index 49d6f27..912f73e 100644 --- a/lib/io.ps1 +++ b/lib/io.ps1 @@ -118,30 +118,79 @@ class IODevice : IRemote { } } + [int] EnumCount () { + throw [System.NotImplementedException]::new("$($this.GetType().Name) must override EnumCount()") + } + + [PSObject] EnumDevice ([int]$eIndex) { + throw [System.NotImplementedException]::new("$($this.GetType().Name) must override EnumDevice()") + } + + [PSObject] Get () { + $device = [PSCustomObject]@{ + Driver = $this.driver + Name = $this.name + HardwareId = '' + IsOutput = $this.kindOfDevice -eq 'Output' + } + if (-not [string]::IsNullOrEmpty($device.Name)) { + for ($i = 0; $i -lt $this.EnumCount(); $i++) { + $eDevice = $this.EnumDevice($i) + if ($eDevice.Name -eq $device.Name -and $eDevice.Driver -eq $device.Driver) { + $device = $eDevice + break + } + } + } + return $device + } + + [void] Set ([PSObject]$device) { + $v = $device.IsOutput -eq ($this.kindOfDevice -eq 'Output') + $d = $device.Driver + $n = $device.Name + + if ($v -and $d -is [string] -and $n -is [string]) { + if ($d -eq '' -and $n -eq '') { + $this.Clear() + return + } + if ($d -in $this.drivers.Values) { + $this.Setter($d, $n) + return + } + } + Write-Warning "Invalid device object provided to Set method." + } + + [void] Clear () { + $this.Setter('mme', '') + } + hidden $_driver = $($this | Add-Member ScriptProperty 'driver' ` { if ([string]::IsNullOrEmpty($this.name)) { return '' } - + $type = $null try { $tmp = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), "vmrtmp-$(New-Guid).xml") $this.remote.Setter('Command.Save', $tmp) - $timeout = New-TimeSpan -Seconds 2 - $sw = [Diagnostics.Stopwatch]::StartNew() - $line = $null - do { + $timeout = New-TimeSpan -Seconds 2 + $sw = [Diagnostics.Stopwatch]::StartNew() + $line = $null + do { if (Test-Path $tmp) { try { $line = Get-Content $tmp | Select-String -Pattern "<$($this.kindOfDevice)Dev index='$($this.index + 1)'" -List if ($line) { break } } catch {} - } - Start-Sleep -Milliseconds 20 - } while ($sw.elapsed -lt $timeout) - if ($line -and $line.ToString() -match "type='(?\d+)'") { - $type = $matches['type'] + } + Start-Sleep -Milliseconds 20 + } while ($sw.elapsed -lt $timeout) + if ($line -and $line.ToString() -match "type='(?\d+)'") { + $type = $matches['type'] } } finally { diff --git a/lib/strip.ps1 b/lib/strip.ps1 index c7d920e..bbc65b3 100644 --- a/lib/strip.ps1 +++ b/lib/strip.ps1 @@ -161,6 +161,14 @@ class StripDevice : IODevice { [string] identifier () { return 'Strip[' + $this.index + '].Device' } + + [int] EnumCount () { + return $this.remote.GetInputCount() + } + + [PSObject] EnumDevice ([int]$eIndex) { + return $this.remote.GetInputDevice($eIndex) + } } class VirtualStrip : Strip { diff --git a/tests/higher.Tests.ps1 b/tests/higher.Tests.ps1 index dc128d9..15d0b0c 100644 --- a/tests/higher.Tests.ps1 +++ b/tests/higher.Tests.ps1 @@ -850,24 +850,47 @@ Describe -Tag 'higher', -TestName 'All Higher Tests' { @{ Index = $phys_in } ) { Context 'Device' -ForEach @( - @{ Value = 'testInput' }, @{ Value = '' } + @{ Driver = 'mme'; Value = 'testMme'; Expected = 'mme' } + @{ Driver = 'wdm'; Value = 'testWdm'; Expected = 'wdm' } + @{ Driver = 'ks'; Value = 'testKs'; Expected = 'ks' } + @{ Driver = 'mme'; Value = ''; Expected = '' } ) { - It "Should set Strip[$index].Device.wdm" { - $vmr.strip[$index].device.wdm = $value + BeforeEach { + $vmr.strip[$index].device.Clear() + Start-Sleep -Milliseconds 800 + } + + It "Should set Strip[$index].Device.$($driver)" { + $vmr.strip[$index].device.name | Should -Be '' + $vmr.strip[$index].device.driver | Should -Be '' + + $vmr.strip[$index].device.$($driver) = $value Start-Sleep -Milliseconds 800 $vmr.strip[$index].device.name | Should -Be $value + $vmr.strip[$index].device.driver | Should -Be $expected } - It "Should set Strip[$index].Device.ks" { - $vmr.strip[$index].device.ks = $value + It "Should set Strip[$index].Device" -ForEach @( + @{ + Clear = [PSCustomObject]@{ Driver = ''; Name = ''; HardwareId = ''; IsOutput = $false } + Device = [PSCustomObject]@{ Driver = $expected; Name = $value; HardwareId = ''; IsOutput = $false } + } + ) { + $initial = $vmr.strip[$index].device.Get() + + $initial.Driver | Should -Be $clear.Driver + $initial.Name | Should -Be $clear.Name + $initial.HardwareId | Should -Be $clear.HardwareId + $initial.IsOutput | Should -Be $clear.IsOutput + + $vmr.strip[$index].device.Set($device) Start-Sleep -Milliseconds 800 - $vmr.strip[$index].device.name | Should -Be $value - } - - It "Should set Strip[$index].Device.mme" { - $vmr.strip[$index].device.mme = $value - Start-Sleep -Milliseconds 800 - $vmr.strip[$index].device.name | Should -Be $value + $result = $vmr.strip[$index].device.Get() + + $result.Driver | Should -Be $device.Driver + $result.Name | Should -Be $device.Name + $result.HardwareId | Should -Be $device.HardwareId + $result.IsOutput | Should -Be $device.IsOutput } } @@ -986,14 +1009,45 @@ Describe -Tag 'higher', -TestName 'All Higher Tests' { @{ Driver = 'mme'; Value = 'testMme'; Expected = 'mme' } @{ Driver = 'wdm'; Value = 'testWdm'; Expected = 'wdm' } @{ Driver = 'ks'; Value = 'testKs'; Expected = 'ks' } - @{ Driver = 'mme'; Value = ''; Expected = 'none' } + @{ Driver = 'mme'; Value = ''; Expected = '' } ) { + BeforeEach { + $vmr.bus[$index].device.Clear() + Start-Sleep -Milliseconds 800 + } + It "Should set Bus[$index].Device.$($driver)" { + $vmr.bus[$index].device.name | Should -Be '' + $vmr.bus[$index].device.driver | Should -Be '' + $vmr.bus[$index].device.$($driver) = $value Start-Sleep -Milliseconds 800 $vmr.bus[$index].device.name | Should -Be $value $vmr.bus[$index].device.driver | Should -Be $expected } + + It "Should set Bus[$index].Device" -ForEach @( + @{ + Clear = [PSCustomObject]@{ Driver = ''; Name = ''; HardwareId = ''; IsOutput = $true } + Device = [PSCustomObject]@{ Driver = $expected; Name = $value; HardwareId = ''; IsOutput = $true } + } + ) { + $initial = $vmr.bus[$index].device.Get() + + $initial.Driver | Should -Be $clear.Driver + $initial.Name | Should -Be $clear.Name + $initial.HardwareId | Should -Be $clear.HardwareId + $initial.IsOutput | Should -Be $clear.IsOutput + + $vmr.bus[$index].device.Set($device) + Start-Sleep -Milliseconds 800 + $result = $vmr.bus[$index].device.Get() + + $result.Driver | Should -Be $device.Driver + $result.Name | Should -Be $device.Name + $result.HardwareId | Should -Be $device.HardwareId + $result.IsOutput | Should -Be $device.IsOutput + } } } @@ -1004,14 +1058,45 @@ Describe -Tag 'higher', -TestName 'All Higher Tests' { @{ Driver = 'mme'; Value = 'testMme'; Expected = 'mme' } @{ Driver = 'wdm'; Value = 'testWdm'; Expected = 'wdm' } @{ Driver = 'ks'; Value = 'testKs'; Expected = 'ks' } - @{ Driver = 'mme'; Value = ''; Expected = 'none' } + @{ Driver = 'mme'; Value = ''; Expected = '' } ) { + BeforeEach { + $vmr.bus[$index].device.Clear() + Start-Sleep -Milliseconds 800 + } + It "Should set Bus[$index].Device.$($driver)" { + $vmr.bus[$index].device.name | Should -Be '' + $vmr.bus[$index].device.driver | Should -Be '' + $vmr.bus[$index].device.$($driver) = $value Start-Sleep -Milliseconds 800 $vmr.bus[$index].device.name | Should -Be $value $vmr.bus[$index].device.driver | Should -Be $expected } + + It "Should set Bus[$index].Device" -ForEach @( + @{ + Clear = [PSCustomObject]@{ Driver = ''; Name = ''; HardwareId = ''; IsOutput = $true } + Device = [PSCustomObject]@{ Driver = $expected; Name = $value; HardwareId = ''; IsOutput = $true } + } + ) { + $initial = $vmr.bus[$index].device.Get() + + $initial.Driver | Should -Be $clear.Driver + $initial.Name | Should -Be $clear.Name + $initial.HardwareId | Should -Be $clear.HardwareId + $initial.IsOutput | Should -Be $clear.IsOutput + + $vmr.bus[$index].device.Set($device) + Start-Sleep -Milliseconds 800 + $result = $vmr.bus[$index].device.Get() + + $result.Driver | Should -Be $device.Driver + $result.Name | Should -Be $device.Name + $result.HardwareId | Should -Be $device.HardwareId + $result.IsOutput | Should -Be $device.IsOutput + } } }