Fix android to not duplicate titleview with shell (#7276)
authorShane Neuville <shneuvil@microsoft.com>
Tue, 27 Aug 2019 07:19:52 +0000 (01:19 -0600)
committerGerald Versluis <gerald.versluis@microsoft.com>
Tue, 27 Aug 2019 07:19:52 +0000 (09:19 +0200)
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/ShellTitleView.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/TestPages/TestPages.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Core.UITests.Shared/UITestCategories.cs
Xamarin.Forms.Platform.Android/Renderers/ShellToolbarTracker.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/ShellTitleView.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/ShellTitleView.cs
new file mode 100644 (file)
index 0000000..d76a34c
--- /dev/null
@@ -0,0 +1,80 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+
+#if UITEST
+using NUnit.Framework;
+using Xamarin.Forms.Core.UITests;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.None, 0, "Shell Title View Test",
+               PlatformAffected.All)]
+#if UITEST
+       [NUnit.Framework.Category(UITestCategories.Shell)]
+       [NUnit.Framework.Category(UITestCategories.TitleView)]
+#endif
+       public class ShellTitleView : TestShell
+       {
+               protected override void Init()
+               {
+                       AddTopTab(createContentPage("title 1"), "page 1");
+                       AddTopTab(createContentPage(null), "page 2");
+                       AddTopTab(createContentPage("title 3"), "page 3");
+                       AddTopTab(createContentPage("title 4"), "page 4");
+
+                       ContentPage createContentPage(string titleView)
+                       {
+                               ContentPage page = new ContentPage()
+                               {
+                                       Content = new StackLayout()
+                                       {
+                                               Children =
+                                               {
+                                                       new Label()
+                                                       {
+                                                               Text = "Click through the tabs and make sure title view changes and doesn't duplicate"
+                                                       }
+                                               }
+                                       }
+                               };
+
+                               page.ToolbarItems.Add(new ToolbarItem() { IconImageSource = "coffee.png", Order = ToolbarItemOrder.Primary, Priority = 0 });
+
+                               if (!string.IsNullOrWhiteSpace(titleView))
+                               {
+                                       Shell.SetTitleView(page,
+                                               new StackLayout()
+                                               {
+                                                       AutomationId = "TitleViewId",
+                                                       Children = { new Label() { Text = titleView } }
+                                               });
+                               }
+
+                               return page;
+                       }
+               }
+
+
+#if UITEST && (__IOS__ || __ANDROID__)
+
+               [Test]
+               public void NoDuplicateTitleViews()
+               {
+                       var titleView = RunningApp.WaitForElement("TitleViewId");
+
+                       Assert.AreEqual(1, titleView.Length);
+
+                       RunningApp.Tap("page 2");
+                       RunningApp.Tap("page 3");
+                       RunningApp.Tap("page 4");
+                       titleView = RunningApp.WaitForElement("TitleViewId");
+
+                       Assert.AreEqual(1, titleView.Length);
+               }
+#endif
+
+       }
+}
index 63a56bb..279890c 100644 (file)
@@ -586,13 +586,26 @@ namespace Xamarin.Forms.Controls
 
                public ContentPage AddTopTab(string title)
                {
-                       ContentPage page = new ContentPage();
+                       var page = new ContentPage();
+                       AddTopTab(page, title);
+                       return page;
+               }
+
+
+               public void AddTopTab(ContentPage page, string title = null)
+               {
+                       if(Items.Count == 0)
+                       {
+                               var item = AddContentPage(page);
+                               item.Items[0].Items[0].Title = title ?? page.Title;
+                               return;
+                       }
+
                        Items[0].Items[0].Items.Add(new ShellContent()
                        {
-                               Title = title,
+                               Title = title ?? page.Title,
                                Content = page
                        });
-                       return page;
                }
 
                public ContentPage AddBottomTab(string title)
@@ -642,7 +655,7 @@ namespace Xamarin.Forms.Controls
 
                public ShellItem AddContentPage(ContentPage contentPage)
                {
-                       ContentPage page = new ContentPage();
+                       ContentPage page = contentPage ?? new ContentPage();
                        ShellItem item = new ShellItem()
                        {
                                Items =
index 599c51b..afeeaf2 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Issue6738.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)GitHub6926.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue5503.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)ShellTitleView.cs" />
   </ItemGroup>
   <ItemGroup>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
   <ItemGroup>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)_TemplateMarkup.xaml">
       <SubType>Designer</SubType>
-      <Generator>MSBuild:Compile</Generator>
+      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue5801.xaml">
       <SubType>Designer</SubType>
   <ItemGroup>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue5046.xaml">
       <SubType>Designer</SubType>
-      <Generator>MSBuild:Compile</Generator>
+      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
   </ItemGroup>
   <ItemGroup>
     </EmbeddedResource>
     <EmbeddedResource Include="$(MSBuildThisFileDirectory)Github5623.xaml">
       <SubType>Designer</SubType>
-      <Generator>MSBuild:Compile</Generator>
+      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
   </ItemGroup>
 </Project>
index 5e9c782..f45e9b6 100644 (file)
@@ -52,5 +52,6 @@
                public const string TabbedPage = "TabbedPage";
                public const string CustomRenderers = "CustomRenderers";
                public const string Page = "Page";
+               public const string TitleView = "TitleView";
        }
 }
\ No newline at end of file
index 4702920..8591bbb 100644 (file)
@@ -443,7 +443,7 @@ namespace Xamarin.Forms.Platform.Android
                                        _titleViewContainer = null;
                                }
                        }
-                       else
+                       else if(_titleViewContainer == null)
                        {
                                _titleViewContainer = new ContainerView(context, titleView);
                                _titleViewContainer.MatchHeight = _titleViewContainer.MatchWidth = true;
@@ -457,6 +457,10 @@ namespace Xamarin.Forms.Platform.Android
 
                                _toolbar.AddView(_titleViewContainer);
                        }
+                       else
+                       {
+                               _titleViewContainer.View = titleView;
+                       }
                }
 
                protected virtual void UpdateToolbarItems(Toolbar toolbar, Page page)