|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Aspire.Dashboard.Components.Layout; |
| 5 | +using Aspire.Dashboard.Components.Resize; |
| 6 | +using Aspire.Dashboard.Components.Tests.Shared; |
| 7 | +using Aspire.Dashboard.Configuration; |
| 8 | +using Aspire.Dashboard.Model; |
| 9 | +using Aspire.Dashboard.Model.BrowserStorage; |
| 10 | +using Aspire.Dashboard.Utils; |
| 11 | +using Bunit; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Microsoft.FluentUI.AspNetCore.Components; |
| 14 | +using Microsoft.FluentUI.AspNetCore.Components.Components.Tooltip; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace Aspire.Dashboard.Components.Tests.Layout; |
| 18 | + |
| 19 | +[UseCulture("en-US")] |
| 20 | +public partial class MainLayoutTests : TestContext |
| 21 | +{ |
| 22 | + [Fact] |
| 23 | + public async Task OnInitialize_UnsecuredOtlp_NotDismissed_DisplayMessageBar() |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + var testLocalStorage = new TestLocalStorage(); |
| 27 | + var messageService = new MessageService(); |
| 28 | + |
| 29 | + SetupMainLayoutServices(localStorage: testLocalStorage, messageService: messageService); |
| 30 | + |
| 31 | + Message? message = null; |
| 32 | + var messageShownTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 33 | + messageService.OnMessageItemsUpdatedAsync += () => |
| 34 | + { |
| 35 | + message = messageService.AllMessages.Single(); |
| 36 | + messageShownTcs.TrySetResult(); |
| 37 | + return Task.CompletedTask; |
| 38 | + }; |
| 39 | + |
| 40 | + testLocalStorage.OnGetUnprotectedAsync = key => |
| 41 | + { |
| 42 | + if (key == BrowserStorageKeys.UnsecuredTelemetryMessageDismissedKey) |
| 43 | + { |
| 44 | + return (false, false); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + throw new InvalidOperationException("Unexpected key."); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + var dismissedSettingSetTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 53 | + testLocalStorage.OnSetUnprotectedAsync = (key, value) => |
| 54 | + { |
| 55 | + if (key == BrowserStorageKeys.UnsecuredTelemetryMessageDismissedKey) |
| 56 | + { |
| 57 | + dismissedSettingSetTcs.TrySetResult((bool)value!); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + throw new InvalidOperationException("Unexpected key."); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + // Act |
| 66 | + var cut = RenderComponent<MainLayout>(builder => |
| 67 | + { |
| 68 | + builder.Add(p => p.ViewportInformation, new ViewportInformation(IsDesktop: true, IsUltraLowHeight: false, IsUltraLowWidth: false)); |
| 69 | + }); |
| 70 | + |
| 71 | + // Assert |
| 72 | + await messageShownTcs.Task.WaitAsync(TimeSpan.FromSeconds(5)); |
| 73 | + |
| 74 | + Assert.NotNull(message); |
| 75 | + |
| 76 | + message.Close(); |
| 77 | + |
| 78 | + Assert.True(await dismissedSettingSetTcs.Task.WaitAsync(TimeSpan.FromSeconds(5))); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public async Task OnInitialize_UnsecuredOtlp_Dismissed_NoMessageBar() |
| 83 | + { |
| 84 | + // Arrange |
| 85 | + var testLocalStorage = new TestLocalStorage(); |
| 86 | + var messageService = new MessageService(); |
| 87 | + |
| 88 | + SetupMainLayoutServices(localStorage: testLocalStorage, messageService: messageService); |
| 89 | + |
| 90 | + var messageShownTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 91 | + messageService.OnMessageItemsUpdatedAsync += () => |
| 92 | + { |
| 93 | + messageShownTcs.TrySetResult(); |
| 94 | + return Task.CompletedTask; |
| 95 | + }; |
| 96 | + |
| 97 | + testLocalStorage.OnGetUnprotectedAsync = key => |
| 98 | + { |
| 99 | + if (key == BrowserStorageKeys.UnsecuredTelemetryMessageDismissedKey) |
| 100 | + { |
| 101 | + return (true, true); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + throw new InvalidOperationException("Unexpected key."); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + // Act |
| 110 | + var cut = RenderComponent<MainLayout>(builder => |
| 111 | + { |
| 112 | + builder.Add(p => p.ViewportInformation, new ViewportInformation(IsDesktop: true, IsUltraLowHeight: false, IsUltraLowWidth: false)); |
| 113 | + }); |
| 114 | + |
| 115 | + // Assert |
| 116 | + var timeoutTask = Task.Delay(100); |
| 117 | + var completedTask = await Task.WhenAny(messageShownTcs.Task, timeoutTask).WaitAsync(TimeSpan.FromSeconds(5)); |
| 118 | + |
| 119 | + // It's hard to test something not happening. |
| 120 | + // In this case of checking for a message, apply a small display and then double check that no message was displayed. |
| 121 | + Assert.True(completedTask != messageShownTcs.Task, "No message bar should be displayed."); |
| 122 | + Assert.Empty(messageService.AllMessages); |
| 123 | + } |
| 124 | + |
| 125 | + private void SetupMainLayoutServices(TestLocalStorage? localStorage = null, MessageService? messageService = null) |
| 126 | + { |
| 127 | + Services.AddLocalization(); |
| 128 | + Services.AddOptions(); |
| 129 | + Services.AddSingleton<ThemeManager>(); |
| 130 | + Services.AddSingleton<IDialogService, DialogService>(); |
| 131 | + Services.AddSingleton<IDashboardClient, TestDashboardClient>(); |
| 132 | + Services.AddSingleton<ILocalStorage>(localStorage ?? new TestLocalStorage()); |
| 133 | + Services.AddSingleton<IEffectiveThemeResolver, TestEffectiveThemeResolver>(); |
| 134 | + Services.AddSingleton<ShortcutManager>(); |
| 135 | + Services.AddSingleton<BrowserTimeProvider, TestTimeProvider>(); |
| 136 | + Services.AddSingleton<IMessageService>(messageService ?? new MessageService()); |
| 137 | + Services.AddSingleton<LibraryConfiguration>(); |
| 138 | + Services.AddSingleton<ITooltipService, TooltipService>(); |
| 139 | + Services.AddSingleton<IToastService, ToastService>(); |
| 140 | + Services.AddSingleton<GlobalState>(); |
| 141 | + Services.Configure<DashboardOptions>(o => o.Otlp.AuthMode = OtlpAuthMode.Unsecured); |
| 142 | + |
| 143 | + var version = typeof(FluentMain).Assembly.GetName().Version!; |
| 144 | + |
| 145 | + var overflowModule = JSInterop.SetupModule(GetFluentFile("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Overflow/FluentOverflow.razor.js", version)); |
| 146 | + overflowModule.SetupVoid("fluentOverflowInitialize", _ => true); |
| 147 | + |
| 148 | + var anchorModule = JSInterop.SetupModule(GetFluentFile("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Anchor/FluentAnchor.razor.js", version)); |
| 149 | + |
| 150 | + var themeModule = JSInterop.SetupModule("/js/app-theme.js"); |
| 151 | + |
| 152 | + JSInterop.SetupModule("window.registerGlobalKeydownListener", _ => true); |
| 153 | + JSInterop.SetupModule("window.registerOpenTextVisualizerOnClick", _ => true); |
| 154 | + |
| 155 | + JSInterop.Setup<string>("window.getBrowserTimeZone").SetResult("abc"); |
| 156 | + } |
| 157 | + |
| 158 | + private static string GetFluentFile(string filePath, Version version) |
| 159 | + { |
| 160 | + return $"{filePath}?v={version}"; |
| 161 | + } |
| 162 | +} |
0 commit comments