Skip to content

Commit 9dda1d6

Browse files
nivetha-nagalingamPureWeen
authored andcommitted
[Testing] Migration of Compatibility.Core platform-specific unit tests into device tests - 7 (#28409)
* Enabled the Shell and ObservableItemsSource for device test * Updated CollectionViewTests.Android.cs * Addressed the feedback * Removed three test cases from the CollectionView test * Corrected the misspelled words
1 parent 097f312 commit 9dda1d6

File tree

3 files changed

+229
-0
lines changed

3 files changed

+229
-0
lines changed

src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Android.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.Maui.Handlers;
66
using Microsoft.Maui.Platform;
77
using Xunit;
8+
using System.Collections.ObjectModel;
9+
using System.Linq;
810

911
namespace Microsoft.Maui.DeviceTests
1012
{
@@ -86,6 +88,102 @@ await CreateHandlerAndAddToWindow<IWindowHandler>(contentPage,
8688
});
8789
}
8890

91+
//src/Compatibility/Core/tests/Android/RendererTests.cs
92+
[Fact(DisplayName = "EmptySource should have a count of zero")]
93+
[Trait("Category", "CollectionView")]
94+
public void EmptySourceCountIsZero()
95+
{
96+
var emptySource = new EmptySource();
97+
var count = emptySource.Count;
98+
Assert.Equal(0, count);
99+
}
100+
101+
//src/Compatibility/Core/tests/Android/ObservableItemsSourceTests.cs#L52
102+
[Fact(DisplayName = "CollectionView with SnapPointsType set should not crash")]
103+
public async Task SnapPointsDoNotCrashOnOlderAPIs()
104+
{
105+
SetupBuilder();
106+
107+
var collectionView = new CollectionView();
108+
109+
var itemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical)
110+
{
111+
SnapPointsType = SnapPointsType.Mandatory
112+
};
113+
collectionView.ItemsLayout = itemsLayout;
114+
115+
await InvokeOnMainThreadAsync(() =>
116+
{
117+
var handler = CreateHandler<CollectionViewHandler>(collectionView);
118+
119+
var platformView = handler.PlatformView;
120+
121+
collectionView.Handler = null;
122+
});
123+
}
124+
125+
//src/Compatibility/Core/tests/Android/ObservableItemsSourceTests.cs#L52
126+
[Fact(DisplayName = "ObservableCollection modifications are reflected after UI thread processes them")]
127+
public async Task ObservableSourceItemsCountConsistent()
128+
{
129+
SetupBuilder();
130+
131+
var source = new ObservableCollection<string>();
132+
source.Add("Item 1");
133+
source.Add("Item 2");
134+
var ois = ItemsSourceFactory.Create(source, Application.Current, new MockCollectionChangedNotifier());
135+
136+
Assert.Equal(2, ois.Count);
137+
138+
source.Add("Item 3");
139+
var count = 0;
140+
await InvokeOnMainThreadAsync(() =>
141+
{
142+
count = ois.Count;
143+
Assert.Equal(3, ois.Count);
144+
});
145+
}
146+
147+
class MockCollectionChangedNotifier : ICollectionChangedNotifier
148+
{
149+
public int InsertCount;
150+
public int RemoveCount;
151+
152+
public void NotifyDataSetChanged()
153+
{
154+
}
155+
156+
public void NotifyItemChanged(IItemsViewSource source, int startIndex)
157+
{
158+
}
159+
160+
public void NotifyItemInserted(IItemsViewSource source, int startIndex)
161+
{
162+
InsertCount += 1;
163+
}
164+
165+
public void NotifyItemMoved(IItemsViewSource source, int fromPosition, int toPosition)
166+
{
167+
}
168+
169+
public void NotifyItemRangeChanged(IItemsViewSource source, int start, int end)
170+
{
171+
}
172+
173+
public void NotifyItemRangeInserted(IItemsViewSource source, int startIndex, int count)
174+
{
175+
}
176+
177+
public void NotifyItemRangeRemoved(IItemsViewSource source, int startIndex, int count)
178+
{
179+
}
180+
181+
public void NotifyItemRemoved(IItemsViewSource source, int startIndex)
182+
{
183+
RemoveCount += 1;
184+
}
185+
}
186+
89187
Rect GetCollectionViewCellBounds(IView cellContent)
90188
{
91189
if (!cellContent.ToPlatform().IsLoaded())

src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.iOS.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using UIKit;
1111
using Xunit;
1212
using Xunit.Sdk;
13+
using Foundation;
14+
using Microsoft.Maui.Handlers;
1315

1416
namespace Microsoft.Maui.DeviceTests
1517
{
@@ -125,6 +127,73 @@ await InvokeOnMainThreadAsync(() =>
125127
#endif
126128
}
127129

130+
//src/Compatibility/Core/tests/iOS/ObservableItemsSourceTests.cs
131+
[Fact(DisplayName = "IndexPath Range Generation Is Correct")]
132+
public void GenerateIndexPathRange()
133+
{
134+
SetupBuilder();
135+
136+
var result = IndexPathHelpers.GenerateIndexPathRange(0, 0, 5);
137+
138+
Assert.Equal(5, result.Length);
139+
140+
Assert.Equal(0, result[0].Section);
141+
Assert.Equal(0, (int)result[0].Item);
142+
143+
Assert.Equal(0, result[4].Section);
144+
Assert.Equal(4, (int)result[4].Item);
145+
}
146+
147+
//src/Compatibility/Core/tests/iOS/ObservableItemsSourceTests.cs
148+
[Fact(DisplayName = "IndexPath Range Generation For Loops Is Correct")]
149+
public void GenerateIndexPathRangeForLoop()
150+
{
151+
SetupBuilder();
152+
153+
var result = IndexPathHelpers.GenerateLoopedIndexPathRange(0, 15, 3, 2, 3);
154+
155+
Assert.Equal(9, result.Length);
156+
157+
for (int i = 0; i < result.Length; i++)
158+
{
159+
Assert.Equal(0, result[i].Section);
160+
}
161+
162+
Assert.Equal(2, (int)result[0].Item);
163+
Assert.Equal(3, (int)result[1].Item);
164+
Assert.Equal(4, (int)result[2].Item);
165+
166+
Assert.Equal(7, (int)result[3].Item);
167+
Assert.Equal(8, (int)result[4].Item);
168+
Assert.Equal(9, (int)result[5].Item);
169+
170+
Assert.Equal(12, (int)result[6].Item);
171+
Assert.Equal(13, (int)result[7].Item);
172+
Assert.Equal(14, (int)result[8].Item);
173+
}
174+
175+
//src/Compatibility/Core/tests/iOS/ObservableItemsSourceTests.cs
176+
[Fact(DisplayName = "IndexPath Validity Check Is Correct")]
177+
public void IndexPathValidTest()
178+
{
179+
var list = new List<string>
180+
{
181+
"one",
182+
"two",
183+
"three"
184+
};
185+
186+
var source = new ListSource((IEnumerable<object>)list);
187+
188+
var valid = NSIndexPath.FromItemSection(2, 0);
189+
var invalidItem = NSIndexPath.FromItemSection(7, 0);
190+
var invalidSection = NSIndexPath.FromItemSection(1, 9);
191+
192+
Assert.True(source.IsIndexPathValid(valid));
193+
Assert.False(source.IsIndexPathValid(invalidItem));
194+
Assert.False(source.IsIndexPathValid(invalidSection));
195+
}
196+
128197
/// <summary>
129198
/// Simulates what a developer might do with a Page/View
130199
/// </summary>

src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Android.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,68 @@ await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
454454
});
455455
}
456456

457+
//src/Compatibility/Core/tests/Android/ShellTests.cs
458+
[Fact(DisplayName = "Flyout Header Changes When Updated")]
459+
public async Task FlyoutHeaderReactsToChanges()
460+
{
461+
SetupBuilder();
462+
463+
var initialHeader = new Label() { Text = "Hello" };
464+
var newHeader = new Label() { Text = "Hello Part 2" };
465+
466+
var shell = await CreateShellAsync(shell =>
467+
{
468+
shell.CurrentItem = new FlyoutItem() { Items = { new ContentPage() }, Title = "Flyout Item" };
469+
shell.FlyoutHeader = initialHeader;
470+
});
471+
472+
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
473+
{
474+
var initialHeaderPlatformView = initialHeader.ToPlatform();
475+
Assert.NotNull(initialHeaderPlatformView);
476+
Assert.NotNull(initialHeader.Handler);
477+
478+
shell.FlyoutHeader = newHeader;
479+
480+
var newHeaderPlatformView = newHeader.ToPlatform();
481+
Assert.NotNull(newHeaderPlatformView);
482+
Assert.NotNull(newHeader.Handler);
483+
484+
Assert.Null(initialHeader.Handler);
485+
486+
await OpenFlyout(handler);
487+
488+
var appBar = newHeaderPlatformView.GetParentOfType<AppBarLayout>();
489+
Assert.NotNull(appBar);
490+
});
491+
}
492+
493+
//src/Compatibility/Core/tests/Android/ShellTests.cs
494+
[Fact(DisplayName = "Ensure Default Colors are White for BottomNavigationView")]
495+
public async Task ShellTabColorsDefaultToWhite()
496+
{
497+
SetupBuilder();
498+
499+
var shell = await CreateShellAsync(shell =>
500+
{
501+
shell.Items.Add(new Tab() { Items = { new ContentPage() }, Title = "Tab 1" });
502+
});
503+
504+
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, (handler) =>
505+
{
506+
var bottomNavigationView = GetDrawerLayout(handler).GetFirstChildOfType<BottomNavigationView>();
507+
Assert.NotNull(bottomNavigationView);
508+
509+
var background = bottomNavigationView.Background;
510+
Assert.NotNull(background);
511+
512+
if (background is ColorChangeRevealDrawable changeRevealDrawable)
513+
{
514+
Assert.Equal(Android.Graphics.Color.White, changeRevealDrawable.EndColor);
515+
}
516+
});
517+
}
518+
457519
protected AView GetFlyoutPlatformView(ShellRenderer shellRenderer)
458520
{
459521
var drawerLayout = GetDrawerLayout(shellRenderer);

0 commit comments

Comments
 (0)