Skip to content

Commit a593167

Browse files
TamilarasanSF4853PureWeen
authored andcommitted
[Testing] Migration of Compatibility.Core platform-specific unit tests into device tests - 8 (#28496)
* code changes added * updated code
1 parent 4720b03 commit a593167

File tree

8 files changed

+246
-8
lines changed

8 files changed

+246
-8
lines changed

src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Windows.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#nullable enable
2+
using Xunit;
3+
using System;
4+
using System.ComponentModel;
25
using System.Threading.Tasks;
6+
using Microsoft.Maui.Controls;
7+
using Microsoft.Maui.Dispatching;
38
using Microsoft.Maui.Handlers;
49
using Microsoft.UI.Xaml.Controls;
10+
using WFlowDirection = Microsoft.UI.Xaml.FlowDirection;
11+
using WTextAlignment = Microsoft.UI.Xaml.TextAlignment;
512

613
namespace Microsoft.Maui.DeviceTests
714
{
@@ -41,5 +48,35 @@ Task<bool> GetPlatformIsVisible(EditorHandler editorHandler)
4148
return nativeView.Visibility == Microsoft.UI.Xaml.Visibility.Visible;
4249
});
4350
}
51+
//src/Compatibility/Core/tests/WinUI/FlowDirectionTests.cs
52+
[Theory]
53+
[InlineData(true, FlowDirection.RightToLeft, WTextAlignment.Left, WFlowDirection.RightToLeft)]
54+
[InlineData(true, FlowDirection.LeftToRight, WTextAlignment.Left, WFlowDirection.LeftToRight)]
55+
[InlineData(false, FlowDirection.LeftToRight, WTextAlignment.Left, WFlowDirection.LeftToRight)]
56+
[Description("The Editor's text alignment and flow direction should match the expected values when FlowDirection is applied explicitly or implicitly.")]
57+
public async Task EditorAlignmentMatchesFlowDirection(bool isExplicit, FlowDirection flowDirection, WTextAlignment expectedAlignment, WFlowDirection expectedFlowDirection)
58+
{
59+
var editor = new Editor { Text = " تسجيل الدخول" };
60+
var contentPage = new ContentPage { Title = "Flow Direction", Content = editor };
61+
62+
if (isExplicit)
63+
{
64+
editor.FlowDirection = flowDirection;
65+
}
66+
else
67+
{
68+
contentPage.FlowDirection = flowDirection;
69+
}
70+
71+
var handler = await CreateHandlerAsync<EditorHandler>(editor);
72+
var (nativeAlignment, nativeFlowDirection) = await contentPage.Dispatcher.DispatchAsync(() =>
73+
{
74+
var textField = GetPlatformControl(handler);
75+
return (textField.TextAlignment, textField.FlowDirection);
76+
});
77+
78+
Assert.Equal(expectedAlignment, nativeAlignment);
79+
Assert.Equal(expectedFlowDirection, nativeFlowDirection);
80+
}
4481
}
4582
}

src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.iOS.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using System.Linq;
1+
using UIKit;
2+
using Xunit;
3+
using System.Linq;
24
using System.Threading.Tasks;
35
using Microsoft.Maui.Controls;
6+
using Microsoft.Maui.Dispatching;
47
using Microsoft.Maui.Handlers;
58
using Microsoft.Maui.Hosting;
69
using Microsoft.Maui.Platform;
7-
using Xunit;
810
using static Microsoft.Maui.DeviceTests.AssertHelpers;
11+
using System.ComponentModel;
912

1013
namespace Microsoft.Maui.DeviceTests
1114
{
@@ -48,7 +51,7 @@ Task<float> GetPlatformOpacity(EditorHandler editorHandler)
4851
return InvokeOnMainThreadAsync(() =>
4952
{
5053
var nativeView = GetPlatformControl(editorHandler);
51-
return (float)nativeView.Alpha;
54+
return (float)nativeView.Alpha;
5255
});
5356
}
5457

@@ -103,5 +106,35 @@ await CreateHandlerAndAddToWindow(contentPage, async () =>
103106
});
104107
}
105108
}
109+
110+
//src/Compatibility/Core/tests/iOS/FlowDirectionTests.cs
111+
[Theory]
112+
[InlineData(true, FlowDirection.LeftToRight, UITextAlignment.Left)]
113+
[InlineData(true, FlowDirection.RightToLeft, UITextAlignment.Right)]
114+
[InlineData(false, FlowDirection.LeftToRight, UITextAlignment.Left)]
115+
[Description("The Editor's text alignment should match the expected alignment when FlowDirection is applied explicitly or implicitly")]
116+
public async Task EditorAlignmentMatchesFlowDirection(bool isExplicit, FlowDirection flowDirection, UITextAlignment expectedAlignment)
117+
{
118+
var editor = new Editor { Text = "Checking flow direction" };
119+
var contentPage = new ContentPage { Title = "Flow Direction", Content = editor };
120+
121+
if (isExplicit)
122+
{
123+
editor.FlowDirection = flowDirection;
124+
}
125+
else
126+
{
127+
contentPage.FlowDirection = flowDirection;
128+
}
129+
130+
var handler = await CreateHandlerAsync<EditorHandler>(editor);
131+
var nativeAlignment = await contentPage.Dispatcher.DispatchAsync(() =>
132+
{
133+
var textField = GetPlatformControl(handler);
134+
return textField.TextAlignment;
135+
});
136+
137+
Assert.Equal(expectedAlignment, nativeAlignment);
138+
}
106139
}
107140
}

src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Windows.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#nullable enable
2+
using Xunit;
3+
using System.ComponentModel;
24
using System.Threading.Tasks;
5+
using Microsoft.Maui.Controls;
6+
using Microsoft.Maui.Dispatching;
37
using Microsoft.Maui.Handlers;
48
using Microsoft.UI.Xaml.Controls;
9+
using WTextAlignment = Microsoft.UI.Xaml.TextAlignment;
510

611
namespace Microsoft.Maui.DeviceTests
712
{
@@ -41,5 +46,35 @@ Task<bool> GetPlatformIsVisible(EntryHandler entryHandler)
4146
return nativeView.Visibility == Microsoft.UI.Xaml.Visibility.Visible;
4247
});
4348
}
49+
//src/Compatibility/Core/tests/WinUI/FlowDirectionTests.cs
50+
[Theory]
51+
[InlineData(true, FlowDirection.LeftToRight, WTextAlignment.Left)]
52+
[InlineData(true, FlowDirection.RightToLeft, WTextAlignment.Left)]
53+
[InlineData(false, FlowDirection.LeftToRight, WTextAlignment.Left)]
54+
[InlineData(false, FlowDirection.RightToLeft, WTextAlignment.Left)]
55+
[Description("The Entry's text alignment should match the expected alignment when FlowDirection is applied explicitly or implicitly.")]
56+
public async Task EntryAlignmentMatchesFlowDirection(bool isExplicit, FlowDirection flowDirection, WTextAlignment expectedAlignment)
57+
{
58+
var entry = new Entry { Text = "Checking flow direction", HorizontalTextAlignment = TextAlignment.Start };
59+
var contentPage = new ContentPage { Title = "Flow Direction", Content = entry };
60+
61+
if (isExplicit)
62+
{
63+
entry.FlowDirection = flowDirection;
64+
}
65+
else
66+
{
67+
contentPage.FlowDirection = flowDirection;
68+
}
69+
70+
var handler = await CreateHandlerAsync<EntryHandler>(entry);
71+
var nativeAlignment = await contentPage.Dispatcher.DispatchAsync(() =>
72+
{
73+
var textField = GetPlatformControl(handler);
74+
return textField.TextAlignment;
75+
});
76+
77+
Assert.Equal(expectedAlignment, nativeAlignment);
78+
}
4479
}
4580
}

src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.iOS.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
using System.Linq;
1+
using UIKit;
2+
using Xunit;
3+
using System.ComponentModel;
4+
using System.Linq;
25
using System.Threading.Tasks;
36
using Microsoft.Maui.Controls;
47
using Microsoft.Maui.Controls.Handlers.Compatibility;
58
using Microsoft.Maui.DeviceTests.TestCases;
9+
using Microsoft.Maui.Dispatching;
610
using Microsoft.Maui.Handlers;
711
using Microsoft.Maui.Hosting;
812
using Microsoft.Maui.Platform;
9-
using UIKit;
10-
using Xunit;
1113
using static Microsoft.Maui.DeviceTests.AssertHelpers;
1214

1315
namespace Microsoft.Maui.DeviceTests
@@ -44,13 +46,13 @@ static int GetPlatformSelectionLength(EntryHandler entryHandler)
4446

4547
return -1;
4648
}
47-
49+
4850
Task<float> GetPlatformOpacity(EntryHandler entryHandler)
4951
{
5052
return InvokeOnMainThreadAsync(() =>
5153
{
5254
var nativeView = GetPlatformControl(entryHandler);
53-
return (float)nativeView.Alpha;
55+
return (float)nativeView.Alpha;
5456
});
5557
}
5658

@@ -332,5 +334,35 @@ await CreateHandlerAndAddToWindow(contentPage, async () =>
332334
});
333335
}
334336
}
337+
338+
//src/Compatibility/Core/tests/iOS/FlowDirectionTests.cs
339+
[Theory]
340+
[InlineData(true, FlowDirection.LeftToRight, UITextAlignment.Left)]
341+
[InlineData(true, FlowDirection.RightToLeft, UITextAlignment.Right)]
342+
[InlineData(false, FlowDirection.LeftToRight, UITextAlignment.Left)]
343+
[Description("The Entry's text alignment should match the expected alignment when FlowDirection is applied explicitly or implicitly")]
344+
public async Task EntryAlignmentMatchesFlowDirection(bool isExplicit, FlowDirection flowDirection, UITextAlignment expectedAlignment)
345+
{
346+
var entry = new Entry { Text = "Checking flow direction", HorizontalTextAlignment = TextAlignment.Start };
347+
var contentPage = new ContentPage { Title = "Flow Direction", Content = entry };
348+
349+
if (isExplicit)
350+
{
351+
entry.FlowDirection = flowDirection;
352+
}
353+
else
354+
{
355+
contentPage.FlowDirection = flowDirection;
356+
}
357+
358+
var handler = await CreateHandlerAsync<EntryHandler>(entry);
359+
var nativeAlignment = await contentPage.Dispatcher.DispatchAsync(() =>
360+
{
361+
var textField = GetPlatformControl(handler);
362+
return textField.TextAlignment;
363+
});
364+
365+
Assert.Equal(expectedAlignment, nativeAlignment);
366+
}
335367
}
336368
}

src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.iOS.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
67
using Microsoft.Maui.Controls;
78
using Microsoft.Maui.Controls.Handlers.Compatibility;
89
using Microsoft.Maui.Controls.Platform;
910
using Microsoft.Maui.DeviceTests.Stubs;
11+
using Microsoft.Maui.Dispatching;
1012
using Microsoft.Maui.Handlers;
1113
using Microsoft.Maui.Hosting;
1214
using Microsoft.Maui.Platform;
@@ -65,5 +67,29 @@ public async Task TranslucentNavigationBar(bool enabled)
6567
var translucent = await GetValueAsync(navPage, (handler) => (handler.ViewController as UINavigationController).NavigationBar.Translucent);
6668
Assert.Equal(enabled, translucent);
6769
}
70+
71+
//src/Compatibility/Core/tests/iOS/NavigationTests.cs
72+
[Fact]
73+
[Description("Multiple calls to NavigationRenderer.Dispose shouldn't crash")]
74+
public async Task NavigationRendererDoubleDisposal()
75+
{
76+
SetupBuilder();
77+
78+
var root = new ContentPage()
79+
{
80+
Title = "root",
81+
Content = new Label { Text = "Hello" }
82+
};
83+
84+
await root.Dispatcher.DispatchAsync(() =>
85+
{
86+
var navPage = new NavigationPage(root);
87+
var handler = CreateHandler(navPage);
88+
89+
// Calling Dispose more than once should be fine
90+
(handler as NavigationRenderer).Dispose();
91+
(handler as NavigationRenderer).Dispose();
92+
});
93+
}
6894
}
6995
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Maui.Controls;
3+
using Microsoft.Maui.Dispatching;
4+
using Microsoft.Maui.Handlers;
5+
using Microsoft.Maui.Platform;
6+
using Xunit;
7+
using AView = Android.Views.View;
8+
using AndroidX.Fragment.App;
9+
10+
namespace Microsoft.Maui.DeviceTests
11+
{
12+
public partial class PageTests : ControlsHandlerTestBase
13+
{
14+
//src/Compatibility/Core/tests/Android/EmbeddingTests.cs
15+
[Fact(DisplayName = "Can Create Platform View From ContentPage")]
16+
public async Task CanCreatePlatformViewFromContentPage()
17+
{
18+
19+
var contentPage = new ContentPage { Title = "Embedded Page" };
20+
var handler = CreateHandler<PageHandler>(contentPage);
21+
var mauiContext = handler.MauiContext;
22+
23+
await contentPage.Dispatcher.DispatchAsync(() =>
24+
{
25+
26+
AView platformView = contentPage.ToPlatform(mauiContext);
27+
if (platformView is FragmentContainerView containerView)
28+
{
29+
var activity = mauiContext.Context as AndroidX.Fragment.App.FragmentActivity;
30+
var fragmentManager = activity.SupportFragmentManager;
31+
var fragment = fragmentManager.FindFragmentById(containerView.Id);
32+
Assert.NotNull(fragment);
33+
}
34+
});
35+
}
36+
}
37+
}

src/Controls/tests/DeviceTests/Elements/Page/PageTests.iOS.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
using Microsoft.Maui.Controls;
44
using Microsoft.Maui.Controls.PlatformConfiguration;
55
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;
6+
using Microsoft.Maui.Dispatching;
7+
using Microsoft.Maui.Platform;
68
using Microsoft.Maui.Graphics;
79
using Microsoft.Maui.Handlers;
810
using Microsoft.Maui.Hosting;
11+
using UIKit;
912
using Xunit;
1013

1114
namespace Microsoft.Maui.DeviceTests
@@ -40,5 +43,23 @@ await CreateHandlerAndAddToWindow<IWindowHandler>(shell, (handler) =>
4043
}
4144
});
4245
}
46+
47+
//src/Compatibility/Core/tests/iOS/EmbeddingTests.cs
48+
[Fact(DisplayName = "Can Create Platform View From ContentPage")]
49+
public async Task CanCreateViewControllerFromContentPage()
50+
{
51+
var contentPage = new ContentPage { Title = "Embedded Page" };
52+
await contentPage.Dispatcher.DispatchAsync(async () =>
53+
{
54+
var handler = CreateHandler<PageHandler>(contentPage);
55+
var mauiContext = handler.MauiContext;
56+
57+
await contentPage.Dispatcher.DispatchAsync(() =>
58+
{
59+
UIViewController viewController = contentPage.ToUIViewController(mauiContext);
60+
Assert.NotNull(viewController);
61+
});
62+
});
63+
}
4364
}
4465
}

src/Core/tests/DeviceTests/Handlers/ImageButton/ImageButtonHandlerTests.iOS.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using CoreGraphics;
4+
using Microsoft.Maui.Controls;
45
using Microsoft.Maui.DeviceTests.Stubs;
56
using Microsoft.Maui.Graphics;
67
using Microsoft.Maui.Handlers;
@@ -40,6 +41,22 @@ public async Task StrokeColorInitializesCorrectly()
4041
Assert.Equal(expectedValue, values.PlatformViewValue);
4142
}
4243

44+
//src/Compatibility/Core/tests/iOS/ImageButtonTests.cs
45+
[Fact]
46+
[Trait("Category", "ImageButton")]
47+
public async Task CreatedWithCorrectButtonType()
48+
{
49+
var imageButton = new ImageButton();
50+
var handler = await CreateHandlerAsync(imageButton);
51+
52+
var buttonType = await InvokeOnMainThreadAsync(() =>
53+
{
54+
var uiButton = GetPlatformImageButton(handler);
55+
return uiButton.ButtonType;
56+
});
57+
58+
Assert.NotEqual(UIButtonType.Custom, buttonType);
59+
}
4360
UIButton GetPlatformImageButton(ImageButtonHandler imageButtonHandler) =>
4461
imageButtonHandler.PlatformView;
4562

0 commit comments

Comments
 (0)