Skip to content

Commit

Permalink
[Android] Fix crash adding items to CollectionView on navigating (dot…
Browse files Browse the repository at this point in the history
…net#13385) Fixes dotnet#12219

* Fix crash adding items to CollectionView on Android navigating

* Changes in test
  • Loading branch information
jsuarezruiz authored and TJ Lambert committed Feb 21, 2023
1 parent 1605546 commit a3a2dd2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ protected override void ConnectHandler(RecyclerView platformView)
base.ConnectHandler(platformView);
(platformView as IMauiRecyclerView<TItemsView>)?.SetUpNewElement(VirtualView);
}

protected override void DisconnectHandler(RecyclerView platformView)
{
base.DisconnectHandler(platformView);
(platformView as IMauiRecyclerView<TItemsView>)?.TearDownOldElement(VirtualView);
}

protected override RecyclerView CreatePlatformView() =>
new MauiRecyclerView<TItemsView, ItemsViewAdapter<TItemsView, IItemsViewSource>, IItemsViewSource>(Context, GetItemsLayout, CreateAdapter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ override Microsoft.Maui.Controls.ImageButton.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.SearchBar.IsEnabledCore.get -> bool
~Microsoft.Maui.Controls.WebView.UserAgent.get -> string
~Microsoft.Maui.Controls.WebView.UserAgent.set -> void
~override Microsoft.Maui.Controls.Handlers.Items.ItemsViewHandler<TItemsView>.DisconnectHandler(AndroidX.RecyclerView.Widget.RecyclerView platformView) -> void
~override Microsoft.Maui.Controls.LayoutOptions.Equals(object obj) -> bool
~override Microsoft.Maui.Controls.Region.Equals(object obj) -> bool
~override Microsoft.Maui.Controls.Shapes.Matrix.Equals(object obj) -> bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
namespace Microsoft.Maui.DeviceTests
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class CollectionViewTests
public partial class CollectionViewTests : ControlsHandlerTestBase
{
[Fact]
public async Task PushAndPopPageWithCollectionView()
{
NavigationPage rootPage = new NavigationPage(new ContentPage());
ContentPage modalPage = new ContentPage();

var collectionView = new CollectionView
{
ItemsSource = new string[]
{
"Item 1",
"Item 2",
"Item 3",
}
};

modalPage.Content = collectionView;

SetupBuilder();

await CreateHandlerAndAddToWindow<IWindowHandler>(rootPage,
async (_) =>
{
var currentPage = (rootPage as IPageContainer<Page>).CurrentPage;

await currentPage.Navigation.PushModalAsync(modalPage);
await OnLoadedAsync(modalPage);

await currentPage.Navigation.PopModalAsync();
await OnUnloadedAsync(modalPage);

// Navigate a second time
await currentPage.Navigation.PushModalAsync(modalPage);
await OnLoadedAsync(modalPage);

await currentPage.Navigation.PopModalAsync();
await OnUnloadedAsync(modalPage);
});


// Without Exceptions here, the test has passed.
Assert.Equal(0, (rootPage as IPageContainer<Page>).CurrentPage.Navigation.ModalStack.Count);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Items;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Xunit;
Expand All @@ -16,6 +17,11 @@ void SetupBuilder()
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(Toolbar), typeof(ToolbarHandler));
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationViewHandler));
handlers.AddHandler<Page, PageHandler>();
handlers.AddHandler<Window, WindowHandlerStub>();

handlers.AddHandler<CollectionView, CollectionViewHandler>();
handlers.AddHandler<VerticalStackLayout, LayoutHandler>();
handlers.AddHandler<Label, LabelHandler>();
Expand Down

0 comments on commit a3a2dd2

Please sign in to comment.