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

WinGet Icons support #2810

Merged
merged 6 commits into from
Oct 4, 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
142 changes: 119 additions & 23 deletions src/UniGetUI.Core.IconEngine.Tests/IconCacheEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,58 @@ namespace UniGetUI.Core.IconEngine.Tests
{
public static class IconCacheEngineTests
{
[Theory]
[InlineData("https://marticliment.com/resources/wingetui.png",
new byte[] { 0x24, 0x4e, 0x42, 0xb6, 0xbe, 0x44, 0x04, 0x66, 0xc8, 0x77, 0xf7, 0x68, 0x8a, 0xe0, 0xa9, 0x45, 0xfb, 0x2e, 0x66, 0x8c, 0x41, 0x84, 0x1f, 0x2d, 0x10, 0xcf, 0x92, 0xd4, 0x0d, 0x8c, 0xbb, 0xf6 },
"TestManager", "Package1")]
public static async Task TestCacheEngineForSha256(string url, byte[] data, string managerName, string packageId)
[Fact]
public static async Task TestCacheEngineForSha256()
{
string extension = url.Split(".")[^1];
Uri ICON_1 = new Uri("https://marticliment.com/resources/wingetui.png");
byte[] HASH_1 = [0x24, 0x4e, 0x42, 0xb6, 0xbe, 0x44, 0x04, 0x66, 0xc8, 0x77, 0xf7, 0x68, 0x8a, 0xe0, 0xa9, 0x45, 0xfb, 0x2e, 0x66, 0x8c, 0x41, 0x84, 0x1f, 0x2d, 0x10, 0xcf, 0x92, 0xd4, 0x0d, 0x8c, 0xbb, 0xf6];
Uri ICON_2 = new Uri("https://marticliment.com/resources/elevenclock.png");
byte[] HASH_2 = [0x9E, 0xB8, 0x7A, 0x5A, 0x64, 0xCA, 0x6D, 0x8D, 0x0A, 0x7B, 0x98, 0xC5, 0x4F, 0x6A, 0x58, 0x72, 0xFD, 0x94, 0xC9, 0xA6, 0x82, 0xB3, 0x2B, 0x90, 0x70, 0x66, 0x66, 0x1C, 0xBF, 0x81, 0x97, 0x97];

string managerName = "TestManager";
string packageId = "Package55";

string extension = ICON_1.ToString().Split(".")[^1];
string expectedFile = Path.Join(CoreData.UniGetUICacheDirectory_Icons, managerName, packageId + "." + extension);
if (File.Exists(expectedFile))
{
File.Delete(expectedFile);
}

CacheableIcon icon = new(new Uri(url), data);
string path = await IconCacheEngine.DownloadIconOrCache(icon, managerName, packageId);
// Download a hashed icon
CacheableIcon icon = new(ICON_1, HASH_1);
string? path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

DateTime oldModificationDate = File.GetLastWriteTime(path);

icon = new CacheableIcon(new Uri(url.Replace("icon", "nonexistingicon")), data);
path = await IconCacheEngine.DownloadIconOrCache(icon, managerName, packageId);
// Test the same icon, modification date shouldn't change
icon = new CacheableIcon(ICON_1, HASH_1);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
DateTime newModificationDate = File.GetLastWriteTime(path);

Assert.Equal(oldModificationDate, newModificationDate);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

// Attempt to retrieve a different icon. The modification date SHOULD have changed
icon = new CacheableIcon(ICON_2, HASH_2);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
DateTime newIconModificationDate = File.GetLastWriteTime(path);

Assert.NotEqual(oldModificationDate, newIconModificationDate);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

// Give an invalid hash: The icon should not be cached not returned
icon = new CacheableIcon(ICON_2, HASH_1);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.Null(path);
Assert.False(File.Exists(path));
}

[Theory]
Expand All @@ -44,63 +69,134 @@ public static async Task TestCacheEngineForPackageVersion(string url, string ver
File.Delete(expectedFile);
}

// Download an icon through version verification
CacheableIcon icon = new(new Uri(url), version);
string path = await IconCacheEngine.DownloadIconOrCache(icon, managerName, packageId);
string? path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

DateTime oldModificationDate = File.GetLastWriteTime(path);

// Test the same version, the icon should not get touched
icon = new CacheableIcon(new Uri(url), version);
path = await IconCacheEngine.DownloadIconOrCache(icon, managerName, packageId);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
DateTime newModificationDate = File.GetLastWriteTime(path);

Assert.Equal(oldModificationDate, newModificationDate);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

// Test a new version, the icon should be downloaded again
icon = new CacheableIcon(new Uri(url), version + "-beta0");
path = await IconCacheEngine.DownloadIconOrCache(icon, managerName, packageId);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
DateTime newNewModificationDate = File.GetLastWriteTime(path);

Assert.NotEqual(oldModificationDate, newNewModificationDate);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));
}

[Theory]
[InlineData("https://marticliment.com/resources/wingetui.png", 47903, "TestManager", "Package3")]
public static async Task TestCacheEngineForPackageSize(string url, long size, string managerName, string packageId)
[Fact]
public static async Task TestCacheEngineForIconUri()
{
string extension = url.Split(".")[^1];
Uri URI_1 = new Uri("https://marticliment.com/resources/wingetui.png");
Uri URI_2 = new Uri("https://marticliment.com/resources/elevenclock.png");
string managerName = "TestManager";
string packageId = "Package12";

string extension = URI_1.ToString().Split(".")[^1];
string expectedFile = Path.Join(CoreData.UniGetUICacheDirectory_Icons, managerName, packageId + "." + extension);
if (File.Exists(expectedFile))
{
File.Delete(expectedFile);
}

CacheableIcon icon = new(new Uri(url), size);
string path = await IconCacheEngine.DownloadIconOrCache(icon, managerName, packageId);
// Download an icon through URI verification
CacheableIcon icon = new(URI_1);
string? path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

DateTime oldModificationDate = File.GetLastWriteTime(path);

icon = new CacheableIcon(new Uri(url), size);
path = await IconCacheEngine.DownloadIconOrCache(icon, managerName, packageId);
// Test the same URI, the icon should not get touched
icon = new CacheableIcon(URI_1);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
DateTime newModificationDate = File.GetLastWriteTime(path);

Assert.Equal(oldModificationDate, newModificationDate);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

icon = new CacheableIcon(new Uri(url), size + 1);
path = await IconCacheEngine.DownloadIconOrCache(icon, managerName, packageId);
// Test a new URI, the icon should be downloaded again
icon = new CacheableIcon(URI_2);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
DateTime newNewModificationDate = File.GetLastWriteTime(path);

Assert.NotEqual(oldModificationDate, newNewModificationDate);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));
}

[Fact]
public static async Task TestCacheEngineForPackageSize()
{
Uri ICON_1 = new Uri("https://marticliment.com/resources/wingetui.png");
int ICON_1_SIZE = 47903;
Uri ICON_2 = new Uri("https://marticliment.com/resources/elevenclock.png");
int ICON_2_SIZE = 19747;
string managerName = "TestManager";
string packageId = "Package3";

// Clear any cache for reproducable data
string extension = ICON_1.ToString().Split(".")[^1];
string expectedFile = Path.Join(CoreData.UniGetUICacheDirectory_Icons, managerName, packageId + "." + extension);
if (File.Exists(expectedFile))
{
File.Delete(expectedFile);
}

// Cache an icon
CacheableIcon icon = new(ICON_1, ICON_1_SIZE);
string? path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

DateTime oldModificationDate = File.GetLastWriteTime(path);

// Attempt to retrieve the same icon again.
icon = new CacheableIcon(ICON_1, ICON_1_SIZE);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
DateTime newModificationDate = File.GetLastWriteTime(path);

// The modification date shouldn't have changed
Assert.Equal(oldModificationDate, newModificationDate);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

// Attempt to retrieve a different icon. The modification date SHOULD have changed
icon = new CacheableIcon(ICON_2, ICON_2_SIZE);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.NotNull(path);
DateTime newIconModificationDate = File.GetLastWriteTime(path);

Assert.NotEqual(oldModificationDate, newIconModificationDate);
Assert.Equal(expectedFile, path);
Assert.True(File.Exists(path));

// Give an invalid size: The icon should not be cached not returned
icon = new CacheableIcon(ICON_1, ICON_1_SIZE + 1);
path = await IconCacheEngine.GetCacheOrDownloadIcon(icon, managerName, packageId);
Assert.Null(path);
Assert.False(File.Exists(path));
}
}
}
Loading
Loading