254 lines
16 KiB
C#
254 lines
16 KiB
C#
using System.Diagnostics;
|
|
using System.Text.Json;
|
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
internal static class StartupSelfTest
|
|
{
|
|
// Runs real WinForms first-show/resize/restore behavior, but never reads a
|
|
// login token, calls the platform API, starts a core or touches networking.
|
|
internal static int Run()
|
|
{
|
|
var report = new Dictionary<string, object>();
|
|
try
|
|
{
|
|
using (var control = new Control())
|
|
{
|
|
_ = control.Handle;
|
|
report["synchronization_context"] = SynchronizationContext.Current?.GetType().Name ?? "none";
|
|
async Task OldBlockingPattern() { await Task.Delay(100); }
|
|
var old = OldBlockingPattern();
|
|
report["old_sync_wait_would_block"] = !old.Wait(350);
|
|
// Resume only this synthetic callback after the bounded probe.
|
|
Application.DoEvents();
|
|
}
|
|
var recoveryFinished = false;
|
|
var heartbeat = 0;
|
|
var visibleDuringRecovery = false;
|
|
var watch = Stopwatch.StartNew();
|
|
using var form = new MainForm(uiOnly: true, startupRecovery: async token =>
|
|
{
|
|
await Task.Delay(1400, token);
|
|
recoveryFinished = true;
|
|
});
|
|
form.TopMost = true; // Owned UI fixture only; never a production window policy.
|
|
using var timer = new System.Windows.Forms.Timer { Interval = 80 };
|
|
var passed = false;
|
|
timer.Tick += async (_, _) =>
|
|
{
|
|
try
|
|
{
|
|
heartbeat++;
|
|
if (!recoveryFinished && form.LoginFieldsVisibleForTest) visibleDuringRecovery = true;
|
|
if (recoveryFinished && heartbeat >= 22)
|
|
{
|
|
timer.Stop(); // Input probes resume through the normal UI message loop.
|
|
report["first_login_visible_while_recovering"] = visibleDuringRecovery;
|
|
report["login_ready_after_recovery"] = form.LoginReadyForTest;
|
|
report["registration_text_and_link_same_row"] = form.LoginRegistrationAlignedForTest;
|
|
var registrationDpiPass = true;
|
|
foreach (var dpi in new[] { 96, 120, 144, 168, 192, 240, 288 })
|
|
{
|
|
int S(int n) => (int)Math.Round(n * dpi / 96f);
|
|
// Geometry fixture only; not a claim of physical
|
|
// per-monitor rendering at these simulated DPIs.
|
|
var row = MainForm.LoginRegistrationBounds(S(440), dpi, new Size(S(92), S(18)), new Size(S(86), S(18)));
|
|
registrationDpiPass &= row.Prompt.Top == row.Link.Top && row.Prompt.Height == row.Link.Height
|
|
&& row.Prompt.Right < row.Link.Left && row.Prompt.Left >= S(28) && row.Link.Right <= S(412)
|
|
&& Math.Abs((row.Prompt.Left + row.Link.Right) / 2f - S(440) / 2f) <= 1
|
|
&& row.Prompt.Bottom <= S(306);
|
|
}
|
|
report["registration_geometry_100_to_300_percent"] = registrationDpiPass;
|
|
CheckLoginSurface(report);
|
|
report["dpi"] = form.DeviceDpi;
|
|
report["ui_heartbeat_ticks"] = heartbeat;
|
|
var chrome = form.WindowChromeForTest
|
|
?? throw new InvalidOperationException("Owned MainForm did not initialize window chrome");
|
|
report["login_caption_is_exit_not_minimize"] = chrome.LoginMode
|
|
&& chrome.MinimizeButton.Action == CaptionAction.Close
|
|
&& chrome.MinimizeButton.AccessibleName == "退出软件";
|
|
// A separate UI-only window exercises the actual login
|
|
// X -> RequestExit -> cleanup path, even during recovery.
|
|
// It never loads credentials, starts a core or changes networking.
|
|
using (var loginExitProbe = new MainForm(uiOnly: true,
|
|
startupRecovery: token => Task.Delay(Timeout.Infinite, token)))
|
|
{
|
|
var closed = false;
|
|
loginExitProbe.FormClosed += (_, _) => closed = true;
|
|
loginExitProbe.Show();
|
|
await Task.Delay(30);
|
|
var exitChrome = loginExitProbe.WindowChromeForTest;
|
|
var exitWasVisible = exitChrome is { LoginMode: true }
|
|
&& exitChrome.MinimizeButton.Action == CaptionAction.Close;
|
|
exitChrome?.MinimizeButton.PerformClick();
|
|
report["login_x_closes_software_during_recovery"] = exitWasVisible
|
|
&& closed && loginExitProbe.IsDisposed;
|
|
}
|
|
form.Activate();
|
|
// Caption-state fixture only, not an authenticated login.
|
|
// Keep the login card visible for the rendering checks.
|
|
chrome.SetLoginMode(false);
|
|
report["dashboard_caption_is_minimize_not_exit"] = !chrome.LoginMode
|
|
&& chrome.MinimizeButton.Action == CaptionAction.Minimize
|
|
&& chrome.MinimizeButton.AccessibleName == "最小化";
|
|
form.Close();
|
|
// After authentication, only explicit menu/installer
|
|
// exit may terminate the form; ordinary Close is blocked.
|
|
report["ordinary_close_blocked_without_hiding"] = form.Visible && !form.IsDisposed;
|
|
form.Hide(); // Synthetic tray-hidden state, not a close request.
|
|
form.ShowFromTray();
|
|
report["hidden_window_restored"] = form.LoginReadyForTest;
|
|
form.WindowState = FormWindowState.Minimized;
|
|
report["minimized_on_taskbar_without_exit"] = form.MinimizedOnTaskbarForTest;
|
|
report["production_minimized_tray_available"] = MainForm.ShouldShowTrayIcon(FormWindowState.Minimized, false);
|
|
report["production_restored_tray_available"] = MainForm.ShouldShowTrayIcon(FormWindowState.Normal, false);
|
|
report["offline_preview_never_publishes_tray"] = Enum.GetValues<FormWindowState>()
|
|
.All(state => !MainForm.ShouldShowTrayIcon(state, true));
|
|
form.ShowFromTray();
|
|
report["minimized_window_restored"] = form.WindowState == FormWindowState.Normal && form.LoginReadyForTest;
|
|
var minimizeButton = form.Controls.OfType<BrandHeaderPanel>().Single().Controls.OfType<CaptionButton>().Single();
|
|
var programmaticMinimize = await WindowChromeSelfTest.ProbeMinimizeInputAsync(form, minimizeButton, mouse: false, tray: true);
|
|
report["mainform_performclick_detail"] = programmaticMinimize;
|
|
report["mainform_performclick_hides_to_tray"] = programmaticMinimize.Passed && !form.IsDisposed;
|
|
form.ShowFromTray();
|
|
var mouseMinimize = await WindowChromeSelfTest.ProbeMinimizeInputAsync(form, minimizeButton, mouse: true, tray: true);
|
|
report["mainform_mouse_minimize_detail"] = mouseMinimize;
|
|
report["mainform_mouse_down_up_click_hides_to_tray"] = mouseMinimize.Passed && !form.IsDisposed;
|
|
form.ShowFromTray();
|
|
report["mainform_mouse_minimize_restores"] = form.WindowState == FormWindowState.Normal
|
|
&& form.ShowInTaskbar && form.LoginReadyForTest;
|
|
chrome.SetLoginMode(true);
|
|
report["return_to_login_restores_exit_caption"] = chrome.LoginMode
|
|
&& chrome.MinimizeButton.Action == CaptionAction.Close;
|
|
var originalSize = form.Size;
|
|
form.Size = form.MinimumSize;
|
|
report["minimum_size_login_visible"] = form.LoginFieldsVisibleForTest;
|
|
report["minimum_size_registration_aligned"] = form.LoginRegistrationAlignedForTest;
|
|
form.Size = originalSize;
|
|
using var screenshot = new Bitmap(form.Width, form.Height);
|
|
form.DrawToBitmap(screenshot, new Rectangle(Point.Empty, screenshot.Size));
|
|
screenshot.Save(Path.Combine(AppContext.BaseDirectory, "startup-login-test.png"));
|
|
passed = visibleDuringRecovery && form.LoginReadyForTest
|
|
&& report.Where(item => item.Value is bool).All(item => (bool)item.Value);
|
|
timer.Stop();
|
|
form.RequestExit();
|
|
}
|
|
else if (watch.Elapsed > TimeSpan.FromSeconds(12))
|
|
{
|
|
report["timeout"] = true;
|
|
timer.Stop(); form.RequestExit();
|
|
}
|
|
}
|
|
catch (Exception error) { report["error"] = error.ToString(); timer.Stop(); form.RequestExit(); }
|
|
};
|
|
form.Shown += (_, _) => timer.Start();
|
|
Application.Run(form);
|
|
report["passed"] = passed;
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "startup-self-test.json"),
|
|
JsonSerializer.Serialize(report, new JsonSerializerOptions { WriteIndented = true }));
|
|
return passed ? 0 : 20;
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "startup-self-test.json"), error.ToString());
|
|
return 21;
|
|
}
|
|
}
|
|
|
|
private static void CheckLoginSurface(Dictionary<string, object> report)
|
|
{
|
|
// Owned, off-screen paint fixture only. Never capture the desktop or
|
|
// use account values; these pixels are not production UI evidence.
|
|
using var parent = new ScenicPanel { LoginArtwork = true };
|
|
_ = parent.Handle;
|
|
var fixtureDpi = parent.DeviceDpi;
|
|
int S(int value) => (int)Math.Round(value * fixtureDpi / 96f);
|
|
Point P(int x, int y) => new(S(x), S(y));
|
|
parent.Size = new Size(S(900), S(640));
|
|
using var card = new RoundedCardPanel { Bounds = new Rectangle(S(230), S(100), S(440), S(374)) };
|
|
parent.Controls.Add(card);
|
|
_ = card.Handle;
|
|
using var first = new Bitmap(card.Width, card.Height);
|
|
using var repeated = new Bitmap(card.Width, card.Height);
|
|
using (var g = Graphics.FromImage(first)) g.Clear(Color.Magenta);
|
|
using (var g = Graphics.FromImage(repeated)) g.Clear(Color.Lime);
|
|
card.DrawToBitmap(first, card.ClientRectangle);
|
|
for (var i = 0; i < 3; i++) card.DrawToBitmap(repeated, card.ClientRectangle);
|
|
var stable = true;
|
|
for (var x = S(36); x < card.Width - S(36); x += S(17))
|
|
for (var y = S(36); y < card.Height - S(36); y += S(17))
|
|
stable &= first.GetPixel(x, y).ToArgb() == repeated.GetPixel(x, y).ToArgb();
|
|
report["login_surface_repaint_does_not_accumulate_alpha"] = stable;
|
|
report["login_surface_is_not_opaque_white"] = first.GetPixel(S(40), S(300)).ToArgb() != Color.White.ToArgb();
|
|
report["login_card_background_at_least_two_thirds_transparent"] = RoundedCardPanel.SurfaceAlpha <= 85;
|
|
// The login card uses the blue glass surface (238,247,255), not pure
|
|
// white. Keep the same strict pixel tolerance while predicting each
|
|
// channel of that tint over the artwork in parent coordinates.
|
|
Color Tint(Color background)
|
|
{
|
|
int Channel(int value, int tint) => (tint * RoundedCardPanel.SurfaceAlpha
|
|
+ value * (255 - RoundedCardPanel.SurfaceAlpha)) / 255;
|
|
return Color.FromArgb(Channel(background.R, 238), Channel(background.G, 247), Channel(background.B, 255));
|
|
}
|
|
var tintedPlainSurface = Tint(Brand.Surface);
|
|
var sceneryVisible = false;
|
|
var contrast = double.MaxValue;
|
|
for (var x = S(36); x < card.Width - S(36); x += S(17))
|
|
for (var y = S(36); y < card.Height - S(36); y += S(17))
|
|
{
|
|
var background = first.GetPixel(x, y);
|
|
contrast = Math.Min(contrast, Contrast(Brand.Navy, background));
|
|
if (y < S(170))
|
|
sceneryVisible |= Math.Abs(background.R - tintedPlainSurface.R)
|
|
+ Math.Abs(background.G - tintedPlainSurface.G) + Math.Abs(background.B - tintedPlainSurface.B) > 12;
|
|
}
|
|
report["login_landscape_visible_behind_upper_card"] = sceneryVisible;
|
|
report["login_secondary_text_contrast_at_least_4_5"] = contrast >= 4.5;
|
|
report["login_secondary_text_minimum_contrast"] = Math.Round(contrast, 3);
|
|
card.Location = P(130, 220);
|
|
using var moved = new Bitmap(card.Width, card.Height);
|
|
card.DrawToBitmap(moved, card.ClientRectangle);
|
|
using var backdrop = new Bitmap(parent.Width, parent.Height);
|
|
using (var g = Graphics.FromImage(backdrop))
|
|
ScenicTheme.Backdrop(g, parent.ClientSize, fixtureDpi, login: true);
|
|
report["login_lower_scene_embedded"] = ScenicTheme.LoginSceneAvailable;
|
|
var (sceneArea, sceneSource) = ScenicTheme.LoginSceneLayout(parent.ClientSize);
|
|
report["login_lower_scene_avoids_original_black_borders"] = sceneSource.Left >= 352 && sceneSource.Right <= 2270;
|
|
report["login_lower_scene_preserves_aspect_and_stays_below_header"] = sceneArea.Bottom == parent.Height
|
|
&& sceneArea.Top >= parent.Height / 3 && Math.Abs(sceneArea.Width / (double)sceneArea.Height
|
|
- sceneSource.Width / (double)sceneSource.Height) < .01;
|
|
var lowerDetail = backdrop.GetPixel(parent.Width * 4 / 5, parent.Height * 4 / 5);
|
|
report["login_lower_scene_visible_in_previously_empty_area"] = Math.Abs(lowerDetail.R - Brand.Surface.R)
|
|
+ Math.Abs(lowerDetail.G - Brand.Surface.G) + Math.Abs(lowerDetail.B - Brand.Surface.B) > 30;
|
|
var originCorrect = true;
|
|
var maximumChannelError = 0;
|
|
var originPixels = new List<object>();
|
|
foreach (var point in new[] { P(40, 180), P(220, 300), P(400, 330) })
|
|
{
|
|
var background = backdrop.GetPixel(card.Left + point.X, card.Top + point.Y);
|
|
var actual = moved.GetPixel(point.X, point.Y);
|
|
var expected = Tint(background);
|
|
var difference = Math.Max(Math.Abs(actual.R - expected.R),
|
|
Math.Max(Math.Abs(actual.G - expected.G), Math.Abs(actual.B - expected.B)));
|
|
maximumChannelError = Math.Max(maximumChannelError, difference);
|
|
originCorrect &= difference <= 2;
|
|
originPixels.Add(new { point.X, point.Y, background = background.ToArgb(), actual = actual.ToArgb(), expected = expected.ToArgb(), difference });
|
|
}
|
|
report["login_surface_move_pixel_probes"] = originPixels;
|
|
report["login_surface_move_maximum_channel_error"] = maximumChannelError;
|
|
report["login_surface_move_keeps_parent_artwork_coordinates"] = originCorrect;
|
|
}
|
|
|
|
private static double Contrast(Color foreground, Color background)
|
|
{
|
|
static double Luminance(Color color)
|
|
{
|
|
static double Linear(byte value) => value / 255d <= .04045
|
|
? value / 255d / 12.92 : Math.Pow((value / 255d + .055) / 1.055, 2.4);
|
|
return .2126 * Linear(color.R) + .7152 * Linear(color.G) + .0722 * Linear(color.B);
|
|
}
|
|
var a = Luminance(foreground); var b = Luminance(background);
|
|
return (Math.Max(a, b) + .05) / (Math.Min(a, b) + .05);
|
|
}
|
|
}
|