diff --git a/src/Titano.Setup/Engine/SetupRegistry.cs b/src/Titano.Setup/Engine/SetupRegistry.cs index e1800d36..0810cf49 100644 --- a/src/Titano.Setup/Engine/SetupRegistry.cs +++ b/src/Titano.Setup/Engine/SetupRegistry.cs @@ -102,12 +102,17 @@ internal static partial class SetupRegistry /// /// Removes the sign-in entries of the client and the Uploader (HKCU\…\Run, written by the programs /// themselves) that start one of ( of the files an - /// installation lists); another copy's entries, and other programs' values, stay. + /// installation lists), with the on/off state Task Manager keeps for them (); another + /// copy's entries, and other programs' values, stay. /// public static void DeleteStartupEntries(IReadOnlySet programs) => MoveStartupEntries(programs, NoPrograms); - /// in ; returns the names removed. - public static IReadOnlyList DeleteStartupEntries(RegistryKey run, IReadOnlySet programs) => MoveStartupEntries(run, programs, NoPrograms); + /// + /// in and ; + /// returns the names removed. + /// + public static IReadOnlyList DeleteStartupEntries(RegistryKey run, RegistryKey? approval, IReadOnlySet programs) => + MoveStartupEntries(run, approval, programs, NoPrograms); /// /// After an upgrade: a sign-in entry of the client or the Uploader that starts one of @@ -118,14 +123,18 @@ internal static partial class SetupRegistry public static void MoveStartupEntries(IReadOnlySet dropped, IReadOnlyDictionary programs) { using var run = Registry.CurrentUser.OpenSubKey(StartupCommand.RunKey, writable: true); - if (run is not null) MoveStartupEntries(run, dropped, programs); + if (run is null) return; + using var approval = Registry.CurrentUser.OpenSubKey(StartupApproval.RunKey, writable: true); + MoveStartupEntries(run, approval, dropped, programs); } /// /// in - /// ; returns the names moved or removed. + /// ; a removed entry's on/off state goes from too, a moved one keeps + /// it. Returns the names moved or removed. /// - public static IReadOnlyList MoveStartupEntries(RegistryKey run, IReadOnlySet dropped, IReadOnlyDictionary programs) + public static IReadOnlyList MoveStartupEntries(RegistryKey run, RegistryKey? approval, IReadOnlySet dropped, + IReadOnlyDictionary programs) { var changed = new List(); foreach (var name in (ReadOnlySpan)[StartupCommand.ClientValue, StartupCommand.UploaderValue]) @@ -140,6 +149,8 @@ internal static partial class SetupRegistry else { run.DeleteValue(name, throwOnMissingValue: false); + // Its Task Manager state goes with it: nothing of the entry stays behind. + approval?.DeleteValue(name, throwOnMissingValue: false); Titano.Core.Diagnostics.Log.Info("setup", $"Sign-in entry '{name}' removed"); } changed.Add(name); diff --git a/tests/Titano.App.Tests/Setup/SetupEngineTests.cs b/tests/Titano.App.Tests/Setup/SetupEngineTests.cs index 385ed7ef..2528510c 100644 --- a/tests/Titano.App.Tests/Setup/SetupEngineTests.cs +++ b/tests/Titano.App.Tests/Setup/SetupEngineTests.cs @@ -18,6 +18,9 @@ public sealed class SetupEngineTests(TestContext context) { private const string TestsKey = @"Software\Titano.Tests"; + /// The StartupApproved state Task Manager writes for an entry it turned off. + private static readonly byte[] TurnedOff = [0x03, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + [Theory] [Case(@"""C:\Program Files\Titano\Titano.Server.exe"" --service", @"C:\Program Files\Titano\Titano.Server.exe")] [Case(@"C:\Program Files\Titano\Titano.Server.exe --service", @"C:\Program Files\Titano\Titano.Server.exe")] @@ -158,7 +161,8 @@ public sealed class SetupEngineTests(TestContext context) var keyPath = TestsKey + @"\" + Guid.NewGuid().ToString("N"); try { - using (var run = Registry.CurrentUser.CreateSubKey(keyPath, writable: true)) + using (var run = Registry.CurrentUser.CreateSubKey(keyPath + @"\Run", writable: true)) + using (var approval = Registry.CurrentUser.CreateSubKey(keyPath + @"\Approved", writable: true)) { run.SetValue("Titano Uploader", $"\"{Path.Combine(folder, "Uploader", "Titano.Uploader.exe")}\" --tray"); // A portable Titano unpacked in a subfolder of the same folder: not a program this installation lists. @@ -166,10 +170,14 @@ public sealed class SetupEngineTests(TestContext context) // Another program in the same folder, and a copy in a sibling folder. run.SetValue("Other", $"\"{Path.Combine(folder, "Other.exe")}\" --background"); run.SetValue("Sibling", $"\"{folder} 2\\Titano.Uploader.exe\""); + // Both Titano entries were turned off in Task Manager. + approval.SetValue("Titano Uploader", TurnedOff, RegistryValueKind.Binary); + approval.SetValue("Titano", TurnedOff, RegistryValueKind.Binary); var programs = CommandProgram.FullPaths(folder, ["Titano.exe", "Uploader/Titano.Uploader.exe", "Other.exe"]); - Assert.SequenceEqual(["Titano Uploader"], SetupRegistry.DeleteStartupEntries(run, programs)); + Assert.SequenceEqual(["Titano Uploader"], SetupRegistry.DeleteStartupEntries(run, approval, programs)); Assert.SequenceEqual(["Other", "Sibling", "Titano"], run.GetValueNames().Order(StringComparer.Ordinal)); + Assert.SequenceEqual(["Titano"], approval.GetValueNames()); } } finally @@ -194,21 +202,25 @@ public sealed class SetupEngineTests(TestContext context) Assert.Equal(Path.Combine(folder, "Titano.exe"), programs["Titano"]); try { - using (var run = Registry.CurrentUser.CreateSubKey(keyPath, writable: true)) + using (var run = Registry.CurrentUser.CreateSubKey(keyPath + @"\Run", writable: true)) + using (var approval = Registry.CurrentUser.CreateSubKey(keyPath + @"\Approved", writable: true)) { run.SetValue("Titano", $"\"{Path.Combine(folder, "Titano.old.exe")}\" --minimized"); run.SetValue("Titano Uploader", $"\"{Path.Combine(folder, "Titano.Uploader.exe")}\" --tray"); run.SetValue("Other", $"\"{Path.Combine(folder, "Other.exe")}\" --background"); + approval.SetValue("Titano", TurnedOff, RegistryValueKind.Binary); + approval.SetValue("Titano Uploader", TurnedOff, RegistryValueKind.Binary); var dropped = CommandProgram.FullPaths(folder, ["Titano.old.exe", "Titano.Uploader.exe", "Other.exe"]); - Assert.SequenceEqual(["Titano", "Titano Uploader"], SetupRegistry.MoveStartupEntries(run, dropped, programs)); - // The new client finds its entry on, with its option; the Uploader no longer starts; other values stay. + Assert.SequenceEqual(["Titano", "Titano Uploader"], SetupRegistry.MoveStartupEntries(run, approval, dropped, programs)); + // The new client finds its entry with its option, still turned off; the Uploader no longer starts; other values stay. Assert.Equal($"\"{Path.Combine(folder, "Titano.exe")}\" --minimized", run.GetValue("Titano")); Assert.SequenceEqual(["Other", "Titano"], run.GetValueNames().Order(StringComparer.Ordinal)); + Assert.SequenceEqual(["Titano"], approval.GetValueNames()); // An entry that starts another copy is not this upgrade's to move. var other = $"\"{Path.Combine(folder + " 2", "Titano.old.exe")}\" --minimized"; run.SetValue("Titano", other); - Assert.Empty(SetupRegistry.MoveStartupEntries(run, dropped, programs)); + Assert.Empty(SetupRegistry.MoveStartupEntries(run, approval, dropped, programs)); Assert.Equal(other, run.GetValue("Titano")); } }