Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 022cf08

Browse files
jfversluissamhouts
andauthored
Fixes 15049 (#15236)
* [UITest] Add test for 15049 * [Android] Calling _adapter.ItemCount here seems to cause ref to stick Co-authored-by: Samantha Houts <samhouts@users.noreply.github.com>
1 parent 12708e1 commit 022cf08

File tree

3 files changed

+115
-11
lines changed

3 files changed

+115
-11
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using System.Windows.Input;
6+
using Xamarin.Forms.CustomAttributes;
7+
using Xamarin.Forms.Internals;
8+
using System.Runtime.CompilerServices;
9+
using System.ComponentModel;
10+
11+
#if UITEST
12+
using Xamarin.UITest;
13+
using NUnit.Framework;
14+
#endif
15+
16+
namespace Xamarin.Forms.Controls.Issues
17+
{
18+
#if UITEST
19+
[NUnit.Framework.Category(Core.UITests.UITestCategories.Github10000)]
20+
[NUnit.Framework.Category(Core.UITests.UITestCategories.CollectionView)]
21+
[NUnit.Framework.Category(Core.UITests.UITestCategories.ManualReview)]
22+
#endif
23+
[Preserve(AllMembers = true)]
24+
[Issue(IssueTracker.Github, 15049, "[Android] CollectionView leaks GREF when items are removed", PlatformAffected.Android, issueTestNumber: 1)]
25+
public class GitHub15049 : TestContentPage
26+
{
27+
protected override void Init()
28+
{
29+
BindingContext = new GitHub15049ViewModel();
30+
var collectionView = new CollectionView();
31+
collectionView.ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical);
32+
collectionView.ItemTemplate = new DataTemplate(() =>
33+
{
34+
var label = new Label();
35+
label.SetBinding(Label.TextProperty, "Text");
36+
37+
return label;
38+
});
39+
collectionView.SetBinding(CollectionView.ItemsSourceProperty, "Items");
40+
Content = collectionView;
41+
}
42+
43+
public class GitHub15049ViewModel : INotifyPropertyChanged
44+
{
45+
public event PropertyChangedEventHandler PropertyChanged;
46+
private ObservableCollection<GitHub15049Model> _items;
47+
public ObservableCollection<GitHub15049Model> Items
48+
{
49+
get => _items;
50+
set
51+
{
52+
if (_items != value)
53+
{
54+
_items = value;
55+
RaisePropertyChanged(nameof(Items));
56+
}
57+
}
58+
}
59+
60+
public bool IsStopped { get; set; } = false;
61+
62+
public GitHub15049ViewModel()
63+
{
64+
var collection = new ObservableCollection<GitHub15049Model>();
65+
var pageSize = 10000;
66+
67+
for (var i = 0; i < pageSize; i++)
68+
{
69+
collection.Add(new GitHub15049Model
70+
{
71+
Text = "Item " + i,
72+
});
73+
}
74+
75+
Items = collection;
76+
77+
//Kick off Test
78+
Task.Run(async () =>
79+
{
80+
while (Items.Count > 0 && !IsStopped)
81+
{
82+
await Task.Yield();
83+
84+
await Device.InvokeOnMainThreadAsync(() =>
85+
{
86+
Items.RemoveAt(0);
87+
});
88+
}
89+
});
90+
}
91+
92+
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
93+
{
94+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
95+
}
96+
}
97+
98+
public class GitHub15049Model
99+
{
100+
public string Text { get; set; }
101+
}
102+
}
103+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)CollectionViewGroupTypeIssue.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)GitHub15049.cs" />
1314
<Compile Include="$(MSBuildThisFileDirectory)Issue10124.cs" />
1415
<Compile Include="$(MSBuildThisFileDirectory)Issue12603.xaml.cs">
1516
<DependentUpon>Issue12603.xaml</DependentUpon>

Xamarin.Forms.Platform.Android/CollectionView/AdapterNotifier.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public void NotifyItemInserted(IItemsViewSource source, int startIndex)
3030
{
3131
_adapter.NotifyItemInserted(startIndex);
3232

33-
var changedCount = _adapter.ItemCount - startIndex;
34-
_adapter.NotifyItemRangeChanged(startIndex, changedCount);
33+
//var changedCount = _adapter.ItemCount - startIndex;
34+
//_adapter.NotifyItemRangeChanged(startIndex, changedCount);
3535
}
3636
}
3737

@@ -41,9 +41,9 @@ public void NotifyItemMoved(IItemsViewSource source, int fromPosition, int toPos
4141
{
4242
_adapter.NotifyItemMoved(fromPosition, toPosition);
4343

44-
var minPosition = System.Math.Min(fromPosition, toPosition);
45-
var changedCount = _adapter.ItemCount - minPosition;
46-
_adapter.NotifyItemRangeChanged(minPosition, changedCount);
44+
//var minPosition = System.Math.Min(fromPosition, toPosition);
45+
//var changedCount = _adapter.ItemCount - minPosition;
46+
//_adapter.NotifyItemRangeChanged(minPosition, changedCount);
4747
}
4848
}
4949

@@ -59,8 +59,8 @@ public void NotifyItemRangeInserted(IItemsViewSource source, int startIndex, int
5959
{
6060
_adapter.NotifyItemRangeInserted(startIndex, count);
6161

62-
var changedCount = _adapter.ItemCount - startIndex;
63-
_adapter.NotifyItemRangeChanged(startIndex, changedCount);
62+
//var changedCount = _adapter.ItemCount - startIndex;
63+
//_adapter.NotifyItemRangeChanged(startIndex, changedCount);
6464
}
6565
}
6666

@@ -70,8 +70,8 @@ public void NotifyItemRangeRemoved(IItemsViewSource source, int startIndex, int
7070
{
7171
_adapter.NotifyItemRangeRemoved(startIndex, count);
7272

73-
var changedCount = _adapter.ItemCount - startIndex;
74-
_adapter.NotifyItemRangeChanged(startIndex, changedCount);
73+
//var changedCount = _adapter.ItemCount - startIndex;
74+
//_adapter.NotifyItemRangeChanged(startIndex, changedCount);
7575
}
7676
}
7777

@@ -81,8 +81,8 @@ public void NotifyItemRemoved(IItemsViewSource source, int startIndex)
8181
{
8282
_adapter.NotifyItemRemoved(startIndex);
8383

84-
var changedCount = _adapter.ItemCount - startIndex;
85-
_adapter.NotifyItemRangeChanged(startIndex, changedCount);
84+
//var changedCount = _adapter.ItemCount - startIndex;
85+
//_adapter.NotifyItemRangeChanged(startIndex, changedCount);
8686
}
8787
}
8888

0 commit comments

Comments
 (0)