-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor * Set up unit testing, add net6.0 target * Add more tests * Rename AccentColorService > SeedColorService
- Loading branch information
Showing
27 changed files
with
835 additions
and
362 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
MaterialColorUtilities.Maui.Tests/DynamicColorServiceTests.cs
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,95 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace MaterialColorUtilities.Maui.Tests; | ||
|
||
[TestClass] | ||
public class DynamicColorServiceTests | ||
{ | ||
private readonly ISeedColorService _seedColorService = new MockSeedColorService(); | ||
private readonly Application _application = new(); | ||
private readonly IPreferences _preferences = new MockPreferences(); | ||
|
||
[TestMethod] | ||
public void DisableTheming() | ||
{ | ||
IOptions<DynamicColorOptions> options = CreateOptions(opt => | ||
{ | ||
opt.EnableTheming = false; | ||
}); | ||
|
||
DynamicColorService dynamicColorService = new(options, _seedColorService, _application, _preferences); | ||
|
||
dynamicColorService.Initialize(null); | ||
|
||
Assert.IsFalse(dynamicColorService.EnableTheming); | ||
Assert.IsNull(dynamicColorService.CorePalette); | ||
Assert.IsNull(dynamicColorService.SchemeMaui); | ||
Assert.IsTrue(_application.Resources.Count == 0); | ||
} | ||
|
||
[TestMethod] | ||
public void DisableDynamicColor() | ||
{ | ||
IOptions<DynamicColorOptions> options = CreateOptions(opt => | ||
{ | ||
opt.EnableDynamicColor = false; | ||
}); | ||
|
||
DynamicColorService dynamicColorService = new(options, _seedColorService, _application, _preferences); | ||
|
||
dynamicColorService.Initialize(null); | ||
|
||
Assert.IsTrue(dynamicColorService.EnableTheming); | ||
Assert.IsFalse(dynamicColorService.EnableDynamicColor); | ||
Assert.AreEqual(unchecked((int)0xff4285F4), dynamicColorService.Seed); | ||
Assert.AreEqual(unchecked((int)0xff005AC1), dynamicColorService.SchemeInt.Primary); | ||
Assert.AreEqual(dynamicColorService.SchemeInt.Primary, dynamicColorService.SchemeMaui.Primary.ToInt()); | ||
Assert.IsNotNull(_application.Resources[Schemes.Keys.Primary]); | ||
} | ||
|
||
[TestMethod] | ||
public void EnableDynamicColor_SeedColorNull() | ||
{ | ||
IOptions<DynamicColorOptions> options = CreateOptions(); | ||
|
||
DynamicColorService dynamicColorService = new(options, _seedColorService, _application, _preferences); | ||
|
||
dynamicColorService.Initialize(null); | ||
|
||
Assert.IsTrue(dynamicColorService.EnableTheming); | ||
Assert.IsTrue(dynamicColorService.EnableDynamicColor); | ||
Assert.AreEqual(unchecked((int)0xff4285F4), dynamicColorService.Seed); | ||
Assert.AreEqual(unchecked((int)0xff005AC1), dynamicColorService.SchemeInt.Primary); | ||
Assert.AreEqual(dynamicColorService.SchemeInt.Primary, dynamicColorService.SchemeMaui.Primary.ToInt()); | ||
Assert.IsNotNull(_application.Resources[Schemes.Keys.Primary]); | ||
} | ||
|
||
[TestMethod] | ||
public void EnableDynamicColor_SeedColorAvailable() | ||
{ | ||
IOptions<DynamicColorOptions> options = CreateOptions(); | ||
|
||
ISeedColorService seedColorService = new MockSeedColorService(unchecked((int)0xFFc07d52)); | ||
DynamicColorService dynamicColorService = new(options, seedColorService, _application, _preferences); | ||
|
||
dynamicColorService.Initialize(null); | ||
|
||
Assert.AreEqual(unchecked((int)0xFFc07d52), dynamicColorService.Seed); | ||
Assert.AreEqual(unchecked((int)0xFF96490A), dynamicColorService.SchemeInt.Primary); | ||
Assert.AreEqual(dynamicColorService.SchemeInt.Primary, dynamicColorService.SchemeMaui.Primary.ToInt()); | ||
Assert.IsNotNull(_application.Resources[Schemes.Keys.Primary]); | ||
} | ||
|
||
private static IOptions<DynamicColorOptions> CreateOptions() | ||
{ | ||
DynamicColorOptions options = new(); | ||
return Options.Create(options); | ||
} | ||
|
||
private static IOptions<DynamicColorOptions> CreateOptions(Action<DynamicColorOptions> configure) | ||
{ | ||
DynamicColorOptions options = new(); | ||
configure(options); | ||
return Options.Create(options); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
MaterialColorUtilities.Maui.Tests/MaterialColorUtilities.Maui.Tests.csproj
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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
<UseMaui>true</UseMaui> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MaterialColorUtilities.Maui\MaterialColorUtilities.Maui.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,33 @@ | ||
using System.Text.Json; | ||
|
||
namespace MaterialColorUtilities.Maui.Tests; | ||
|
||
public class MockPreferences : IPreferences | ||
{ | ||
private readonly Dictionary<string, string> _container = new(); | ||
|
||
public bool ContainsKey(string key, string? sharedName = null) | ||
{ | ||
return _container.ContainsKey(key); | ||
} | ||
|
||
public void Remove(string key, string? sharedName = null) | ||
{ | ||
_container.Remove(key); | ||
} | ||
|
||
public void Clear(string? sharedName = null) | ||
{ | ||
_container.Clear(); | ||
} | ||
|
||
public void Set<T>(string key, T value, string? sharedName = null) | ||
{ | ||
_container[key] = JsonSerializer.Serialize(value); | ||
} | ||
|
||
public T Get<T>(string key, T defaultValue, string? sharedName = null) | ||
{ | ||
return JsonSerializer.Deserialize<T>(_container[key]) ?? throw new InvalidOperationException(); | ||
} | ||
} |
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,14 @@ | ||
namespace MaterialColorUtilities.Maui.Tests; | ||
|
||
public class MockSeedColorService : ISeedColorService | ||
{ | ||
public MockSeedColorService() { } | ||
|
||
public MockSeedColorService(int? seedColor) | ||
{ | ||
SeedColor = seedColor; | ||
} | ||
|
||
public int? SeedColor { get; } | ||
public event Action? OnSeedColorChanged { add { } remove { } } | ||
} |
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 Microsoft.VisualStudio.TestTools.UnitTesting; |
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 |
---|---|---|
@@ -1,18 +1,33 @@ | ||
namespace MaterialColorUtilities.Maui; | ||
|
||
public class DynamicColorOptions | ||
public sealed class DynamicColorOptions | ||
{ | ||
/// <summary> | ||
/// Will be used if a dynamic accent color is not available. | ||
/// When true, updates to Application.UserAppTheme will be stored using Preferences | ||
/// and reapplied when the app restarts. Defaults to true. | ||
/// </summary> | ||
public int FallbackSeed { get; set; } = unchecked((int)0xff4285F4); // Google Blue | ||
public bool RememberIsDark { get; set; } = true; | ||
|
||
/// <summary> | ||
/// Determines whether to generate colors as application resources. | ||
/// Set to false when using a custom color scheme source. | ||
/// </summary> | ||
/// <remarks> | ||
/// Can be updated at runtime using DynamicColorService.IsEnabled. | ||
/// </remarks> | ||
public bool EnableTheming { get; set; } = true; | ||
|
||
/// <summary> | ||
/// Whether to use wallpaper/accent color based dynamic theming. | ||
/// Will be used if a dynamic seed color is not available or dynamic theming is disabled. | ||
/// </summary> | ||
public int FallbackSeed { get; set; } = unchecked((int)0xff4285F4); // Google Blue | ||
|
||
/// <summary> | ||
/// Whether to use wallpaper/accent color based dynamic theming. Defaults to true. | ||
/// </summary> | ||
/// <remarks> | ||
/// When set to <see langword="false"/>, <see cref="FallbackSeed"/> will be used as seed, | ||
/// even on platforms that expose an accent color. | ||
/// </remarks> | ||
public bool UseDynamicColor { get; set; } = true; | ||
} | ||
public bool EnableDynamicColor { get; set; } = true; | ||
} |
168 changes: 0 additions & 168 deletions
168
MaterialColorUtilities.Maui/DynamicColorService.Android.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.