Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

using System;
using System.Collections.Generic;
using System.Threading;

using Castle.Core.Internal;
using Microsoft.Plugin.Program.Programs;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Plugin.Common.Win32;

namespace Microsoft.Plugin.Program.UnitTests.Programs
{
Expand Down Expand Up @@ -93,5 +97,65 @@ public void PowerToysRunShouldNotAddInvalidAppWhenIndexingUWPApplications()
// Assert
Assert.AreEqual(0, applications.Length);
}

[TestMethod]
public void UwpResultActionShouldHandleEmptyUserModelIdGracefully()
{
// Arrange
string displayName = "PackagedApp";
string emptyUserModelId = string.Empty;
string logoUri = "Assets\\Logo.png";
string description = "Description";
string backgroundColor = "transparent";
string entryPoint = string.Empty;

StringMatcher.Instance = new StringMatcher();

var manifestApp = new Mock<IAppxManifestApplication>();
manifestApp.Setup(x => x.GetAppUserModelId(out emptyUserModelId)).Returns(HRESULT.S_OK);
manifestApp.Setup(x => x.GetStringValue("DisplayName", out displayName)).Returns(HRESULT.S_OK);
manifestApp.Setup(x => x.GetStringValue("Description", out description)).Returns(HRESULT.S_OK);
manifestApp.Setup(x => x.GetStringValue("BackgroundColor", out backgroundColor)).Returns(HRESULT.S_OK);
manifestApp.Setup(x => x.GetStringValue("EntryPoint", out entryPoint)).Returns(HRESULT.S_OK);
manifestApp.Setup(x => x.GetStringValue("Square44x44Logo", out logoUri)).Returns(HRESULT.S_OK);

var package = new UWP(PackagedApp)
{
Location = PackagedApp.InstalledLocation,
LocationLocalized = PackagedApp.InstalledLocation,
Version = UWP.PackageVersion.Windows10,
};

var application = new UWPApplication(manifestApp.Object, package)
{
DisplayName = displayName,
Description = description,
UserModelId = string.Empty,
};
using var showMessageCalled = new ManualResetEventSlim();
var api = new Mock<IPublicAPI>();
api.Setup(x => x.ShowMsg(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
.Callback<string, string, string, bool>((_, _, _, _) => showMessageCalled.Set());

// Assert
Assert.AreEqual(displayName, application.DisplayName);
Assert.AreEqual(description, application.Description);

// Act
var result = application.Result(displayName, string.Empty, api.Object);

// Assert
Assert.IsNotNull(result);
var actionResult = result.Action(new ActionContext());
Assert.IsTrue(actionResult);
Assert.IsTrue(showMessageCalled.Wait(TimeSpan.FromSeconds(5)));
api.Verify(
x => x.ShowMsg(
It.IsAny<string>(),
It.Is<string>(message => message.Contains(displayName, StringComparison.Ordinal)),
string.Empty,
It.IsAny<bool>()),
Times.Once);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,23 @@ await Task.Run(() =>
{
try
{
if (string.IsNullOrEmpty(UserModelId))
{
ProgramLogger.Exception($"UserModelId is null or empty for {DisplayName}", new InvalidOperationException(), GetType(), queryArguments);
var name = "Plugin: " + Properties.Resources.wox_plugin_program_plugin_name;
var message = $"{Properties.Resources.powertoys_run_plugin_program_uwp_failed}: {DisplayName}";
api?.ShowMsg(name, message, string.Empty);
return;
Comment on lines +214 to +220
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a regression test in src/modules/launcher/Plugins/Microsoft.Plugin.Program.UnitTests/Programs/UWPTests.cs that exercises launching a UWPApplication with an empty UserModelId.

The test verifies the result action returns successfully and reports the launch failure through ShowMsg instead of throwing. I also rebuilt Microsoft.Plugin.Program and Microsoft.Plugin.Program.UnitTests, then ran the full Microsoft.Plugin.Program.UnitTests assembly locally (96 passing).

}

appManager.ActivateApplication(UserModelId, queryArguments, noFlags, out var unusedPid);
}
catch (Exception ex)
{
ProgramLogger.Exception($"Unable to launch UWP {DisplayName}", ex, MethodBase.GetCurrentMethod().DeclaringType, queryArguments);
ProgramLogger.Exception($"Unable to launch UWP {DisplayName}", ex, GetType(), queryArguments);
var name = "Plugin: " + Properties.Resources.wox_plugin_program_plugin_name;
var message = $"{Properties.Resources.powertoys_run_plugin_program_uwp_failed}: {DisplayName}";
api.ShowMsg(name, message, string.Empty);
api?.ShowMsg(name, message, string.Empty);
}
}).ConfigureAwait(false);
}
Expand Down Expand Up @@ -284,7 +293,7 @@ private bool IfApplicationCanRunElevated()
}
catch (Exception e)
{
ProgramLogger.Exception($"Unable to parse manifest file for {DisplayName}", e, MethodBase.GetCurrentMethod().DeclaringType, manifest);
ProgramLogger.Exception($"Unable to parse manifest file for {DisplayName}", e, GetType(), manifest);
}
}
}
Expand Down
Loading