Beispiel #1
0
        public void AggregateUndoServiceUndoRedoTest()
        {
            // UndoServiceAggregate is created using an IUndoService array:
            var undoServiceForInt    = new UndoService <int>(GetIntState, SetIntState, null);
            var undoServiceForString = new UndoService <string>(GetStringState, SetStringState, null);

            IUndoService[] subservices      = { undoServiceForInt, undoServiceForString };
            var            serviceAggregate = new UndoServiceAggregate(subservices);


            // Changes are recorded by the individual UndoServices
            _statefulInt = 1;
            undoServiceForInt.RecordState();
            _statefulString = "One";
            undoServiceForString.RecordState();
            _statefulInt = 2;
            undoServiceForInt.RecordState();
            _statefulInt = 3;
            undoServiceForInt.RecordState();
            _statefulString = "Two";
            undoServiceForString.RecordState();


            /*
             * The UndoServiceAggregate provides a unified interface for performing undo/redo on the different tracked objects.
             * (You can also perform Undo/Redo on the individual services, which will undo the last change on the corresponding object.)
             */
            serviceAggregate.Undo();
            Assert.IsTrue(_statefulString.Equals("One"));
            Assert.IsTrue(_statefulInt == 3);

            serviceAggregate.Undo();
            Assert.IsTrue(_statefulString.Equals("One"));
            Assert.IsTrue(_statefulInt == 2);

            serviceAggregate.Undo();
            Assert.IsTrue(_statefulString.Equals("One"));
            Assert.IsTrue(_statefulInt == 1);

            serviceAggregate.Redo();
            Assert.IsTrue(_statefulString.Equals("One"));
            Assert.IsTrue(_statefulInt == 2);

            serviceAggregate.Redo();
            Assert.IsTrue(_statefulString.Equals("One"));
            Assert.IsTrue(_statefulInt == 3);

            serviceAggregate.Redo();
            Assert.IsTrue(_statefulString.Equals("Two"));
            Assert.IsTrue(_statefulInt == 3);
        }
        public void CanUndoCanRedoChangedAggregateTest()
        {
            _aggregateService.CanUndoChanged += _aggregateService_CanUndoChanged;
            _aggregateService.CanRedoChanged += _aggregateService_CanRedoChanged;
            _statefulInt = 1;
            _subUndoServiceForInt.RecordState();
            Assert.IsTrue(_canRedoChangedFiredCount == 0);
            Assert.IsTrue(_canUndoChangedFiredCount == 1);  //Item added to empty undo stack

            _statefulString = "One";
            _subUndoServiceForString.RecordState();
            Assert.IsTrue(_canUndoChangedFiredCount == 1); //Adding an item to an empty sub stack does not fire the event in the aggregate per se

            _aggregateService.Undo();
            Assert.IsTrue(_canUndoChangedFiredCount == 1); //Doesn't necessarily fire the event if the last item is removed from a subservice
            Assert.IsTrue(_canRedoChangedFiredCount == 1); //Item added to empty redo stack

            _aggregateService.Undo();
            Assert.IsTrue(_canUndoChangedFiredCount == 2); //Last item removed from undo stack (event fires)
            Assert.IsTrue(_canRedoChangedFiredCount == 1); //Item added to already filled redo stack (no event)

            _aggregateService.Reset();
            Assert.IsTrue(_canRedoChangedFiredCount == 2); //Redo stack cleared
            Assert.IsTrue(_canUndoChangedFiredCount == 2); //Undo Stack was already empty so there was no change

            _statefulInt = 2;
            _subUndoServiceForInt.RecordState();           //Item added to undo stack (event count = 3)
            _aggregateService.Undo();                      //Item added to redo stack (event count = 3), item removed from undo stack (event count = 4)
            _aggregateService.Redo();
            Assert.IsTrue(_canRedoChangedFiredCount == 4); //Last item removed from redo stack (event fired).
            Assert.IsTrue(_canUndoChangedFiredCount == 5); //Item added to empty undo stack (via undo operation).
        }