generated from jfversluis/Plugin.Maui.Feature
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System.Globalization; | ||
using Plugin.OCR.Maui.Tests.Mocks; | ||
|
||
namespace Plugin.OCR.Maui.Tests | ||
{ | ||
public abstract class BaseTest : IDisposable | ||
{ | ||
readonly CultureInfo _defaultCulture, _defaultUiCulture; | ||
|
||
bool _isDisposed; | ||
|
||
protected enum TestDuration | ||
{ | ||
Short = 2000, | ||
Medium = 5000, | ||
Long = 10000 | ||
} | ||
|
||
protected BaseTest() | ||
{ | ||
_defaultCulture = Thread.CurrentThread.CurrentCulture; | ||
_defaultUiCulture = Thread.CurrentThread.CurrentUICulture; | ||
|
||
DispatcherProvider.SetCurrent(new MockDispatcherProvider()); | ||
//DeviceDisplay.SetCurrent(null); | ||
} | ||
|
||
~BaseTest() => Dispose(false); | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool isDisposing) | ||
{ | ||
if (_isDisposed) | ||
{ | ||
return; | ||
} | ||
|
||
Thread.CurrentThread.CurrentCulture = _defaultCulture; | ||
Thread.CurrentThread.CurrentUICulture = _defaultUiCulture; | ||
|
||
//DeviceDisplay.SetCurrent(null); | ||
DispatcherProvider.SetCurrent(null); | ||
|
||
_isDisposed = true; | ||
} | ||
|
||
protected static Task<Stream> GetStreamFromImageSource(StreamImageSource imageSource, CancellationToken token) | ||
=> imageSource.Stream(token); | ||
|
||
protected static bool StreamEquals(Stream a, Stream b) | ||
{ | ||
if (a == b) | ||
{ | ||
return true; | ||
} | ||
|
||
if (a.Length != b.Length) | ||
{ | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < a.Length; i++) | ||
{ | ||
if (a.ReadByte() != b.ReadByte()) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
namespace Plugin.OCR.Maui.Tests.Mocks; | ||
|
||
// Inspired by https://github.com/dotnet/maui/blob/main/src/Core/tests/UnitTests/TestClasses/DispatcherStub.cs | ||
sealed class MockDispatcherProvider : IDispatcherProvider, IDisposable | ||
{ | ||
static readonly DispatcherMock dispatcherMock = new(); | ||
|
||
readonly ThreadLocal<IDispatcher> dispatcherInstance = new(() => dispatcherMock); | ||
|
||
public IDispatcher GetForCurrentThread() => dispatcherInstance.Value ?? throw new InvalidOperationException(); | ||
|
||
void IDisposable.Dispose() => dispatcherInstance.Dispose(); | ||
|
||
sealed class DispatcherMock : IDispatcher | ||
{ | ||
public DispatcherMock() => ManagedThreadId = Environment.CurrentManagedThreadId; | ||
|
||
public bool IsDispatchRequired => false; | ||
|
||
public int ManagedThreadId { get; } | ||
|
||
public IDispatcherTimer CreateTimer() | ||
{ | ||
return new DispatcherTimerStub(this); | ||
} | ||
|
||
public bool Dispatch(Action action) | ||
{ | ||
action(); | ||
|
||
return true; | ||
} | ||
|
||
public bool DispatchDelayed(TimeSpan delay, Action action) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
sealed class DispatcherTimerStub : IDispatcherTimer, IDisposable | ||
{ | ||
readonly DispatcherMock dispatcher; | ||
|
||
Timer? timer; | ||
|
||
public DispatcherTimerStub(DispatcherMock dispatcher) | ||
{ | ||
this.dispatcher = dispatcher; | ||
} | ||
|
||
public TimeSpan Interval { get; set; } | ||
|
||
public bool IsRepeating { get; set; } | ||
|
||
public bool IsRunning => timer != null; | ||
|
||
public event EventHandler? Tick; | ||
|
||
public void Start() | ||
{ | ||
timer = new Timer(OnTimeout, null, Interval, IsRepeating ? Interval : Timeout.InfiniteTimeSpan); | ||
|
||
void OnTimeout(object? state) | ||
{ | ||
dispatcher.Dispatch(() => Tick?.Invoke(this, EventArgs.Empty)); | ||
} | ||
} | ||
|
||
public void Stop() | ||
{ | ||
Dispose(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
timer?.Dispose(); | ||
timer = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<UseMaui>true</UseMaui> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
<PackageReference Include="xunit" Version="2.7.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Plugin.Maui.OCR\Plugin.Maui.OCR.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |