|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using MudBlazor; |
| 4 | +using MudBlazor.Services; |
| 5 | + |
| 6 | +namespace CodeBeam.MudExtensions.UnitTests.Mocks |
| 7 | +{ |
| 8 | +#pragma warning disable CS1998 // Justification - Implementing IResizeListenerService |
| 9 | + public class MockBreakpointService : IBreakpointService |
| 10 | + { |
| 11 | + private int _width, _height; |
| 12 | + |
| 13 | + internal void ApplyScreenSize(int width, int height) |
| 14 | + { |
| 15 | + _width = width; |
| 16 | + _height = height; |
| 17 | + |
| 18 | + OnResized?.Invoke(this, new BrowserWindowSize() |
| 19 | + { |
| 20 | + Width = _width, |
| 21 | + Height = _height |
| 22 | + }); |
| 23 | + OnBreakpointChanged?.Invoke(this, GetBreakpointInternal()); |
| 24 | + } |
| 25 | + |
| 26 | +#nullable enable |
| 27 | +#pragma warning disable CS0414 // justification implementing interface |
| 28 | + public event EventHandler<BrowserWindowSize>? OnResized; |
| 29 | + public event EventHandler<Breakpoint>? OnBreakpointChanged; |
| 30 | +#pragma warning restore CS0414 |
| 31 | +#nullable disable |
| 32 | + public async ValueTask<BrowserWindowSize> GetBrowserWindowSize() |
| 33 | + { |
| 34 | + return new BrowserWindowSize() |
| 35 | + { |
| 36 | + Width = _width, |
| 37 | + Height = _height |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + public async Task<bool> IsMediaSize(Breakpoint breakpoint) |
| 42 | + { |
| 43 | + if (breakpoint == Breakpoint.None) |
| 44 | + return false; |
| 45 | + |
| 46 | + return IsMediaSize(breakpoint, await GetBreakpoint()); |
| 47 | + } |
| 48 | + |
| 49 | + public bool IsMediaSize(Breakpoint breakpoint, Breakpoint reference) |
| 50 | + { |
| 51 | + if (breakpoint == Breakpoint.None) |
| 52 | + return false; |
| 53 | + |
| 54 | + return breakpoint switch |
| 55 | + { |
| 56 | + Breakpoint.Xs => reference == Breakpoint.Xs, |
| 57 | + Breakpoint.Sm => reference == Breakpoint.Sm, |
| 58 | + Breakpoint.Md => reference == Breakpoint.Md, |
| 59 | + Breakpoint.Lg => reference == Breakpoint.Lg, |
| 60 | + Breakpoint.Xl => reference == Breakpoint.Xl, |
| 61 | + // * and down |
| 62 | + Breakpoint.SmAndDown => reference <= Breakpoint.Sm, |
| 63 | + Breakpoint.MdAndDown => reference <= Breakpoint.Md, |
| 64 | + Breakpoint.LgAndDown => reference <= Breakpoint.Lg, |
| 65 | + // * and up |
| 66 | + Breakpoint.SmAndUp => reference >= Breakpoint.Sm, |
| 67 | + Breakpoint.MdAndUp => reference >= Breakpoint.Md, |
| 68 | + Breakpoint.LgAndUp => reference >= Breakpoint.Lg, |
| 69 | + _ => false, |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + public async Task<Breakpoint> GetBreakpoint() => GetBreakpointInternal(); |
| 74 | + |
| 75 | + public Task<BreakpointServiceSubscribeResult> Subscribe(Action<Breakpoint> callback) => Task.FromResult(new BreakpointServiceSubscribeResult(Guid.NewGuid(),Breakpoint.Sm)); |
| 76 | + public Task<BreakpointServiceSubscribeResult> Subscribe(Action<Breakpoint> callback, ResizeOptions options) => Task.FromResult(new BreakpointServiceSubscribeResult(Guid.NewGuid(), Breakpoint.Sm)); |
| 77 | + public Task<bool> Unsubscribe(Guid subscriptionId) => Task.FromResult(true); |
| 78 | + |
| 79 | + private Breakpoint GetBreakpointInternal() |
| 80 | + { |
| 81 | + if (_width >= ResizeListenerService.BreakpointDefinitions[Breakpoint.Xl]) |
| 82 | + return Breakpoint.Xl; |
| 83 | + else if (_width >= ResizeListenerService.BreakpointDefinitions[Breakpoint.Lg]) |
| 84 | + return Breakpoint.Lg; |
| 85 | + else if (_width >= ResizeListenerService.BreakpointDefinitions[Breakpoint.Md]) |
| 86 | + return Breakpoint.Md; |
| 87 | + else if (_width >= ResizeListenerService.BreakpointDefinitions[Breakpoint.Sm]) |
| 88 | + return Breakpoint.Sm; |
| 89 | + else |
| 90 | + return Breakpoint.Xs; |
| 91 | + } |
| 92 | + |
| 93 | + public ValueTask DisposeAsync() |
| 94 | + { |
| 95 | + OnResized = null; |
| 96 | + OnBreakpointChanged = null; |
| 97 | + return ValueTask.CompletedTask; |
| 98 | + } |
| 99 | + } |
| 100 | +#pragma warning restore CS1998 |
| 101 | +} |
0 commit comments