Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance ServiceProvider for Unit Tests #532

Merged
merged 4 commits into from
Jan 10, 2024
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
2 changes: 1 addition & 1 deletion src/UraniumUI/UraniumServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public static IServiceProvider Current
#elif IOS || MACCATALYST
MauiUIApplicationDelegate.Current.Services;
#else
null;
Application.Current.Handler.MauiContext.Services;
#endif
}
17 changes: 10 additions & 7 deletions test/UraniumUI.Material.Tests/Controls/MultiplePickerField_Test.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.ObjectModel;
using Shouldly;
using System.Collections.ObjectModel;
using UraniumUI.Dialogs;
using UraniumUI.Material.Controls;
using UraniumUI.Material.Tests.Mocks;
using UraniumUI.Tests.Core;

namespace UraniumUI.Material.Tests.Controls;
Expand All @@ -8,27 +11,27 @@ public class MultiplePickerField_Test
{
public MultiplePickerField_Test()
{
ApplicationExtensions.CreateAndSetMockApplication();
ApplicationExtensions.CreateAndSetMockApplication(builder =>
{
builder.Services.AddSingleton<IDialogService, MockDialogService>();
});
}

private string[] GetItemsSource() => new string[] { "Option 1", "Option 2", "Option 3", "Option 4", };

[Fact]
public void Initialize_WithSelectedItems()
{
var control = AnimationReadyHandler.Prepare(new MultiplePickerField());
var viewModel = new TestViewModel();
viewModel.SelectedItems.Add(viewModel.ItemsSource[0]);
viewModel.SelectedItems.Add(viewModel.ItemsSource[2]);

control.BindingContext = viewModel;
control.ItemsSource = viewModel.ItemsSource;

control.SetBinding(MultiplePickerField.SelectedItemsProperty, new Binding(nameof(TestViewModel.SelectedItems)));

// Assert
Assert.True(control.SelectedItems.Contains(viewModel.ItemsSource[0]));
Assert.True(control.SelectedItems.Contains(viewModel.ItemsSource[2]));
control.SelectedItems.Count.ShouldBe(1);
control.SelectedItems[0].ShouldBe(viewModel.ItemsSource[0]);
}

public class TestViewModel : UraniumBindableObject
Expand Down
25 changes: 25 additions & 0 deletions test/UraniumUI.Material.Tests/Mocks/MockDialogService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using UraniumUI.Dialogs;

namespace UraniumUI.Material.Tests.Mocks;
internal class MockDialogService : IDialogService
{
public Task<bool> ConfirmAsync(string title, string message, string okText = "OK", string cancelText = "Cancel")
{
return Task.FromResult(default(bool));
}

public Task<IEnumerable<T>> DisplayCheckBoxPromptAsync<T>(string message, IEnumerable<T> selectionSource, IEnumerable<T> selectedItems = null, string accept = "OK", string cancel = "Cancel", string displayMember = null)
{
return Task.FromResult(Enumerable.Empty<T>());
}

public Task<T> DisplayRadioButtonPromptAsync<T>(string message, IEnumerable<T> selectionSource, T selected = default, string accept = "Ok", string cancel = "Cancel", string displayMember = null)
{
return Task.FromResult(default(T));
}

public Task<string> DisplayTextPromptAsync(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = null, string initialValue = "")
{
return Task.FromResult(string.Empty);
}
}
8 changes: 7 additions & 1 deletion test/UraniumUI.Tests.Core/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ public static Window LoadPage(this Application app, Page page)
return ((IApplication)app).CreateWindow(null) as Window;
}

public static void CreateAndSetMockApplication()
public static void CreateAndSetMockApplication(Action<MauiAppBuilder> builder = null)
{
var appBuilder = MauiApp.CreateBuilder()
.UseMauiApp<MockApplication>();

builder?.Invoke(appBuilder);

var mauiApp = appBuilder.Build();
var application = mauiApp.Services.GetRequiredService<IApplication>();

Application.Current = application as Application;

application.Handler = new ApplicationHandlerStub();
application.Handler.SetMauiContext(new HandlersContextStub(mauiApp.Services));
}
Expand Down
Loading