public ControllerActionPageViewModel( INavigationService navigationService, ITranslationService translationService, ICreationManager creationManager, IDeviceManager deviceManager, IDialogService dialogService, IPreferences preferences, NavigationParameters parameters) : base(navigationService, translationService) { _creationManager = creationManager; _deviceManager = deviceManager; _dialogService = dialogService; _preferences = preferences; ControllerAction = parameters.Get <ControllerAction>("controlleraction", null); ControllerEvent = parameters.Get <ControllerEvent>("controllerevent", null) ?? ControllerAction?.ControllerEvent; var device = _deviceManager.GetDeviceById(ControllerAction?.DeviceId); if (ControllerAction != null && device != null) { SelectedDevice = device; Action.Channel = ControllerAction.Channel; Action.IsInvert = ControllerAction.IsInvert; Action.ChannelOutputType = ControllerAction.ChannelOutputType; Action.MaxServoAngle = ControllerAction.MaxServoAngle; Action.ButtonType = ControllerAction.ButtonType; Action.AxisType = ControllerAction.AxisType; Action.AxisCharacteristic = ControllerAction.AxisCharacteristic; Action.MaxOutputPercent = ControllerAction.MaxOutputPercent; Action.AxisDeadZonePercent = ControllerAction.AxisDeadZonePercent; Action.ServoBaseAngle = ControllerAction.ServoBaseAngle; Action.StepperAngle = ControllerAction.StepperAngle; Action.SequenceName = ControllerAction.SequenceName; } else { var lastSelectedDeviceId = _preferences.Get <string>("LastSelectedDeviceId", null, "com.scn.BrickController2.ControllerActionPage"); SelectedDevice = _deviceManager.GetDeviceById(lastSelectedDeviceId) ?? _deviceManager.Devices.FirstOrDefault(); Action.Channel = 0; Action.IsInvert = false; Action.ChannelOutputType = ChannelOutputType.NormalMotor; Action.MaxServoAngle = 90; Action.ButtonType = ControllerButtonType.Normal; Action.AxisType = ControllerAxisType.Normal; Action.AxisCharacteristic = ControllerAxisCharacteristic.Linear; Action.MaxOutputPercent = 100; Action.AxisDeadZonePercent = 0; Action.ServoBaseAngle = 0; Action.StepperAngle = 90; Action.SequenceName = string.Empty; } SaveControllerActionCommand = new SafeCommand(async() => await SaveControllerActionAsync(), () => SelectedDevice != null); DeleteControllerActionCommand = new SafeCommand(async() => await DeleteControllerActionAsync()); OpenDeviceDetailsCommand = new SafeCommand(async() => await OpenDeviceDetailsAsync(), () => SelectedDevice != null); OpenChannelSetupCommand = new SafeCommand(async() => await OpenChannelSetupAsync(), () => SelectedDevice != null); OpenSequenceEditorCommand = new SafeCommand(async() => await OpenSequenceEditorAsync()); }
public void ExecuteAsyncT_RunsOnTaskPool(string parameter) { ICommand command = new SafeCommand <string>(MockTask); bool isExecuting = true; Thread callingThread = null, executingThread = null; async Task MockTask(string text) { await Task.Delay(Delay); executingThread = Thread.CurrentThread; isExecuting = false; } var thread = new Thread(new ThreadStart(() => { callingThread = Thread.CurrentThread; command.Execute(parameter); })); thread.Start(); while (isExecuting) { Thread.Sleep(Delay / 25); } Assert.False(callingThread.IsThreadPoolThread); Assert.True(executingThread.IsThreadPoolThread); }
public void AsyncCommand_CanExecuteChanged_Test() { //Arrange bool canCommandExecute = false; bool didCanExecuteChangeFire = false; SafeCommand command = new SafeCommand(NoParameterTask, canExecute: commandCanExecute); command.CanExecuteChanged += handleCanExecuteChanged; void handleCanExecuteChanged(object sender, EventArgs e) => didCanExecuteChangeFire = true; bool commandCanExecute() => canCommandExecute; Assert.False(command.CanExecute(null)); //Act canCommandExecute = true; //Assert Assert.True(command.CanExecute(null)); Assert.False(didCanExecuteChangeFire); //Act command.RaiseCanExecuteChanged(); //Assert Assert.True(didCanExecuteChangeFire); Assert.True(command.CanExecute(null)); }
public CreationPageViewModel( INavigationService navigationService, ITranslationService translationService, ICreationManager creationManager, IDialogService dialogService, ISharedFileStorageService sharedFileStorageService, IPlayLogic playLogic, NavigationParameters parameters) : base(navigationService, translationService) { _creationManager = creationManager; _dialogService = dialogService; SharedFileStorageService = sharedFileStorageService; _playLogic = playLogic; Creation = parameters.Get <Creation>("creation"); ImportControllerProfileCommand = new SafeCommand(async() => await ImportControllerProfileAsync(), () => SharedFileStorageService.IsSharedStorageAvailable); ExportCreationCommand = new SafeCommand(async() => await ExportCreationAsync(), () => SharedFileStorageService.IsSharedStorageAvailable); RenameCreationCommand = new SafeCommand(async() => await RenameCreationAsync()); PlayCommand = new SafeCommand(async() => await PlayAsync()); AddControllerProfileCommand = new SafeCommand(async() => await AddControllerProfileAsync()); ControllerProfileTappedCommand = new SafeCommand <ControllerProfile>(async controllerProfile => await NavigationService.NavigateToAsync <ControllerProfilePageViewModel>(new NavigationParameters(("controllerprofile", controllerProfile)))); DeleteControllerProfileCommand = new SafeCommand <ControllerProfile>(async controllerProfile => await DeleteControllerProfileAsync(controllerProfile)); }
public void ExecuteAsync_RunsOnNewThread() { ICommand command = new SafeCommand(MockTask); bool isExecuting = true; Thread callingThread = null, executingThread = null; async Task MockTask() { await Task.Delay(Delay); executingThread = Thread.CurrentThread; isExecuting = false; } var thread = new Thread(new ThreadStart(() => { callingThread = Thread.CurrentThread; command.Execute(null); })); thread.Start(); while (isExecuting) { Thread.Sleep(Delay / 25); } Assert.NotEqual(callingThread.ManagedThreadId, executingThread.ManagedThreadId); }
public ControllerProfilePageViewModel( INavigationService navigationService, ITranslationService translationService, ICreationManager creationManager, IDeviceManager deviceManager, IDialogService dialogService, IPlayLogic playLogic, NavigationParameters parameters) : base(navigationService, translationService) { _creationManager = creationManager; _deviceManager = deviceManager; _dialogService = dialogService; _playLogic = playLogic; ControllerProfile = parameters.Get <ControllerProfile>("controllerprofile"); RenameProfileCommand = new SafeCommand(async() => await RenameControllerProfileAsync()); AddControllerEventCommand = new SafeCommand(async() => await AddControllerEventAsync()); PlayCommand = new SafeCommand(async() => await PlayAsync()); ControllerActionTappedCommand = new SafeCommand <ControllerActionViewModel>(async controllerActionViewModel => await NavigationService.NavigateToAsync <ControllerActionPageViewModel>(new NavigationParameters(("controlleraction", controllerActionViewModel.ControllerAction)))); DeleteControllerEventCommand = new SafeCommand <ControllerEvent>(async controllerEvent => await DeleteControllerEventAsync(controllerEvent)); DeleteControllerActionCommand = new SafeCommand <ControllerAction>(async controllerAction => await DeleteControllerActionAsync(controllerAction)); PopulateControllerEvents(); }
public void ExecuteT_SecondCallAfterException_Executes() { int times = 0; async Task MockTask(string text) { await Task.Delay(Delay); if (times++ == 0) { throw new Exception(); //Throws only on first try } } var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false); ICommand command = new SafeCommand <string>(MockTask, dts, null, null); command.Execute("test"); dts.RunTasksUntilIdle(); command.Execute("test"); dts.RunTasksUntilIdle(); Assert.NotEmpty(dts.Exceptions); Assert.Equal(2, times); }
public void ExecuteAsync_MustRunOnCurrentSyncContextTrue_RunsOnCurrentThread() { ICommand command = new SafeCommand(MockTask, mustRunOnCurrentSyncContext: true); bool isExecuting = true; Thread callingThread = null, executingThread = null; async Task MockTask() { executingThread = Thread.CurrentThread; await Task.Delay(Delay).ConfigureAwait(true); isExecuting = false; } var thread = new Thread(new ThreadStart(() => { callingThread = Thread.CurrentThread; command.Execute(null); })); thread.Start(); while (isExecuting) { Thread.Sleep(Delay / 25); } Assert.Equal(callingThread.ManagedThreadId, executingThread.ManagedThreadId); }
public void ExecuteT_IBusy_CalledTwice_FiresOnceIfBusy() { int times = 0; async Task MockTask(string text) { await Task.Delay(Delay); times++; } var mockVm = new Mock <IBusy>(); var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false); ICommand command = new SafeCommand <string>(MockTask, dts, mockVm.Object); command.Execute("test"); command.Execute("test"); dts.RunTasksUntilIdle(); Assert.Equal(1, times); mockVm.VerifyGet(vm => vm.IsBusy, Times.Exactly(2)); mockVm.VerifySet(vm => vm.IsBusy = true); mockVm.VerifySet(vm => vm.IsBusy = false); }
public void ExecuteAsync_MustRunOnCurrentSyncContextTrue_NotRunOnTaskPool() { ICommand command = new SafeCommand(MockTask, mustRunOnCurrentSyncContext: true); bool isExecuting = true; Thread callingThread = null, executingThread = null; async Task MockTask() { executingThread = Thread.CurrentThread; await Task.Delay(Delay); isExecuting = false; } var thread = new Thread(new ThreadStart(() => { callingThread = Thread.CurrentThread; command.Execute(null); })); thread.Start(); while (isExecuting) { Thread.Sleep(Delay / 25); } Assert.False(executingThread.IsThreadPoolThread); Assert.False(callingThread.IsThreadPoolThread); }
protected PageViewModelBase(INavigationService navigationService, ITranslationService translationService) { NavigationService = navigationService; TranslationService = translationService; BackCommand = new SafeCommand(() => NavigationService.NavigateBackAsync()); }
public void Execute_SecondCallAfterException_Executes() { SafeExecutionHelpers.RevertToDefaultImplementation(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); SafeExecutionHelpers.Implementation = mockHelpers.Object; Exception exception = new Exception(); int times = 0; void MockTask(string text) { if (times++ == 0) { throw exception; //Throws only on first try } } ICommand command = new SafeCommand <string>(MockTask); //Assert.Throws<Exception>(()=>command.Execute("test")); //First run command.Execute("test"); mockHelpers.Verify(h => h.HandleException <Exception>(exception, null)); //Second run command.Execute("test"); Assert.Equal(2, times); SafeExecutionHelpers.RevertToDefaultImplementation(); }
public SequenceEditorPageViewModel( INavigationService navigationService, ITranslationService translationService, IDialogService dialogService, ICreationManager creationManager, NavigationParameters parameters) : base(navigationService, translationService) { _dialogService = dialogService; _creationManager = creationManager; OriginalSequence = parameters.Get <Sequence>("sequence"); Sequence = new Sequence { Name = OriginalSequence.Name, Loop = OriginalSequence.Loop, Interpolate = OriginalSequence.Interpolate, ControlPoints = new ObservableCollection <SequenceControlPoint>(OriginalSequence.ControlPoints.Select(cp => new SequenceControlPoint { Value = cp.Value, DurationMs = cp.DurationMs }).ToArray()) }; RenameSequenceCommand = new SafeCommand(async() => await RenameSequenceAsync()); AddControlPointCommand = new SafeCommand(() => AddControlPoint()); DeleteControlPointCommand = new SafeCommand <SequenceControlPoint>(async(controlPoint) => await DeleteControlPointAsync(controlPoint)); SaveSequenceCommand = new SafeCommand(async() => await SaveSequenceAsync(), () => !_dialogService.IsDialogOpen); ChangeControlPointDurationCommand = new SafeCommand <SequenceControlPoint>(async(controlPoint) => await ChangeControlPointDurationAsync(controlPoint)); }
public void Execute_ParameterIsWrongValueType_ThrowsInvalidCommandParameterException() { int executions = 0; var Command = new SafeCommand <int>(executeAction: context => executions += 1); Assert.Throws <InvalidCommandParameterException>(() => Command.Execute(10.5)); Assert.True(executions == 0); // "the Command should not have executed"); }
public void Execute_ParameterIsWrongReferenceType_ThrowsInvalidCommandParameterException() { int executions = 0; var Command = new SafeCommand <FakeChildContext>(executeAction: context => executions += 1); Assert.Throws <InvalidCommandParameterException>(() => Command.Execute(new FakeParentContext())); Assert.True(executions == 0); //, "the Command should not have executed"); }
public void GenericExecuteWithCanExecute() { string result = null; var cmd = new SafeCommand <string>(s => result = s, canExecute: s => true); cmd.Execute("Foo"); Assert.Equal("Foo", result); }
public void Execute_ValueTypeAndSetToNull_ThrowsInvalidCommandParameterException() { int executions = 0; var Command = new SafeCommand <int>(executeAction: context => executions += 1); Assert.Throws <InvalidCommandParameterException>(() => Command.Execute(null)); Assert.True(executions == 0, "the Command should not have executed"); }
public void ExecuteWithCanExecute() { bool executed = false; var cmd = new SafeCommand(() => executed = true, canExecute: () => true); cmd.Execute(null); Assert.True(executed); }
public void AsyncCommand_Parameter_CanExecuteFalse_Test() { //Arrange SafeCommand <int> command = new SafeCommand <int>(IntParameterTask, canExecute: o => CanExecuteFalse(o)); //Act //Assert Assert.False(command.CanExecute(null)); }
public void AsyncCommand_NoParameter_CanExecuteFalse_Test() { //Arrange SafeCommand command = new SafeCommand(NoParameterTask, canExecute: CanExecuteFalse); //Act //Assert Assert.False(command.CanExecute(null)); }
public void ChangeCanExecute() { bool signaled = false; var cmd = new SafeCommand(() => { }); cmd.CanExecuteChanged += (sender, args) => signaled = true; cmd.RaiseCanExecuteChanged(); Assert.True(signaled); }
public void CanExecuteT_NullParameterWithNonNullableValueType_False() { //Arrange SafeCommand <int> command = new SafeCommand <int>(IntParameterTask, canExecute: o => CanExecuteTrue(o)); //Act //Assert Assert.False(command.CanExecute(null)); }
public void Execute_WithIBusy_IsBusyTrueWhileRunning() { var vm = new MockViewModel(); var command = new SafeCommand(executeAction: () => { Assert.True(vm.IsBusy); }, vm); Assert.False(vm.IsBusy); command.Execute(null); //see Assert in command Assert.False(vm.IsBusy); }
public void ExecuteT_WithIViewModelBase_IsBusyTrueWhileRunning(int number) { var vm = new MockViewModel(); var command = new SafeCommand <int>(executeAction: (i) => { Assert.True(vm.IsBusy); }, vm); Assert.False(vm.IsBusy); command.Execute(number); //see Assert in command Assert.False(vm.IsBusy); }
public void GenericCanExecute(bool expected) { string result = null; var cmd = new SafeCommand <string>(s => { }, canExecute: s => { result = s; return(expected); }); Assert.Equal(expected, cmd.CanExecute("Foo")); Assert.Equal("Foo", result); }
public void ExecuteRunsIfReferenceTypeAndSetToNull() { int executions = 0; var Command = new SafeCommand <FakeChildContext>(context => executions += 1); var exception = Record.Exception(() => Command.Execute(null)); Assert.Null(exception); //"null is a valid value for a reference type"); Assert.True(executions == 1, "the Command should have executed"); }
public void Execute_NullableAndSetToNull_Runs() { int executions = 0; var Command = new SafeCommand <int?>(executeAction: context => executions += 1); var exception = Record.Exception(() => Command.Execute(null)); //"null is a valid value for a Nullable<int> type"); Assert.Null(exception); Assert.True(executions == 1); // "the Command should have executed"); }
public void CanExecute(bool expected) { bool canExecuteRan = false; var cmd = new SafeCommand(() => { }, canExecute: () => { canExecuteRan = true; return(expected); }); Assert.Equal(expected, cmd.CanExecute(null)); Assert.True(canExecuteRan); }
public void ExecuteParameterized() { object executed = null; var cmd = new SafeCommand <object>(executeAction: o => executed = o); var expected = new object(); cmd.Execute(expected); Assert.Equal(expected, executed); }
public void AsyncCommand_ExecuteAsync_StringParameter_Test(string parameter) { //Arrange var dts = new DeterministicTaskScheduler(); ICommand command = new SafeCommand <string>(StringParameterTask, dts, null, null); //Act command.Execute(parameter); dts.RunTasksUntilIdle(); //Assert }