Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public MainPage()
{
InitializeComponent();
}
}
}
1 change: 1 addition & 0 deletions src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<ItemGroup>
<MauiImage Include="Resources\Images\*" />
<MauiImage Include="Resources\Images\white.png" Link="Resources\Images\white_tab.png" BaseSize="23,23" />
<MauiImage Remove="Resources\Images\red-embedded.png" />
<None Remove="Resources\Images\red-embedded.png" />
<EmbeddedResource Include="Resources\Images\red-embedded.png" LogicalName="red-embedded.png" />
Expand Down
10 changes: 9 additions & 1 deletion src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.DeviceTests.Stubs;
Expand Down Expand Up @@ -146,8 +147,11 @@ protected Task CreateHandlerAndAddToWindow<THandler>(IElement view, Func<THandle
else
timeOut ??= TimeSpan.FromSeconds(15);

TestRunnerLogger.LogDebug($"Before InvokeOnMainThreadAsync");

return InvokeOnMainThreadAsync(async () =>
{
TestRunnerLogger.LogDebug($"Starting InvokeOnMainThreadAsync");
IWindow window = CreateWindowForContent(view);

var application = mauiContext.Services.GetService<IApplication>();
Expand All @@ -162,10 +166,13 @@ protected Task CreateHandlerAndAddToWindow<THandler>(IElement view, Func<THandle

try
{
TestRunnerLogger.LogDebug($"Requesting Semaphore to Start");
await _takeOverMainContentSempahore.WaitAsync();
TestRunnerLogger.LogDebug($"Obtained Semaphore");

await SetupWindowForTests<THandler>(window, async () =>
{
TestRunnerLogger.LogDebug($"Window Setup For Tests");
IView content = window.Content;

if (content is FlyoutPage fp)
Expand Down Expand Up @@ -249,9 +256,9 @@ void OnBatchCommitted(object sender, Controls.Internals.EventArg<VisualElement>
else
throw new Exception($"I can't work with {typeof(THandler)}");

TestRunnerLogger.LogDebug($"Running Test");
await action(handler).WaitAsync(timeOut.Value);


#if !WINDOWS
bool isActivated = controlsWindow?.IsActivated ?? false;
bool isDestroyed = controlsWindow?.IsDestroyed ?? false;
Expand All @@ -268,6 +275,7 @@ void OnBatchCommitted(object sender, Controls.Internals.EventArg<VisualElement>
finally
{
_takeOverMainContentSempahore.Release();
TestRunnerLogger.LogDebug($"Finished Running Test");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using Xunit;
using Microsoft.Extensions.Logging;

#if ANDROID || IOS || MACCATALYST
using ShellHandler = Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer;
Expand Down Expand Up @@ -100,23 +101,23 @@ async Task RunShellTabBarTests(Action<Shell> setup, Func<Shell, Task> runTest)
Route = "Tab1",
Title = "Tab1",
Content = new ContentPage(),
Icon = "white.png"
Icon = "white_tab.png"
};

var unselectedContent = new ShellContent()
{
Route = "Tab2",
Title = "Tab2",
Content = new ContentPage(),
Icon = "white.png"
Icon = "white_tab.png"
};

var unselectedContent_2 = new ShellContent()
{
Route = "Tab3",
Title = "Tab3",
Content = new ContentPage(),
Icon = "white.png"
Icon = "white_tab.png"
};

var shell = await CreateShellAsync((shell) =>
Expand Down
7 changes: 7 additions & 0 deletions src/Core/src/Hosting/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Microsoft.Maui
{
Expand All @@ -10,6 +11,9 @@ internal static class ServiceProviderExtensions
internal static ILogger<T>? CreateLogger<T>(this IMauiContext context) =>
context.Services.CreateLogger<T>();

internal static ILogger CreateLogger(this IMauiContext context, Type type) =>
context.Services.GetService<ILoggerFactory>()?.CreateLogger(type) ?? NullLogger.Instance;

internal static ILogger<T>? CreateLogger<T>(this IServiceProvider services) =>
services.GetService<ILogger<T>>();

Expand All @@ -18,5 +22,8 @@ internal static class ServiceProviderExtensions

internal static ILogger? CreateLogger(this IServiceProvider services, string loggerName) =>
services.GetService<ILoggerFactory>()?.CreateLogger(loggerName);

internal static ILogger CreateLogger(this IServiceProvider services, Type type) =>
services.GetService<ILoggerFactory>()?.CreateLogger(type) ?? NullLogger.Instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Maui;
using Microsoft.Maui.DeviceTests.ImageAnalysis;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Dispatching;
Expand Down Expand Up @@ -46,12 +49,17 @@ public void EnsureHandlerCreated(Action<MauiAppBuilder> additionalCreationAction
appBuilder = ConfigureBuilder(appBuilder);
additionalCreationActions?.Invoke(appBuilder);

appBuilder.Services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, NullLoggerFactory>());
appBuilder.Services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(NullLogger<>)));

_mauiApp = appBuilder.Build();
_servicesProvider = _mauiApp.Services;

_mauiContext = new ContextStub(_servicesProvider);
}

protected ILogger TestRunnerLogger => MauiContext.CreateLogger(this.GetType());

protected virtual MauiAppBuilder ConfigureBuilder(MauiAppBuilder mauiAppBuilder)
{
return mauiAppBuilder;
Expand Down
24 changes: 24 additions & 0 deletions src/Core/tests/DeviceTests.Shared/Stubs/ContextStub.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Maui.Animations;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.TestUtils.DeviceTests.Runners;

Expand Down Expand Up @@ -70,6 +73,27 @@ public object GetService(Type serviceType)
if (serviceType == typeof(IDispatcher))
return _services.GetService(serviceType) ?? TestDispatcher.Current;

if (serviceType.IsGenericType && serviceType.GetGenericTypeDefinition() == typeof(ILogger<>) ||
serviceType == typeof(ILogger) ||
serviceType == typeof(ILoggerFactory))
{
// This verifies that the test itself hasn't registered a logger
// if the test itself has registered a logger than return that because the logger is under tests
var registeredLogger = _services.GetService(serviceType);
var registeredLoggerType = registeredLogger.GetType();

if (registeredLogger is NullLoggerFactory)
{
return TestServices.Services.GetService(serviceType) ?? registeredLogger;
}

if (registeredLogger is NullLogger ||
registeredLoggerType.IsGenericType && registeredLoggerType.GetGenericTypeDefinition() == typeof(NullLogger<>))
{
return TestServices.Services.GetService(serviceType) ?? registeredLogger;
}
}

return _services.GetService(serviceType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ await InvokeOnMainThreadAsync(async () =>
#if ANDROID
Assert.Equal(GetDrawableId("red"), platformImage);
#elif IOS || MACCATALYST
platformImage.AssertContainsColor(Colors.Red.ToPlatform());
await platformImage.AssertContainsColor(Colors.Red.ToPlatform()).ConfigureAwait(false);
#endif
});
}
Expand Down Expand Up @@ -441,7 +441,7 @@ await InvokeOnMainThreadAsync(async () =>
#if ANDROID
Assert.Equal(GetDrawableId("blue"), platformImage);
#elif IOS || MACCATALYST
platformImage.AssertContainsColor(Colors.Blue.ToPlatform());
await platformImage.AssertContainsColor(Colors.Blue.ToPlatform()).ConfigureAwait(false);
#endif
});
}
Expand All @@ -463,7 +463,7 @@ public async Task ImageLoadSequenceIsCorrectWithChecks()
platformImage.Color.IsEquivalent(Colors.Blue.ToPlatform());
#elif IOS || MACCATALYST
var platformImage = Assert.IsType<UIImage>(events[0].Value);
platformImage.AssertContainsColor(Colors.Blue.ToPlatform());
await platformImage.AssertContainsColor(Colors.Blue.ToPlatform()).ConfigureAwait(false);
#endif
}

Expand All @@ -484,7 +484,7 @@ public async Task InterruptingLoadCancelsAndStartsOverWithChecks()
platformImage.Color.IsEquivalent(Colors.Red.ToPlatform());
#elif IOS || MACCATALYST
var platformImage = Assert.IsType<UIImage>(events[0].Value);
platformImage.AssertContainsColor(Colors.Red.ToPlatform());
await platformImage.AssertContainsColor(Colors.Red.ToPlatform()).ConfigureAwait(false);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task GetDrawableAsync(string colorHex)

var bitmap = bitmapDrawable.Bitmap;

bitmap.AssertContainsColor(expectedColor.ToPlatform());
await bitmap.AssertContainsColor(expectedColor.ToPlatform()).ConfigureAwait(false);
}

[Theory]
Expand Down Expand Up @@ -92,7 +92,7 @@ public async Task GetDrawableAsyncWithCustomFont()

var bitmap = bitmapDrawable.Bitmap;

bitmap.AssertContainsColor(Colors.Red.ToPlatform());
await bitmap.AssertContainsColor(Colors.Red.ToPlatform()).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task GetImageAsync(string colorHex)

var uiimage = Assert.IsType<UIImage>(drawable.Value);

uiimage.AssertContainsColor(expectedColor.ToPlatform());
await uiimage.AssertContainsColor(expectedColor.ToPlatform()).ConfigureAwait(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the same changes are required on the Android tests:

D:\a\_work\1\s\src\Core\tests\DeviceTests\Services\ImageSource\FontImageSourceServiceTests.Android.cs(56,4): error CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. [D:\a\_work\1\s\src\Core\tests\DeviceTests\Core.DeviceTests.csproj::TargetFramework=net9.0-android]
D:\a\_work\1\s\src\Core\tests\DeviceTests\Services\ImageSource\FontImageSourceServiceTests.Android.cs(95,4): error CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call. [D:\a\_work\1\s\src\Core\tests\DeviceTests\Core.DeviceTests.csproj::TargetFramework=net9.0-android]
    8944 Warning(s)
    2 Error(s)

}

[Fact]
Expand Down Expand Up @@ -80,7 +80,7 @@ public async Task GetImageAsyncWithCustomFont()

var uiimage = Assert.IsType<UIImage>(drawable.Value);

uiimage.AssertContainsColor(Colors.Red.ToPlatform());
await uiimage.AssertContainsColor(Colors.Red.ToPlatform()).ConfigureAwait(false);
}
}
}
6 changes: 3 additions & 3 deletions src/Essentials/test/DeviceTests/Tests/Accelerometer_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task Monitor(SensorSpeed sensorSpeed)
if (!HardwareSupport.HasAccelerometer)
return;

var tcs = new TaskCompletionSource<AccelerometerData>();
var tcs = new TaskCompletionSource<AccelerometerData>(TaskCreationOptions.RunContinuationsAsynchronously);
Accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
Accelerometer.Start(sensorSpeed);

Expand All @@ -48,7 +48,7 @@ public async Task IsMonitoring(SensorSpeed sensorSpeed)
if (!HardwareSupport.HasAccelerometer)
return;

var tcs = new TaskCompletionSource<AccelerometerData>();
var tcs = new TaskCompletionSource<AccelerometerData>(TaskCreationOptions.RunContinuationsAsynchronously);
Accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
Accelerometer.Start(sensorSpeed);

Expand All @@ -72,7 +72,7 @@ public async Task Stop_Monitor(SensorSpeed sensorSpeed)
if (!HardwareSupport.HasAccelerometer)
return;

var tcs = new TaskCompletionSource<AccelerometerData>();
var tcs = new TaskCompletionSource<AccelerometerData>(TaskCreationOptions.RunContinuationsAsynchronously);

Accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
Accelerometer.Start(sensorSpeed);
Expand Down
2 changes: 1 addition & 1 deletion src/Essentials/test/DeviceTests/Tests/AppActions_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task GetSetItems()

await AppActions.SetAsync(actions);

var get = await AppActions.GetAsync();
var get = await AppActions.GetAsync().ConfigureAwait(false);

Assert.Contains(get, a => a.Id == "TEST1");
}
Expand Down
6 changes: 3 additions & 3 deletions src/Essentials/test/DeviceTests/Tests/Barometer_Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task Monitor()
if (!HardwareSupport.HasBarometer)
return;

var tcs = new TaskCompletionSource<BarometerData>();
var tcs = new TaskCompletionSource<BarometerData>(TaskCreationOptions.RunContinuationsAsynchronously);

Barometer.ReadingChanged += Barometer_ReadingChanged;
void Barometer_ReadingChanged(object sender, BarometerChangedEventArgs e)
Expand All @@ -43,7 +43,7 @@ public async Task IsMonitoring()
if (!HardwareSupport.HasBarometer)
return;

var tcs = new TaskCompletionSource<BarometerData>();
var tcs = new TaskCompletionSource<BarometerData>(TaskCreationOptions.RunContinuationsAsynchronously);
Barometer.ReadingChanged += Barometer_ReadingChanged;
void Barometer_ReadingChanged(object sender, BarometerChangedEventArgs e)
{
Expand All @@ -66,7 +66,7 @@ public async Task Stop_Monitor()
if (!HardwareSupport.HasBarometer)
return;

var tcs = new TaskCompletionSource<BarometerData>();
var tcs = new TaskCompletionSource<BarometerData>(TaskCreationOptions.RunContinuationsAsynchronously);
Barometer.ReadingChanged += Barometer_ReadingChanged;
void Barometer_ReadingChanged(object sender, BarometerChangedEventArgs e)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Essentials/test/DeviceTests/Tests/Compass_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task Monitor(SensorSpeed sensorSpeed)
if (!HardwareSupport.HasCompass)
return;

var tcs = new TaskCompletionSource<CompassData>();
var tcs = new TaskCompletionSource<CompassData>(TaskCreationOptions.RunContinuationsAsynchronously);

Compass.ReadingChanged += Compass_ReadingChanged;
void Compass_ReadingChanged(object sender, CompassChangedEventArgs e)
Expand All @@ -49,7 +49,7 @@ public async Task IsMonitoring(SensorSpeed sensorSpeed)
if (!HardwareSupport.HasCompass)
return;

var tcs = new TaskCompletionSource<CompassData>();
var tcs = new TaskCompletionSource<CompassData>(TaskCreationOptions.RunContinuationsAsynchronously);
Compass.ReadingChanged += Compass_ReadingChanged;
void Compass_ReadingChanged(object sender, CompassChangedEventArgs e)
{
Expand All @@ -73,7 +73,7 @@ public async Task Stop_Monitor(SensorSpeed sensorSpeed)
if (!HardwareSupport.HasCompass)
return;

var tcs = new TaskCompletionSource<CompassData>();
var tcs = new TaskCompletionSource<CompassData>(TaskCreationOptions.RunContinuationsAsynchronously);
Compass.ReadingChanged += Compass_ReadingChanged;
void Compass_ReadingChanged(object sender, CompassChangedEventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task Test()
{
var current = Connectivity.Current.NetworkAccess;

var thread = await Task.Run(() => Connectivity.Current.NetworkAccess);
var thread = await Task.Run(() => Connectivity.Current.NetworkAccess).ConfigureAwait(false);

Assert.Equal(current, thread);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Essentials/test/DeviceTests/Tests/Contacts_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task GetAll()
{
await MainThread.InvokeOnMainThreadAsync(async () =>
{
await Permissions.RequestAsync<Permissions.ContactsRead>();
await Permissions.RequestAsync<Permissions.ContactsRead>().ConfigureAwait(false);
});

var list = new List<Contact>();
Expand Down
4 changes: 2 additions & 2 deletions src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public async Task OpenAppPackageFileAsync_Can_Load_File(string filename, string
Assert.NotNull(stream);

using var reader = new StreamReader(stream);
var text = await reader.ReadToEndAsync();
var text = await reader.ReadToEndAsync().ConfigureAwait(false);

Assert.Equal(contents, text);
}

[Fact]
public async Task OpenAppPackageFileAsync_Throws_If_File_Is_Not_Found()
{
await Assert.ThrowsAsync<FileNotFoundException>(() => FileSystem.OpenAppPackageFileAsync("MissingFile.txt"));
await Assert.ThrowsAsync<FileNotFoundException>(() => FileSystem.OpenAppPackageFileAsync("MissingFile.txt")).ConfigureAwait(false);
}
}
}
Loading