Files
PowerToys/src/modules/cmdpal/Tests/Microsoft.CmdPal.UI.ViewModels.UnitTests/CmdPalProtocolActivationTests.cs
Jiří Polášek 5b7b91c9de CmdPal: Add extension gallery deep links (#49780)
## Summary of the Pull Request


This PR extends the x-cmdpal:// protocol to allow opening the Extension
Gallery and individual Extension Gallery items.

Extension developers can use this to link users directly from their
websites to the gallery, shortening the path to discovering and
installing Command Palette extensions.

- Adds typed x-cmdpal:// URI parsing in CmdPalProtocolActivation.
- Adds gallery and specific-extension deep links.
    - `x-cmdpal://extensions/gallery`
    - `x-cmdpal://extensions/gallery/{extension-id}`
- Opens extension details after the gallery loads.
- Integrate into existing gallery navigation to prevent garbage nav
stack.

Pictures? Pictures!


https://github.com/user-attachments/assets/d4d55b2a-2624-4a93-bdf7-55348b044b76

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] Closes: #49770
<!-- - [ ] Closes: #yyy (add separate lines for additional resolved
issues) -->
- [ ] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
2026-08-09 14:15:20 -05:00

123 lines
4.2 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.CmdPal.UI.ViewModels.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.CmdPal.UI.ViewModels.UnitTests;
[TestClass]
public class CmdPalProtocolActivationTests
{
private readonly CmdPalProtocolActivation _protocolActivation = new();
[TestMethod]
public void TryParse_SettingsUri_ReturnsGeneralSettingsRoute()
{
Assert.IsTrue(_protocolActivation.TryParse(new Uri("x-cmdpal://settings"), out var result));
Assert.IsInstanceOfType<CmdPalProtocolRoute.OpenSettings>(result);
var message = ((CmdPalProtocolRoute.OpenSettings)result).Message;
Assert.AreEqual(string.Empty, message.SettingsPageTag);
Assert.IsNull(message.ExtensionGalleryId);
}
[TestMethod]
public void TryParse_GalleryUri_ReturnsGallerySettingsRoute()
{
Assert.IsTrue(_protocolActivation.TryParse(new Uri("x-cmdpal://extensions/gallery"), out var result));
Assert.IsInstanceOfType<CmdPalProtocolRoute.OpenSettings>(result);
var message = ((CmdPalProtocolRoute.OpenSettings)result).Message;
Assert.AreEqual("Gallery", message.SettingsPageTag);
Assert.IsNull(message.ExtensionGalleryId);
}
[TestMethod]
public void TryParse_GalleryExtensionUri_ReturnsDecodedExtensionId()
{
Assert.IsTrue(_protocolActivation.TryParse(
new Uri("X-CMDPAL://EXTENSIONS/GALLERY/sample%20extension?source=web"),
out var result));
Assert.IsInstanceOfType<CmdPalProtocolRoute.OpenSettings>(result);
var message = ((CmdPalProtocolRoute.OpenSettings)result).Message;
Assert.AreEqual("Gallery", message.SettingsPageTag);
Assert.AreEqual("sample extension", message.ExtensionGalleryId);
}
[TestMethod]
public void TryParseExtensionId_PreservesValidIdExactly()
{
Assert.IsTrue(_protocolActivation.TryParseExtensionId("sample extension", out var extensionId));
Assert.AreEqual("sample extension", extensionId);
}
[TestMethod]
public void TryParseExtensionId_RejectsNonCanonicalOrUnsafeId()
{
string[] invalidIds =
[
string.Empty,
" colors",
"colors ",
"sample/extension",
"sample\\extension",
"sample\nextension",
new('a', 257),
];
foreach (var invalidId in invalidIds)
{
Assert.IsFalse(_protocolActivation.TryParseExtensionId(invalidId, out var extensionId), invalidId);
Assert.AreEqual(string.Empty, extensionId);
}
}
[TestMethod]
public void TryParse_BackgroundUri_ReturnsBackgroundRoute()
{
Assert.IsTrue(_protocolActivation.TryParse(new Uri("x-cmdpal://background"), out var result));
Assert.IsInstanceOfType<CmdPalProtocolRoute.Background>(result);
}
[TestMethod]
public void TryParse_ReloadUri_ReturnsReloadRoute()
{
Assert.IsTrue(_protocolActivation.TryParse(new Uri("x-cmdpal://reload"), out var result));
Assert.IsInstanceOfType<CmdPalProtocolRoute.Reload>(result);
}
[DataTestMethod]
[DataRow("https://extensions/gallery/sample")]
[DataRow("x-cmdpal://settings/unknown")]
[DataRow("x-cmdpal://settings-extra")]
[DataRow("x-cmdpal://background-task")]
[DataRow("x-cmdpal://reload-anything")]
[DataRow("x-cmdpal://extensions/gallery/%20colors%20")]
[DataRow("x-cmdpal://extensions/gallery/sample%2Fextension")]
[DataRow("x-cmdpal://extensions/gallery/sample%5Cextension")]
[DataRow("x-cmdpal://extensions/gallery/sample%0Aextension")]
[DataRow("x-cmdpal://extensions/gallery/sample#fragment")]
public void TryParse_UnknownOrUnsafeUri_ReturnsFalse(string uri)
{
var parsed = _protocolActivation.TryParse(new Uri(uri), out var result);
Assert.IsFalse(parsed);
Assert.IsNull(result);
}
[TestMethod]
public void TryParse_NullUri_ReturnsFalse()
{
var parsed = _protocolActivation.TryParse(null, out var result);
Assert.IsFalse(parsed);
Assert.IsNull(result);
}
}