From 77fda87ae0e6fde292e48f40482f31b4ea9fde63 Mon Sep 17 00:00:00 2001 From: Samantha Houts Date: Tue, 23 Apr 2019 17:09:20 -0700 Subject: [PATCH] Add unit tests for TabIndexExtensions (#5722) * Add check to FindNextElement * Add unit tests for TabIndexExtensions --- Xamarin.Forms.Core.UnitTests/TabIndexTests.cs | 229 ++++++++++++++++++ .../Xamarin.Forms.Core.UnitTests.csproj | 1 + Xamarin.Forms.Core/TabIndexExtensions.cs | 4 +- 3 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 Xamarin.Forms.Core.UnitTests/TabIndexTests.cs diff --git a/Xamarin.Forms.Core.UnitTests/TabIndexTests.cs b/Xamarin.Forms.Core.UnitTests/TabIndexTests.cs new file mode 100644 index 000000000..63f016315 --- /dev/null +++ b/Xamarin.Forms.Core.UnitTests/TabIndexTests.cs @@ -0,0 +1,229 @@ +using NUnit.Framework; +using System.Linq; + +namespace Xamarin.Forms.Core.UnitTests +{ + [TestFixture] + public class TabIndexTests : BaseTestFixture + { + + [Test] + public void GetTabIndexesOnParentPage_ImplicitZero() + { + var target = new StackLayout + { + Children = { + new Label { TabIndex = 1 }, + new Label { TabIndex = 0 }, + new Label { TabIndex = 3 }, + new Label { TabIndex = 2 }, + } + }; + + var page = new ContentPage { Content = target }; + + var tabIndexes = target.GetTabIndexesOnParentPage(out int _); + + //StackLayout is technically the first element with TabIndex 0. + Assert.AreEqual(target, tabIndexes[0][0]); + } + + [Test] + public void GetTabIndexesOnParentPage_ExplicitZero() + { + Label target = new Label { TabIndex = 0 }; + var stackLayout = new StackLayout + { + Children = { + new Label { TabIndex = 1 }, + target, + new Label { TabIndex = 3 }, + new Label { TabIndex = 2 }, + } + }; + + var page = new ContentPage { Content = stackLayout }; + + var tabIndexes = stackLayout.GetTabIndexesOnParentPage(out int _); + + Assert.AreEqual(target, tabIndexes[0][1]); + } + + [Test] + public void GetTabIndexesOnParentPage_NegativeTabIndex() + { + Label target = new Label { TabIndex = -1 }; + var stackLayout = new StackLayout + { + Children = { + new Label { TabIndex = 1 }, + target, + new Label { TabIndex = 3 }, + new Label { TabIndex = 2 }, + } + }; + + var page = new ContentPage { Content = stackLayout }; + + var tabIndexes = stackLayout.GetTabIndexesOnParentPage(out int _); + + Assert.AreEqual(target, tabIndexes[-1][0]); + } + + [Test] + public void FindNextElement_Forward_NextTabIndex() + { + Label target = new Label { TabIndex = 1 }; + Label nextElement = new Label { TabIndex = 2 }; + var stackLayout = new StackLayout + { + Children = { + new Label { TabIndex = 1 }, + target, + new Label { TabIndex = 3 }, + nextElement, + } + }; + + var page = new ContentPage { Content = stackLayout }; + + var tabIndexes = stackLayout.GetTabIndexesOnParentPage(out int maxAttempts); + + int _ = target.TabIndex; + + var found = target.FindNextElement(true, tabIndexes, ref _); + + Assert.AreEqual(nextElement, found); + } + + [Test] + public void FindNextElement_Forward_DeclarationOrder() + { + Label target = new Label { TabIndex = 1 }; + Label nextElement = new Label { TabIndex = 2 }; + var stackLayout = new StackLayout + { + Children = { + new Label { TabIndex = 1 }, + target, + nextElement, + new Label { TabIndex = 2 }, + } + }; + + var page = new ContentPage { Content = stackLayout }; + + var tabIndexes = stackLayout.GetTabIndexesOnParentPage(out int maxAttempts); + + int _ = target.TabIndex; + + var found = target.FindNextElement(true, tabIndexes, ref _); + + Assert.AreEqual(nextElement, found); + } + + [Test] + public void FindNextElement_Forward_TabIndex() + { + Label target = new Label { TabIndex = 1 }; + Label nextElement = new Label { TabIndex = 2 }; + var stackLayout = new StackLayout + { + Children = { + new Label { TabIndex = 1 }, + target, + nextElement, + new Label { TabIndex = 2 }, + } + }; + + var page = new ContentPage { Content = stackLayout }; + + var tabIndexes = stackLayout.GetTabIndexesOnParentPage(out int maxAttempts); + + int tabIndex = target.TabIndex; + + var found = target.FindNextElement(true, tabIndexes, ref tabIndex); + + Assert.AreEqual(2, tabIndex); + } + + [Test] + public void FindNextElement_Backward_NextTabIndex() + { + Label target = new Label { TabIndex = 2 }; + Label nextElement = new Label { TabIndex = 1 }; + var stackLayout = new StackLayout + { + Children = { + new Label { TabIndex = 3 }, + target, + new Label { TabIndex = 3 }, + nextElement, + } + }; + + var page = new ContentPage { Content = stackLayout }; + + var tabIndexes = stackLayout.GetTabIndexesOnParentPage(out int maxAttempts); + + int _ = target.TabIndex; + + var found = target.FindNextElement(false, tabIndexes, ref _); + + Assert.AreEqual(nextElement, found); + } + + [Test] + public void FindNextElement_Backward_DeclarationOrder() + { + Label target = new Label { TabIndex = 2 }; + Label nextElement = new Label { TabIndex = 1 }; + var stackLayout = new StackLayout + { + Children = { + new Label { TabIndex = 3 }, + target, + nextElement, + new Label { TabIndex = 1 }, + } + }; + + var page = new ContentPage { Content = stackLayout }; + + var tabIndexes = stackLayout.GetTabIndexesOnParentPage(out int maxAttempts); + + int _ = target.TabIndex; + + var found = target.FindNextElement(false, tabIndexes, ref _); + + Assert.AreEqual(nextElement, found); + } + + [Test] + public void FindNextElement_Backward_TabIndex() + { + Label target = new Label { TabIndex = 2 }; + Label nextElement = new Label { TabIndex = 1 }; + var stackLayout = new StackLayout + { + Children = { + new Label { TabIndex = 3 }, + target, + nextElement, + new Label { TabIndex = 2 }, + } + }; + + var page = new ContentPage { Content = stackLayout }; + + var tabIndexes = stackLayout.GetTabIndexesOnParentPage(out int maxAttempts); + + int tabIndex = target.TabIndex; + + var found = target.FindNextElement(false, tabIndexes, ref tabIndex); + + Assert.AreEqual(1, tabIndex); + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Core.UnitTests/Xamarin.Forms.Core.UnitTests.csproj b/Xamarin.Forms.Core.UnitTests/Xamarin.Forms.Core.UnitTests.csproj index 39ddee336..33158f1c4 100644 --- a/Xamarin.Forms.Core.UnitTests/Xamarin.Forms.Core.UnitTests.csproj +++ b/Xamarin.Forms.Core.UnitTests/Xamarin.Forms.Core.UnitTests.csproj @@ -86,6 +86,7 @@ + diff --git a/Xamarin.Forms.Core/TabIndexExtensions.cs b/Xamarin.Forms.Core/TabIndexExtensions.cs index ddd575bc1..4dc928e4e 100644 --- a/Xamarin.Forms.Core/TabIndexExtensions.cs +++ b/Xamarin.Forms.Core/TabIndexExtensions.cs @@ -32,7 +32,9 @@ namespace Xamarin.Forms public static VisualElement FindNextElement(this VisualElement element, bool forwardDirection, IDictionary> tabIndexes, ref int tabIndex) { - var tabGroup = tabIndexes[tabIndex]; + if (!tabIndexes.TryGetValue(tabIndex, out var tabGroup)) + return null; + if (!forwardDirection) { // search prev element in same TabIndex group