From 3518da587631428105e7b3436d75ffde5ad05e50 Mon Sep 17 00:00:00 2001 From: jbe2277 Date: Sat, 5 Oct 2024 10:27:12 +0200 Subject: [PATCH] UITest/InfoMan: AddAndRemoveEntriesTest --- .../Tests/AddressBookTest.cs | 73 +++++++++++++++++-- .../Views/ShellWindow.cs | 8 ++ .../Resources/ControlResources.xaml | 8 ++ 3 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/Samples.UITest/InformationManager.Test/Tests/AddressBookTest.cs b/src/Samples.UITest/InformationManager.Test/Tests/AddressBookTest.cs index d2e22abb..b6932bdc 100644 --- a/src/Samples.UITest/InformationManager.Test/Tests/AddressBookTest.cs +++ b/src/Samples.UITest/InformationManager.Test/Tests/AddressBookTest.cs @@ -2,13 +2,14 @@ using Xunit.Abstractions; using Xunit; using UITest.InformationManager.Views; +using FlaUI.Core.Definitions; namespace UITest.InformationManager.Tests; public class AddressBookTest(ITestOutputHelper log) : UITest(log) { [Fact] - public void ShowContactsTest() => Run(() => + public void SearchContactsAndChangeEntriesTest() => Run(() => { Launch(); var window = GetShellWindow(); @@ -38,17 +39,79 @@ public void ShowContactsTest() => Run(() => Assert.Equal(2, contactListView.ContactItems.Count); Assert.Null(contactListView.ContactList.SelectedItem); contactListView.ContactList.Select(0); - AssertContactItem(contactListView.ContactList.SelectedItem.As(), contactView, "Michael", "Pfeiffer", "michael.pfeiffer@fabrikam.com", "(222) 555-0105"); + var item = contactListView.ContactList.SelectedItem.As(); + AssertContactItem(item, contactView, "Michael", "Pfeiffer", "michael.pfeiffer@fabrikam.com", "(222) 555-0105"); + contactView.FirstnameBox.Text = "TFirstname"; + Assert.Equal("TFirstname", item.FirstnameLabel.Text); + contactView.PhoneBox.Text = "TPhone"; + Assert.Equal("TPhone", item.PhoneLabel.Text); window.ExitCommand.Click(); }); - private static void AssertContactItem(ContactListItem contactItem, ContactView? contactView, string firstName, string lastName, string email, string phone) + [Fact] + public void AddAndRemoveEntriesTest() => Run(() => + { + SkipAppClose = true; + + Launch(); + var window = GetShellWindow(); + window.WindowState = WindowVisualState.Maximized; + window.RootTreeItem.ContactsNode.Click(); + var contactListView = window.ContactLayoutView.ContactListView; + var contactView = window.ContactLayoutView.ContactView; + + Assert.Equal(5, contactListView.ContactItems.Count); + window.NewContactCommand.Click(); + Assert.Equal(6, contactListView.ContactItems.Count); + var newItem = contactListView.ContactList.SelectedItem.As(); + + // ItemStatus contains the validation error message or string.Empty if no error exists + AssertContactItem(null, contactView, "", "", "", ""); + AssertContactItem(newItem, null, "(none)", "(none)", "(none)", "(none)"); + Assert.Equal("The Firstname field is required.", contactView.FirstnameBox.ItemStatus); + + contactView.FirstnameBox.Text = "AFirstname"; + Assert.Equal("AFirstname", newItem.FirstnameLabel.Text); + Assert.Equal("", contactView.FirstnameBox.ItemStatus); + contactView.LastnameBox.Text = "ALastname"; + contactView.EmailBox.Text = "AEmail@mail.com"; + contactView.PhoneBox.Text = "1234"; + AssertContactItem(newItem, contactView, "AFirstname", "ALastname", "AEmail@mail.com", "1234"); + + var secondItem = contactListView.ContactItems[1]; + contactListView.ContactList.Select(0); + window.DeleteCommand.Click(); + Assert.Equal(5, contactListView.ContactItems.Count); + Assert.Equal(secondItem.FirstnameLabel.Text, contactListView.ContactList.SelectedItem.As().FirstnameLabel.Text); + + window.ExitCommand.Click(); + + + Launch(resetSettings: false, resetContainer: false); + window = GetShellWindow(); + Assert.Equal(WindowVisualState.Maximized, window.WindowState); + window.RootTreeItem.ContactsNode.Click(); + contactListView = window.ContactLayoutView.ContactListView; + Assert.Equal(5, contactListView.ContactItems.Count); + Assert.Equal("AFirstname", contactListView.ContactItems[^1].FirstnameLabel.Text); + + var count = contactListView.ContactItems.Count; + for (int i = 0; i < count; i++) window.DeleteCommand.Click(); + Assert.False(window.DeleteCommand.IsEnabled); + Assert.Null(contactListView.ContactList.SelectedItem); + window.Close(); + }); + + private static void AssertContactItem(ContactListItem? contactItem, ContactView? contactView, string firstName, string lastName, string email, string phone) { - Assert.Equal((firstName, lastName, email, phone), contactItem.ToTuple()); + if (contactItem is not null) + { + Assert.Equal((firstName, lastName, email, phone), contactItem.ToTuple()); + } if (contactView is not null) { - Assert.Equal(contactItem.ToTuple(), (contactView.FirstnameBox.Text, contactView.LastnameBox.Text, contactView.EmailBox.Text, contactView.PhoneBox.Text)); + Assert.Equal((firstName, lastName, email, phone), (contactView.FirstnameBox.Text, contactView.LastnameBox.Text, contactView.EmailBox.Text, contactView.PhoneBox.Text)); } } } diff --git a/src/Samples.UITest/InformationManager.Test/Views/ShellWindow.cs b/src/Samples.UITest/InformationManager.Test/Views/ShellWindow.cs index 0fe2c916..062d4e11 100644 --- a/src/Samples.UITest/InformationManager.Test/Views/ShellWindow.cs +++ b/src/Samples.UITest/InformationManager.Test/Views/ShellWindow.cs @@ -1,5 +1,6 @@ using FlaUI.Core; using FlaUI.Core.AutomationElements; +using FlaUI.Core.Definitions; namespace UITest.InformationManager.Views; @@ -23,6 +24,13 @@ public class ShellWindow(FrameworkAutomationElementBase element) : Window(elemen public NavigationRootTreeItem RootTreeItem => this.Find("RootTreeItem").As(); public ContactLayoutView ContactLayoutView => this.Find("ContactLayoutView").As(); + + + public WindowVisualState WindowState + { + get => Patterns.Window.Pattern.WindowVisualState; + set => Patterns.Window.Pattern.SetWindowVisualState(value); + } } public class NavigationRootTreeItem(FrameworkAutomationElementBase element) : TreeItem(element) diff --git a/src/System.Waf/Samples/InformationManager/Common.Presentation/Resources/ControlResources.xaml b/src/System.Waf/Samples/InformationManager/Common.Presentation/Resources/ControlResources.xaml index f7ef3e1e..3821c242 100644 --- a/src/System.Waf/Samples/InformationManager/Common.Presentation/Resources/ControlResources.xaml +++ b/src/System.Waf/Samples/InformationManager/Common.Presentation/Resources/ControlResources.xaml @@ -30,6 +30,14 @@