123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Threading;
- using CefGlue.Tests.CustomSchemes;
- using CefGlue.Tests.Helpers;
- using NUnit.Framework;
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using Xilium.CefGlue.Avalonia;
- using Xilium.CefGlue.Common;
- using Xilium.CefGlue.Common.Shared;
- namespace CefGlue.Tests
- {
- public class TestBase
- {
- private static object initLock = new object();
- private static bool initialized = false;
- private AvaloniaCefBrowser browser;
- private Window window;
- protected AvaloniaCefBrowser Browser => browser;
- [OneTimeSetUp]
- protected async Task SetUp()
- {
- if (initialized)
- {
- return;
- }
- var initializationTaskCompletionSource = new TaskCompletionSource<bool>();
- CefRuntimeLoader.Initialize(customSchemes: new[] {
- new CustomScheme()
- {
- SchemeName = CustomSchemeHandlerFactory.SchemeName,
- SchemeHandlerFactory = new CustomSchemeHandlerFactory()
- }
- });
- lock (initLock)
- {
- if (initialized)
- {
- return;
- }
- var uiThread = new Thread(() =>
- {
- AppBuilder.Configure<App>().UsePlatformDetect().SetupWithoutStarting();
- Dispatcher.UIThread.Post(() =>
- {
- initialized = true;
- initializationTaskCompletionSource.SetResult(true);
- });
- Dispatcher.UIThread.MainLoop(CancellationToken.None);
- });
- uiThread.IsBackground = true;
- uiThread.Start();
- }
- await initializationTaskCompletionSource.Task;
- }
- [SetUp]
- protected async Task Setup()
- {
- var testName = TestContext.CurrentContext.Test.FullName; // capture test name outside the async part (otherwise wont work properly)
- await Run(async () =>
- {
- if (window == null)
- {
- window = new Window();
- window.Width = 1;
- window.Height = 1;
- window.Show();
- }
- window.Title = testName;
- var browserInitTaskCompletionSource = new TaskCompletionSource<bool>();
- browser = new AvaloniaCefBrowser();
- browser.Settings.WebSecurity = Xilium.CefGlue.CefState.Disabled;
- browser.BrowserInitialized += delegate () { browserInitTaskCompletionSource.SetResult(true); };
- window.Content = browser;
- await browserInitTaskCompletionSource.Task;
- });
- await ExtraSetup();
- }
- protected virtual Task ExtraSetup()
- {
- return Task.CompletedTask;
- }
- [TearDown]
- protected void TearDown()
- {
- browser.Dispose();
- }
- [OneTimeTearDown]
- protected async Task OneTimeTearDown()
- {
- await Run(() => {
- window.Close();
- window = null;
- });
- }
- protected Task Run(Func<Task> func) => Dispatcher.UIThread.InvokeAsync(func, DispatcherPriority.Background);
- protected Task Run(Action action) => Dispatcher.UIThread.InvokeAsync(action, DispatcherPriority.Background);
- protected Task<T> EvaluateJavascript<T>(string script, TimeSpan? timeout = null) => Browser.EvaluateJavaScript<T>("(function() { " + script + " })()", timeout: timeout);
- }
- }
|