|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Erik Darling, Darling Data LLC |
| 3 | + * |
| 4 | + * This file is part of the SQL Server Performance Monitor. |
| 5 | + * |
| 6 | + * Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.IO; |
| 11 | +using System.Runtime.CompilerServices; |
| 12 | +using System.Text.RegularExpressions; |
| 13 | +using PerformanceMonitor.Ui; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace Darling.Tests; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// #3577 (arm B) as it lands in the Darling viewer. The loader, the key list, the rewriter and the |
| 20 | +/// regeneration are shared <c>PerformanceMonitor.Ui</c> code and are pinned once, in |
| 21 | +/// <c>Lite.Tests/ThemeColorOverrideTests</c>, against the theme files of BOTH apps. What is the viewer's own |
| 22 | +/// is where its file lives and where it does not: <c>theme-overrides.json</c> sits beside |
| 23 | +/// <c>viewer-settings.json</c> under the viewer's per-user directory, and the preference is LOCAL to the |
| 24 | +/// machine — it never goes to the store or the control plane, because a color is a preference of the |
| 25 | +/// person at this screen and not a fact about the fleet. A viewer seat on another machine keeps its own. |
| 26 | +/// </summary> |
| 27 | +public sealed class ViewerThemeColorOverridesTests |
| 28 | +{ |
| 29 | + private static readonly string ViewerDir = Path.Combine("Darling", "PerformanceMonitor.Darling.Viewer"); |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void TheOverridesFile_SitsBesideViewerSettings_UnderTheViewersOwnDirectory() |
| 33 | + { |
| 34 | + var app = File.ReadAllText(Path.Combine(RepoRoot(), ViewerDir, "App.xaml.cs")); |
| 35 | + |
| 36 | + Assert.Contains("Path.GetDirectoryName(ViewerAppSettingsStore.DefaultFilePath())", app, StringComparison.Ordinal); |
| 37 | + Assert.Contains("ThemeColorOverrides.FileName", app, StringComparison.Ordinal); |
| 38 | + Assert.Equal("theme-overrides.json", ThemeColorOverrides.FileName); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// The preference is not a settings-model property, not a store column and not a control-plane knob. |
| 43 | + /// <c>ViewerAppSettings</c> keeps <c>ColorTheme</c> (which theme) and nothing about the colors inside |
| 44 | + /// it; the service and storage projects never see the word. |
| 45 | + /// </summary> |
| 46 | + [Fact] |
| 47 | + public void TheColorOverrides_AreNotPersistedInTheSettingsModel_TheStore_OrTheControlPlane() |
| 48 | + { |
| 49 | + var root = RepoRoot(); |
| 50 | + |
| 51 | + var settingsModel = File.ReadAllText(Path.Combine(root, ViewerDir, "ViewerAppSettings.cs")); |
| 52 | + Assert.DoesNotContain("ThemeColorOverride", settingsModel, StringComparison.Ordinal); |
| 53 | + Assert.DoesNotContain("theme-overrides", settingsModel, StringComparison.Ordinal); |
| 54 | + |
| 55 | + foreach (var project in new[] { "PerformanceMonitor.Darling.Storage", "PerformanceMonitor.Darling.Service" }) |
| 56 | + { |
| 57 | + var directory = Path.Combine(root, "Darling", project); |
| 58 | + foreach (var file in Directory.EnumerateFiles(directory, "*.cs", SearchOption.AllDirectories)) |
| 59 | + { |
| 60 | + if (file.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}", StringComparison.Ordinal)) |
| 61 | + { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + var text = File.ReadAllText(file); |
| 66 | + Assert.False(text.Contains("ThemeColorOverride", StringComparison.Ordinal) || text.Contains("theme-overrides", StringComparison.Ordinal), |
| 67 | + $"{Path.GetRelativePath(root, file)} mentions the viewer's color overrides; they are viewer-local and must stay out of the store and the service."); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// The viewer's Settings window hosts the same shared panel Lite does, beneath its theme combo, and the |
| 74 | + /// panel is the ONLY colors surface — no viewer-only copy of the rows that could drift from Lite's. |
| 75 | + /// </summary> |
| 76 | + [Fact] |
| 77 | + public void TheViewerSettingsWindow_HostsTheSharedPanel_AndNoPrivateCopy() |
| 78 | + { |
| 79 | + var xaml = File.ReadAllText(Path.Combine(RepoRoot(), ViewerDir, "SettingsWindow.xaml")); |
| 80 | + |
| 81 | + Assert.Single(Regex.Matches(xaml, "<ui:ThemeColorsPanel")); |
| 82 | + Assert.True(xaml.IndexOf("x:Name=\"ColorThemeCombo\"", StringComparison.Ordinal) < xaml.IndexOf("<ui:ThemeColorsPanel", StringComparison.Ordinal)); |
| 83 | + Assert.DoesNotContain("AccentForegroundColor", xaml, StringComparison.Ordinal); // no hand-rolled rows |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary>The viewer's own three theme files are the ones the shared regeneration reads — embedded under the loader's names.</summary> |
| 87 | + [Fact] |
| 88 | + public void TheViewerProject_EmbedsItsThemeText() |
| 89 | + { |
| 90 | + var csproj = File.ReadAllText(Path.Combine(RepoRoot(), ViewerDir, "PerformanceMonitor.Darling.Viewer.csproj")); |
| 91 | + |
| 92 | + Assert.Contains("<EmbeddedResource Include=\"Themes\\*.xaml\" LogicalName=\"Themes/%(Filename).xaml\" />", csproj, StringComparison.Ordinal); |
| 93 | + foreach (var theme in ThemeColorOverrides.ThemeNames) |
| 94 | + { |
| 95 | + Assert.True(File.Exists(Path.Combine(RepoRoot(), ViewerDir, "Themes", theme + "Theme.xaml")), |
| 96 | + $"{ThemeManager.ThemeTextResourceName(theme)} names a file the viewer does not ship."); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static string RepoRoot([CallerFilePath] string thisFile = "") |
| 101 | + => Path.GetFullPath(Path.Combine(Path.GetDirectoryName(thisFile)!, "..", "..")); |
| 102 | +} |
0 commit comments