完善 XPath 定位与控件监控体验
- 增加简明到深入的 XPath 定位教程 - 保留增量控件树刷新与可编辑 Path - 完善悬浮条条件选择和诊断 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using pacs_win_uia.Models;
|
||||
using pacs_win_uia.Services;
|
||||
|
||||
namespace PacsWinUia.RegressionTests
|
||||
{
|
||||
internal static class PersistenceTests
|
||||
{
|
||||
public static void Register(IList<KeyValuePair<string, Action>> tests)
|
||||
{
|
||||
tests.Add(new KeyValuePair<string, Action>("Store saves XPath without dynamic UIA fields and copies flags", StoreXPath));
|
||||
tests.Add(new KeyValuePair<string, Action>("Store reads legacy UiaPath definitions", StoreLegacy));
|
||||
tests.Add(new KeyValuePair<string, Action>("Settings missing refresh interval defaults to 300", SettingsDefault));
|
||||
tests.Add(new KeyValuePair<string, Action>("Floating JSON missing defaults differs from explicit disabled", FloatingDefaults));
|
||||
}
|
||||
|
||||
private static void StoreXPath()
|
||||
{
|
||||
using (var directory = new TemporaryDirectory())
|
||||
{
|
||||
string path = directory.File("control-monitors.json");
|
||||
var definition = new ControlMonitorDefinition
|
||||
{
|
||||
Id = "11111111-1111-1111-1111-111111111111",
|
||||
ControlPath = "/Window[@Name='设置']/Button[@Name='保存']",
|
||||
ContentKey = "xpath_save",
|
||||
SourceName = "Uia",
|
||||
RuntimeId = "dynamic-runtime-id",
|
||||
Value = "live-value-must-not-persist",
|
||||
UiaPathFromInnerWindow = true,
|
||||
Disabled = true,
|
||||
UiaPath = new List<ControlLocatorSegment>
|
||||
{
|
||||
new ControlLocatorSegment { ControlType = "ControlType.Button", Name = "stale legacy", Index = 4 }
|
||||
},
|
||||
ProcessName = "fixture",
|
||||
WindowClass = "RootClass",
|
||||
WindowTitleKeyword = "设置"
|
||||
};
|
||||
new ControlMonitorStore(path).Save(new[] { definition });
|
||||
string json = File.ReadAllText(path, Encoding.UTF8);
|
||||
Check.True(json.IndexOf("dynamic-runtime-id", StringComparison.Ordinal) < 0, "RuntimeId leaked into JSON");
|
||||
Check.True(json.IndexOf("live-value-must-not-persist", StringComparison.Ordinal) < 0, "Value leaked into JSON");
|
||||
Check.True(json.IndexOf("\"UiaPath\":", StringComparison.Ordinal) < 0, "UiaPath must not be persisted with XPath");
|
||||
Check.True(json.IndexOf("UiaPathFromInnerWindow", StringComparison.Ordinal) < 0, "Obsolete inner-window flag persisted");
|
||||
Check.True(json.IndexOf("Disabled", StringComparison.Ordinal) >= 0, "Disabled flag missing");
|
||||
IList<ControlMonitorDefinition> loaded = new ControlMonitorStore(path).Load();
|
||||
Check.Equal(1, loaded.Count, "Saved definition count");
|
||||
Check.Equal(definition.ControlPath, loaded[0].ControlPath, "XPath roundtrip");
|
||||
Check.True(loaded[0].Disabled && !loaded[0].Enabled, "Disabled flag roundtrip");
|
||||
Check.True(!loaded[0].UiaPathFromInnerWindow, "Obsolete inner-window flag survived XPath save");
|
||||
Check.Equal<string>(null, loaded[0].RuntimeId, "RuntimeId must remain runtime-only");
|
||||
Check.Equal<string>(null, loaded[0].Value, "Value must remain runtime-only");
|
||||
Check.True(loaded[0].UiaPath == null || loaded[0].UiaPath.Count == 0, "XPath must not restore stale UiaPath");
|
||||
}
|
||||
}
|
||||
|
||||
private static void StoreLegacy()
|
||||
{
|
||||
using (var directory = new TemporaryDirectory())
|
||||
{
|
||||
string path = directory.File("legacy.json");
|
||||
string json = "{\"Version\":1,\"Items\":[{"
|
||||
+ "\"Id\":\"22222222-2222-2222-2222-222222222222\","
|
||||
+ "\"ControlPath\":\"[Uia] Window:设置 > Button:保存\","
|
||||
+ "\"ContentKey\":\"legacy_save\","
|
||||
+ "\"Source\":\"Uia\",\"UiaPathFromInnerWindow\":true,\"Disabled\":true,"
|
||||
+ "\"UiaPath\":[{\"ControlType\":\"ControlType.Window\",\"Name\":\"设置\",\"Index\":0},"
|
||||
+ "{\"ControlType\":\"ControlType.Button\",\"Name\":\"保存\",\"Index\":0}]"
|
||||
+ "}]}";
|
||||
File.WriteAllText(path, json, new UTF8Encoding(false));
|
||||
IList<ControlMonitorDefinition> loaded = new ControlMonitorStore(path).Load();
|
||||
Check.Equal(1, loaded.Count, "Legacy definition count");
|
||||
ControlMonitorDefinition definition = loaded[0];
|
||||
Check.Equal("[Uia] Window:设置 > Button:保存", definition.ControlPath, "Legacy display path");
|
||||
Check.True(definition.Disabled && !definition.Enabled, "Legacy disabled flag");
|
||||
Check.True(ControlMonitorRules.ValidateField(definition, "ControlPath") != null, "Old path needs an editable validation error");
|
||||
bool rejected = false;
|
||||
try { new ControlMonitorStore(path).Save(loaded); }
|
||||
catch (InvalidDataException) { rejected = true; }
|
||||
Check.True(rejected, "Saving old path silently succeeded");
|
||||
Check.Equal(json, File.ReadAllText(path, Encoding.UTF8), "Failed save overwrote old file");
|
||||
definition.ControlPath = "/Window[1]//Button[@AutomationId='save']";
|
||||
new ControlMonitorStore(path).Save(loaded);
|
||||
Check.Equal(definition.ControlPath, new ControlMonitorStore(path).Load()[0].ControlPath, "Corrected XPath did not persist");
|
||||
}
|
||||
}
|
||||
|
||||
private static void FloatingDefaults()
|
||||
{
|
||||
using (var directory = new TemporaryDirectory())
|
||||
{
|
||||
string preferred = directory.File("floating-defaults.json");
|
||||
File.WriteAllText(preferred,
|
||||
"{\"Version\":1,\"FloatingButton\":{\"Bars\":[{\"Name\":\"missing-defaults\",\"Buttons\":[{\"Label\":\"test\"}]},{\"Name\":\"disabled\",\"Enabled\":false,\"Buttons\":[{\"Label\":\"off\",\"Enabled\":false}]}]}}",
|
||||
new UTF8Encoding(false));
|
||||
AppSettings settings = new AppSettingsStore(preferred, directory.File("fallback.json")).Load();
|
||||
Check.Equal(2, settings.FloatingButton.Bars.Count, "JSON bars disappeared");
|
||||
FloatingBarDefinition first = settings.FloatingButton.Bars[0];
|
||||
Check.True(first.Enabled && first.RequireWindowVisible, "Missing bar defaults were not initialized");
|
||||
Check.True(first.Buttons[0].Enabled, "Missing button enabled default");
|
||||
Check.True(!settings.FloatingButton.Bars[1].Enabled && !settings.FloatingButton.Bars[1].Buttons[0].Enabled,
|
||||
"Explicit disabled flags must not be overridden");
|
||||
new AppSettingsStore(preferred, directory.File("fallback.json")).Save(settings);
|
||||
Check.True(!new AppSettingsStore(preferred, directory.File("fallback.json")).Load().FloatingButton.Bars[1].Enabled,
|
||||
"Disabled bar changed after roundtrip");
|
||||
}
|
||||
}
|
||||
|
||||
private static void SettingsDefault()
|
||||
{
|
||||
using (var directory = new TemporaryDirectory())
|
||||
{
|
||||
string preferred = directory.File("settings.json");
|
||||
string fallback = directory.File("fallback-settings.json");
|
||||
File.WriteAllText(preferred, "{\"Version\":1,\"Ui\":{\"TreeMaxDepth\":12}}", new UTF8Encoding(false));
|
||||
AppSettings settings = new AppSettingsStore(preferred, fallback).Load();
|
||||
Check.Equal(AppSettings.DefaultRefreshIntervalMs, settings.Ui.TreeRefreshIntervalMs, "Missing Ui refresh interval");
|
||||
Check.True(settings.FloatingButton != null, "Missing FloatingButton section default");
|
||||
Check.Equal(AppSettings.DefaultRefreshIntervalMs, settings.FloatingButton.RefreshIntervalMs, "Missing floating refresh interval");
|
||||
Check.True(settings.Commands != null && settings.Ocr != null && settings.Read != null && settings.Screenshot != null,
|
||||
"Missing settings sections were not restored");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user