| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System;
- using System.Reflection;
- using Avalonia.Threading;
- namespace Avalonia.Headless.UnitTests;
- public class IsolationTests
- {
- private static WeakReference<Application> s_previousAppRef;
- private static WeakReference<Dispatcher> s_previousDispatcherRef;
- #if NUNIT
- [AvaloniaTheory, Timeout(10000)]
- [TestCase(1), TestCase(2), TestCase(3)]
- #elif XUNIT
- [AvaloniaTheory]
- [InlineData(1), InlineData(2), InlineData(3)]
- [System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(
- "Usage",
- "xUnit1026:Theory methods should use all of their parameters",
- Justification = "Used to run the test several times with the proper isolation level")]
- #endif
- public void Application_Instance_Should_Match_Isolation_Level(int runIndex)
- {
- var currentApp = Application.Current;
- var currentDispatcher = Dispatcher.UIThread;
- if (s_previousAppRef is not null && s_previousDispatcherRef is not null)
- {
- var isolationLevel =
- GetType().Assembly.GetCustomAttribute<AvaloniaTestIsolationAttribute>()?.IsolationLevel ??
- AvaloniaTestIsolationLevel.PerTest;
- if (isolationLevel == AvaloniaTestIsolationLevel.PerTest)
- {
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- Assert.False(s_previousAppRef.TryGetTarget(out var previousApp),
- "Previous Application instance should have been collected.");
- Assert.False(s_previousDispatcherRef.TryGetTarget(out var previousDispatcher),
- "Previous Dispatcher instance should have been collected.");
- Assert.False(previousApp == currentApp);
- Assert.False(previousDispatcher == currentDispatcher);
- }
- else if (isolationLevel == AvaloniaTestIsolationLevel.PerAssembly)
- {
- Assert.True(s_previousAppRef.TryGetTarget(out var previousApp),
- "Previous Application instance should still be alive.");
- Assert.True(s_previousDispatcherRef.TryGetTarget(out var previousDispatcher),
- "Previous Dispatcher instance should still be alive.");
- Assert.True(previousApp == currentApp);
- Assert.True(previousDispatcher == currentDispatcher);
- }
- else
- {
- throw new InvalidOperationException($"Unknown isolation level: {isolationLevel}");
- }
- }
- s_previousAppRef = new WeakReference<Application>(currentApp);
- s_previousDispatcherRef = new WeakReference<Dispatcher>(currentDispatcher);
- }
- }
|