public void PropertyChanged_CanGoBack_IsNotCalledWheNavigatingBackToFirstPage()
        {
            NavigationStack navigationStack = new NavigationStack();

            navigationStack.NavigateTo(new PageInfo("Page 1", null));
            navigationStack.NavigateTo(new PageInfo("Page 2", null));
            navigationStack.NavigateTo(new PageInfo("Page 3", null));

            int changedCount = 0;
            navigationStack.PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "CanGoBack") changedCount++; };

            navigationStack.GoBackTo(navigationStack[0]);

            Assert.Equal(0, changedCount);
        }
        public void NavigatingFrom_IsCalledWhenNavigatingBackToAPage()
        {
            NavigationStack navigationStack = new NavigationStack();

            navigationStack.NavigateTo(new PageInfo("Page 1", null));
            navigationStack.NavigateTo(new PageInfo("Page 2", null));
            navigationStack.NavigateTo(new PageInfo("Page 3", null));

            List<PageNavigationEventArgs> navigationEvents = new List<PageNavigationEventArgs>();
            navigationStack.NavigatingFrom += delegate (object sender, PageNavigationEventArgs e) { navigationEvents.Add(e); };

            navigationStack.GoBackTo(navigationStack[0]);

            Assert.Equal(1, navigationEvents.Count);
            PageNavigationEventArgs navigationEvent = navigationEvents[0];
            Assert.Equal(PageNavigationMode.Back, navigationEvent.NavigationMode);
            Assert.Equal("Page 3", navigationEvent.Page.PageName);
        }
        public void PageDisposed_IsCalledWhenNavigatingBackMultiplePages()
        {
            NavigationStack navigationStack = new NavigationStack();

            navigationStack.NavigateTo(new PageInfo("Page 1", null));
            navigationStack.NavigateTo(new PageInfo("Page 2", null));
            navigationStack.NavigateTo(new PageInfo("Page 3", null));

            List<PageNavigationEventArgs> pageDisposedEvents = new List<PageNavigationEventArgs>();
            navigationStack.PageDisposed += delegate (object sender, PageNavigationEventArgs e) { pageDisposedEvents.Add(e); };

            navigationStack.GoBackTo(navigationStack[0]);

            Assert.Equal(2, pageDisposedEvents.Count);
            PageNavigationEventArgs pageDisposedEventPage2 = pageDisposedEvents.First(e => e.Page.PageName == "Page 2");
            PageNavigationEventArgs pageDisposedEventPage3 = pageDisposedEvents.First(e => e.Page.PageName == "Page 3");
            Assert.Equal(PageNavigationMode.Back, pageDisposedEventPage2.NavigationMode);
            Assert.Equal("Page 2", pageDisposedEventPage2.Page.PageName);
            Assert.Equal(PageNavigationMode.Back, pageDisposedEventPage3.NavigationMode);
            Assert.Equal("Page 3", pageDisposedEventPage3.Page.PageName);
        }
        public void CollectionChanged_IsCalledWhenNavigatingBackTo_OnPage()
        {
            NavigationStack navigationStack = new NavigationStack();

            navigationStack.NavigateTo(new PageInfo("Page 1", null));
            navigationStack.NavigateTo(new PageInfo("Page 2", null));
            navigationStack.NavigateTo(new PageInfo("Page 3", null));

            List<NotifyCollectionChangedEventArgs> changeEvents = new List<NotifyCollectionChangedEventArgs>();
            navigationStack.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) { changeEvents.Add(e); };

            navigationStack.GoBackTo(navigationStack[1]);

            Assert.Equal(1, changeEvents.Count);
            NotifyCollectionChangedEventArgs changeEvent = changeEvents[0];
            Assert.Equal(NotifyCollectionChangedAction.Remove, changeEvent.Action);
            Assert.Equal(1, changeEvent.OldItems.Count);
            Assert.Equal("Page 3", ((PageInfo)changeEvent.OldItems[0]).PageName);
            Assert.Equal(2, changeEvent.OldStartingIndex);
        }
        public void CollectionChanged_IsCalledWhenNavigatingBackTo_MultiplePages()
        {
            NavigationStack navigationStack = new NavigationStack();

            navigationStack.NavigateTo(new PageInfo("Page 1", null));
            navigationStack.NavigateTo(new PageInfo("Page 2", null));
            navigationStack.NavigateTo(new PageInfo("Page 3", null));

            List<NotifyCollectionChangedEventArgs> changeEvents = new List<NotifyCollectionChangedEventArgs>();
            navigationStack.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) { changeEvents.Add(e); };

            navigationStack.GoBackTo(navigationStack[0]);

            Assert.Equal(1, changeEvents.Count);
            NotifyCollectionChangedEventArgs changeEvent = changeEvents[0];
            Assert.Equal(NotifyCollectionChangedAction.Reset, changeEvent.Action);
        }
        public void GoBackTo_ThrowsException_NullPage()
        {
            NavigationStack navigationStack = new NavigationStack();

            var e = Assert.Throws<ArgumentNullException>(() => navigationStack.GoBackTo(null));

            Assert.Equal("Value cannot be null.\r\nParameter name: page", e.Message);
            Assert.Equal("page", e.ParamName);
        }
        public void GoBackTo_ThrowsException_SpecifiedPageNotInBackStack()
        {
            NavigationStack navigationStack = new NavigationStack();

            navigationStack.NavigateTo(new PageInfo("Page 1", null));
            navigationStack.NavigateTo(new PageInfo("Page 2", null));

            PageInfo unknownPage = new PageInfo("Page 3", null);

            var e = Assert.Throws<InvalidOperationException>(() => navigationStack.GoBackTo(unknownPage));

            Assert.Equal("The specified page 'Page 3' does not exist in the navigation stack.", e.Message);
        }
        public void GoBackTo_NavigatesBackToSpecifiedPage()
        {
            NavigationStack navigationStack = new NavigationStack();
            PageInfo page2 = new PageInfo("Page 2", null);

            navigationStack.NavigateTo(new PageInfo("Page 1", null));
            navigationStack.NavigateTo(page2);
            navigationStack.NavigateTo(new PageInfo("Page 3", null));
            navigationStack.NavigateTo(new PageInfo("Page 4", null));

            navigationStack.GoBackTo(page2);

            Assert.Equal(2, navigationStack.Count);
            Assert.Equal("Page 1", navigationStack[0].PageName);
            Assert.Equal("Page 2", navigationStack[1].PageName);
        }