From f04c0ab373ff69ea8cafb0a1dcf16e4f351a7d90 Mon Sep 17 00:00:00 2001 From: Nick Randolph Date: Thu, 21 Sep 2023 01:48:33 +1000 Subject: [PATCH] feat: Clear mappings between tests cases --- .../Frame/HRApp/Tests/BaseTestClass.cs | 14 + .../Frame/HRApp/Tests/Given_Frame.cs | 387 +++++++++--------- .../HRApp/Tests/Given_Frame_DataContext.cs | 278 ++++++------- .../Frame/HRApp/Tests/Given_TextBlock.cs | 69 ++-- .../HRApp/Tests/RemoteControlExtensions.cs | 7 +- src/Uno.UI/Helpers/TypeMappingHelper.cs | 11 + 6 files changed, 392 insertions(+), 374 deletions(-) create mode 100644 src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/BaseTestClass.cs diff --git a/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/BaseTestClass.cs b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/BaseTestClass.cs new file mode 100644 index 000000000000..0e7919e0d0e9 --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/BaseTestClass.cs @@ -0,0 +1,14 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Uno.UI.Helpers; + +namespace Uno.UI.RuntimeTests.Tests.HotReload.Frame.HRApp.Tests; + +public class BaseTestClass +{ + [TestInitialize] + public void InitHotReload() + { + TypeMappingHelper.ClearMappings(); + } + +} diff --git a/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_Frame.cs b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_Frame.cs index 62a101e49c24..d99a65320240 100644 --- a/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_Frame.cs +++ b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_Frame.cs @@ -12,203 +12,202 @@ using Uno.UI.RuntimeTests.Tests.HotReload; using Uno.UI.RuntimeTests.Tests.HotReload.Frame; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Uno.UI.Helpers; -namespace Uno.UI.RuntimeTests.Tests.HotReload.Frame.HRApp.Tests +namespace Uno.UI.RuntimeTests.Tests.HotReload.Frame.HRApp.Tests; + +[TestClass] +[RunsOnUIThread] +public class Given_Frame : BaseTestClass { - [TestClass] - [RunsOnUIThread] - public class Given_Frame + public const string SimpleTextChange = " (changed)"; + + public const string FirstPageTextBlockOriginalText = "First page"; + public const string FirstPageTextBlockChangedText = FirstPageTextBlockOriginalText + SimpleTextChange; + + public const string SecondPageTextBlockOriginalText = "Second page"; + public const string SecondPageTextBlockChangedText = SecondPageTextBlockOriginalText + SimpleTextChange; + + + /// + /// No Change to Page 1 - just loads Page 1 without triggering any HR changes + /// Useful for doing manual xaml changes and validating HR is applied + /// + [TestMethod] + public async Task Check_NoChange_Page1() + { + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; + + var frame = new Windows.UI.Xaml.Controls.Frame(); + UnitTestsUIContentHelper.Content = frame; + + frame.Navigate(typeof(HR_Frame_Pages_Page1)); + + var message = new HR_Frame_Pages_Page1().CreateUpdateFileMessage( + originalText: FirstPageTextBlockOriginalText, + replacementText: FirstPageTextBlockChangedText); + + // Check the initial text of the TextBlock + await frame.ValidateFirstTextBlockOnCurrentPageText(message.OriginalXaml); + } + + /// + /// Checks that a simple change to a XAML element (change Text on TextBlock) will be applied to + /// the currently visible page: + /// Open Page1 + /// Change Page1 + /// + [TestMethod] + public async Task Check_Can_Change_Page1() + { + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; + + var frame = new Windows.UI.Xaml.Controls.Frame(); + UnitTestsUIContentHelper.Content = frame; + + frame.Navigate(typeof(HR_Frame_Pages_Page1)); + + // Check the initial text of the TextBlock + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); + + // Check the updated text of the TextBlock + await HotReloadHelper.UpdateServerFileAndRevert( + FirstPageTextBlockOriginalText, + FirstPageTextBlockChangedText, + () => frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText), + ct); + } + + /// + /// Checks that a simple xaml change to the current page will be retained when + /// navigating forward to a new page and then going back to the original page + /// Open Page1 + /// Change Page1 + /// Navigate to Page2 + /// Navigate back to Page1 + /// + [TestMethod] + public async Task Check_Can_Change_Page1_Navigate_And_Return() + { + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; + + var frame = new Windows.UI.Xaml.Controls.Frame(); + UnitTestsUIContentHelper.Content = frame; + + frame.Navigate(typeof(HR_Frame_Pages_Page1)); + + // Check the initial text of the TextBlock + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); + + await HotReloadHelper.UpdateServerFileAndRevert( + FirstPageTextBlockOriginalText, + FirstPageTextBlockChangedText, + async () => + { + // Check to make sure the TextBlock was updated (see Check_Can_Change_Page1 for this test) + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText); + + // Navigate to the second page, verify navigation worked, and then navigate back + frame.Navigate(typeof(HR_Frame_Pages_Page2)); + await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); + frame.GoBack(); + + // Validate again that the TextBlock still has the updated value + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText); + }, + ct); + + // Check that after the test has executed, the xaml is back to the original text + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); + } + + + /// + /// Checks that a simple xaml change to the a page that is not currently visible will be + /// applied when the page is navigated to + /// Open Page1 + /// Change Page2 + /// Navigate to Page2 + /// + [TestMethod] + public async Task Check_Can_Change_Page2_Before_Navigation() + { + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; + + var frame = new Windows.UI.Xaml.Controls.Frame(); + UnitTestsUIContentHelper.Content = frame; + + // Navigate to Page1 + frame.Navigate(typeof(HR_Frame_Pages_Page1)); + + // Check the initial text of the TextBlock + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); + + await HotReloadHelper.UpdateServerFileAndRevert( + SecondPageTextBlockOriginalText, + SecondPageTextBlockChangedText, + async () => + { + // Check to make sure the current page wasn't changed + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); + + // Navigate to the second page, verify the TextBlock on the second page has the updated value + (frame.Content as HR_Frame_Pages_Page1)?.Page2Click(null, null); + + await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockChangedText); + + // Go back and Validate again that the TextBlock still has same value + frame.GoBack(); + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); + }, + ct); + + // Check that after the test has executed, the xaml is back to the original text + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); + } + + /// + /// Checks that a simple xaml change to the a page that is in the backstack will be + /// applied when the page is navigated back to + /// Open Page1 + /// Navigate to Page2 + /// Change Page1 + /// Navigate back to Page1 + /// + [TestMethod] + public async Task Check_Can_Change_Page1_Before_Navigating_Back() { - public const string SimpleTextChange = " (changed)"; - - public const string FirstPageTextBlockOriginalText = "First page"; - public const string FirstPageTextBlockChangedText = FirstPageTextBlockOriginalText + SimpleTextChange; - - public const string SecondPageTextBlockOriginalText = "Second page"; - public const string SecondPageTextBlockChangedText = SecondPageTextBlockOriginalText + SimpleTextChange; - - /// - /// No Change to Page 1 - just loads Page 1 without triggering any HR changes - /// Useful for doing manual xaml changes and validating HR is applied - /// - [TestMethod] - public async Task Check_NoChange_Page1() - { - var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; - - var frame = new Windows.UI.Xaml.Controls.Frame(); - UnitTestsUIContentHelper.Content = frame; - - frame.Navigate(typeof(HR_Frame_Pages_Page1)); - - var message = new HR_Frame_Pages_Page1().CreateUpdateFileMessage( - originalText: FirstPageTextBlockOriginalText, - replacementText: FirstPageTextBlockChangedText); - - // Check the initial text of the TextBlock - await frame.ValidateFirstTextBlockOnCurrentPageText(message.OriginalXaml); - } - - /// - /// Checks that a simple change to a XAML element (change Text on TextBlock) will be applied to - /// the currently visible page: - /// Open Page1 - /// Change Page1 - /// - [TestMethod] - public async Task Check_Can_Change_Page1() - { - var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; - - var frame = new Windows.UI.Xaml.Controls.Frame(); - UnitTestsUIContentHelper.Content = frame; - - frame.Navigate(typeof(HR_Frame_Pages_Page1)); - - // Check the initial text of the TextBlock - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - - // Check the updated text of the TextBlock - await HotReloadHelper.UpdateServerFileAndRevert( - FirstPageTextBlockOriginalText, - FirstPageTextBlockChangedText, - () => frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText), - ct); - } - - /// - /// Checks that a simple xaml change to the current page will be retained when - /// navigating forward to a new page and then going back to the original page - /// Open Page1 - /// Change Page1 - /// Navigate to Page2 - /// Navigate back to Page1 - /// - [TestMethod] - public async Task Check_Can_Change_Page1_Navigate_And_Return() - { - var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; - - var frame = new Windows.UI.Xaml.Controls.Frame(); - UnitTestsUIContentHelper.Content = frame; - - frame.Navigate(typeof(HR_Frame_Pages_Page1)); - - // Check the initial text of the TextBlock - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - - await HotReloadHelper.UpdateServerFileAndRevert( - FirstPageTextBlockOriginalText, - FirstPageTextBlockChangedText, - async () => - { - // Check to make sure the TextBlock was updated (see Check_Can_Change_Page1 for this test) - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText); - - // Navigate to the second page, verify navigation worked, and then navigate back - frame.Navigate(typeof(HR_Frame_Pages_Page2)); - await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); - frame.GoBack(); - - // Validate again that the TextBlock still has the updated value - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText); - }, - ct); - - // Check that after the test has executed, the xaml is back to the original text - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - } - - - /// - /// Checks that a simple xaml change to the a page that is not currently visible will be - /// applied when the page is navigated to - /// Open Page1 - /// Change Page2 - /// Navigate to Page2 - /// - [TestMethod] - public async Task Check_Can_Change_Page2_Before_Navigation() - { - var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; - - var frame = new Windows.UI.Xaml.Controls.Frame(); - UnitTestsUIContentHelper.Content = frame; - - // Navigate to Page1 - frame.Navigate(typeof(HR_Frame_Pages_Page1)); - - // Check the initial text of the TextBlock - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - - await HotReloadHelper.UpdateServerFileAndRevert( - SecondPageTextBlockOriginalText, - SecondPageTextBlockChangedText, - async () => - { - // Check to make sure the current page wasn't changed - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - - // Navigate to the second page, verify the TextBlock on the second page has the updated value - frame.Navigate(typeof(HR_Frame_Pages_Page2)); - - // This won't work since the current page might not be HR_Frame_Pages_Page1) - // (frame.Content as HR_Frame_Pages_Page1)?.Page2Click(null, null); - await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockChangedText); - - // Go back and Validate again that the TextBlock still has same value - frame.GoBack(); - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - }, - ct); - - // Check that after the test has executed, the xaml is back to the original text - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - } - - /// - /// Checks that a simple xaml change to the a page that is in the backstack will be - /// applied when the page is navigated back to - /// Open Page1 - /// Navigate to Page2 - /// Change Page1 - /// Navigate back to Page1 - /// - [TestMethod] - public async Task Check_Can_Change_Page1_Before_Navigating_Back() - { - var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; - - var frame = new Windows.UI.Xaml.Controls.Frame(); - UnitTestsUIContentHelper.Content = frame; - - // Navigate to Page1 - frame.Navigate(typeof(HR_Frame_Pages_Page1)); - - // Check the initial text of the TextBlock - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - - // Navigate to the second page, verify the TextBlock on the second page has the updated value - frame.Navigate(typeof(HR_Frame_Pages_Page2)); - await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); - - await HotReloadHelper.UpdateServerFileAndRevert( - FirstPageTextBlockOriginalText, - FirstPageTextBlockChangedText, - async () => - { - // Check to make sure the current page wasn't changed - await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); - - // Go back and Validate again that the TextBlock has changed value - frame.GoBack(); - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText); - }, - ct); - - // Check that after the test has executed, the xaml is back to the original text - await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - } + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; + + var frame = new Windows.UI.Xaml.Controls.Frame(); + UnitTestsUIContentHelper.Content = frame; + // Navigate to Page1 + frame.Navigate(typeof(HR_Frame_Pages_Page1)); + + // Check the initial text of the TextBlock + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); + + // Navigate to the second page, verify the TextBlock on the second page has the updated value + frame.Navigate(typeof(HR_Frame_Pages_Page2)); + await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); + + await HotReloadHelper.UpdateServerFileAndRevert( + FirstPageTextBlockOriginalText, + FirstPageTextBlockChangedText, + async () => + { + // Check to make sure the current page wasn't changed + await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); + + // Go back and Validate again that the TextBlock has changed value + frame.GoBack(); + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText); + }, + ct); + + // Check that after the test has executed, the xaml is back to the original text + await frame.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); } + } diff --git a/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_Frame_DataContext.cs b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_Frame_DataContext.cs index 95d1ff7a56dd..b955f8785e74 100644 --- a/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_Frame_DataContext.cs +++ b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_Frame_DataContext.cs @@ -15,146 +15,146 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Uno.UI.RuntimeTests.Tests.HotReload.Frame.HRApp.Tests.Pages; -namespace Uno.UI.RuntimeTests.Tests.HotReload.Frame.HRApp.Tests +namespace Uno.UI.RuntimeTests.Tests.HotReload.Frame.HRApp.Tests; + +[TestClass] +[RunsOnUIThread] +public class Given_Frame_DataContext : BaseTestClass { - [TestClass] - [RunsOnUIThread] - public class Given_Frame_DataContext + private const string SimpleTextChange = " (changed)"; + private const string VMText = " VM Text"; + private const string FirstPageTextBlockOriginalText = "First page"; + private const string FirstPageTextBlockChangedText = FirstPageTextBlockOriginalText + SimpleTextChange; + private const string FirstPageVMText = FirstPageTextBlockOriginalText + VMText; + + private const string SecondPageTextBlockOriginalText = "Second page"; + private const string SecondPageTextBlockChangedText = SecondPageTextBlockOriginalText + SimpleTextChange; + private const string SecondPageVMText = SecondPageTextBlockOriginalText + VMText; + + /// + /// Checks that a simple change to a XAML element (change Text on TextBlock) will be applied to + /// the currently visible page: + /// Open Page1 + /// Change Page1 + /// + [TestMethod] + public async Task Check_Can_Change_Page1_With_DataContext() { - private const string SimpleTextChange = " (changed)"; - private const string VMText = " VM Text"; - private const string FirstPageTextBlockOriginalText = "First page"; - private const string FirstPageTextBlockChangedText = FirstPageTextBlockOriginalText + SimpleTextChange; - private const string FirstPageVMText = FirstPageTextBlockOriginalText + VMText; - - private const string SecondPageTextBlockOriginalText = "Second page"; - private const string SecondPageTextBlockChangedText = SecondPageTextBlockOriginalText + SimpleTextChange; - private const string SecondPageVMText = SecondPageTextBlockOriginalText + VMText; - - /// - /// Checks that a simple change to a XAML element (change Text on TextBlock) will be applied to - /// the currently visible page: - /// Open Page1 - /// Change Page1 - /// - [TestMethod] - public async Task Check_Can_Change_Page1_With_DataContext() - { - var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; - - var frame = new Windows.UI.Xaml.Controls.Frame(); - UnitTestsUIContentHelper.Content = frame; - - frame.Navigate(typeof(HR_Frame_Pages_Page1)); - - var vm = new HR_Frame_Pages_Page1_VM(FirstPageVMText); - (frame.Content as Page).DataContext = vm; - - // Check the initial text of the TextBlock - await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); - - // Check the text of the TextBlock doesn't change - await HotReloadHelper.UpdateServerFileAndRevert( - FirstPageTextBlockOriginalText, - FirstPageTextBlockChangedText, - () => frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1), - ct); - - // Check that the text is still the original value - await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); - } - - /// - /// Checks that a simple xaml change to the current page will be retained when - /// navigating forward to a new page and then going back to the original page - /// Open Page1 - /// Change Page1 - /// Navigate to Page2 - /// Navigate back to Page1 - /// - [TestMethod] - public async Task Check_Can_Change_Page1_Navigate_And_Return_With_DataContext() - { - var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; - - var frame = new Windows.UI.Xaml.Controls.Frame(); - UnitTestsUIContentHelper.Content = frame; - - frame.Navigate(typeof(HR_Frame_Pages_Page1)); - - var vm = new HR_Frame_Pages_Page1_VM(FirstPageVMText); - (frame.Content as Page).DataContext = vm; - - // Check the initial text of the TextBlock - await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); - - await HotReloadHelper.UpdateServerFileAndRevert( - FirstPageTextBlockOriginalText, - FirstPageTextBlockChangedText, - async () => - { - // Check to make sure the TextBlock was updated (see Check_Can_Change_Page1 for this test) - await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); - - // Navigate to the second page, verify navigation worked, and then navigate back - frame.Navigate(typeof(HR_Frame_Pages_Page2)); - await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); - frame.GoBack(); - - // Validate again that the TextBlock still has the updated value - await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); - }, - ct); - - // Check that after the test has executed, the xaml is back to the original text - await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); - } - - /// - /// Checks that a simple xaml change to the a page that is in the backstack will be - /// applied when the page is navigated back to - /// Open Page1 - /// Navigate to Page2 - /// Change Page1 - /// Navigate back to Page1 - /// - [TestMethod] - public async Task Check_Can_Change_Page1_Before_Navigating_Back_With_DataContext() - { - var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; - - var frame = new Windows.UI.Xaml.Controls.Frame(); - UnitTestsUIContentHelper.Content = frame; - - // Navigate to Page1 - frame.Navigate(typeof(HR_Frame_Pages_Page1)); - - var vm = new HR_Frame_Pages_Page1_VM(FirstPageVMText); - (frame.Content as Page).DataContext = vm; - - // Check the initial text of the TextBlock - await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); - - // Navigate to the second page, verify the TextBlock on the second page has the updated value - frame.Navigate(typeof(HR_Frame_Pages_Page2)); - await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); - - await HotReloadHelper.UpdateServerFileAndRevert( - FirstPageTextBlockOriginalText, - FirstPageTextBlockChangedText, - async () => - { - // Check to make sure the current page wasn't changed - await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); - - // Go back and Validate again that the TextBlock has changed value - frame.GoBack(); - await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); - }, - ct); - - // Check that after the test has executed, the xaml is back to the original text - await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); - } + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; + + var frame = new Windows.UI.Xaml.Controls.Frame(); + UnitTestsUIContentHelper.Content = frame; + + frame.Navigate(typeof(HR_Frame_Pages_Page1)); + + var vm = new HR_Frame_Pages_Page1_VM(FirstPageVMText); + (frame.Content as Page).DataContext = vm; + + // Check the initial text of the TextBlock + await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); + + // Check the text of the TextBlock doesn't change + await HotReloadHelper.UpdateServerFileAndRevert( + FirstPageTextBlockOriginalText, + FirstPageTextBlockChangedText, + () => frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1), + ct); + + // Check that the text is still the original value + await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); + } + + /// + /// Checks that a simple xaml change to the current page will be retained when + /// navigating forward to a new page and then going back to the original page + /// Open Page1 + /// Change Page1 + /// Navigate to Page2 + /// Navigate back to Page1 + /// + [TestMethod] + public async Task Check_Can_Change_Page1_Navigate_And_Return_With_DataContext() + { + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; + + var frame = new Windows.UI.Xaml.Controls.Frame(); + UnitTestsUIContentHelper.Content = frame; + + frame.Navigate(typeof(HR_Frame_Pages_Page1)); + + var vm = new HR_Frame_Pages_Page1_VM(FirstPageVMText); + (frame.Content as Page).DataContext = vm; + + // Check the initial text of the TextBlock + await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); + + await HotReloadHelper.UpdateServerFileAndRevert( + FirstPageTextBlockOriginalText, + FirstPageTextBlockChangedText, + async () => + { + // Check to make sure the TextBlock was updated (see Check_Can_Change_Page1 for this test) + await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); + + // Navigate to the second page, verify navigation worked, and then navigate back + frame.Navigate(typeof(HR_Frame_Pages_Page2)); + await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); + frame.GoBack(); + + // Validate again that the TextBlock still has the updated value + await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); + }, + ct); + + // Check that after the test has executed, the xaml is back to the original text + await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); + } + + /// + /// Checks that a simple xaml change to the a page that is in the backstack will be + /// applied when the page is navigated back to + /// Open Page1 + /// Navigate to Page2 + /// Change Page1 + /// Navigate back to Page1 + /// + [TestMethod] + [Ignore("Not yet working")] + public async Task Check_Can_Change_Page1_Before_Navigating_Back_With_DataContext() + { + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token; + + var frame = new Windows.UI.Xaml.Controls.Frame(); + UnitTestsUIContentHelper.Content = frame; + + // Navigate to Page1 + frame.Navigate(typeof(HR_Frame_Pages_Page1)); + + var vm = new HR_Frame_Pages_Page1_VM(FirstPageVMText); + (frame.Content as Page).DataContext = vm; + + // Check the initial text of the TextBlock + await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); + + // Navigate to the second page, verify the TextBlock on the second page has the updated value + frame.Navigate(typeof(HR_Frame_Pages_Page2)); + await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); + + await HotReloadHelper.UpdateServerFileAndRevert( + FirstPageTextBlockOriginalText, + FirstPageTextBlockChangedText, + async () => + { + // Check to make sure the current page wasn't changed + await frame.ValidateFirstTextBlockOnCurrentPageText(SecondPageTextBlockOriginalText); + + // Go back and Validate again that the TextBlock has changed value + frame.GoBack(); + await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); + }, + ct); + + // Check that after the test has executed, the xaml is back to the original text + await frame.ValidateTextBlockOnCurrentPageText(vm.TitleText, 1); } } diff --git a/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_TextBlock.cs b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_TextBlock.cs index 0da27bcbb88f..1174394bb7ec 100644 --- a/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_TextBlock.cs +++ b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/Given_TextBlock.cs @@ -13,46 +13,45 @@ using Uno.UI.RuntimeTests.Tests.HotReload.Frame; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace Uno.UI.RuntimeTests.Tests.HotReload.Frame.HRApp.Tests +namespace Uno.UI.RuntimeTests.Tests.HotReload.Frame.HRApp.Tests; + +[TestClass] +[RunsOnUIThread] +public class Given_TextBlock : BaseTestClass { - [TestClass] - [RunsOnUIThread] - public class Given_TextBlock - { - public const string SimpleTextChange = " (changed)"; + public const string SimpleTextChange = " (changed)"; - public const string FirstPageTextBlockOriginalText = "First page"; - public const string FirstPageTextBlockChangedText = FirstPageTextBlockOriginalText + SimpleTextChange; + public const string FirstPageTextBlockOriginalText = "First page"; + public const string FirstPageTextBlockChangedText = FirstPageTextBlockOriginalText + SimpleTextChange; - public const string SecondPageTextBlockOriginalText = "Second page"; - public const string SecondPageTextBlockChangedText = SecondPageTextBlockOriginalText + SimpleTextChange; + public const string SecondPageTextBlockOriginalText = "Second page"; + public const string SecondPageTextBlockChangedText = SecondPageTextBlockOriginalText + SimpleTextChange; - /// - /// Checks that a simple change to a XAML element (change Text on TextBlock) will be applied to - /// the currently visible page: - /// Open Page1 - /// Change Page1 - /// - [TestMethod] - public async Task When_Changing_TextBlock() + /// + /// Checks that a simple change to a XAML element (change Text on TextBlock) will be applied to + /// the currently visible page: + /// Open Page1 + /// Change Page1 + /// + [TestMethod] + public async Task When_Changing_TextBlock() + { + var ct = new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token; + + UnitTestsUIContentHelper.Content = new ContentControl { - var ct = new CancellationTokenSource(TimeSpan.FromSeconds(60)).Token; - - UnitTestsUIContentHelper.Content = new ContentControl - { - Content = new HR_Frame_Pages_Page1() - }; - - // Check the initial text of the TextBlock - await UnitTestsUIContentHelper.Content.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); - - // Check the updated text of the TextBlock - await HotReloadHelper.UpdateServerFileAndRevert( - FirstPageTextBlockOriginalText, - FirstPageTextBlockChangedText, - () => UnitTestsUIContentHelper.Content.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText), - ct); - } + Content = new HR_Frame_Pages_Page1() + }; + + // Check the initial text of the TextBlock + await UnitTestsUIContentHelper.Content.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockOriginalText); + + // Check the updated text of the TextBlock + await HotReloadHelper.UpdateServerFileAndRevert( + FirstPageTextBlockOriginalText, + FirstPageTextBlockChangedText, + () => UnitTestsUIContentHelper.Content.ValidateFirstTextBlockOnCurrentPageText(FirstPageTextBlockChangedText), + ct); } } diff --git a/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/RemoteControlExtensions.cs b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/RemoteControlExtensions.cs index 75ea1622565d..52a9cbba111b 100644 --- a/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/RemoteControlExtensions.cs +++ b/src/Uno.UI.RuntimeTests/Tests/HotReload/Frame/HRApp/Tests/RemoteControlExtensions.cs @@ -25,12 +25,7 @@ public static async Task UpdateServerFile(string originalText, string replace await RemoteControlClient.Instance.WaitForConnection(); - var replacementType = typeof(T).GetReplacementType(); - var element = replacementType != typeof(T) ? - Activator.CreateInstance(replacementType) as FrameworkElement : - new T(); - - var message = element?.CreateUpdateFileMessage( + var message = new T().CreateUpdateFileMessage( originalText: originalText, replacementText: replacementText); diff --git a/src/Uno.UI/Helpers/TypeMappingHelper.cs b/src/Uno.UI/Helpers/TypeMappingHelper.cs index de1847fcc65f..cb0c3036424e 100644 --- a/src/Uno.UI/Helpers/TypeMappingHelper.cs +++ b/src/Uno.UI/Helpers/TypeMappingHelper.cs @@ -62,5 +62,16 @@ internal static void RegisterMapping(Type mappedType, Type originalType) MappedTypeToOrignalTypeMapings[mappedType] = originalType; OriginalTypeToMappedType[originalType] = mappedType; } + + /// + /// This method is required for testing purposes. A typical test will navigate + /// to a specific page and expect that page (not a replacement type) as a starting + /// point. For this to work, we need to reset the mappings. + /// + internal static void ClearMappings() + { + MappedTypeToOrignalTypeMapings.Clear(); + OriginalTypeToMappedType.Clear(); + } } }