Clean up some references try to get tests to work

This commit is contained in:
Timothy Baldridge 2019-12-26 17:41:33 -07:00
parent 8c79b795e1
commit e4c78ebed9
15 changed files with 83 additions and 168 deletions

View File

@ -5,5 +5,6 @@
<Workers>0</Workers>
<Scope>MethodLevel</Scope>
</Parallelize>
<DisableAppDomain>True</DisableAppDomain>
</MSTest>
</RunSettings>

View File

@ -1,3 +1,4 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<ReactiveUI />
<ModuleInit />
</Weavers>

View File

@ -5,6 +5,7 @@
<xs:complexType>
<xs:all>
<xs:element name="ReactiveUI" minOccurs="0" maxOccurs="1" type="xs:anyType" />
<xs:element name="ModuleInit" minOccurs="0" maxOccurs="1" type="xs:anyType" />
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
<xs:annotation>

View File

@ -9,18 +9,17 @@ using System.Threading;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using CefSharp;
using CefSharp.OffScreen;
using Wabbajack.Common;
namespace Wabbajack.Lib.LibCefHelpers
{
public static class Helpers
{
private static readonly Task _initTask;
/// <summary>
/// We bundle the cef libs inside the .exe, we need to extract them before loading any wpf code that requires them
/// </summary>
private static async Task ExtractLibs()
private static void ExtractLibs()
{
if (File.Exists("cefsharp.7z") && File.Exists("libcef.dll")) return;
@ -32,18 +31,15 @@ namespace Wabbajack.Lib.LibCefHelpers
}
using (var wq = new WorkQueue(1))
{
await FileExtractor.ExtractAll(wq, "cefsharp.7z", ".");
FileExtractor.ExtractAll(wq, "cefsharp.7z", ".").Wait();
}
}
static Helpers()
{
_initTask = Task.Run(ExtractLibs);
}
public static Task Initialize()
{
return _initTask;
ExtractLibs();
if (!Cef.IsInitialized)
Cef.Initialize(new CefSettings { MultiThreadedMessageLoop = true });
}
public static HttpClient GetClient(IEnumerable<Cookie> cookies, string referer)
@ -112,5 +108,18 @@ namespace Wabbajack.Lib.LibCefHelpers
public string Domain { get; set; }
public string Path { get; set; }
}
public static void Init()
{
// does nothing, but kicks off the static constructor
}
}
public static class ModuleInitializer
{
public static void Initialize()
{
Helpers.Init();
}
}
}

View File

@ -156,10 +156,6 @@
<Compile Include="WebAutomation\CefSharpWrapper.cs" />
<Compile Include="WebAutomation\IWebDriver.cs" />
<Compile Include="WebAutomation\WebAutomation.cs" />
<Compile Include="WebAutomation\WebAutomationWindow.xaml.cs">
<DependentUpon>WebAutomationWindow.xaml</DependentUpon>
</Compile>
<Compile Include="WebAutomation\WebAutomationWindowViewModel.cs" />
<Compile Include="zEditIntegration.cs" />
</ItemGroup>
<ItemGroup>
@ -208,6 +204,9 @@
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.Controls.WebView">
<Version>6.0.0</Version>
</PackageReference>
<PackageReference Include="ModuleInit.Fody">
<Version>2.1.0</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.3</Version>
</PackageReference>
@ -233,12 +232,6 @@
<Version>8.0.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Page Include="WebAutomation\WebAutomationWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -2,93 +2,54 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using CefSharp;
using CefSharp.OffScreen;
namespace Wabbajack.Lib.WebAutomation
{
public enum DisplayMode
{
Visible,
Hidden
}
public class Driver : IDisposable
{
private WebAutomationWindow _window;
private WebAutomationWindowViewModel _ctx;
private Task<WebAutomationWindow> _windowTask;
private IWebBrowser _browser;
private CefSharpWrapper _driver;
private Driver(DisplayMode displayMode = DisplayMode.Hidden)
public Driver()
{
var windowTask = new TaskCompletionSource<WebAutomationWindow>();
var t = new Thread(() =>
{
_window = new WebAutomationWindow();
_ctx = (WebAutomationWindowViewModel)_window.DataContext;
// Initiates the dispatcher thread shutdown when the window closes
_window.Closed += (s, e) => _window.Dispatcher.InvokeShutdown();
if (displayMode == DisplayMode.Hidden)
{
_window.WindowState = WindowState.Minimized;
_window.ShowInTaskbar = false;
}
_window.Show();
windowTask.SetResult(_window);
// Makes the thread support message pumping
System.Windows.Threading.Dispatcher.Run();
});
_windowTask = windowTask.Task;
t.SetApartmentState(ApartmentState.STA);
t.Start();
_browser = new ChromiumWebBrowser();
_driver = new CefSharpWrapper(_browser);
}
public static async Task<Driver> Create()
{
var driver = new Driver();
driver._window = await driver._windowTask;
driver._ctx = (WebAutomationWindowViewModel) driver._window.DataContext;
await driver._driver.WaitForInitialized();
return driver;
}
public Task<Uri> NavigateTo(Uri uri)
public async Task<Uri> NavigateTo(Uri uri)
{
return _ctx.NavigateTo(uri);
await _driver.NavigateTo(uri);
return await GetLocation();
}
public Task<Uri> GetLocation()
public async ValueTask<Uri> GetLocation()
{
var tcs = new TaskCompletionSource<Uri>();
_window.Dispatcher.Invoke(() => tcs.SetResult(_window.WebView.Source));
return tcs.Task;
try
{
return new Uri(_browser.Address);
}
catch (UriFormatException)
{
return null;
}
}
public Task<string> GetAttr(string selector, string attr)
{
var tcs = new TaskCompletionSource<string>();
_window.Dispatcher.Invoke(() =>
{
try
{
var script = $"document.querySelector(\"{selector}\").{attr}";
var result = _window.WebView.InvokeScript("eval", script);
tcs.SetResult(result);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
});
return tcs.Task;
return _driver.EvaluateJavaScript($"document.querySelector(\"{selector}\").{attr}");
}
public void Dispose()
{
_window.Dispatcher.Invoke(_window.Close);
_browser.Dispose();
}
}
}

View File

@ -1,14 +0,0 @@
<Window x:Class="Wabbajack.WebAutomationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wabbajack"
xmlns:controls="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls.WebView"
mc:Ignorable="d"
Title="WebAutomationWindow" Height="450" Width="800">
<Grid>
<controls:WebView Name="WebView">
</controls:WebView>
</Grid>
</Window>

View File

@ -1,17 +0,0 @@
using System.Windows;
using Wabbajack.Lib.WebAutomation;
namespace Wabbajack
{
/// <summary>
/// Interaction logic for WebAutomationWindow.xaml
/// </summary>
public partial class WebAutomationWindow : Window
{
public WebAutomationWindow()
{
InitializeComponent();
DataContext = new WebAutomationWindowViewModel(this);
}
}
}

View File

@ -1,36 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;
using Microsoft.Toolkit.Wpf.UI.Controls;
namespace Wabbajack.Lib.WebAutomation
{
public class WebAutomationWindowViewModel : ViewModel
{
private WebAutomationWindow _window;
private WebView Browser => _window.WebView;
public WebAutomationWindowViewModel(WebAutomationWindow window)
{
_window = window;
}
public Task<Uri> NavigateTo(Uri uri)
{
var tcs = new TaskCompletionSource<Uri>();
EventHandler<WebViewControlNavigationCompletedEventArgs> handler = null;
handler = (s, e) =>
{
Browser.NavigationCompleted -= handler;
tcs.SetResult(uri);
};
Browser.NavigationCompleted += handler;
Browser.Source = uri;
return tcs.Task;
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wabbajack.Common;
using Wabbajack.Lib;
@ -15,7 +16,7 @@ namespace Wabbajack.Test
[TestInitialize]
public async Task TestInitialize()
{
await Helpers.Initialize();
Helpers.Init();
Consts.TestMode = true;
utils = new TestUtils();

View File

@ -4,6 +4,8 @@ using System.Threading.Tasks;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using Alphaleonis.Win32.Filesystem;
using CefSharp;
using CefSharp.OffScreen;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wabbajack.Common;
using Wabbajack.Common.StatusFeed;
@ -12,6 +14,7 @@ using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.LibCefHelpers;
using Wabbajack.Lib.NexusApi;
using Wabbajack.Lib.Validation;
using Wabbajack.Lib.WebAutomation;
using File = Alphaleonis.Win32.Filesystem.File;
using Game = Wabbajack.Common.Game;
@ -20,13 +23,17 @@ namespace Wabbajack.Test
[TestClass]
public class DownloaderTests
{
static DownloaderTests()
{
Helpers.Init();
}
public TestContext TestContext { get; set; }
[TestInitialize]
public async Task Setup()
{
await Helpers.Initialize();
Helpers.Init();
Utils.LogMessages.OfType<IInfo>().Subscribe(onNext: msg => TestContext.WriteLine(msg.ShortDescription));
Utils.LogMessages.OfType<IUserIntervention>().Subscribe(msg =>
TestContext.WriteLine("ERROR: User intervetion required: " + msg.ShortDescription));

View File

@ -32,8 +32,8 @@
<PlatformTarget>x64</PlatformTarget>
<NoWarn>CS1998</NoWarn>
<WarningsAsErrors>CS4014</WarningsAsErrors>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -44,8 +44,8 @@
<WarningLevel>4</WarningLevel>
<NoWarn>CS1998</NoWarn>
<WarningsAsErrors>CS4014</WarningsAsErrors>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
@ -55,8 +55,8 @@
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
@ -66,8 +66,8 @@
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
@ -78,8 +78,8 @@
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
@ -90,10 +90,18 @@
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
<Reference Include="CefSharp.Core, Version=75.1.143.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Users\tbald\.nuget\packages\cefsharp.common\75.1.143\CefSharp\x64\CefSharp.Core.dll</HintPath>
</Reference>
<Reference Include="CefSharp.OffScreen, Version=75.1.143.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Users\tbald\.nuget\packages\cefsharp.offscreen\75.1.143\CefSharp\x64\CefSharp.OffScreen.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
@ -161,8 +169,11 @@
<PackageReference Include="AlphaFS">
<Version>2.2.6</Version>
</PackageReference>
<PackageReference Include="CefGlue.Wpf">
<Version>75.1.28</Version>
<PackageReference Include="CefSharp.Common">
<Version>75.1.143</Version>
</PackageReference>
<PackageReference Include="CefSharp.OffScreen">
<Version>75.1.143</Version>
</PackageReference>
<PackageReference Include="MSTest.TestAdapter">
<Version>2.0.0</Version>

View File

@ -10,7 +10,6 @@ using ReactiveUI.Fody.Helpers;
using Wabbajack.Lib;
using Wabbajack.Lib.LibCefHelpers;
using Wabbajack.Lib.WebAutomation;
using Xilium.CefGlue.WPF;
namespace Wabbajack
{
@ -33,7 +32,6 @@ namespace Wabbajack
public static async Task<WebBrowserVM> GetNew(string url = "http://www.wabbajack.org")
{
// Make sure libraries are extracted first
await Helpers.Initialize();
return new WebBrowserVM(url);
}
}

View File

@ -20,6 +20,7 @@ namespace Wabbajack
public MainWindow()
{
Helpers.Init();
// Wire any unhandled crashing exceptions to log before exiting
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
@ -32,7 +33,6 @@ namespace Wabbajack
// Run some init tasks in background
Task.Run(async () =>
{
await Helpers.Initialize();
var appPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
try
{

View File

@ -5,7 +5,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:wabbajack="clr-namespace:Wabbajack"
xmlns:wpf="clr-namespace:Xilium.CefGlue.WPF;assembly=Xilium.CefGlue.WPF"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>