115 lines
6.9 KiB
C#
115 lines
6.9 KiB
C#
using System.Reflection;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Headless;
|
|
using Avalonia.Markup.Xaml.Styling;
|
|
using Avalonia.Themes.Fluent;
|
|
using Avalonia.Threading;
|
|
using Avalonia.VisualTree;
|
|
using OpenCvSharp;
|
|
using SopHelper.Engine.Camera;
|
|
using SopHelper.Training;
|
|
using Window = Avalonia.Controls.Window;
|
|
|
|
internal static class Program
|
|
{
|
|
[STAThread]
|
|
public static void Main()
|
|
{
|
|
var root = Path.Combine(Path.GetTempPath(), "recording-limit-smoke-" + Guid.NewGuid().ToString("N"));
|
|
try
|
|
{
|
|
AppBuilder.Configure<Application>().UseHeadless(new AvaloniaHeadlessPlatformOptions()).SetupWithoutStarting();
|
|
Application.Current!.Styles.Add(new FluentTheme());
|
|
Application.Current.Styles.Add(new StyleInclude(new Uri("avares://SopHelper.Avalonia/")) { Source = new Uri("avares://SopHelper.Avalonia/Themes/Styles.axaml") });
|
|
Check(VideoRecorder.MaximumDuration == TimeSpan.FromMinutes(5), "Production recording limit is five minutes");
|
|
var clock = new ManualClock();
|
|
using var camera = new FakeCamera();
|
|
var model = new CameraViewModel(camera, root, timeProvider: clock);
|
|
var window = new CameraWindow(model); window.Show();
|
|
model.RefreshCommand.Execute(); model.PreviewCommand.Execute();
|
|
Thread.Sleep(75); camera.Emit(); Dispatcher.UIThread.RunJobs();
|
|
Check(model.RecordCommand.CanExecute(), "Preview is ready for recording");
|
|
var notices = 0;
|
|
model.RecordingLimitReached += message => { notices++; Check(message.Contains("5分钟") && message.Contains(model.LastRecording), "Timeout notice contains the limit and saved path"); };
|
|
model.RecordCommand.Execute();
|
|
for (var second = 1; second <= 299; second++) { clock.Advance(TimeSpan.FromSeconds(1)); camera.Emit(); }
|
|
Tick(model);
|
|
Check(model.IsRecording && notices == 0, "Recording continues at 4:59 without a popup");
|
|
clock.Advance(TimeSpan.FromSeconds(1)); camera.Emit();
|
|
// Simulate a delayed UI callback while the acquisition thread keeps receiving frames.
|
|
clock.Advance(TimeSpan.FromMilliseconds(700)); camera.Emit();
|
|
Tick(model);
|
|
Check(!model.IsRecording && model.IsPreviewing && notices == 1, "Timeout stops recording once and keeps the preview running");
|
|
CheckVideo(model.LastRecording, 300);
|
|
var popup = (Window?)typeof(CameraWindow).GetField("_recordingLimitDialog", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(window);
|
|
Check(popup is { IsVisible: true } && popup.Title == "录像时长提示", "Timeout opens the modal notification");
|
|
Check(popup!.GetVisualDescendants().OfType<TextBlock>().Any(t => t.Text?.Contains("5分钟") == true), "Popup displays the five-minute explanation");
|
|
Tick(model); Tick(model); Check(notices == 1, "Later timer ticks do not repeat the popup");
|
|
popup!.Close(); Dispatcher.UIThread.RunJobs();
|
|
model.RecordCommand.Execute();
|
|
Check(model.IsRecording, "A second recording starts with a fresh timeout");
|
|
clock.Advance(TimeSpan.FromSeconds(1)); camera.Emit(); Tick(model);
|
|
Check(model.IsRecording && notices == 1, "Previous recording duration does not carry over");
|
|
model.RecordCommand.Execute();
|
|
Check(!model.IsRecording && notices == 1, "Manual stop saves without a timeout popup");
|
|
CheckVideo(model.LastRecording, 1);
|
|
clock.Advance(TimeSpan.FromMinutes(6)); Tick(model);
|
|
Check(notices == 1, "Stopped recordings do not later trigger a timeout");
|
|
camera.Emit(); model.RecordCommand.Execute();
|
|
for (var second = 1; second <= 300; second++) { clock.Advance(TimeSpan.FromSeconds(1)); camera.Emit(); }
|
|
Tick(model);
|
|
Check(notices == 2 && !model.IsRecording, "A later recording independently stops at its own five-minute limit");
|
|
CheckVideo(model.LastRecording, 300);
|
|
window.Close();
|
|
var shutdown = model.ShutdownAsync();
|
|
var wait = System.Diagnostics.Stopwatch.StartNew();
|
|
while (!shutdown.IsCompleted && wait.Elapsed < TimeSpan.FromSeconds(5)) { Dispatcher.UIThread.RunJobs(); Thread.Sleep(1); }
|
|
shutdown.GetAwaiter().GetResult(); Dispatcher.UIThread.RunJobs();
|
|
Check(!window.IsVisible, "Closing the camera also closes its notification and releases resources");
|
|
}
|
|
finally
|
|
{
|
|
if (Path.GetFileName(root).StartsWith("recording-limit-smoke-") && Directory.Exists(root)) Directory.Delete(root, true);
|
|
}
|
|
}
|
|
private static void Tick(CameraViewModel model) => typeof(CameraViewModel).GetMethod("Tick", BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(model, null);
|
|
private static void CheckVideo(string path, double expectedSeconds)
|
|
{
|
|
using var video = new VideoCapture(path);
|
|
Check(video.IsOpened(), "Automatically/manual saved AVI can be reopened");
|
|
var duration = video.FrameCount / video.Fps;
|
|
var tolerance = expectedSeconds == 300 ? .001 : 1 / video.Fps + .001;
|
|
Check(Math.Abs(duration - expectedSeconds) < tolerance && duration <= 300, $"Encoded video duration is {expectedSeconds} seconds (within frame precision)");
|
|
using var frame = new Mat(); Check(video.Read(frame) && !frame.Empty(), "Saved AVI contains decodable frames");
|
|
}
|
|
private static void Check(bool value, string message) { if (!value) throw new Exception(message); Console.WriteLine("PASS: " + message); }
|
|
}
|
|
|
|
internal sealed class ManualClock : TimeProvider
|
|
{
|
|
private long _ticks;
|
|
public override long TimestampFrequency => TimeSpan.TicksPerSecond;
|
|
public override long GetTimestamp() => _ticks;
|
|
public void Advance(TimeSpan duration) => _ticks += duration.Ticks;
|
|
}
|
|
internal sealed class FakeCamera : ICameraService
|
|
{
|
|
public bool IsConnected { get; private set; }
|
|
public bool IsGrabbing { get; private set; }
|
|
public event Action<CameraFrame>? FrameArrived;
|
|
public event Action<bool>? ConnectionChanged;
|
|
public event Action<string>? Error { add { } remove { } }
|
|
public Task<List<CameraInfo>> EnumerateAsync() => Task.FromResult(new List<CameraInfo> { new() { Name = "测试相机" } });
|
|
public Task<bool> ConnectAsync(CameraInfo camera) { IsConnected = true; ConnectionChanged?.Invoke(true); return Task.FromResult(true); }
|
|
public Task<bool> StartAsync() { IsGrabbing = true; return Task.FromResult(true); }
|
|
public void Stop() => IsGrabbing = false;
|
|
public void Disconnect() { Stop(); IsConnected = false; ConnectionChanged?.Invoke(false); }
|
|
public void Dispose() => Disconnect();
|
|
public void Emit()
|
|
{
|
|
using var frame = new CameraFrame { Bgr = new Mat(48, 64, MatType.CV_8UC3, new Scalar(35, 60, 90)) };
|
|
FrameArrived?.Invoke(frame);
|
|
}
|
|
}
|