Skip to content

Commit 374e1f6

Browse files
committed
Added unit testing
1 parent f40d4fc commit 374e1f6

File tree

3 files changed

+74
-41
lines changed

3 files changed

+74
-41
lines changed

Flow.Launcher.Test/Flow.Launcher.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
</ItemGroup>
3939

4040
<ItemGroup>
41+
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Program\Flow.Launcher.Plugin.Program.csproj" />
4142
<ProjectReference Include="..\Plugins\Flow.Launcher.Plugin.Url\Flow.Launcher.Plugin.Url.csproj" />
4243
<ProjectReference Include="..\Flow.Launcher.Core\Flow.Launcher.Core.csproj" />
4344
<ProjectReference Include="..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Flow.Launcher.Plugin.Program.Programs;
2+
using NUnit.Framework;
3+
using System;
4+
using Windows.ApplicationModel;
5+
6+
namespace Flow.Launcher.Test.Plugins
7+
{
8+
[TestFixture]
9+
public class ProgramTest
10+
{
11+
[TestCase("Microsoft.WindowsCamera", "ms-resource:LensSDK/Resources/AppTitle", "ms-resource://Microsoft.WindowsCamera/LensSDK/Resources/AppTitle")]
12+
[TestCase("microsoft.windowscommunicationsapps", "ms-resource://microsoft.windowscommunicationsapps/hxoutlookintl/AppManifest_MailDesktop_DisplayName",
13+
"ms-resource://microsoft.windowscommunicationsapps/hxoutlookintl/AppManifest_MailDesktop_DisplayName")]
14+
[TestCase("windows.immersivecontrolpanel", "ms-resource:DisplayName", "ms-resource://windows.immersivecontrolpanel/Resources/DisplayName")]
15+
[TestCase("Microsoft.MSPaint", "ms-resource:AppName", "ms-resource://Microsoft.MSPaint/Resources/AppName")]
16+
public void WhenGivenPriReferenceValueShouldReturnCorrectFormat(string packageName, string rawPriReferenceValue, string expectedFormat)
17+
{
18+
// Arrange
19+
var app = new UWP.Application();
20+
21+
// Act
22+
var result = app.FormattedPriReferenceValue(packageName, rawPriReferenceValue);
23+
24+
// Assert
25+
Assert.IsTrue(result == expectedFormat,
26+
$"Expected Pri reference format: {expectedFormat}{Environment.NewLine} " +
27+
$"Actual: {result}{Environment.NewLine}");
28+
}
29+
}
30+
}

Plugins/Flow.Launcher.Plugin.Program/Programs/UWP.cs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ public class Application : IProgram
263263
public string LogoPath { get; set; }
264264
public UWP Package { get; set; }
265265

266+
public Application(){}
267+
266268
private int Score(string query)
267269
{
268270
var displayNameMatch = StringMatcher.FuzzySearch(query, DisplayName);
@@ -371,62 +373,62 @@ public Application(IAppxManifestApplication manifestApp, UWP package)
371373
Enabled = true;
372374
}
373375

374-
internal string ResourceFromPri(string packageFullName, string packageName, string resourceReference)
376+
internal string ResourceFromPri(string packageFullName, string packageName, string rawReferenceValue)
375377
{
376-
const string prefix = "ms-resource:";
377-
if (!string.IsNullOrWhiteSpace(resourceReference) && resourceReference.StartsWith(prefix))
378-
{
379-
string key = resourceReference.Substring(prefix.Length);
380-
string parsed;
381-
if (key.StartsWith("//"))
382-
{
383-
parsed = $"{prefix}{key}";
384-
}
385-
else
386-
{
387-
if (!key.StartsWith("/"))
388-
{
389-
key = $"/{key}";
390-
}
378+
if (string.IsNullOrWhiteSpace(rawReferenceValue) || !rawReferenceValue.StartsWith("ms-resource:"))
379+
return rawReferenceValue;
391380

392-
if (!key.ToLower().Contains("resources"))
393-
{
394-
key = $"/Resources{key}";
395-
}
396-
parsed = $"{prefix}//{packageName}{key}";
397-
}
381+
var formattedPriReference = FormattedPriReferenceValue(packageName, rawReferenceValue);
398382

399-
var outBuffer = new StringBuilder(128);
400-
string source = $"@{{{packageFullName}? {parsed}}}";
401-
var capacity = (uint)outBuffer.Capacity;
402-
var hResult = SHLoadIndirectString(source, outBuffer, capacity, IntPtr.Zero);
403-
if (hResult == Hresult.Ok)
383+
var outBuffer = new StringBuilder(128);
384+
string source = $"@{{{packageFullName}? {formattedPriReference}}}";
385+
var capacity = (uint)outBuffer.Capacity;
386+
var hResult = SHLoadIndirectString(source, outBuffer, capacity, IntPtr.Zero);
387+
if (hResult == Hresult.Ok)
388+
{
389+
var loaded = outBuffer.ToString();
390+
if (!string.IsNullOrEmpty(loaded))
404391
{
405-
var loaded = outBuffer.ToString();
406-
if (!string.IsNullOrEmpty(loaded))
407-
{
408-
return loaded;
409-
}
410-
else
411-
{
412-
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Can't load null or empty result "
413-
+ $"pri {source} in uwp location {Package.Location}", new NullReferenceException());
414-
return string.Empty;
415-
}
392+
return loaded;
416393
}
417394
else
418395
{
419-
var e = Marshal.GetExceptionForHR((int)hResult);
420-
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Load pri failed {source} with HResult {hResult} and location {Package.Location}", e);
396+
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Can't load null or empty result "
397+
+ $"pri {source} in uwp location {Package.Location}", new NullReferenceException());
421398
return string.Empty;
422399
}
423400
}
424401
else
425402
{
426-
return resourceReference;
403+
var e = Marshal.GetExceptionForHR((int)hResult);
404+
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Load pri failed {source} with HResult {hResult} and location {Package.Location}", e);
405+
return string.Empty;
427406
}
428407
}
429408

409+
public string FormattedPriReferenceValue(string packageName, string rawPriReferenceValue)
410+
{
411+
const string prefix = "ms-resource:";
412+
413+
if (string.IsNullOrWhiteSpace(rawPriReferenceValue) || !rawPriReferenceValue.StartsWith(prefix))
414+
return rawPriReferenceValue;
415+
416+
string key = rawPriReferenceValue.Substring(prefix.Length);
417+
if (key.StartsWith("//"))
418+
return $"{prefix}{key}";
419+
420+
if (!key.StartsWith("/"))
421+
{
422+
key = $"/{key}";
423+
}
424+
425+
if (!key.ToLower().Contains("resources"))
426+
{
427+
key = $"/Resources{key}";
428+
}
429+
430+
return $"{prefix}//{packageName}{key}";
431+
}
430432

431433
internal string LogoUriFromManifest(IAppxManifestApplication app)
432434
{

0 commit comments

Comments
 (0)