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 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 TriggeredTransitionWithConditionIsNotMade() { var evt = new ManualResetEvent(false); var startedEvt = new ManualResetEvent(false); var transitionMade = false; var trigger = new Subject <Object>(); StateMachine.AddTransition(TestStates.Collapsed, TestStates.FadingIn, trigger, args => false); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { transitionMade = true; _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.StateMachineStarted += (sender, args) => startedEvt.Set(); StateMachine.Start(); startedEvt.WaitOne(); trigger.OnNext(null); evt.WaitOne(2000); Assert.False(transitionMade); Assert.AreEqual(StateMachine.CurrentState, TestStates.Collapsed); }
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 TransitionActionOfTriggeredTransitionWithConditionIsCalled() { var trigger = new Subject <object>(); var evt = new ManualResetEvent(false); var startedEvt = new ManualResetEvent(false); var transitionActionCalled = false; Action <object> transitionAction = o => transitionActionCalled = true; StateMachine.AddTransition(TestStates.Collapsed, TestStates.FadingIn, trigger, o => true, transitionAction); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.StateMachineStarted += (sender, args) => startedEvt.Set(); StateMachine.Start(); startedEvt.WaitOne(); trigger.OnNext(null); evt.WaitOne(); Assert.True(transitionActionCalled); }
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 TransitionActionOfTriggeredTransitionWithConditionIsNotCalled() { var trigger = new Subject <object>(); var evt = new ManualResetEvent(false); var startedEvt = new ManualResetEvent(false); var transitionActionCalled = false; Action <object> transitionAction = o => transitionActionCalled = true; StateMachine.AddTransition(TestStates.Collapsed, TestStates.FadingIn, trigger, o => false, transitionAction); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.StateMachineStarted += (sender, args) => startedEvt.Set(); StateMachine.Start(); while (!startedEvt.WaitOne(50)) { DispatcherHelper.DoEvents(); } trigger.OnNext(null); evt.WaitOne(2000); DispatcherHelper.DoEvents(); Assert.False(transitionActionCalled); Assert.AreEqual(StateMachine.CurrentState, TestStates.Collapsed); }
public void TimedTransitionWithConditionIsNotMade() { var evt = new ManualResetEvent(false); var transitionMade = false; StateMachine.AddTimedTransition(TestStates.Collapsed, TestStates.FadingIn, TimeSpan.FromMilliseconds(1000), () => false); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { transitionMade = true; _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.Start(); evt.WaitOne(3000); Assert.False(transitionMade); }
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(); evt.WaitOne(); Assert.True(transitionMade); }
public void TransitionActionOfTimedTransitionIsCalled() { var evt = new ManualResetEvent(false); var transitionActionCalled = false; Action transitionAction = () => transitionActionCalled = true; StateMachine.AddTimedTransition(TestStates.Collapsed, TestStates.FadingIn, TimeSpan.FromMilliseconds(1000), transitionAction); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.Start(); evt.WaitOne(); Assert.True(transitionActionCalled); }
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); Assert.False(transitionActionCalled); }
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(); }); StateMachine.Start(); evt.WaitOne(2000); DispatcherHelper.DoEvents(); Assert.False(transitionMade); Assert.AreEqual(StateMachine.CurrentState, TestStates.Collapsed); }
public void SingleEntryActionIsCalled() { var evt = new ManualResetEvent(false); var entryActionCalled = false; Action entryAction = () => entryActionCalled = true; StateMachine.AddAutomaticTransition(TestStates.Collapsed, TestStates.FadingIn); StateMachine.AddEntryAction(TestStates.FadingIn, entryAction); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.Start(); evt.WaitOne(); Assert.True(entryActionCalled); }
public void TimedTransitionIsMade() { var evt = new ManualResetEvent(false); var transitionMade = false; StateMachine.AddTimedTransition(TestStates.Collapsed, TestStates.FadingIn, TimeSpan.FromMilliseconds(1000)); _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 TriggeredTransitionWithConditionIsMade() { var evt = new ManualResetEvent(false); var startedEvt = new ManualResetEvent(false); var transitionMade = false; var trigger = new Subject <Object>(); StateMachine.AddTransition(TestStates.Collapsed, TestStates.FadingIn, trigger, args => true); _stateChangedSubscription = StateChanged.Where(args => args.ToState == TestStates.FadingIn).Subscribe(args => { transitionMade = true; _stateChangedSubscription.Dispose(); evt.Set(); }); StateMachine.StateMachineStarted += (sender, args) => startedEvt.Set(); StateMachine.Start(); while (!startedEvt.WaitOne(50)) { DispatcherHelper.DoEvents(); } trigger.OnNext(null); while (!evt.WaitOne(50)) { DispatcherHelper.DoEvents(); } Assert.True(transitionMade); }