Skip to content

Commit

Permalink
[Android] CarouselView: VirtualView cannot be null here, when clearin…
Browse files Browse the repository at this point in the history
…g and adding items on second navigation - fix (#22207)

* Fix #22035

* Added UITest

* Updated test

* Updated test

* Updated test

* Updated test

* - small change to code and fix test

* Update Issue22035.cs

* Update Issue22035.cs

* - fix test

* - fix test

---------

Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
Co-authored-by: Shane Neuville <shane94@hotmail.com>
  • Loading branch information
4 people committed May 10, 2024
1 parent a1c9b93 commit 4bf8af5
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue22035Page1">
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label
Text="Navigate to the page with the CarouselView several times. If the CarouselView always loads items without exceptions, the test passes."/>
<Button
x:Name="NavigateBtn"
AutomationId="TestNavigateButton"
Text="Go to CarouselView"
SemanticProperties.Hint="Goes to page with CarouselView"
Clicked="OnNavigateClicked"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentPage>
@@ -0,0 +1,30 @@
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 22035, "[Android] CarouselView: VirtualView cannot be null here, when clearing and adding items on second navigation", PlatformAffected.Android)]
public class Issue22035 : NavigationPage
{
public Issue22035() : base(new Issue22035Page1())
{

}
}

public partial class Issue22035Page1 : ContentPage
{
Issue22035Page2 _Issue22035Page2 = new Issue22035Page2();
public Issue22035Page1()
{
InitializeComponent();
}

async void OnNavigateClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(_Issue22035Page2);
}
}
}
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue22035Page2"
xmlns:local="clr-namespace:Maui.Controls.Sample.Issues">
<Grid RowDefinitions="Auto,*">
<Button
Grid.Row="0"
AutomationId="TestLoadButton"
Text="Load Items"
Pressed="OnLoadButtonPressed" />
<CarouselView
Grid.Row="1"
ItemsSource="{Binding Images}"
MaximumHeightRequest="150">
<CarouselView.ItemTemplate>
<DataTemplate x:DataType="local:Issue22035Model">
<Grid
RowDefinitions="*, Auto">
<Image
Source="{Binding ImagePath}"
AutomationId="{Binding AutomationId}" />
<Label
Grid.Row="1"
HorizontalOptions="Center"
AutomationId="{Binding AutomationId}"
Text="{Binding Text}"/>
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</Grid>
</ContentPage>
@@ -0,0 +1,38 @@
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Issue22035Page2 : ContentPage
{
public Issue22035Page2()
{
InitializeComponent();

BindingContext = this;
}

public ObservableCollection<Issue22035Model> Images { get; set; } = new();

void OnLoadButtonPressed(object sender, EventArgs e)
{
Images.Clear();

Images.Add(new Issue22035Model { Text = "Item 1", ImagePath = "photo21314.jpg" });
Images.Add(new Issue22035Model { Text = "Item 2", ImagePath = "oasis.jpg" });
Images.Add(new Issue22035Model { Text = "Item 3", ImagePath = "photo21314.jpg" });
Images.Add(new Issue22035Model { Text = "Item 4", ImagePath = "oasis.jpg" });
}
}

public class Issue22035Model
{
public string Text { get; set; }
public string ImagePath { get; set; }
public string AutomationId => Text.Replace(" ", "", StringComparison.InvariantCultureIgnoreCase);
}
}
Expand Up @@ -101,10 +101,11 @@ public override void SetUpNewElement(CarouselView newElement)

public override void TearDownOldElement(CarouselView oldElement)
{
if (ItemsView != null)
if (ItemsView is not null)
ItemsView.Scrolled -= CarouselViewScrolled;

ClearLayoutListener();
UnsubscribeCollectionItemsSourceChanged(ItemsViewAdapter);
base.TearDownOldElement(oldElement);
}

Expand Down
35 changes: 35 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue22035.cs
@@ -0,0 +1,35 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
[Category(UITestCategories.CarouselView)]
public class Issue22035 : _IssuesUITest
{
public Issue22035(TestDevice device) : base(device)
{
}

public override string Issue => "[Android] CarouselView: VirtualView cannot be null here, when clearing and adding items on second navigation";

[Test]
public void CarouselViewVirtualViewNotNull()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.iOS, TestDevice.Mac, TestDevice.Windows },
"This test is failing, likely due to product issue. More information: https://github.com/dotnet/maui/issues/22287");

for (int i = 0; i < 2; i++)
{
App.WaitForElement("TestNavigateButton");
App.Tap("TestNavigateButton");
App.WaitForElement("TestLoadButton");
App.Tap("TestLoadButton");
App.WaitForElement("Item1");
App.Back();
}

// Without exceptions, the test has passed.
}
}
}

0 comments on commit 4bf8af5

Please sign in to comment.