// SPDX-License-Identifier: GPL-3.0-or-later // Copyright (c) CaptureImage Contributors using System.ComponentModel; using System.Globalization; using CaptureImage.Core.Abstractions; using CaptureImage.Core.Models; using CaptureImage.Core.Pipeline; using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; namespace CaptureImage.ViewModels.Tests; /// In-memory that applies updates synchronously. internal sealed class FakeSettingsStore(AppSettings? initial = null) : ISettingsStore { public AppSettings Current { get; private set; } = initial ?? new AppSettings(); public event EventHandler? Changed; public string SettingsFilePath => @"C:\data\settings.json"; public int UpdateCount { get; private set; } public Task LoadAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; public void Update(Func mutator) { var next = mutator(Current); if (ReferenceEquals(next, Current) || next == Current) { return; } Current = next; UpdateCount++; Changed?.Invoke(this, EventArgs.Empty); } /// A change made elsewhere (import, another view model). public void ReplaceExternally(AppSettings settings) { Current = settings; Changed?.Invoke(this, EventArgs.Empty); } public Task FlushAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; public Task ExportAsync(string exportPath, CancellationToken cancellationToken = default) => Task.CompletedTask; public Task ImportAsync(string importPath, CancellationToken cancellationToken = default) => Task.CompletedTask; public Task ResetAsync(CancellationToken cancellationToken = default) { ReplaceExternally(new AppSettings { Culture = Current.Culture }); return Task.CompletedTask; } } /// Runs everything inline, as if every caller were on the UI thread. internal sealed class InlineDispatcher : IUIThreadDispatcher { public bool IsOnUIThread => true; public void Post(Action action) => action(); public Task InvokeAsync(Action action) { action(); return Task.CompletedTask; } public Task InvokeAsync(Func func) => Task.FromResult(func()); public Task InvokeAsync(Func func) => func(); } /// Returns the key itself, so tests assert which text was chosen. internal sealed class KeyLocalization : ILocalizationService { public event PropertyChangedEventHandler? PropertyChanged; public event EventHandler? CultureChanged; public CultureInfo CurrentCulture { get; private set; } = CultureInfo.GetCultureInfo("en-US"); public TextFlowDirection CurrentFlowDirection => TextFlowDirection.LeftToRight; public IReadOnlyList SupportedCultures { get; } = [CultureInfo.GetCultureInfo("en-US"), CultureInfo.GetCultureInfo("vi-VN"), CultureInfo.GetCultureInfo("ar-SA")]; public string this[string key] => key; public void SetCulture(CultureInfo culture) { CurrentCulture = culture; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item[]")); CultureChanged?.Invoke(this, EventArgs.Empty); } } /// A real wired to substitutes, for view models that drive it. internal sealed class CoordinatorFixture : IDisposable { public CoordinatorFixture(ISettingsStore settings) { Settings = settings; Hotkeys.Activate(Arg.Any()).Returns(call => { var bindings = call.Arg(); return Enum.GetValues() .Select(mode => new HotkeyRegistration(mode, bindings.Get(mode), bindings.Get(mode).IsUnbound ? HotkeyRegistrationStatus.Unbound : HotkeyRegistrationStatus.Registered)) .ToList(); }); Performance.BeginForegroundWork().Returns(Substitute.For()); Coordinator = new CaptureCoordinator( settings, Hotkeys, Watcher, Targets, Substitute.For(), Substitute.For(), Substitute.For(), Substitute.For(), new CaptureFileWriter([], new FileNameStrategy(_ => false), NullLogger.Instance), Substitute.For(), Substitute.For(), Substitute.For(), Toasts, Substitute.For(), Substitute.For(), Performance, new KeyLocalization(), TimeProvider.System, NullLogger.Instance); } public ISettingsStore Settings { get; } public IHotkeyService Hotkeys { get; } = Substitute.For(); public IWindowWatcher Watcher { get; } = Substitute.For(); public ITargetCatalog Targets { get; } = Substitute.For(); public IToastService Toasts { get; } = Substitute.For(); public IPerformanceModeService Performance { get; } = Substitute.For(); public CaptureCoordinator Coordinator { get; } public void Dispose() => Coordinator.Dispose(); }