Files
ucvl-zonghengjia-open-source/windows/DashboardInteractionSelfTest.cs
T

140 lines
7.4 KiB
C#
Raw Normal View History

using System.Text.Json;
namespace UCVL.VPNPlus;
internal static class DashboardInteractionSelfTest
{
internal static int Run()
{
var checks = new Dictionary<string, bool>();
string? error = null;
try
{
using var form = new MainForm(preview: true, uiOnly: true) { Opacity = 0, ShowInTaskbar = false };
foreach (var check in form.CheckDashboardInteraction()) checks.Add(check.Key, check.Value);
}
catch (Exception exception) { error = exception.ToString(); }
var passed = error is null && checks.Count >= 15 && checks.Values.All(value => value);
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "dashboard-interaction-self-test.json"),
JsonSerializer.Serialize(new { passed, checks, error, scope = "owned synthetic WinForms fixture; no physical user-input or production acceptance claim",
network_modified = false, installed_client_modified = false, credentials_loaded = false }, new JsonSerializerOptions { WriteIndented = true }));
return passed ? 0 : 28;
}
}
internal sealed partial class MainForm
{
internal Dictionary<string, bool> CheckDashboardInteraction()
{
var checks = new Dictionary<string, bool>();
var profile = new Profile
{
DisplayName = "滚动测试", Email = "fixture@example.invalid",
Nodes = [new NodeInfo { Slug = "fixture", Name = "测试节点" }],
RouteModes = [new RouteModeInfo { Id = "smart", Name = "AI 智能代理" }]
};
Show();
ShowDashboard(profile);
_refreshTimer.Stop();
Size = MinimumSize;
PerformLayout();
LayoutDashboard();
Application.DoEvents();
int Limit() => Math.Max(0, _dashboardScrollBar.Maximum - _dashboardScrollBar.LargeChange + 1);
bool Wheel(Control target, int delta, Point? screenPoint = null)
{
var screen = screenPoint ?? _dashboardView.PointToScreen(new Point(20, 20));
var packedPoint = unchecked((int)((ushort)screen.X | (uint)(ushort)screen.Y << 16));
var packedDelta = unchecked((int)((uint)(ushort)(short)delta << 16));
var message = Message.Create(target.Handle, 0x20a, (IntPtr)packedDelta, (IntPtr)packedPoint);
return Application.FilterMessage(ref message);
}
checks["compact_view_has_real_scroll_range"] = _dashboardScrollBar.Visible && Limit() > 0;
foreach (var (name, control) in new (string, Control)[]
{
("card", _trafficCard), ("nested_metric", _uploadMetric), ("chart", _trafficChart),
("nested_label", _uploadLegend), ("closed_history_combo", _historyRangeCombo),
("closed_node_combo", _connectionModeCombo), ("closed_mode_combo", _routeCombo),
("button", _connectButton)
})
{
_dashboardScrollBar.Value = 0;
var oldSelection = control is ComboBox combo ? combo.SelectedIndex : -1;
checks["wheel_scrolls_over_" + name] = Wheel(control, -120) && _dashboardScrollBar.Value > 0
&& (control is not ComboBox selector || selector.SelectedIndex == oldSelection);
}
_dashboardScrollBar.Value = 0;
var halfHandled = Wheel(_trafficChart, -60);
var firstHalf = _dashboardScrollBar.Value;
checks["high_resolution_wheel_accumulates_half_notches"] = halfHandled && firstHalf == 0
&& Wheel(_trafficChart, -60) && _dashboardScrollBar.Value > 0;
_dashboardScrollBar.Value = Limit();
checks["wheel_bottom_clamped"] = Wheel(_dailyChart, -240) && _dashboardScrollBar.Value == Limit();
checks["wheel_up_returns_from_bottom"] = Wheel(_dailyChart, 120) && _dashboardScrollBar.Value < Limit();
_dashboardScrollBar.Value = Math.Min(137, Limit());
var previous = _dashboardScrollBar.Value;
UpdateProfile(profile, preserveSelections: true);
DisplayTraffic(1234, 5678, "隔离刷新");
LayoutDashboard();
checks["profile_and_metric_refresh_preserve_scroll"] = _dashboardScrollBar.Value == previous && _dashboardBody.Top == -previous;
ClientSize = new Size(ClientSize.Width, ClientSize.Height + ScaleLogical(20));
LayoutDashboard();
checks["resize_preserves_valid_scroll"] = _dashboardScrollBar.Value == Math.Min(previous, Limit());
_dashboardScrollBar.Value = Limit();
checks["footer_reachable_without_horizontal_scroll"] = !_dashboardView.HorizontalScroll.Visible
&& _footer.RectangleToScreen(_footer.ClientRectangle).Bottom <= _dashboardView.RectangleToScreen(_dashboardView.ClientRectangle).Bottom;
previous = _dashboardScrollBar.Value;
checks["header_pointer_does_not_scroll_dashboard"] = !Wheel(_trafficChart, -120, _brandHeader.PointToScreen(new Point(20, 20)))
&& _dashboardScrollBar.Value == previous;
using (var other = new Form())
checks["other_window_messages_not_captured"] = !Wheel(other, -120) && _dashboardScrollBar.Value == previous;
_dashboardScrollBar.Value = 0;
_historyRangeCombo.DroppedDown = true;
checks["open_combo_keeps_native_wheel_navigation"] = !Wheel(_historyRangeCombo, -120) && _dashboardScrollBar.Value == 0;
_historyRangeCombo.DroppedDown = false;
checks["dashboard_reuses_full_login_artwork"] = _dashboardView.LoginArtwork && _dashboardBody.ParentArtwork;
checks["card_surface_is_half_transparent"] = CardPanel.SurfaceAlpha == 128;
checks["whole_content_composition_disabled"] = !_content.ComposeChildren;
checks["metric_dirty_refresh_clears_previous_digits"] = CheckMetricRefreshPaint();
ShowLogin();
checks["hidden_dashboard_does_not_consume_login_wheel"] = !Wheel(_loginText, -120);
RequestExit();
return checks;
}
private static bool CheckMetricRefreshPaint()
{
// Repaint only an owned control into a deliberately dirty backing
// surface. This checks the real WM_PRINTCLIENT path, not OnPaint alone.
using var parent = new ScenicPanel { LoginArtwork = true, Size = new Size(700, 500) };
using var card = new CardPanel { Bounds = new Rectangle(30, 150, 500, 170), MetricBand = true };
using var metric = new MetricCard("本次代理流量", DashboardIcon.Document)
{ Embedded = true, Bounds = new Rectangle(30, 40, 250, 85) };
parent.Controls.Add(card);
card.Controls.Add(metric);
_ = parent.Handle; _ = card.Handle; _ = metric.Handle;
using var actual = new Bitmap(metric.Width, metric.Height);
void Print()
{
using var graphics = Graphics.FromImage(actual);
var hdc = graphics.GetHdc();
try { ChromeNative.SendMessage(metric.Handle, 0x318, hdc, (IntPtr)(0x4 | 0x8)); }
finally { graphics.ReleaseHdc(hdc); }
}
metric.SetBytes(1_073_741_824, hint: "本次连接累计");
Print();
using var expected = new Bitmap(actual);
for (var iteration = 0; iteration < 5; iteration++)
{
metric.SetBytes(999_999_999_999, hint: "旧数据应清除");
Print();
metric.SetBytes(1_073_741_824, hint: "本次连接累计");
Print();
for (var y = 0; y < actual.Height; y++)
for (var x = 0; x < actual.Width; x++)
if (actual.GetPixel(x, y) != expected.GetPixel(x, y)) return false;
}
return true;
}
}