public void CanRemoveEntries() { var ids = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var nm = new NativeMethodsMock { ProcessIds = ids }; var pl = new ProcessList(nm); pl.Refresh(); Assert.False(pl.Remove(11)); Assert.True(pl.Remove(3)); var n = (int)_fiN.GetValue(pl); Assert.Equal(8, n); var buf = ((int[])_fiBuf.GetValue(pl)).Take(n).ToArray(); Assert.Equal(new[] { 1, 2, 4, 5, 6, 7, 8, 9 }, buf); nm.ProcessIds = new[] { 1 }; pl.Refresh(); Assert.True(pl.Remove(1)); }
public void ExceptReturnsExpectedValues(int[] first, int[] second) { var nm1 = new NativeMethodsMock { ProcessIds = first }; var pl1 = new ProcessList(nm1); pl1.Refresh(); var nm2 = new NativeMethodsMock { ProcessIds = second }; var pl2 = new ProcessList(nm2); pl2.Refresh(); var expectedAdded = second.Except(first).ToList(); var expectedRemoved = first.Except(second).ToList(); var expectedAll = expectedAdded.Union(expectedRemoved).OrderBy(x => x).ToList(); var diff = pl2.Diff(pl1).ToList(); var added = diff.Where(x => x.Added).Select(x => x.ProcessId).ToList(); var removed = diff.Where(x => !x.Added).Select(x => x.ProcessId).ToList(); var all = diff.Select(x => x.ProcessId).ToList(); Assert.Equal <int>(expectedAll, all); Assert.Equal(expectedAdded, added); Assert.Equal(expectedRemoved, removed); }
public void WatchesForGameProcessChanges() { var nm = new NativeMethodsMock() { Processes = { [0] = "System", [1000] = "Process 1000", [2000] = _gif.MainExecutable.FullName, }, }; using var gpw = new GameProcessWatcher(_gif, nm); var evs = new EventCollector <GameProcessState>(h => gpw.Changed += h, h => gpw.Changed -= h, nameof(WatchesForGameProcessChanges)); var pss = evs.Wait(3, () => { InvokeTimerElapsed(gpw); nm.ForegroundWindow = new IntPtr(2000); InvokeTimerElapsed(gpw); nm.ForegroundWindow = new IntPtr(1000); InvokeTimerElapsed(gpw); }); Assert.Equal(GameProcessState.NotRunning, pss[0]); Assert.Equal(GameProcessState.InForeground, pss[1]); Assert.Equal(GameProcessState.InBackground, pss[2]); }
public void GetsAllPressedModifiers() { var nmm = new NativeMethodsMock() { PressedKeys = { VirtualKey.VK_LCONTROL, (VirtualKey)'A', }, }; using var mkw = new ModifierKeysWatcher(nmm); var key1 = FromXml <DeviceKey>("<Key1 Device='Keyboard' Key='Key_LeftControl' />"); var key2 = FromXml <DeviceKey>("<Key1 Device='Keyboard' Key='Key_LeftShift' />"); var key3 = FromXml <DeviceKey>("<Key2 Device='Keyboard' Key='Key_A' />"); mkw.Watch(new[] { key1, key2, key3 }, "en-US", false); var keys = mkw.InvokePrivateMethod <IEnumerable <DeviceKey> >("GetAllPressedModifiers").ToList(); Assert.Contains(key1, keys); Assert.DoesNotContain(key2, keys); Assert.Contains(key3, keys); }
public void ChecksIfChromaSdkIsAvailable(bool sdkAvailable) { var nm = new NativeMethodsMock(sdkAvailable); var available = ChromaController.IsChromaSdkAvailable(nm); Assert.Equal(sdkAvailable, available); }
public void RefreshReturnsAnOrderedListOfUniqueProcessIds() { var nm = new NativeMethodsMock { ProcessIds = new[] { 4, 3, 2, 1 } }; var pl = new ProcessList(nm); pl.Refresh(); var n = (int)_fiN.GetValue(pl); Assert.Equal(4, n); var buf = ((int[])_fiBuf.GetValue(pl)).Take(n).ToArray(); Assert.Equal(new[] { 1, 2, 3, 4 }, buf); }
public void RefreshWillNotChangeTheInternalCollectionWhenEnumProcessesFails() { var ids = new[] { 1, 2, 3, 4 }; var nm = new NativeMethodsMock { ProcessIds = ids }; var pl = new ProcessList(nm); pl.Refresh(); var n = (int)_fiN.GetValue(pl); Assert.Equal(ids.Length, n); nm.ProcessIds = null; pl.Refresh(); var buf = ((int[])_fiBuf.GetValue(pl)).Take(n).ToArray(); Assert.Equal(ids, buf); }
public void OnChangedIsNotReentrant() { var nm = new NativeMethodsMock() { Processes = { [0] = "System", [1000] = "Process 1000", [2000] = _gif.MainExecutable.FullName, }, }; using var gpw = new GameProcessWatcher(_gif, nm); var nOnChangedCalls = 0; using var mre = new ManualResetEventSlim(); gpw.Changed += (sender, e) => { Interlocked.Increment(ref nOnChangedCalls); mre.Wait(); }; void TimerElapsed(int pid) { nm.ForegroundWindow = new IntPtr(pid); InvokeTimerElapsed(gpw); mre.Set(); } Task.WaitAll(new[] { Task.Run(() => TimerElapsed(1000)), Task.Run(() => TimerElapsed(2000)), }); Assert.Equal(1, nOnChangedCalls); }