public void EntryActionsAreCalledInSeries() { var evt = new ManualResetEvent(false); const int numEntryActionsToCall = 5; var numEntryActionsCalled = 0; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn); StateMachine.AddAutomaticTransition(TestStates.FadingIn, TestStates.Visible); StateMachine.AddAutomaticTransition(TestStates.Visible, TestStates.FadingOut); StateMachine.AddAutomaticTransition(TestStates.FadingOut, TestStates.NotStarted); Action entryAction = () => numEntryActionsCalled++; StateMachine.AddEntryAction(TestStates.Collapsed, entryAction); StateMachine.AddEntryAction(TestStates.FadingIn, entryAction); StateMachine.AddEntryAction(TestStates.Visible, entryAction); StateMachine.AddEntryAction(TestStates.FadingOut, entryAction); StateMachine.AddEntryAction(TestStates.NotStarted, entryAction); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.NotStarted).Subscribe(args => { _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.Start(); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } Assert.AreEqual(numEntryActionsToCall, numEntryActionsCalled); }
public void EntryActionIsNotCalledOnOtherTransitions() { var evt = new ManualResetEvent(false); var entryActionCalled = false; Action entryAction = () => entryActionCalled = true; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn); StateMachine.AddEntryAction(TestStates.FadingIn, TestStates.NotStarted, entryAction); StateMachine.AddEntryAction(TestStates.FadingIn, TestStates.FadingOut, entryAction); StateMachine.AddEntryAction(TestStates.FadingIn, TestStates.Visible, entryAction); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { evt.Set(); _stateChangedSubscription.Dispose(); }); StateMachine.Start(); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } Assert.False(entryActionCalled); }
public void MultipleEntryActionsAreCalled() { var evt = new ManualResetEvent(false); const int numEntryActionsToCall = 10; var numEntryActionsCalled = 0; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn); Action entryAction = () => numEntryActionsCalled++; for (int i = 0; i < numEntryActionsToCall; i++) { StateMachine.AddEntryAction(TestStates.FadingIn, entryAction); } _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.Start(); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } Assert.AreEqual(numEntryActionsToCall, numEntryActionsCalled); }
public void ConditionOfExitActionCanAccessDispatcher() { var evt = new ManualResetEvent(false); var dispatcherObject = new Window(); var exitAction = new Action(() => {}); var condition = new Func <bool>(() => { Assert.DoesNotThrow(() => dispatcherObject.Dispatcher.VerifyAccess()); return(true); }); StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn); StateMachine.AddExitAction(TestStates.Collapsed, exitAction, condition); StateMachine.StateChanged += (sender, args) => evt.Set(); StateMachine.Start(); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } }
public void ExceptionInConditionOfAutomaticTransitionIsHandledAndReported() { var evt = new ManualResetEvent(false); var exceptionHandledAndReported = false; var condition = new Func <bool>(() => { throw new Exception(); }); StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn, condition); StateMachine.StateMachineException += (sender, args) => { exceptionHandledAndReported = true; evt.Set(); }; StateMachine.Start(); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } Assert.True(StateMachine.CurrentState == TestStates.Collapsed); Assert.True(exceptionHandledAndReported); }
public void ExitActionIsCalledOnSpecificTransition() { var evt = new ManualResetEvent(false); var exitActionCalled = false; Action exitAction = () => exitActionCalled = true; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn); StateMachine.AddExitAction(TestStates.Collapsed, TestStates.FadingIn, exitAction); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { evt.Set(); _stateChangedSubscription.Dispose(); }); StateMachine.Start(); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } Assert.True(exitActionCalled); }
public void ActionOfAutomaticTransitionCanAccessDispatcher() { var evt = new ManualResetEvent(false); var dispatcherObject = new Window(); var transitionAction = new Action(() => Assert.DoesNotThrow(() => dispatcherObject.Dispatcher.VerifyAccess())); StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn, transitionAction); StateMachine.StateChanged += (sender, args) => evt.Set(); StateMachine.Start(); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } }
public void ExceptionInConditionOfAutomaticTransitionIsHandledAndReported() { var evt = new ManualResetEvent(false); var exceptionHandledAndReported = false; var condition = new Func <bool>(() => { throw new Exception(); }); StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn, condition); StateMachine.StateMachineException += (sender, args) => exceptionHandledAndReported = true; StateMachine.Start(); evt.WaitOne(2000); Assert.AreEqual(StateMachine.CurrentState, TestStates.Collapsed); Assert.True(exceptionHandledAndReported); }
public void AutomaticTransitionWithConditionIsNotMade() { var evt = new ManualResetEvent(false); var transitionMade = false; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn, () => false); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { transitionMade = true; _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.Start(); evt.WaitOne(2000); Assert.False(transitionMade); }
public void ExceptionInTransitionActionOfAutomaticTransitionIsHandledAndReported() { var evt = new ManualResetEvent(false); var exceptionHandledAndReported = false; var transitionAction = new Action(() => { throw new Exception(); }); StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn, transitionAction); StateMachine.StateChanged += (sender, args) => evt.Set(); StateMachine.StateMachineException += (sender, args) => exceptionHandledAndReported = true; StateMachine.Start(); evt.WaitOne(); Assert.True(exceptionHandledAndReported); }
public void TransitionActionOfAutomaticTransitionIsCalled() { var evt = new ManualResetEvent(false); var transitionActionCalled = false; Action transitionAction = () => transitionActionCalled = true; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn, transitionAction); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.Start(); evt.WaitOne(); Assert.True(transitionActionCalled); }
public void ExceptionInConditionOfExitActionIsHandledAndReported() { var evt = new ManualResetEvent(false); var exceptionHandledAndReported = false; Action exitAction = () => { }; Func <bool> condition = () => { throw new Exception(); }; StateMachine.AddExitAction(TestStates.Collapsed, exitAction, condition); StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn); StateMachine.StateChanged += (sender, args) => evt.Set(); StateMachine.StateMachineException += (sender, args) => exceptionHandledAndReported = true; StateMachine.Start(); evt.WaitOne(); Assert.True(exceptionHandledAndReported); }
public void ConditionalEntryActionIsNotCalled() { var evt = new ManualResetEvent(false); var entryActionCalled = false; Action entryAction = () => entryActionCalled = true; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn); StateMachine.AddEntryAction(TestStates.FadingIn, entryAction, () => false); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { evt.Set(); _stateChangedSubscription.Dispose(); }); StateMachine.Start(); evt.WaitOne(); Assert.False(entryActionCalled); }
public void AutomaticTransitionWithConditionIsMade() { var evt = new ManualResetEvent(false); var transitionMade = false; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn, () => true); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { transitionMade = true; _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.Start(); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } Assert.True(transitionMade); }
public void ExceptionInExitActionIsHandledAndReported() { var evt = new ManualResetEvent(false); var exceptionHandledAndReported = false; Action exitAction = () => { throw new Exception(); }; StateMachine.AddExitAction(TestStates.Collapsed, exitAction); StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn); StateMachine.StateChanged += (sender, args) => evt.Set(); StateMachine.StateMachineException += (sender, args) => exceptionHandledAndReported = true; StateMachine.Start(); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } Assert.True(exceptionHandledAndReported); }
public void TransitionActionOfAutomaticTransitionWithConditionIsNotCalled() { var evt = new ManualResetEvent(false); var transitionActionCalled = false; Action transitionAction = () => transitionActionCalled = true; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn, () => false, transitionAction); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.Start(); evt.WaitOne(2000); DispatcherHelper.DoEvents(); Assert.False(transitionActionCalled); Assert.AreEqual(StateMachine.CurrentState, TestStates.Collapsed); }