TestBase.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Threading;
  4. using CefGlue.Tests.CustomSchemes;
  5. using CefGlue.Tests.Helpers;
  6. using NUnit.Framework;
  7. using System;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Xilium.CefGlue.Avalonia;
  11. using Xilium.CefGlue.Common;
  12. using Xilium.CefGlue.Common.Shared;
  13. namespace CefGlue.Tests
  14. {
  15. public class TestBase
  16. {
  17. private static object initLock = new object();
  18. private static bool initialized = false;
  19. private AvaloniaCefBrowser browser;
  20. private Window window;
  21. protected AvaloniaCefBrowser Browser => browser;
  22. [OneTimeSetUp]
  23. protected async Task SetUp()
  24. {
  25. if (initialized)
  26. {
  27. return;
  28. }
  29. var initializationTaskCompletionSource = new TaskCompletionSource<bool>();
  30. CefRuntimeLoader.Initialize(customSchemes: new[] {
  31. new CustomScheme()
  32. {
  33. SchemeName = CustomSchemeHandlerFactory.SchemeName,
  34. SchemeHandlerFactory = new CustomSchemeHandlerFactory()
  35. }
  36. });
  37. lock (initLock)
  38. {
  39. if (initialized)
  40. {
  41. return;
  42. }
  43. var uiThread = new Thread(() =>
  44. {
  45. AppBuilder.Configure<App>().UsePlatformDetect().SetupWithoutStarting();
  46. Dispatcher.UIThread.Post(() =>
  47. {
  48. initialized = true;
  49. initializationTaskCompletionSource.SetResult(true);
  50. });
  51. Dispatcher.UIThread.MainLoop(CancellationToken.None);
  52. });
  53. uiThread.IsBackground = true;
  54. uiThread.Start();
  55. }
  56. await initializationTaskCompletionSource.Task;
  57. }
  58. [SetUp]
  59. protected async Task Setup()
  60. {
  61. var testName = TestContext.CurrentContext.Test.FullName; // capture test name outside the async part (otherwise wont work properly)
  62. await Run(async () =>
  63. {
  64. if (window == null)
  65. {
  66. window = new Window();
  67. window.Width = 1;
  68. window.Height = 1;
  69. window.Show();
  70. }
  71. window.Title = testName;
  72. var browserInitTaskCompletionSource = new TaskCompletionSource<bool>();
  73. browser = new AvaloniaCefBrowser();
  74. browser.Settings.WebSecurity = Xilium.CefGlue.CefState.Disabled;
  75. browser.BrowserInitialized += delegate () { browserInitTaskCompletionSource.SetResult(true); };
  76. window.Content = browser;
  77. await browserInitTaskCompletionSource.Task;
  78. });
  79. await ExtraSetup();
  80. }
  81. protected virtual Task ExtraSetup()
  82. {
  83. return Task.CompletedTask;
  84. }
  85. [TearDown]
  86. protected void TearDown()
  87. {
  88. browser.Dispose();
  89. }
  90. [OneTimeTearDown]
  91. protected async Task OneTimeTearDown()
  92. {
  93. await Run(() => {
  94. window.Close();
  95. window = null;
  96. });
  97. }
  98. protected Task Run(Func<Task> func) => Dispatcher.UIThread.InvokeAsync(func, DispatcherPriority.Background);
  99. protected Task Run(Action action) => Dispatcher.UIThread.InvokeAsync(action, DispatcherPriority.Background);
  100. protected Task<T> EvaluateJavascript<T>(string script, TimeSpan? timeout = null) => Browser.EvaluateJavaScript<T>("(function() { " + script + " })()", timeout: timeout);
  101. }
  102. }