Add missing null check on ItemsSource when IsGrouped is true; fixes #8269 (#8273)
authorE.Z. Hart <hartez@users.noreply.github.com>
Mon, 4 Nov 2019 21:21:59 +0000 (14:21 -0700)
committerGitHub <noreply@github.com>
Mon, 4 Nov 2019 21:21:59 +0000 (14:21 -0700)
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8269.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.Android/CollectionView/ItemsSourceFactory.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8269.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8269.cs
new file mode 100644 (file)
index 0000000..00788ce
--- /dev/null
@@ -0,0 +1,49 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+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, 8269, "[Bug] CollectionView exception when IsGrouped=true and null ItemSource", 
+               PlatformAffected.Android)]
+       public class Issue8269 : TestContentPage
+       {
+               const string Success = "Success";
+
+               protected override void Init()
+               {
+                       var layout = new StackLayout();
+
+                       var instructions = new Label { Text = "If this page has not crashed, the test is sucessful." };
+                       var success = new Label { AutomationId = Success, Text = Success };
+
+                       var cv = new CollectionView { ItemsSource = null, IsGrouped = true };
+
+                       layout.Children.Add(success);
+                       layout.Children.Add(instructions);
+                       layout.Children.Add(cv);
+                       
+                       Content = layout;
+               }
+
+#if UITEST
+               [Test]
+               public void IsGroupedWithNullItemsSourceShouldNotCrash()
+               {
+                       RunningApp.WaitForElement(Success);
+               }
+#endif
+       }
+}
index 60ac858..5822171 100644 (file)
@@ -96,6 +96,7 @@
       <DependentUpon>Issue7803.xaml</DependentUpon>
     </Compile>
     <Compile Include="$(MSBuildThisFileDirectory)Issue8167.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue8269.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)RefreshViewTests.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7338.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ScrollToGroup.cs" />
index a03fe0a..9cbd965 100644 (file)
@@ -37,7 +37,9 @@ namespace Xamarin.Forms.Platform.Android
 
                public static IGroupableItemsViewSource Create(GroupableItemsView itemsView, RecyclerView.Adapter adapter)
                {
-                       if (itemsView.IsGrouped)
+                       var source = itemsView.ItemsSource;
+
+                       if (itemsView.IsGrouped && source != null)
                        {
                                return new ObservableGroupedSource(itemsView, new AdapterNotifier(adapter));
                        }