Skip to content

Commit

Permalink
Verify sections exist before calling NumberOfItemsInSection; fixes xa…
Browse files Browse the repository at this point in the history
  • Loading branch information
hartez authored and samhouts committed Sep 3, 2019
1 parent ea78ad5 commit 6568351
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -1371,4 +1372,4 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
</Project>
6 changes: 6 additions & 0 deletions Xamarin.Forms.Platform.iOS/CollectionView/GridViewLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ bool NeedsPartialColumnAdjustment(int section = 0)
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
Expand Down

0 comments on commit 6568351

Please sign in to comment.