Skip to content

Commit

Permalink
start adding some test base
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrancis committed May 30, 2024
1 parent 16fdb80 commit c57103a
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Plugin.All.OCR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plugin.OCR.Maui.Tests", "..\test\Plugin.OCR.Tests\Plugin.OCR.Maui.Tests.csproj", "{6DE9A37F-8E12-4884-BB7E-970C35AB0FD6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -36,6 +38,10 @@ Global
{BC8D5E54-81F7-45F1-AC48-F269DD5C95B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC8D5E54-81F7-45F1-AC48-F269DD5C95B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC8D5E54-81F7-45F1-AC48-F269DD5C95B3}.Release|Any CPU.Build.0 = Release|Any CPU
{6DE9A37F-8E12-4884-BB7E-970C35AB0FD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6DE9A37F-8E12-4884-BB7E-970C35AB0FD6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DE9A37F-8E12-4884-BB7E-970C35AB0FD6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DE9A37F-8E12-4884-BB7E-970C35AB0FD6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
78 changes: 78 additions & 0 deletions test/Plugin.OCR.Tests/BaseTest.cs
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;
}
}
}
80 changes: 80 additions & 0 deletions test/Plugin.OCR.Tests/Mocks/MockDispatcherProvider.cs
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;
}
}
}
29 changes: 29 additions & 0 deletions test/Plugin.OCR.Tests/Plugin.OCR.Maui.Tests.csproj
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>
1 change: 1 addition & 0 deletions test/Plugin.OCR.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;

0 comments on commit c57103a

Please sign in to comment.