Skip to content

Commit

Permalink
Fixes dotnet#16923
Browse files Browse the repository at this point in the history
  • Loading branch information
greg84 committed Aug 22, 2023
1 parent a7d2c9d commit 3396d23
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Core/src/Fonts/FontRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Register(string filename, string? alias)
/// <inheritdoc/>
public string? GetFont(string font)
{
if (_fontLookupCache.TryGetValue(font, out var foundResult))
if (_fontLookupCache.TryGetValue(font, out var foundResult) && File.Exists(foundResult))
return foundResult;

try
Expand Down
36 changes: 36 additions & 0 deletions src/Core/tests/UnitTests/Hosting/HostBuilderFontsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,42 @@ public void ConfigureFontsRegistersFonts(string filename, string alias)
Directory.Delete(root, true);
}

[Fact]
public void FontShouldBeCopiedAgainIfTempFolderIsClearedDuringRuntime()
{
var root = Path.Combine(Path.GetTempPath(), "Microsoft.Maui.UnitTests", "ConfigureFontsRegistersFonts", Guid.NewGuid().ToString());
var filename = "Dokdo-Regular.ttf";
var alias = "Dokdo";

var builder = MauiApp
.CreateBuilder()
.ConfigureFonts(fonts => fonts.AddEmbeddedResourceFont(GetType().Assembly, filename, alias));
builder.Services.AddSingleton<IEmbeddedFontLoader>(_ => new FileSystemEmbeddedFontLoader(root));
var mauiApp = builder.Build();

var registrar = mauiApp.Services.GetRequiredService<IFontRegistrar>();

var path = registrar.GetFont(filename);
Assert.True(File.Exists(path));

File.Delete(path);

path = registrar.GetFont(alias);
Assert.True(File.Exists(path));

File.Delete(path);

path = registrar.GetFont(filename);
Assert.True(File.Exists(path));

File.Delete(path);

path = registrar.GetFont(alias);
Assert.True(File.Exists(path));

Directory.Delete(root, true);
}

[Fact]
public void NullAssemblyForEmbeddedFontThrows()
{
Expand Down

0 comments on commit 3396d23

Please sign in to comment.