diff --git a/src/ReactiveUI.XamForms.Tests/Activation/ActivatingReactivePagesTests.cs b/src/ReactiveUI.XamForms.Tests/Activation/ActivatingReactivePagesTests.cs new file mode 100644 index 0000000000..6372b20e03 --- /dev/null +++ b/src/ReactiveUI.XamForms.Tests/Activation/ActivatingReactivePagesTests.cs @@ -0,0 +1,146 @@ +// Copyright (c) 2021 .NET Foundation and Contributors. All rights reserved. +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using ReactiveUI.XamForms; +using ReactiveUI.XamForms.Tests.Activation; +using ReactiveUI.XamForms.Tests.Activation.Mocks; +using Splat; +using Xamarin.Forms; +using Xunit; + +namespace ReactiveUI.Tests +{ + /// + /// Tests for activating views. + /// + public class ActivatingReactivePagesTests : IClassFixture> + { + private ApplicationFixture _fixture; + + /// + /// Initializes a new instance of the class. + /// + /// The fixture. + public ActivatingReactivePagesTests(ApplicationFixture fixture) + { + _fixture = fixture; + Locator.CurrentMutable.Register(() => new ActivationForViewFetcher(), typeof(IActivationForViewFetcher)); + Locator.CurrentMutable.Register>(() => new ShellView()); + Locator.CurrentMutable.Register>(() => new ContentPageView()); + Locator.CurrentMutable.Register>(() => new TabbedPageView()); + Locator.CurrentMutable.Register>(() => new CarouselPageView()); + Locator.CurrentMutable.Register>(() => new FlyoutPageView()); + _fixture.ActivateApp(new App()); + } + + /// + /// Tests to make sure that views generally activate. + /// + [Fact] + public void ActivatingReactiveShellTest() + { + var main = _fixture.AppMock!.MainPage as AppShell; + + Assert.Equal(1, main!.ViewModel!.IsActiveCount); + Assert.Equal(1, main.IsActiveCount); + } + + /// + /// Tests to make sure that views generally activate. + /// + [Fact] + public void ActivatingReactiveContentPageTest() + { + var vm = new ContentPageViewModel(); + var fixture = new ContentPageView + { + ViewModel = vm + }; + + // Activate + Shell.Current.Navigation.PushAsync(fixture); + Assert.Equal(1, fixture.ViewModel.IsActiveCount); + Assert.Equal(1, fixture.IsActiveCount); + + // Deactivate + Shell.Current.GoToAsync(".."); + fixture.ViewModel = null; + Assert.Equal(0, vm.IsActiveCount); + Assert.Equal(0, fixture.IsActiveCount); + } + + /// + /// Tests to make sure that views generally activate. + /// + [Fact] + public void ActivatingReactiveTabbedPageTest() + { + var vm1 = new TabbedPageViewModel(); + var fixture1 = new TabbedPageView + { + ViewModel = vm1 + }; + + // Activate + Shell.Current.Navigation.PushAsync(fixture1); + Assert.Equal(1, fixture1.ViewModel.IsActiveCount); + Assert.Equal(1, fixture1.IsActiveCount); + + // Deactivate + Shell.Current.GoToAsync(".."); + fixture1.ViewModel = null; + Assert.Equal(0, vm1.IsActiveCount); + Assert.Equal(0, fixture1.IsActiveCount); + } + + /// + /// Tests to make sure that views generally activate. + /// + [Fact] + public void ActivatingReactiveFlyoutPageTest() + { + var vm3 = new FlyOutPageViewModel(); + var fixture3 = new FlyoutPageView + { + ViewModel = vm3 + }; + + // Activate + Shell.Current.Navigation.PushAsync(fixture3); + Assert.Equal(1, fixture3.ViewModel!.IsActiveCount); + Assert.Equal(1, fixture3.IsActiveCount); + + // Deactivate + Shell.Current.GoToAsync(".."); + fixture3.ViewModel = null; + Assert.Equal(0, vm3.IsActiveCount); + Assert.Equal(0, fixture3.IsActiveCount); + } + + /// + /// Tests to make sure that views generally activate. + /// + [Fact] + public void ActivatingReactiveCarouselPageTest() + { + var vm4 = new CarouselPageViewModel(); + var fixture4 = new CarouselPageView + { + ViewModel = vm4 + }; + + // Activate + Shell.Current.Navigation.PushAsync(fixture4); + Assert.Equal(1, fixture4.ViewModel!.IsActiveCount); + Assert.Equal(1, fixture4.IsActiveCount); + + // Deactivate + Shell.Current.GoToAsync(".."); + fixture4.ViewModel = null; + Assert.Equal(0, vm4.IsActiveCount); + Assert.Equal(0, fixture4.IsActiveCount); + } + } +} diff --git a/src/ReactiveUI.XamForms.Tests/Activation/Mocks/App.xaml b/src/ReactiveUI.XamForms.Tests/Activation/Mocks/App.xaml new file mode 100644 index 0000000000..393be06d9b --- /dev/null +++ b/src/ReactiveUI.XamForms.Tests/Activation/Mocks/App.xaml @@ -0,0 +1,34 @@ + + + + + + #2196F3 + + + + diff --git a/src/ReactiveUI.XamForms.Tests/Activation/Mocks/App.xaml.cs b/src/ReactiveUI.XamForms.Tests/Activation/Mocks/App.xaml.cs new file mode 100644 index 0000000000..e26216449e --- /dev/null +++ b/src/ReactiveUI.XamForms.Tests/Activation/Mocks/App.xaml.cs @@ -0,0 +1,57 @@ +// Copyright (c) 2021 .NET Foundation and Contributors. All rights reserved. +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using Xamarin.Forms; + +namespace ReactiveUI.XamForms.Tests.Activation +{ + /// + /// The App. + /// + /// + public partial class App + { + /// + /// Initializes a new instance of the class. + /// + /// The page. + public App() + { + InitializeComponent(); + + MainPage = new AppShell(); + } + + /// + /// Application developers override this method to perform actions when the application starts. + /// + /// + /// To be added. + /// + protected override void OnStart() + { + } + + /// + /// Application developers override this method to perform actions when the application enters the sleeping state. + /// + /// + /// To be added. + /// + protected override void OnSleep() + { + } + + /// + /// Application developers override this method to perform actions when the application resumes from a sleeping state. + /// + /// + /// To be added. + /// + protected override void OnResume() + { + } + } +} diff --git a/src/ReactiveUI.XamForms.Tests/Activation/Mocks/AppShell.xaml b/src/ReactiveUI.XamForms.Tests/Activation/Mocks/AppShell.xaml new file mode 100644 index 0000000000..8378f7896b --- /dev/null +++ b/src/ReactiveUI.XamForms.Tests/Activation/Mocks/AppShell.xaml @@ -0,0 +1,39 @@ + + + + + + + +