From: Samantha Houts Date: Wed, 24 Apr 2019 00:09:20 +0000 (-0700) Subject: Add unit tests for TabIndexExtensions (#5722) X-Git-Tag: accepted/tizen/5.5/unified/20200421.150457~378^2^2~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=77fda87ae0e6fde292e48f40482f31b4ea9fde63;p=platform%2Fcore%2Fcsapi%2Fxsf.git Add unit tests for TabIndexExtensions (#5722) * Add check to FindNextElement * Add unit tests for TabIndexExtensions --- diff --git a/Xamarin.Forms.Core.UnitTests/TabIndexTests.cs b/Xamarin.Forms.Core.UnitTests/TabIndexTests.cs new file mode 100644 index 0000000..63f0163 --- /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 39ddee3..33158f1 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 ddd575b..4dc928e 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