public void VariousOperations_PublishCorrectEvents() { ParameterizedTest .TestCase <Action <ScreenLifecycleOperations>, IEvent>(x => x.Initialize(), ScreenEvents.Initialize()) .TestCase(x => x.Initialize(new Subject()), ScreenEvents.Initialize <Subject>()) .TestCase(x => x.Activate(), ScreenEvents.Activate) .TestCase(x => x.Deactivate(), ScreenEvents.Deactivate) .TestCase(x => x.RequestClose(), ScreenEvents.RequestClose) .TestCase(x => x.Close(), ScreenEvents.Close) .Run((lifecycleAction, expectedEvent) => { List <IEvent> actualEvents = new List <IEvent>(); TestScreen screen = new TestScreen(); ScreenLifecycleOperations ops = new ScreenLifecycleOperations(Aggregator, screen); AddEventHandlerForAllEvents( screen, handlerAction: actualEvents.Add, includeExceptionOccured: true ); lifecycleAction(ops); IEvent actualEvent = actualEvents.SingleOrDefault(); Assert.AreEqual(expectedEvent, actualEvent); }); }
public void RequestClose_ReturnsTrueIfNoHandlerSetsIsCloseAllowedToFalse() { TestScreen screen = new TestScreen(); ScreenLifecycleOperations ops = new ScreenLifecycleOperations(Aggregator, screen); bool result = new ScreenLifecycleOperations(Aggregator, screen) .RequestClose(); Assert.IsTrue(result); AddEventHandlerFor( ScreenEvents.RequestClose, screen, (ev, args) => { } ); result = new ScreenLifecycleOperations(Aggregator, screen) .RequestClose(); Assert.IsTrue(result); AddEventHandlerFor( ScreenEvents.RequestClose, screen, (ev, args) => { args.IsCloseAllowed = false; } ); result = new ScreenLifecycleOperations(Aggregator, screen) .RequestClose(); Assert.IsFalse(result); }
public void RequestClose_StopsAfterFirstChildReturnsFalse() { ScreenConductor conductor = CreateScreenConductor(); var first = new ScreenMock(Aggregator) { RequestCloseResult = true }; var second = new ScreenMock(Aggregator) { RequestCloseResult = false }; var third = new ScreenMock(Aggregator) { RequestCloseResult = true }; OpenScreen(conductor, first); OpenScreen(conductor, second); OpenScreen(conductor, third); bool result = new ScreenLifecycleOperations(Aggregator, conductor) .RequestClose(); Assert.IsFalse(result, "RequestClose should return false."); Assert.IsTrue(first.WasCloseRequested); Assert.IsTrue(second.WasCloseRequested); Assert.IsFalse(third.WasCloseRequested); }
private void Initialize() { _window.Closed += HandleWindowClosedToDisconnected; _screenOps = new ScreenLifecycleOperations(_aggregator, _screen); _screenOps.Activate(); try { bool windowImplementsViewInterface = ViewFactory.TryInitializeView(_window, _screen); if (!windowImplementsViewInterface) { _window.Content = ViewFactory.CreateView(_screen); } } catch (Exception ex) { if (!ex.IsCritical()) { CloseScreen(checkState: false); } throw; } _screen.Children.Add(this); _aggregator.Publish( WindowService.InitializeWindowEvent, new InitializeWindowEventArgs(_screen, _window) ); AttachHandlers(); }
protected void SetLifecycleStateTo(LifecycleState state) { var operations = new ScreenLifecycleOperations(Aggregator, Screen); if (state == LifecycleState.Created) { return; } operations.Initialize(); if (state == LifecycleState.Initialized) { return; } operations.Activate(); if (state == LifecycleState.Activated) { return; } operations.Deactivate(); if (state == LifecycleState.Deactivated) { return; } operations.Close(); if (state == LifecycleState.Closed) { return; } if (state == LifecycleState.ExceptionOccured) { Aggregator.Publish( ScreenEvents.LifecycleExceptionOccured, new ScreenEventArgs(Screen) ); } else { throw new NotSupportedException(); } }
private void CloseScreen(bool checkState) { var screenLifecycle = ScreenHelper.GetChild <ScreenLifecycle>(_screen); var ops = new ScreenLifecycleOperations(_aggregator, _screen); if (!checkState || screenLifecycle.CanDeactivate) { ops.Deactivate(); } if (!checkState || screenLifecycle.CanClose) { ops.Close(); } }
public void VariousOperations_WhenHandlerThrowsException_RaiseLifecycleExceptionOccuredEventAndThrowLifecycleException() { ParameterizedTest .TestCase <Action <ScreenLifecycleOperations>, IEvent>(x => x.Initialize(), ScreenEvents.Initialize()) .TestCase(x => x.Initialize(new Subject()), ScreenEvents.Initialize <Subject>()) .TestCase(x => x.Activate(), ScreenEvents.Activate) .TestCase(x => x.Deactivate(), ScreenEvents.Deactivate) .TestCase(x => x.RequestClose(), ScreenEvents.RequestClose) .TestCase(x => x.Close(), ScreenEvents.Close) .Run((lifecycleAction, expectedEvent) => { List <IEvent> actualEvents = new List <IEvent>(); TestScreen screen = new TestScreen(); ScreenLifecycleOperations ops = new ScreenLifecycleOperations(Aggregator, screen); InvalidOperationException sourceException = new InvalidOperationException(); AddEventHandlerForAllEvents( screen, handlerAction: e => { actualEvents.Add(e); throw sourceException; }, includeExceptionOccured: false ); AddEventHandlerFor( ScreenEvents.LifecycleExceptionOccured, screen, handlerAction: (ev, _) => actualEvents.Add(ev) ); var exceptionExpr = AssertHelper.Throws <ScreenLifecycleException>(() => lifecycleAction(ops) ); CollectionAssert.AreEquivalent( new IEvent[] { expectedEvent, ScreenEvents.LifecycleExceptionOccured }, actualEvents ); Assert.AreEqual(sourceException, exceptionExpr.Exception.InnerException); }); }