diff --git a/src/Models/HOTASCollection.cs b/src/Models/HOTASCollection.cs index 104c15c..dc8bdc0 100644 --- a/src/Models/HOTASCollection.cs +++ b/src/Models/HOTASCollection.cs @@ -1,6 +1,5 @@ using Newtonsoft.Json; using SharpDX.DirectInput; -using SierraHOTAS.ModeProfileWindow.ViewModels; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -86,6 +85,7 @@ private void RebuildMapForNewDevice(IHOTASDevice device, IHOTASDevice newDevice) { newDevice.ApplyButtonMap(device.ButtonMap.ToObservableCollection()); newDevice.SetModeProfile(device.ModeProfiles); + newDevice.SetModeActivation(ModeProfileActivationButtons); Devices.Add(newDevice); } @@ -159,6 +159,8 @@ public void ListenToDevice(IHOTASDevice device) device.ModeProfileSelected += Device_ModeProfileSelected; device.ShiftReleased += Device_ShiftReleased; device.LostConnectionToDevice += Device_LostConnectionToDevice; + + device.SetModeActivation(ModeProfileActivationButtons); device.ListenAsync(); } @@ -349,16 +351,16 @@ public bool RemoveModeProfile(ModeActivationItem item) foreach (var d in Devices) { - d.ModeProfiles.Remove(item.Mode); - } + d.RemoveModeProfile(item.Mode); + } Logging.Log.Debug($"DELETED Mode {item.Mode}!"); - if (ModeProfileActivationButtons.Count > 0) - { - var firstMode = ModeProfileActivationButtons.Keys.FirstOrDefault(); - SetMode(firstMode); - Logging.Log.Debug($"Setting Mode to {firstMode}!"); - } + + if (ModeProfileActivationButtons.Count <= 0) return true; + + var firstMode = ModeProfileActivationButtons.Keys.FirstOrDefault(); + SetMode(firstMode); + Logging.Log.Debug($"Setting Mode to {firstMode}!"); return true; } return false; diff --git a/src/Models/HOTASDevice.cs b/src/Models/HOTASDevice.cs index 6d288b9..d9fe204 100644 --- a/src/Models/HOTASDevice.cs +++ b/src/Models/HOTASDevice.cs @@ -46,6 +46,7 @@ public class HOTASDevice : IHOTASDevice private readonly IDirectInput _directInput; private IJoystick Joystick { get; set; } private IHOTASQueue _hotasQueue; + private Dictionary _modeProfileActivationButtons; public HOTASDevice() { } @@ -60,10 +61,11 @@ public HOTASDevice(IDirectInput directInput, Guid productGuid, Guid deviceId, st DeviceId = deviceId; ProductId = productGuid; Name = name; - ModeProfiles.Add(1, ButtonMap); + InitializeModeProfile(); } - public HOTASDevice(IDirectInput directInput, JoystickFactory joystickFactory, Guid productGuid, Guid deviceId, string name, IHOTASQueue hotasQueue) : this(directInput, productGuid, deviceId, name, hotasQueue) + public HOTASDevice(IDirectInput directInput, JoystickFactory joystickFactory, Guid productGuid, Guid deviceId, string name, IHOTASQueue hotasQueue) : + this(directInput, productGuid, deviceId, name, hotasQueue) { _directInput = directInput ?? throw new ArgumentNullException(nameof(directInput)); _joystickFactory = joystickFactory ?? throw new ArgumentNullException(nameof(joystickFactory)); @@ -76,6 +78,20 @@ public HOTASDevice(IDirectInput directInput, JoystickFactory joystickFactory, Gu LoadCapabilitiesMapping(); } + public void RemoveModeProfile(int mode) + { + ModeProfiles.Remove(mode); + if (ModeProfiles.Count == 0) + { + InitializeModeProfile(); + } + } + + private void InitializeModeProfile() + { + ModeProfiles.Add(1, ButtonMap); + } + public void SetModeProfile(Dictionary> profile) { ModeProfiles = profile; @@ -199,12 +215,14 @@ private void AcquireJoystick() Joystick.Acquire(); } + public void ListenAsync() { + if (_modeProfileActivationButtons == null) throw new InvalidOperationException("ModeProfileActivationButtons must be set before listening to device"); + RemoveQueueHandlers(); AddQueueHandlers(); - - _hotasQueue.Listen(Joystick, ButtonMap); + _hotasQueue.Listen(Joystick, ModeProfiles, _modeProfileActivationButtons); } private void AddQueueHandlers() @@ -241,7 +259,6 @@ public void OverlayAllProfilesToDevice() foreach (var p in ModeProfiles) { - if (p.Value.Count == 0) continue; var d = deviceButtons.ToObservableCollection();//make a copy since we could have more than 1 profile and we don't need to rescan caps mergedModeProfiles.Add(p.Key, MergeMaps(d, p.Value)); } @@ -297,11 +314,16 @@ public void SetMode(int mode) { if (!ModeProfiles.ContainsKey(mode)) { - Logging.Log.Warn($"Tried to change device to Mode {mode}, but there was no profile for it. Profile will remain unchanged. Device ID: {DeviceId}, Device Name: {Name}"); + Logging.Log.Debug($"Tried to change device to Mode {mode}, but there was no profile for it. Profile will remain unchanged. Device ID: {DeviceId}, Device Name: {Name}"); return; } ButtonMap = ModeProfiles[mode]; - _hotasQueue.SetButtonMap(ButtonMap); + _hotasQueue.SetMode(mode); + } + + public void SetModeActivation(Dictionary modeProfileActivationButtons) + { + _modeProfileActivationButtons = modeProfileActivationButtons; } public void ForceButtonPress(JoystickOffset offset, bool isDown) diff --git a/src/Models/HOTASQueue.cs b/src/Models/HOTASQueue.cs index d06b8a3..8a0b295 100644 --- a/src/Models/HOTASQueue.cs +++ b/src/Models/HOTASQueue.cs @@ -1,8 +1,10 @@ -using SharpDX.DirectInput; +//todo can remove sharpdx dependency...replace JoystickUpdate with custom array. can do this in the joystickwrapper for the call to GetCurrentState +using SharpDX.DirectInput; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -29,20 +31,28 @@ public class HOTASQueue : IHOTASQueue private Dictionary _jitterDetectionDictionary; + private Dictionary _modeProfileActivationButtons; + + private int _mode; private IKeyboard Keyboard { get; set; } private IJoystick Joystick { get; set; } private ObservableCollection _buttonMap; + private Dictionary> _modeProfiles; public HOTASQueue(IKeyboard keyboard) { Keyboard = keyboard; } - public void Listen(IJoystick joystick, ObservableCollection buttonMap) + public void Listen(IJoystick joystick, Dictionary> modeProfiles, Dictionary modeProfileActivationButtons) { Joystick = joystick; - _buttonMap = buttonMap; + _modeProfiles = modeProfiles; + _modeProfileActivationButtons = modeProfileActivationButtons; + + if (_modeProfiles.Count > 0) _buttonMap = _modeProfiles[1]; + _jitterDetectionDictionary = new Dictionary(); _actionJobs = new BlockingCollection(); @@ -225,6 +235,19 @@ private void HandleStandardButton(int offset, int value) //if action list has a timer in it, then it is a macro and executes on another thread independently. does not interrupt other buttons HandleButtonPressed(map, offset); } + else + { + if (_modeProfileActivationButtons.ContainsKey(_mode) && + _modeProfileActivationButtons[_mode].InheritFromMode>0) + { + map = GetMapFromParentMode(_modeProfileActivationButtons[_mode].InheritFromMode, offset) as HOTASButton; + if (map != null) + { + HandleButtonPressed(map, offset); + } + } + } + OnButtonPress(offset); } else @@ -279,7 +302,7 @@ private async Task PlayMacroOnce(int offset, ObservableCollection foreach (var action in actions) { if (_activeMacros.ContainsKey(offset) == false) break; - + if (action.TimeInMilliseconds > 0) { //yes this is precise only to the nearest KeyDownRepeatDelay milliseconds. repeated keys are on a 60 millisecond boundary, so the UI could be locked to 60ms increments only @@ -287,6 +310,7 @@ private async Task PlayMacroOnce(int offset, ObservableCollection while (timeLeft > 0) { await Task.Delay(Keyboard.KeyDownRepeatDelay); + if (!_activeMacros.ContainsKey(offset)) return; timeLeft -= Keyboard.KeyDownRepeatDelay; } } @@ -339,9 +363,30 @@ private void OnAxisChanged(JoystickUpdate state) AxisChanged?.Invoke(this, new AxisChangedEventArgs() { AxisId = (int)state.Offset, Value = state.Value, Device = null }); } + //public IHotasBaseMap GetMap(int buttonOffset) + //{ + // return _buttonMap.FirstOrDefault(m => m.MapId == buttonOffset); + //} + public IHotasBaseMap GetMap(int buttonOffset) { - return _buttonMap.FirstOrDefault(m => m.MapId == buttonOffset); + var map = _buttonMap.FirstOrDefault(m => m.MapId == buttonOffset); + if (map == null) + { + Debug.WriteLine("fuck"); + } + return map; + } + + private IHotasBaseMap GetMapFromParentMode(int parentModeId, int buttonOffset) + { + var parentMode = _modeProfiles[parentModeId]; + var map = parentMode.FirstOrDefault(m => m.MapId == buttonOffset) as HOTASButton; + //if (map.ActionCatalogItem.Actions.Count > 0 && map.ShiftModePage <= 0) + //{ + // ModeProfileSelected?.Invoke(this, new ModeProfileSelectedEventArgs(){IsShift = true, Mode = parentModeId }); + //} + return map; } private void OnButtonPress(int buttonId) @@ -354,10 +399,17 @@ private void OnButtonRelease(int buttonId) ButtonReleased?.Invoke(this, new ButtonPressedEventArgs() { ButtonId = buttonId, Device = null }); } + [Obsolete] public void SetButtonMap(ObservableCollection buttonMap) { _buttonMap = buttonMap; } + + public void SetMode(int mode) + { + _mode = mode; + _buttonMap = _modeProfiles[_mode]; + } } } diff --git a/src/Models/IHOTASCollection.cs b/src/Models/IHOTASCollection.cs index 2f6b6b6..1af7191 100644 --- a/src/Models/IHOTASCollection.cs +++ b/src/Models/IHOTASCollection.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using SharpDX.DirectInput; -using SierraHOTAS.ModeProfileWindow.ViewModels; namespace SierraHOTAS.Models { diff --git a/src/Models/IHOTASDevice.cs b/src/Models/IHOTASDevice.cs index 0458bcf..8270618 100644 --- a/src/Models/IHOTASDevice.cs +++ b/src/Models/IHOTASDevice.cs @@ -30,7 +30,6 @@ public interface IHOTASDevice void SetModeProfile(Dictionary> profile); int SetupNewModeProfile(); void CopyModeProfileFromTemplate(int templateModeSource, int destinationMode); - //void ReAcquireJoystick(); void ListenAsync(); void OverlayAllProfilesToDevice(); void ApplyButtonMap(ObservableCollection existingButtonMap); @@ -38,7 +37,9 @@ public interface IHOTASDevice void ForceButtonPress(JoystickOffset offset, bool isDown); void Stop(); void ClearUnassignedActions(); + void RemoveModeProfile(int mode); void ClearButtonMap(); bool GetButtonState(int mapId); + void SetModeActivation(Dictionary modeProfileActivationButtons); } } diff --git a/src/Models/IHOTASQueue.cs b/src/Models/IHOTASQueue.cs index 96df227..17813e4 100644 --- a/src/Models/IHOTASQueue.cs +++ b/src/Models/IHOTASQueue.cs @@ -1,6 +1,6 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; -using SharpDX.DirectInput; namespace SierraHOTAS.Models { @@ -16,10 +16,11 @@ public interface IHOTASQueue event EventHandler ModeProfileSelected; event EventHandler ShiftReleased; event EventHandler LostConnectionToDevice; - void Listen(IJoystick joystick, ObservableCollection buttonMap); + void Listen(IJoystick joystick, Dictionary> modeProfiles, Dictionary modeProfileActivationButtons); void ForceButtonPress(JoystickOffset offset, bool isDown); void Stop(); IHotasBaseMap GetMap(int buttonOffset); void SetButtonMap(ObservableCollection buttonMap); + void SetMode(int mode); } } \ No newline at end of file diff --git a/src/Models/ModeActivationItem.cs b/src/Models/ModeActivationItem.cs index aba2d6e..b4b5f97 100644 --- a/src/Models/ModeActivationItem.cs +++ b/src/Models/ModeActivationItem.cs @@ -1,11 +1,12 @@ using System; using Newtonsoft.Json; -namespace SierraHOTAS.ModeProfileWindow.ViewModels +namespace SierraHOTAS.Models { public class ModeActivationItem { public int Mode { get; set; } + public int InheritFromMode { get; set; } public bool IsShift { get; set; } public string ProfileName { get; set; } public string DeviceName { get; set; } diff --git a/src/ViewModels/DeleteModeProfileEvent.cs b/src/ViewModels/DeleteModeProfileEvent.cs index 9231002..4efddb3 100644 --- a/src/ViewModels/DeleteModeProfileEvent.cs +++ b/src/ViewModels/DeleteModeProfileEvent.cs @@ -1,4 +1,4 @@ -using SierraHOTAS.ModeProfileWindow.ViewModels; +using SierraHOTAS.Models; namespace SierraHOTAS.ViewModels { diff --git a/src/ViewModels/HOTASCollectionViewModel.cs b/src/ViewModels/HOTASCollectionViewModel.cs index 59baaf7..998d91a 100644 --- a/src/ViewModels/HOTASCollectionViewModel.cs +++ b/src/ViewModels/HOTASCollectionViewModel.cs @@ -1,7 +1,6 @@ using SierraHOTAS.Annotations; using SierraHOTAS.Factories; using SierraHOTAS.Models; -using SierraHOTAS.ModeProfileWindow.ViewModels; using SierraHOTAS.ViewModels.Commands; using System; using System.Collections.ObjectModel; diff --git a/src/ViewModels/ModeProfileConfigWindowViewModel.cs b/src/ViewModels/ModeProfileConfigWindowViewModel.cs index 1fc2131..db9aedc 100644 --- a/src/ViewModels/ModeProfileConfigWindowViewModel.cs +++ b/src/ViewModels/ModeProfileConfigWindowViewModel.cs @@ -1,14 +1,11 @@ using SierraHOTAS.Annotations; using SierraHOTAS.Models; -using SierraHOTAS.ModeProfileWindow.ViewModels; using SierraHOTAS.ViewModels.Commands; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; -using System.Windows; -using System.Windows.Controls; using System.Windows.Input; namespace SierraHOTAS.ViewModels @@ -74,25 +71,35 @@ public bool IsShift } } - public bool IsShiftVisible + public int CopyTemplateMode { - get => _mode != 1; + get => _copyTemplateMode; set { + if (value == _copyTemplateMode) return; + _copyTemplateMode = value; + OnPropertyChanged(); } } - public bool IsTemplateModeVisible + public int InheritFromMode { - get => _isTemplateModeVisible; + get => _inheritFromMode; set { - if (value == _isTemplateModeVisible) return; - _isTemplateModeVisible = value; + if (value == _inheritFromMode) return; + _inheritFromMode = value; OnPropertyChanged(); } } + + public bool IsInheritedVisible => _mode != 1; + public bool IsShiftVisible => _mode != 1; + + public bool IsTemplateModeVisible => _mode != 1 && _activationButtonList != null && !_activationButtonList.ContainsKey(_mode); + public Dictionary TemplateModes { get; set; } + public Dictionary InheritModes { get; set; } private readonly IDispatcher _appDispatcher; private int _activationButtonId; @@ -104,8 +111,8 @@ public bool IsTemplateModeVisible private string _activationButtonName; private bool _isActivationErrorVisible; private bool _isShift; - private bool _isTemplateModeVisible = true; - private int _selectedTemplateMode; + private int _inheritFromMode; + private int _copyTemplateMode; private readonly int _mode; private readonly Dictionary _activationButtonList; private HOTASButton _button; @@ -130,7 +137,7 @@ public ModeProfileConfigWindowViewModel(IEventAggregator eventAggregator, IDispa _mode = mode; BuildTemplateList(); - IsTemplateModeVisible = TemplateModes.Count > 1; + BuildInheritList(); //if there is no activation item, then we are creating a new one for the mode supplied. there will be nothing to assign here. if (!_activationButtonList.TryGetValue(_mode, out _activationItem)) return; @@ -139,11 +146,11 @@ public ModeProfileConfigWindowViewModel(IEventAggregator eventAggregator, IDispa DeviceName = _activationItem.DeviceName; ActivationButtonName = _activationItem.ButtonName; IsShift = _activationItem.IsShift; + InheritFromMode = _activationItem.InheritFromMode; _deviceId = _activationItem.DeviceId; _activationButtonId = _activationItem.ButtonId; _isActivationButtonValid = true; - IsTemplateModeVisible = false; _appDispatcher.Invoke(() => { @@ -172,6 +179,7 @@ public void DeviceList_ButtonPressed(object sender, ButtonPressedEventArgs e) _appDispatcher?.Invoke(() => { OnPropertyChanged(nameof(TemplateModes)); + OnPropertyChanged(nameof(InheritModes)); _saveModeProfileCommand.ForceCanExecuteChanged(); }); } @@ -185,6 +193,16 @@ private void BuildTemplateList() } } + private void BuildInheritList() + { + InheritModes = new Dictionary { { 0, "- Do Not Inherit -" } }; + foreach (var kv in _activationButtonList) + { + if (kv.Key == _mode) continue; + InheritModes.Add(kv.Key, kv.Value.ProfileName); + } + } + private void SaveModeProfile() { if (_activationButtonList.ContainsKey(_mode)) @@ -204,13 +222,14 @@ private void SaveModeProfile() _activationItem = new ModeActivationItem() { Mode = _mode, - IsShift = IsShift, - ProfileName = ProfileName, - DeviceName = DeviceName, + InheritFromMode = _inheritFromMode, + IsShift = _isShift, + ProfileName = _profileName, + DeviceName = _deviceName, DeviceId = _deviceId, - ButtonName = ActivationButtonName, + ButtonName = _activationButtonName, ButtonId = _activationButtonId, - TemplateMode = _selectedTemplateMode + TemplateMode = _copyTemplateMode }; _activationButtonList.Add(_mode, _activationItem); @@ -251,15 +270,5 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } - - public void TemplateModeSelected(object sender, RoutedEventArgs routedEventArgs) - { - if ((routedEventArgs is SelectionChangedEventArgs args) && args.AddedItems.Count > 0) - { - Logging.Log.Info($"{args.AddedItems[0]}"); - var selectedItem = (KeyValuePair)args.AddedItems[0]; - _selectedTemplateMode = selectedItem.Key; - } - } } } diff --git a/src/ViewModels/ShowModeProfileConfigWindowEvent.cs b/src/ViewModels/ShowModeProfileConfigWindowEvent.cs index 140b2a8..001da7f 100644 --- a/src/ViewModels/ShowModeProfileConfigWindowEvent.cs +++ b/src/ViewModels/ShowModeProfileConfigWindowEvent.cs @@ -1,5 +1,4 @@ using SierraHOTAS.Models; -using SierraHOTAS.ModeProfileWindow.ViewModels; using System; using System.Collections.Generic; diff --git a/src/Views/MainWindow.xaml b/src/Views/MainWindow.xaml index 0d230a9..11f7994 100644 --- a/src/Views/MainWindow.xaml +++ b/src/Views/MainWindow.xaml @@ -7,7 +7,7 @@ xmlns:profileViewModel="clr-namespace:SierraHOTAS.ViewModels" xmlns:controls="clr-namespace:SierraHOTAS.Controls" xmlns:tb="http://www.hardcodet.net/taskbar" - xmlns:viewModels="clr-namespace:SierraHOTAS.ModeProfileWindow.ViewModels" + xmlns:models="clr-namespace:SierraHOTAS.Models" d:DataContext="{d:DesignInstance viewModel:HOTASCollectionViewModel}" x:Class="SierraHOTAS.Views.MainWindow" mc:Ignorable="d" @@ -124,7 +124,7 @@ - + diff --git a/src/Views/ModeProfileConfigWindow.xaml.cs b/src/Views/ModeProfileConfigWindow.xaml.cs index 8b23a41..bf7109c 100644 --- a/src/Views/ModeProfileConfigWindow.xaml.cs +++ b/src/Views/ModeProfileConfigWindow.xaml.cs @@ -1,5 +1,4 @@ using SierraHOTAS.Models; -using SierraHOTAS.ModeProfileWindow.ViewModels; using SierraHOTAS.ViewModels; using System; using System.Collections.Generic; @@ -67,10 +66,5 @@ private void OnClosed(object sender, EventArgs e) RemoveHandlers(); Close(); } - - private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e) - { - ModeProfileConfigViewModel.TemplateModeSelected(sender, e); - } } } diff --git a/tests/CustomJsonConverterTests.cs b/tests/CustomJsonConverterTests.cs index d9789c8..5fa482d 100644 --- a/tests/CustomJsonConverterTests.cs +++ b/tests/CustomJsonConverterTests.cs @@ -3,7 +3,6 @@ using SharpDX.DirectInput; using SierraHOTAS.Factories; using SierraHOTAS.Models; -using SierraHOTAS.ModeProfileWindow.ViewModels; using System; using System.Collections.Generic; using System.Collections.ObjectModel; diff --git a/tests/DeviceViewModelTests.cs b/tests/DeviceViewModelTests.cs index d6eb34d..5917da7 100644 --- a/tests/DeviceViewModelTests.cs +++ b/tests/DeviceViewModelTests.cs @@ -4,6 +4,7 @@ using SierraHOTAS.Models; using SierraHOTAS.ViewModels; using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using SierraHOTAS.Win32; @@ -335,11 +336,18 @@ public void rebuild_map_with_new_map() public void force_button_press() { var deviceVm = CreateDeviceViewMode(out var hotasQueue, out var hotasDevice, out var subJoystick); - var list = new ObservableCollection(); - list.Add(new HOTASButton() { MapId = 48, Type = HOTASButton.ButtonType.Button, ActionCatalogItem = new ActionCatalogItem() { Actions = new ObservableCollection() { new ButtonAction() } }, ActionName = "first" }); - deviceVm.RebuildMap(list); - hotasQueue.Listen(subJoystick, list); + + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + + var map = new ObservableCollection(); + map.Add(new HOTASButton() { MapId = 48, Type = HOTASButton.ButtonType.Button, ActionCatalogItem = new ActionCatalogItem() { Actions = new ObservableCollection() { new ButtonAction() } }, ActionName = "first" }); + modeProfiles.Add(1, map); + + deviceVm.RebuildMap(map); + + hotasQueue.Listen(subJoystick, modeProfiles, activationList); Assert.Raises( a => hotasQueue.ButtonPressed += a, diff --git a/tests/HOTASCollectionTests.cs b/tests/HOTASCollectionTests.cs index 6ea8ab0..f4d68b1 100644 --- a/tests/HOTASCollectionTests.cs +++ b/tests/HOTASCollectionTests.cs @@ -2,7 +2,6 @@ using SharpDX.DirectInput; using SierraHOTAS.Factories; using SierraHOTAS.Models; -using SierraHOTAS.ModeProfileWindow.ViewModels; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -49,7 +48,7 @@ public void add_hotas_device() subDeviceFactory.CreateHOTASDevice(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(newDevice); var list = new HOTASCollection(Substitute.For(), Substitute.For(), Substitute.For(Substitute.For()), subDeviceFactory); - var device = new HOTASDevice() { DeviceId = deviceId }; + var device = new HOTASDevice(Substitute.For(), Guid.Empty, deviceId, "test device", Substitute.For()); device.ButtonMap.Add(new HOTASButton() { MapId = 1, MapName = "first button", ActionName = "tes action", IsShift = true, ShiftModePage = 2, Type = HOTASButton.ButtonType.Button }); list.AddDevice(device); var addedDevice = list.Devices.First(d => d.DeviceId == deviceId); @@ -65,7 +64,7 @@ public void add_hotas_device_with_mode_profile() subDeviceFactory.CreateHOTASDevice(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(newDevice); var list = new HOTASCollection(Substitute.For(), Substitute.For(), Substitute.For(Substitute.For()), subDeviceFactory); - var device = new HOTASDevice() { DeviceId = deviceId }; + var device = new HOTASDevice(Substitute.For(), Guid.Empty, deviceId, "test device", Substitute.For()); device.ButtonMap.Add(new HOTASButton() { MapId = 1, MapName = "first button", ActionName = "tes action", IsShift = true, ShiftModePage = 2, Type = HOTASButton.ButtonType.Button }); device.ModeProfiles.Add(43, new ObservableCollection() { { new HOTASButton() { MapName = "mode profile map" } } }); list.AddDevice(device); @@ -77,9 +76,9 @@ public void add_hotas_device_with_mode_profile() public void replace_device() { var deviceId = Guid.NewGuid(); - var firstDevice = new HOTASDevice() { DeviceId = deviceId, Name = "existing device"}; + var firstDevice = new HOTASDevice(Substitute.For(), Guid.Empty, deviceId, "existing device", Substitute.For()); - var replaceDevice = new HOTASDevice() { DeviceId = deviceId, Name = "replace device" }; + var replaceDevice = new HOTASDevice(Substitute.For(), Guid.Empty, deviceId, "replace device", Substitute.For()); var subDeviceFactory = Substitute.For(); subDeviceFactory.CreateHOTASDevice(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(firstDevice); diff --git a/tests/HOTASCollectionViewModelTests.cs b/tests/HOTASCollectionViewModelTests.cs index 588247f..0616665 100644 --- a/tests/HOTASCollectionViewModelTests.cs +++ b/tests/HOTASCollectionViewModelTests.cs @@ -4,7 +4,6 @@ using SharpDX.DirectInput; using SierraHOTAS.Factories; using SierraHOTAS.Models; -using SierraHOTAS.ModeProfileWindow.ViewModels; using SierraHOTAS.ViewModels; using System; using System.Collections.Generic; @@ -67,7 +66,7 @@ private static HOTASCollectionViewModel CreateHotasCollectionViewModel_WithEvent var deviceViewModelFactory = new DeviceViewModelFactory(); subHotasCollection = Substitute.For(); - subHotasCollection.Devices = new ObservableCollection() { new HOTASDevice() }; + subHotasCollection.Devices = new ObservableCollection() { new HOTASDevice(Substitute.For(), Guid.Empty, Guid.NewGuid(), "test device", Substitute.For()) }; var subModeProfileButtons = new Dictionary(); subHotasCollection.ModeProfileActivationButtons.Returns(subModeProfileButtons); @@ -849,7 +848,7 @@ public void delete_new_mode_profile_command_via_publish() { var deviceGuid = Guid.NewGuid(); const int modeActivationButtonId = 1000; - var hotasVm = CreateHotasCollectionViewModel_WithEventAggregator(out var eventAggregator, out IHOTASCollection subDeviceList); + var hotasVm = CreateHotasCollectionViewModel_WithEventAggregator(out var eventAggregator, out var subDeviceList); var item = new ModeActivationItem() { ButtonId = modeActivationButtonId, DeviceId = deviceGuid, Mode = 1 }; subDeviceList.ModeProfileActivationButtons.Add(1, item); subDeviceList.RemoveModeProfile(item).Returns(true); diff --git a/tests/HOTASDeviceTests.cs b/tests/HOTASDeviceTests.cs index 19a427f..9ef68be 100644 --- a/tests/HOTASDeviceTests.cs +++ b/tests/HOTASDeviceTests.cs @@ -272,6 +272,50 @@ public void set_mode_profile() Assert.Equal("test map 1,1", device.ButtonMap[2].MapName); } + [Fact] + public void remove_mode_profile() + { + var device = CreateHotasDevice(); + + + var newMapProfile = new ObservableCollection() + { + new HOTASButton() {MapName = "test map 2,1", MapId = 0}, + }; + + device.ModeProfiles.Add(2, newMapProfile); + + Assert.Equal(2, device.ModeProfiles.Count); + Assert.Equal(3, device.ModeProfiles[1].Count); + Assert.Equal("Y", device.ModeProfiles[1][1].MapName); + Assert.Equal("test map 2,1", device.ModeProfiles[2][0].MapName); + + device.RemoveModeProfile(1); + + //removing the last one should result in creating a new default one + Assert.Single(device.ModeProfiles); + Assert.Single(device.ModeProfiles[2]); + Assert.Equal("test map 2,1", device.ModeProfiles[2][0].MapName); + } + + + [Fact] + public void remove_mode_profile_last_one() + { + var device = CreateHotasDevice(); + + Assert.Single(device.ModeProfiles); + Assert.Equal(3, device.ModeProfiles[1].Count); + Assert.Equal("Y", device.ModeProfiles[1][1].MapName); + + device.RemoveModeProfile(1); + + //removing the last one should result in creating a new default one + Assert.Single(device.ModeProfiles); + Assert.Equal(3, device.ModeProfiles[1].Count); + Assert.Equal("Y", device.ModeProfiles[1][1].MapName); + } + [Fact] public void copy_mode_profile_from_template() { @@ -340,13 +384,25 @@ public void copy_button_map_profile() Assert.Equal("reverse", ((HOTASAxis)destinationProfile[1]).ReverseButtonMap[0].MapName); } + [Fact] + public void list_async_no_mode_activation() + { + var device = CreateHotasDevice(out _, out _, out _); + Assert.Throws(() => device.ListenAsync()); + } + [Fact] public void list_async() { var device = CreateHotasDevice(out _, out _, out var hotasQueue); + device.SetModeActivation(new Dictionary()); device.ListenAsync(); + hotasQueue.Received().Listen(Arg.Any(), Arg.Any>>(), Arg.Any>()); + + hotasQueue.DidNotReceive(); + Assert.Raises( a => device.KeystrokeDownSent += a, a => device.KeystrokeDownSent -= a, @@ -563,14 +619,16 @@ public void overlay_all_profiles_to_device() device.ModeProfiles[1].RemoveAt(0); device.ModeProfiles[1].RemoveAt(0); var button = device.ModeProfiles[1][0] as HOTASButton; - + //baseline Assert.NotNull(button); Assert.Empty(button.ActionCatalogItem.Actions); button.ActionCatalogItem = new ActionCatalogItem() { - ActionName = "Fire", NoAction = false, Actions = new ObservableCollection() + ActionName = "Fire", + NoAction = false, + Actions = new ObservableCollection() { new ButtonAction() {ScanCode = 1}, new ButtonAction() {ScanCode = 1, IsKeyUp = true} @@ -579,7 +637,7 @@ public void overlay_all_profiles_to_device() //baseline Assert.Single(device.ModeProfiles); - + device.OverlayAllProfilesToDevice(); //verify 2 axis and a button are in the profile and that the button retained its values diff --git a/tests/HOTASQueueTests.cs b/tests/HOTASQueueTests.cs index c7173d7..fa00812 100644 --- a/tests/HOTASQueueTests.cs +++ b/tests/HOTASQueueTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Threading; @@ -25,10 +26,12 @@ public HOTASQueueTests(ITestOutputHelper output) private class TestJoystick_BasicQueue : IJoystick { public JoystickUpdate[] TestData { get; set; } + private int _dataBufferSize; public TestJoystick_BasicQueue(int dataBufferSize) { - TestData = new JoystickUpdate[dataBufferSize]; + _dataBufferSize = dataBufferSize; + TestData = new JoystickUpdate[_dataBufferSize]; Capabilities = new Capabilities() { AxeCount = 2, ButtonCount = 128, PovCount = 4 }; } @@ -64,7 +67,7 @@ public JoystickUpdate[] GetBufferedData() var returnData = new JoystickUpdate[TestData.Length]; TestData.CopyTo(returnData, 0); - TestData = new JoystickUpdate[0]; + TestData = new JoystickUpdate[_dataBufferSize]; return returnData; } @@ -187,8 +190,12 @@ public void keystroke_up_sent() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 2); var map = CreateTestHotasTestMapWithButton(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.KeystrokeUpSent += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -208,10 +215,14 @@ public void keystroke_down_sent() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithButton(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); - var mre = new ManualResetEventSlim(); + var mre = new ManualResetEventSlim(); queue.KeystrokeDownSent += (sender, e) => { isEventCalled = true; mre.Set(); }; Assert.False(isEventCalled); joystick.TestData[0] = new JoystickUpdate() { RawOffset = (int)JoystickOffset.Button1, Sequence = 0, Timestamp = 0, Value = (int)JoystickOffsetValues.ButtonState.ButtonPressed }; @@ -224,21 +235,42 @@ public void keystroke_down_sent() [Fact] public void macro_start() { - var isEventCalled = false; + var isMacroStartedEventCalled = false; + var isKeyDownEventCalled = false; + var isKeyUpEventCalled = false; var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithButton(timeInMilliseconds: 1); var subKeyboard = Substitute.For(); subKeyboard.KeyDownRepeatDelay.Returns(35); + + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(subKeyboard); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); - var mre = new ManualResetEventSlim(); - queue.MacroStarted += (sender, e) => { isEventCalled = true; mre.Set(); }; - Assert.False(isEventCalled); + var mreMacro = new ManualResetEventSlim(); + var mreKeyDown = new ManualResetEventSlim(); + var mreKeyUp = new ManualResetEventSlim(); + + queue.MacroStarted += (sender, e) => { isMacroStartedEventCalled = true; mreMacro.Set(); }; + queue.KeystrokeDownSent += (sender, e) => { isKeyDownEventCalled = true; mreKeyDown.Set(); }; + queue.KeystrokeUpSent += (sender, e) => { isKeyUpEventCalled = true; mreKeyUp.Set(); }; + + Assert.False(isMacroStartedEventCalled); joystick.TestData[0] = new JoystickUpdate() { RawOffset = (int)JoystickOffset.Button1, Sequence = 0, Timestamp = 0, Value = (int)JoystickOffsetValues.ButtonState.ButtonPressed }; - mre.Wait(5000); - Assert.True(isEventCalled); + + mreMacro.Wait(5000); + mreKeyDown.Wait(5000); + mreKeyUp.Wait(5000); + + Assert.True(isMacroStartedEventCalled); + Assert.True(isKeyDownEventCalled); + Assert.True(isKeyUpEventCalled); + + subKeyboard.Received().SendKeyPress(Arg.Any(), Arg.Any(), Arg.Any()); } [Fact] @@ -248,9 +280,12 @@ public void macro_cancelled() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 2); var map = CreateTestHotasTestMapWithButton(timeInMilliseconds: 1); - var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.MacroCancelled += (sender, e) => { isEventCalled = true; mre.Set(); }; Assert.False(isEventCalled); @@ -267,14 +302,17 @@ public void button_pressed() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithButton(); - var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.ButtonPressed += (sender, e) => { isEventCalled = true; mre.Set(); }; Assert.False(isEventCalled); joystick.TestData[0] = new JoystickUpdate() { RawOffset = (int)JoystickOffset.Button1, Sequence = 0, Timestamp = 0, Value = (int)JoystickOffsetValues.ButtonState.ButtonPressed }; - + mre.Wait(5000); Assert.True(isEventCalled); @@ -287,8 +325,12 @@ public void button_released() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 2); var map = CreateTestHotasTestMapWithButton(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.ButtonReleased += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -299,6 +341,76 @@ public void button_released() Assert.True(isEventCalled); } + [Fact] + public void button_pressed_use_inherited_parent() + { + var isEventCalled = false; + var actualButtonId = 0; + var actualScanCode = 0; + + var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); + + var activationList = new Dictionary(); + activationList.Add(1, new ModeActivationItem(){Mode = 1}); + activationList.Add(2, new ModeActivationItem(){Mode = 2, InheritFromMode = 1}); + + var mode1map = CreateTestHotasTestMapWithButton(); + mode1map.Add(new HOTASButton(){MapId = (int)JoystickOffset.Button2, ActionCatalogItem = new ActionCatalogItem(){Actions = new ObservableCollection(){new ButtonAction(){ScanCode = 1}, new ButtonAction(){ScanCode = 1, IsKeyUp = true}}}}); + var mode2map = CreateTestHotasTestMapWithButton(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, mode1map); + modeProfiles.Add(2, mode2map); + + var queue = new HOTASQueue(Substitute.For()); + queue.Listen(joystick, modeProfiles, activationList); + + //inherited key should be active + queue.SetMode(2); + + var mre = new ManualResetEventSlim(); + queue.KeystrokeDownSent += (sender, e) => + { + isEventCalled = true; + actualButtonId = e.MapId; + actualScanCode = e.ScanCode; + mre.Set(); + }; + + Assert.False(isEventCalled); + + joystick.TestData[0] = new JoystickUpdate() { RawOffset = (int)JoystickOffset.Button2, Sequence = 0, Timestamp = 0, Value = (int)JoystickOffsetValues.ButtonState.ButtonPressed }; + mre.Wait(5000); + + Assert.True(isEventCalled); + Assert.Equal((int)JoystickOffset.Button2, actualButtonId); + Assert.Equal(1, actualScanCode); + + //inherited key should not be active + activationList[2].InheritFromMode = 0; + + isEventCalled = false; + actualButtonId = 0; + actualScanCode = 0; + mre.Reset(); + + queue.ButtonPressed += (sender, e) => { isEventCalled = true; actualButtonId = e.ButtonId; mre.Set(); }; + + joystick.TestData[0] = new JoystickUpdate() { RawOffset = (int)JoystickOffset.Button2, Sequence = 0, Timestamp = 0, Value = (int)JoystickOffsetValues.ButtonState.ButtonPressed }; + mre.Wait(5000); + Assert.True(isEventCalled); + Assert.Equal(49, actualButtonId); + Assert.Equal(0, actualScanCode); + + joystick.TestData[0] = new JoystickUpdate() { RawOffset = (int)JoystickOffset.Button1, Sequence = 0, Timestamp = 0, Value = (int)JoystickOffsetValues.ButtonState.ButtonReleased }; + mre.Reset(); + mre.Wait(5000); + + joystick.TestData[0] = new JoystickUpdate() { RawOffset = (int)JoystickOffset.Button2, Sequence = 0, Timestamp = 0, Value = (int)JoystickOffsetValues.ButtonState.ButtonReleased }; + mre.Reset(); + mre.Wait(5000); + } + + [Fact] public void axis_changed() { @@ -306,8 +418,12 @@ public void axis_changed() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithAxis(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.AxisChanged += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -324,8 +440,12 @@ public void axis_changed_causes_keypress_down() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 2); var map = CreateTestHotasTestMapWithAxis(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.KeystrokeDownSent += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -343,8 +463,12 @@ public void axis_changed_causes_keypress_up() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 2); var map = CreateTestHotasTestMapWithAxis(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.KeystrokeUpSent += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -362,8 +486,12 @@ public void mode_profile_selected() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithShiftMode(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.ModeProfileSelected += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -380,8 +508,12 @@ public void shift_released() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithShiftMode(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.ShiftReleased += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -398,8 +530,12 @@ public void lost_connection_to_device() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithButton(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.LostConnectionToDevice += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -416,11 +552,16 @@ public void force_button_press() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithButton(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); + Assert.False(isEventCalled); - queue.ButtonPressed += (sender, e) => { isEventCalled = true;}; + queue.ButtonPressed += (sender, e) => { isEventCalled = true; }; queue.ForceButtonPress(JoystickOffset.Button1, true); Assert.True(isEventCalled); @@ -433,8 +574,12 @@ public void stop_loop() var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithButton(); + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.KeystrokeDownSent += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -462,8 +607,13 @@ public void handle_pov_button_press() var isEventCalled = false; var joystick = new TestJoystick_BasicQueue(dataBufferSize: 1); var map = CreateTestHotasTestMapWithPOV(); + + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.ButtonPressed += (sender, e) => { isEventCalled = true; mre.Set(); }; @@ -479,8 +629,13 @@ public void handle_pov_button_release() var isEventCalled = false; var joystick = new TestJoystick_BasicQueue(dataBufferSize: 2); var map = CreateTestHotasTestMapWithPOV(); + + var activationList = new Dictionary(); + var modeProfiles = new Dictionary>(); + modeProfiles.Add(1, map); + var queue = new HOTASQueue(Substitute.For()); - queue.Listen(joystick, map); + queue.Listen(joystick, modeProfiles, activationList); var mre = new ManualResetEventSlim(); queue.ButtonReleased += (sender, e) => { isEventCalled = true; mre.Set(); }; diff --git a/tests/ModeProfileConfigWindowViewModelTests.cs b/tests/ModeProfileConfigWindowViewModelTests.cs index ea8f2c2..789cb37 100644 --- a/tests/ModeProfileConfigWindowViewModelTests.cs +++ b/tests/ModeProfileConfigWindowViewModelTests.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Controls; -using System.Windows.Threading; -using NSubstitute; +using NSubstitute; using SierraHOTAS.Models; -using SierraHOTAS.ModeProfileWindow.ViewModels; using SierraHOTAS.ViewModels; +using System; +using System.Collections.Generic; +using System.Windows.Controls; using Xunit; namespace SierraHOTAS.Tests @@ -53,13 +47,28 @@ public void basic_constructor_no_mode() } [Fact] - public void basic_constructor_template_not_visible() + public void basic_constructor_template_not_visible_by_mode() { - const int mode = 0; + const int mode = 1; var profileVm = new ModeProfileConfigWindowViewModel(Substitute.For(), Substitute.For(), mode, new Dictionary()); Assert.False(profileVm.IsTemplateModeVisible); } + [Fact] + public void basic_constructor_template_not_visible_by_null_activation_button_list() + { + var profileVm = new ModeProfileConfigWindowViewModel(); + Assert.False(profileVm.IsTemplateModeVisible); + } + + [Fact] + public void basic_constructor_template_not_visible_activation_button_list_has_items() + { + const int mode = 1; + var profileVm = new ModeProfileConfigWindowViewModel(Substitute.For(), Substitute.For(), mode, new Dictionary(){ {1,new ModeActivationItem()}}); + Assert.False(profileVm.IsTemplateModeVisible); + } + [Fact] public void basic_constructor_no_valid_mode() { @@ -111,14 +120,12 @@ public void properties_raise_property_changed() profileVm.ActivationButtonName = string.Empty; profileVm.IsActivationErrorVisible = false; profileVm.IsShift = false; - profileVm.IsTemplateModeVisible = false; Assert.PropertyChanged(profileVm, "ProfileName", () => profileVm.ProfileName = "changed'"); Assert.PropertyChanged(profileVm, "DeviceName", () => profileVm.DeviceName = "changed'"); Assert.PropertyChanged(profileVm, "ActivationButtonName", () => profileVm.ActivationButtonName = "changed'"); Assert.PropertyChanged(profileVm, "IsActivationErrorVisible", () => profileVm.IsActivationErrorVisible = true); Assert.PropertyChanged(profileVm, "IsShift", () => profileVm.IsShift = true); - Assert.PropertyChanged(profileVm, "IsTemplateModeVisible", () => profileVm.IsTemplateModeVisible = true); } [Fact] @@ -126,7 +133,6 @@ public void is_shift_visible() { var mode = 0; var profileVm = new ModeProfileConfigWindowViewModel(Substitute.For(), Substitute.For(), mode, new Dictionary()); - profileVm.IsShiftVisible = false; Assert.True(profileVm.IsShiftVisible); @@ -373,13 +379,7 @@ public void template_mode_selected() profileVm.SaveModeProfileCommand.Execute(default); Assert.Equal(0, activationButtonList[1].TemplateMode);//template mode is unselected by default - var args = new SelectionChangedEventArgs( - System.Windows.Controls.Primitives.Selector.SelectionChangedEvent, - new List> { }, - new List> {new KeyValuePair(selectThisTemplateMode, activationButton2.ProfileName) } - ); - - profileVm.TemplateModeSelected(new object(), args); + profileVm.CopyTemplateMode = selectThisTemplateMode; button = new HOTASButton() { MapId = 44, ShiftModePage = 0, ActionName = "new action 2", MapName = "new map 2" }; device1.ButtonMap.Add(button); diff --git a/tests/QuickProfilePanelViewModelTests.cs b/tests/QuickProfilePanelViewModelTests.cs index 6cf9dea..c16da75 100644 --- a/tests/QuickProfilePanelViewModelTests.cs +++ b/tests/QuickProfilePanelViewModelTests.cs @@ -1,14 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using NSubstitute; +using NSubstitute; using SierraHOTAS.Factories; using SierraHOTAS.Models; -using SierraHOTAS.ModeProfileWindow.ViewModels; using SierraHOTAS.ViewModels; using SierraHOTAS.Win32; +using System; +using System.Collections.Generic; using Xunit; namespace SierraHOTAS.Tests