2018-09-10 02:41:13 +02:00
// Copyright © 2017 The CefSharp Authors. All rights reserved.
2017-10-24 12:21:04 +10:00
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System ;
using System.IO ;
2020-10-13 13:40:46 +10:00
using System.Net ;
2019-10-01 21:39:14 +10:00
using System.Threading.Tasks ;
2020-05-19 21:55:16 +10:00
using CefSharp.Example ;
2019-10-01 21:39:14 +10:00
using CefSharp.OffScreen ;
2020-03-01 21:28:09 +01:00
using Nito.AsyncEx ;
2020-10-13 13:40:46 +10:00
using Titanium.Web.Proxy ;
using Titanium.Web.Proxy.Models ;
2020-02-29 13:47:27 +01:00
using Xunit ;
2022-12-05 09:10:55 +10:00
using Xunit.Abstractions ;
using Xunit.Sdk ;
2017-10-24 12:21:04 +10:00
namespace CefSharp.Test
{
2020-02-29 14:22:48 +01:00
public class CefSharpFixture : IAsyncLifetime , IDisposable
2018-09-10 02:41:13 +02:00
{
2020-03-01 21:28:09 +01:00
private readonly AsyncContextThread contextThread ;
2020-10-13 13:40:46 +10:00
private ProxyServer proxyServer ;
2022-12-05 09:10:55 +10:00
private readonly IMessageSink diagnosticMessageSink ;
2019-10-01 21:39:14 +10:00
2022-12-05 09:10:55 +10:00
public CefSharpFixture ( IMessageSink messageSink )
2018-09-10 02:41:13 +02:00
{
2020-03-01 21:28:09 +01:00
contextThread = new AsyncContextThread ( ) ;
2022-12-05 09:10:55 +10:00
diagnosticMessageSink = messageSink ;
2020-02-29 14:22:48 +01:00
}
private void CefInitialize ( )
{
2018-09-10 02:41:13 +02:00
if ( ! Cef . IsInitialized )
{
var isDefault = AppDomain . CurrentDomain . IsDefaultAppDomain ( ) ;
if ( ! isDefault )
{
throw new Exception ( @"Add <add key=""xunit.appDomain"" value=""denied""/> to your app.config to disable appdomains" ) ;
}
2017-10-24 12:21:04 +10:00
2020-05-03 21:58:05 +10:00
Cef . EnableWaitForBrowsersToClose ( ) ;
2022-05-31 15:43:34 +10:00
CefSharp . Internals . BrowserRefCounter . Instance . EnableLogging ( ) ;
2020-05-03 21:58:05 +10:00
2020-03-02 20:30:41 +01:00
CefSharpSettings . ShutdownOnExit = false ;
2018-09-10 02:41:13 +02:00
var settings = new CefSettings ( ) ;
2017-10-24 12:21:04 +10:00
2020-05-19 21:55:16 +10:00
settings . RegisterScheme ( new CefCustomScheme
{
SchemeName = "https" ,
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory ( ) ,
DomainName = CefExample . ExampleDomain
} ) ;
2018-09-10 02:41:13 +02:00
//The location where cache data will be stored on disk. If empty an in-memory cache will be used for some features and a temporary disk cache for others.
//HTML5 databases such as localStorage will only persist across sessions if a cache path is specified.
settings . CachePath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , "CefSharp\\Tests\\Cache" ) ;
2021-05-12 10:59:18 +10:00
settings . RootCachePath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , "CefSharp\\Tests" ) ;
2021-06-04 13:26:00 +10:00
//settings.CefCommandLineArgs.Add("renderer-startup-dialog");
2022-09-06 13:08:10 +10:00
//settings.CefCommandLineArgs.Add("disable-site-isolation-trials");
2017-10-24 12:21:04 +10:00
2022-12-05 09:10:55 +10:00
var success = Cef . Initialize ( settings , performDependencyCheck : false , browserProcessHandler : null ) ;
diagnosticMessageSink . OnMessage ( new DiagnosticMessage ( "Cef Initialized:" + success ) ) ;
2018-09-10 02:41:13 +02:00
}
}
2017-10-24 12:21:04 +10:00
2020-02-29 14:22:48 +01:00
private void CefShutdown ( )
{
if ( Cef . IsInitialized )
{
2022-12-05 09:10:55 +10:00
diagnosticMessageSink . OnMessage ( new DiagnosticMessage ( "Before Cef Shutdown" ) ) ;
2022-09-19 16:34:37 +10:00
Cef . WaitForBrowsersToClose ( ) ;
2022-12-05 09:10:55 +10:00
try
{
Cef . Shutdown ( ) ;
}
catch ( Exception ex )
{
diagnosticMessageSink . OnMessage ( new DiagnosticMessage ( "Cef Shutdown Exception:" + ex . ToString ( ) ) ) ;
}
diagnosticMessageSink . OnMessage ( new DiagnosticMessage ( "After Cef Shutdown" ) ) ;
2020-02-29 14:22:48 +01:00
}
2020-10-13 13:40:46 +10:00
StopProxyServer ( ) ;
2020-02-29 14:22:48 +01:00
}
2020-02-29 13:47:27 +01:00
public Task InitializeAsync ( )
{
2020-03-01 21:28:09 +01:00
return contextThread . Factory . StartNew ( CefInitialize ) ;
2020-02-29 13:47:27 +01:00
}
public Task DisposeAsync ( )
2018-09-10 02:41:13 +02:00
{
2020-03-01 21:28:09 +01:00
return contextThread . Factory . StartNew ( CefShutdown ) ;
2020-02-29 14:22:48 +01:00
}
public void Dispose ( )
{
2020-03-01 21:28:09 +01:00
contextThread . Dispose ( ) ;
2018-09-10 02:41:13 +02:00
}
2020-10-13 13:40:46 +10:00
public void StartProxyServerIfRequired ( )
{
if ( proxyServer = = null )
{
proxyServer = new ProxyServer ( userTrustRootCertificate : false ) ;
var explicitEndPoint = new ExplicitProxyEndPoint ( IPAddress . Loopback , 8080 , false ) ;
// An explicit endpoint is where the client knows about the existence of a proxy
// So client sends request in a proxy friendly manner
proxyServer . AddEndPoint ( explicitEndPoint ) ;
proxyServer . Start ( ) ;
}
}
public void StopProxyServer ( )
{
proxyServer ? . Stop ( ) ;
proxyServer = null ;
}
2018-09-10 02:41:13 +02:00
}
2017-10-24 12:21:04 +10:00
}