Verify sections exist before calling NumberOfItemsInSection; fixes #7338 (#7354)
authorE.Z. Hart <hartez@users.noreply.github.com>
Tue, 3 Sep 2019 17:03:16 +0000 (11:03 -0600)
committerSamantha Houts <samhouts@users.noreply.github.com>
Tue, 3 Sep 2019 17:03:16 +0000 (10:03 -0700)
fixes #7338

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7338.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7338.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue7338.cs
new file mode 100644 (file)
index 0000000..1f422d8
--- /dev/null
@@ -0,0 +1,71 @@
+using System;
+using System.Collections.Generic;
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+#if UITEST
+using Xamarin.Forms.Core.UITests;
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+
+namespace Xamarin.Forms.Controls.Issues
+{
+#if UITEST
+       [Category(UITestCategories.CollectionView)]
+#endif
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Github, 7338, "[Bug] CollectionView crash if source is empty in XF 4.2.0.709249",
+               PlatformAffected.iOS)]
+       class Issue7338 : TestNavigationPage
+       {
+               const string Success = "success";
+
+               protected override void Init()
+               {
+                       FlagTestHelpers.SetCollectionViewTestFlag();
+                       PushAsync(CreateRoot());
+               }
+
+               Page CreateRoot()
+               {
+                       var page = new ContentPage() { Title = "Issue7338" };
+
+                       var instructions = new Label { AutomationId = Success, Text = "If you can see this label, the test has passed." };
+
+                       var layout = new StackLayout();
+
+                       var cv = new CollectionView
+                       {
+                               ItemsLayout = new GridItemsLayout(orientation: ItemsLayoutOrientation.Horizontal),
+                               ItemTemplate = new DataTemplate(() =>
+                               {
+                                       return Template();
+                               })
+                       };
+
+                       layout.Children.Add(instructions);
+                       layout.Children.Add(cv);
+
+                       page.Content = layout;
+
+                       return page;
+               }
+
+               View Template()
+               {
+                       var label1 = new Label { Text = "Text", HeightRequest = 100 };
+                       return label1;
+               }
+
+#if UITEST
+               [Test]
+               public void EmptyHorizontalCollectionShouldNotCrash()
+               {
+                       // If the instructions are visible at all, then this has succeeded
+                       RunningApp.WaitForElement(Success);
+               }
+#endif
+       }
+}
index f12c343..a466d0e 100644 (file)
@@ -32,6 +32,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)Issue7049.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7061.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7111.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue7338.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ScrollToGroup.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)NestedCollectionViews.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutBehavior.cs" />
       <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
   </ItemGroup>
-</Project>
+</Project>
\ No newline at end of file
index f8e52e1..b1f07a9 100644 (file)
@@ -138,6 +138,12 @@ namespace Xamarin.Forms.Platform.iOS
                                return false; 
                        }
 
+                       if (CollectionView.NumberOfSections() == 0)
+                       {
+                               // And it only happens if there are items
+                               return false;
+                       }
+
                        if (EstimatedItemSize.IsEmpty)
                        {
                                // The bug only occurs when using Autolayout; with a set ItemSize, we don't have to worry about it