From 0b7623b5e6dea1a4ab0257b94b6ce6c67864f34b Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Mon, 30 Jan 2017 12:29:27 +0000 Subject: [MacOS] Fixes to ToolbarItems (#728) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [MacOS] Support toolbar item property changes * [UITest,MacOS] Support query by button hardcoded * [UITests] Add and fix more toolbar tests --- .../ControlGalleryPages/ToolbarItems.cs | 34 +++--- .../Tests/ToolbarItemTests.cs | 129 ++++++++++++++------- Xamarin.Forms.Core.macOS.UITests/MacOSApp.cs | 26 ++++- .../NativeToolbarTracker.cs | 41 ++++++- 4 files changed, 166 insertions(+), 64 deletions(-) diff --git a/Xamarin.Forms.Controls/ControlGalleryPages/ToolbarItems.cs b/Xamarin.Forms.Controls/ControlGalleryPages/ToolbarItems.cs index 3e98fc88..b3b3521e 100644 --- a/Xamarin.Forms.Controls/ControlGalleryPages/ToolbarItems.cs +++ b/Xamarin.Forms.Controls/ControlGalleryPages/ToolbarItems.cs @@ -7,43 +7,49 @@ namespace Xamarin.Forms.Controls public class ToolbarItems : ContentPage { bool _isEnable = false; - public ToolbarItems () + public ToolbarItems() { - var label = new Label { Text = "Hello ContentPage", AutomationId ="label_id" }; - var tb1 = new ToolbarItem ("tb1", "menuIcon.png", () => { + var label = new Label { Text = "Hello ContentPage", AutomationId = "label_id" }; + + var command = new Command((obj) => + { + label.Text = "tb4"; + }, (obj) => _isEnable); + var tb1 = new ToolbarItem("tb1", "menuIcon.png", () => + { label.Text = "tb1"; }, ToolbarItemOrder.Primary); tb1.IsEnabled = _isEnable; tb1.AutomationId = "toolbaritem_primary"; - tb1.IsEnabled = _isEnable; - var tb2 = new ToolbarItem ("tb2", null, () => { + var tb2 = new ToolbarItem("tb2", null, () => + { label.Text = "tb2"; }, ToolbarItemOrder.Primary); tb2.AutomationId = "toolbaritem_primary2"; - var tb3 = new ToolbarItem ("tb3", "bank.png", () => { + var tb3 = new ToolbarItem("tb3", "bank.png", () => + { label.Text = "tb3"; + _isEnable = !_isEnable; + command.ChangeCanExecute(); }, ToolbarItemOrder.Secondary); tb3.AutomationId = "toolbaritem_secondary"; - var tb4 = new ToolbarItem (); + var tb4 = new ToolbarItem(); tb4.Text = "tb4"; tb4.Order = ToolbarItemOrder.Secondary; - tb4.Command = new Command( (obj)=> { - _isEnable = true; - label.Text = "tb4"; - (tb4.Command as Command).ChangeCanExecute(); - },(obj) => _isEnable); + tb4.Command = command; tb4.AutomationId = "toolbaritem_secondary2"; - + ToolbarItems.Add(tb1); ToolbarItems.Add(tb2); ToolbarItems.Add(tb3); ToolbarItems.Add(tb4); - Content = new StackLayout { + Content = new StackLayout + { Children = { label } diff --git a/Xamarin.Forms.Core.iOS.UITests/Tests/ToolbarItemTests.cs b/Xamarin.Forms.Core.iOS.UITests/Tests/ToolbarItemTests.cs index 1c8a1565..0231ca57 100644 --- a/Xamarin.Forms.Core.iOS.UITests/Tests/ToolbarItemTests.cs +++ b/Xamarin.Forms.Core.iOS.UITests/Tests/ToolbarItemTests.cs @@ -1,14 +1,10 @@ -using System; -using NUnit.Framework; +using NUnit.Framework; using Xamarin.Forms.CustomAttributes; -using Xamarin.UITest.Android; -using Xamarin.UITest.iOS; + +using Xamarin.UITest.Queries; namespace Xamarin.Forms.Core.UITests { -#if __MACOS__ - [Ignore("Not tested in MacOS yet")] -#endif [TestFixture] [Category(UITestCategories.ToolbarItem)] internal class ToolbarItemTests : BaseTestFixture @@ -18,28 +14,29 @@ namespace Xamarin.Forms.Core.UITests #if __ANDROID__ static bool isSecondaryMenuOpen = false; #endif - static void ShouldShowMenu () + static void ShouldShowMenu() { #if __ANDROID__ isSecondaryMenuOpen = true; //show secondary menu - App.Tap (c => c.Class ("android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton")); + App.Tap(c => c.Class("android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton")); #endif } - static void ShouldHideMenu () + static void ShouldHideMenu() { #if __ANDROID__ - if (isSecondaryMenuOpen) { + if (isSecondaryMenuOpen) + { isSecondaryMenuOpen = false; - App.Back (); + App.Back(); } #endif } - protected override void NavigateToGallery () + protected override void NavigateToGallery() { - App.NavigateToGallery (GalleryQueries.ToolbarItemGallery); + App.NavigateToGallery(GalleryQueries.ToolbarItemGallery); #if __IOS__ btn1Id = "menuIcon"; btn4Id = "tb4"; @@ -47,59 +44,105 @@ namespace Xamarin.Forms.Core.UITests } [Test] - public void ToolbarButtonsClick () + public void ToolbarButtonsClick() { - ShouldHideMenu (); - App.Tap (c => c.Marked (btn1Id)); + ShouldHideMenu(); +#if __MACOS__ + App.Tap(c => c.Button().Index(4)); +#else + App.Tap(c => c.Marked(btn1Id)); +#endif + var textLabel = App.Query((arg) => arg.Marked("label_id"))[0]; + Assert.False(textLabel.Text == "tb1"); + Assert.True(textLabel.Text == "Hello ContentPage"); } [Test] - public void ToolbarButtonsCommand () + public void ToolbarButtonsCommand() { - ShouldShowMenu (); + ShouldShowMenu(); #if __ANDROID__ //App.Query (c => c.Marked (btn4Id))[0]; #else - App.Tap (c => c.Marked (btn4Id)); + App.Tap(c => c.Marked(btn4Id)); + var textLabel = App.Query((arg) => arg.Marked("label_id"))[0]; + Assert.False(textLabel.Text == "tb4"); +#if __MACOS__ + App.Tap(c => c.Button().Index(6)); +#else + App.Tap(c => c.Marked("tb3")); +#endif + App.Tap(c => c.Marked(btn4Id)); + textLabel = App.Query((arg) => arg.Marked("label_id"))[0]; + Assert.IsTrue(textLabel.Text == "tb4"); +#if __MACOS__ + App.Tap(c => c.Button().Index(6)); +#else + App.Tap(c => c.Marked("tb3")); +#endif #endif } [Test] - public void ToolbarButtonsDisable () + public void ToolbarButtonsDisable() { - ShouldHideMenu (); - var btn1 = App.Query (c => c.Marked (btn1Id)) [0]; - ShouldShowMenu (); - //var btn2 = App.Query (c => c.Marked (btn4Id)) [0]; - Assert.False (btn1.Enabled, "Toolbar Item should be disable"); + ShouldHideMenu(); +#if __MACOS__ + var result = App.Query(c => c.Button()); + var btn1 = result[4]; + var btn2 = App.Query(c => c.Marked(btn4Id))[0]; + Assert.False(btn2.Enabled, "Toolbar Item should be disable"); +#else + var btn1 = App.Query(c => c.Marked(btn1Id))[0]; + ShouldShowMenu(); + //var btn2 = App.Query (c => c.Marked (btn4Id)) [0]; //TODO: how to check Enable for the textview //Assert.False (btn2.Enabled, "Toolbar Item should be disable"); +#endif + Assert.False(btn1.Enabled, "Toolbar Item should be disable"); } [Test] - public void ToolbarButtonsExist () + public void ToolbarButtonsExist() { - ShouldHideMenu (); - var existsPrimary = App.Query (c => c.Marked (btn1Id)).Length; - var existsPrimary2 = App.Query (c => c.Marked ("tb2")).Length; - ShouldShowMenu (); - var existsSecondary = App.Query (c => c.Marked ("tb3")).Length; - var existsSecondary2 = App.Query (c => c.Marked (btn4Id)).Length; - Assert.True (existsPrimary > 0, "Toolbar Item 1 no name, not found"); - Assert.True (existsPrimary2 > 0, "Toolbar Item 2, not found"); - Assert.True (existsSecondary > 0, "Toolbar Item 1 no name, not found"); - Assert.True (existsSecondary2 > 0, "Toolbar Item 1, not found"); + ShouldHideMenu(); +#if __MACOS__ + var existsPrimary = App.Query(c => c.Button())[4]; + Assert.True(existsPrimary != null, "Toolbar Item 1 no name, not found"); +#else + var existsPrimary = App.Query(c => c.Marked(btn1Id)).Length; + Assert.True(existsPrimary > 0, "Toolbar Item 1 no name, not found"); +#endif + var existsPrimary2 = App.Query(c => c.Marked("tb2")).Length; + Assert.True(existsPrimary2 > 0, "Toolbar Item 2, not found"); + ShouldShowMenu(); + +#if __MACOS__ + var existsSecondary = App.Query(c => c.Button())[7]; + Assert.True(existsSecondary != null, "Toolbar Item 3 no name, not found"); +#else + var existsSecondary = App.Query(c => c.Marked("tb3")).Length; + Assert.True(existsSecondary > 0, "Toolbar Item 1 no name, not found"); +#endif + var existsSecondary2 = App.Query(c => c.Marked(btn4Id)).Length; + Assert.True(existsSecondary2 > 0, "Toolbar Item 4, not found"); } [Test] - public void ToolbarButtonsOrder () + public void ToolbarButtonsOrder() { - ShouldHideMenu (); - var btn1 = App.Query (c => c.Marked (btn1Id)) [0]; - ShouldShowMenu (); - var btn2 = App.Query (c => c.Marked ("tb4")) [0]; + ShouldHideMenu(); +#if __MACOS__ + var btn1 = App.Query(c => c.Button())[4]; +#else + var btn1 = App.Query(c => c.Marked(btn1Id))[0]; +#endif + ShouldShowMenu(); + var btn2 = App.Query(c => c.Marked("tb4"))[0]; #if __IOS__ - Assert.True (btn1.Rect.CenterY < btn2.Rect.CenterY); + Assert.True(btn1.Rect.CenterY < btn2.Rect.CenterY); +#elif __MACOS__ + Assert.True(btn1.Rect.CenterX < btn2.Rect.CenterX); #endif } diff --git a/Xamarin.Forms.Core.macOS.UITests/MacOSApp.cs b/Xamarin.Forms.Core.macOS.UITests/MacOSApp.cs index 08c3118d..bb0e9a6b 100644 --- a/Xamarin.Forms.Core.macOS.UITests/MacOSApp.cs +++ b/Xamarin.Forms.Core.macOS.UITests/MacOSApp.cs @@ -290,6 +290,15 @@ namespace Xamarin.Forms.Core.macOS.UITests var result = allREsults[0].Children[0].Children[0].Children[1]; results.Add(result.ToUITestResult()); } + else if (queryStr.Contains("button")) + { + var allREsults = _cocoaApp.QueryByType("button"); + foreach (var item in allREsults) + { + results.Add(item.ToUITestResult()); + } + + } return results.ToArray(); } @@ -615,6 +624,7 @@ namespace Xamarin.Forms.Core.macOS.UITests indexMarked = 0; markedWord = string.Empty; var isSuccess = false; + var queryStr = query(new AppQuery(QueryPlatform.iOS)).ToString(); var isIndex = System.Text.RegularExpressions.Regex.IsMatch(queryStr, @"\bindex\b"); if (isIndex) @@ -641,14 +651,26 @@ namespace Xamarin.Forms.Core.macOS.UITests markedWord = markedWords[1].Replace("'", "").Trim(); isSuccess = true; } + if (!isSuccess) + { + if (queryStr == "button") + { + isSuccess = true; + } + } return isSuccess; } void Tap(string marked, int index) { var safeIndex = Math.Max(index, 0); - var queryById = _cocoaApp.QueryById(marked.Trim())[safeIndex]; - _cocoaApp.Click(queryById.Rect.CenterX, queryById.Rect.CenterY); + var all = _cocoaApp.Query(); + var centerPoint = new PointF(); + if (!string.IsNullOrEmpty(marked)) + centerPoint = _cocoaApp.QueryById(marked.Trim())[safeIndex].Rect.Center; + else + centerPoint = _cocoaApp.QueryByType("Button")[safeIndex].Rect.Center; + _cocoaApp.Click(centerPoint.X, centerPoint.Y); Thread.Sleep(1000); } diff --git a/Xamarin.Forms.Platform.MacOS/NativeToolbarTracker.cs b/Xamarin.Forms.Platform.MacOS/NativeToolbarTracker.cs index fc8bec04..fc24b770 100644 --- a/Xamarin.Forms.Platform.MacOS/NativeToolbarTracker.cs +++ b/Xamarin.Forms.Platform.MacOS/NativeToolbarTracker.cs @@ -15,6 +15,7 @@ namespace Xamarin.Forms.Platform.MacOS { public NSToolbarItem ToolbarItem; public NSButton Button; + public ToolbarItem Element; } public NativeToolbarGroup(NSToolbarItemGroup itemGroup) @@ -370,8 +371,8 @@ namespace Xamarin.Forms.Platform.MacOS UpdateGroup(_tabbedGroup, items, ToolbarItemWidth, ToolbarItemSpacing); } - static void UpdateGroup(NativeToolbarGroup group, IList toolbarItems, double itemWidth, - double itemSpacing) + void UpdateGroup(NativeToolbarGroup group, IList toolbarItems, double itemWidth, + double itemSpacing) { int count = toolbarItems.Count; group.Items.Clear(); @@ -390,6 +391,7 @@ namespace Xamarin.Forms.Platform.MacOS var button = new NSButton(); button.Title = element.Text ?? ""; + button.SizeToFit(); var buttonWidth = itemWidth; if (button.FittingSize.Width > itemWidth) @@ -406,13 +408,17 @@ namespace Xamarin.Forms.Platform.MacOS button.Image = new NSImage(element.Icon); button.SizeToFit(); - view.AddSubview(button); - item.Label = item.PaletteLabel = item.ToolTip = button.ToolTip = element.Text ?? ""; + button.Enabled = item.Enabled = element.IsEnabled; + element.PropertyChanged -= ToolBarItemPropertyChanged; + element.PropertyChanged += ToolBarItemPropertyChanged; + + view.AddSubview(button); + //item.Label = item.PaletteLabel = item.ToolTip = element.Text ?? ""; subItems[i] = item; - group.Items.Add(new NativeToolbarGroup.Item { ToolbarItem = item, Button = button }); + group.Items.Add(new NativeToolbarGroup.Item { ToolbarItem = item, Button = button, Element = element }); } view.Frame = new CGRect(0, 0, totalWidth + (itemSpacing * (count - 1)), ToolbarItemHeight); @@ -425,5 +431,30 @@ namespace Xamarin.Forms.Platform.MacOS group.Group.View = new NSView(); } } + + void ToolBarItemPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) + { + var nativeToolbarItem = _toolbarGroup.Items.FirstOrDefault((NativeToolbarGroup.Item arg1) => arg1.Element == sender); + if (nativeToolbarItem != null) + { + if (e.PropertyName.Equals(VisualElement.IsEnabledProperty.PropertyName)) + { + nativeToolbarItem.Button.Enabled = nativeToolbarItem.ToolbarItem.Enabled = nativeToolbarItem.Element.IsEnabled; + } + + if (e.PropertyName.Equals(ToolbarItem.TextProperty.PropertyName)) + { + nativeToolbarItem.Button.Title = nativeToolbarItem.ToolbarItem.Label = nativeToolbarItem.Element.Text; + } + } + } + + class ToolBarItemNSButton : NSView + { + public ToolBarItemNSButton(string automationID) + { + AccessibilityIdentifier = automationID; + } + } } } \ No newline at end of file -- cgit v1.2.3