2020-07-26 15:54:57 -04:00
using NUnit.Framework ;
using System ;
using System.Linq ;
using System.Windows.Forms ;
using Vanara.PInvoke ;
using static Vanara . PInvoke . Shell32 ;
using Clipboard = Vanara . Windows . Shell . NativeClipboard ;
using WFClipboard = System . Windows . Forms . Clipboard ;
namespace Vanara.Windows.Shell.Tests
{
[TestFixture, SingleThreaded]
public class ClipboardTests
{
private const string html = "<pre style=\"font-family:Consolas;font-size:13px;color:#dadada;\"><span style=\"color:#dcdcaa;\">“We’ ve been here”</span></pre>" ;
[Test]
public void DumpWFClipboardTest ( )
{
TestContext . WriteLine ( $"ContainsAudio: {WFClipboard.ContainsAudio()}" ) ;
TestContext . WriteLine ( $"ContainsData: {WFClipboard.ContainsData(DataFormats.StringFormat)}" ) ;
TestContext . WriteLine ( $"ContainsFileDropList: {WFClipboard.ContainsFileDropList()}" ) ;
TestContext . WriteLine ( $"ContainsImage: {WFClipboard.ContainsImage()}" ) ;
TestContext . WriteLine ( $"ContainsText: {WFClipboard.ContainsText()}" ) ;
TestContext . WriteLine ( $"GetAudioStream: {WFClipboard.GetAudioStream()}" ) ;
TestContext . WriteLine ( $"GetData: {WFClipboard.GetData(DataFormats.StringFormat)}" ) ;
TestContext . WriteLine ( $"GetDataObject: {WFClipboard.GetDataObject()}" ) ;
TestContext . WriteLine ( $"GetFileDropList: {string.Join(" \ n ", WFClipboard.GetFileDropList().Cast<string>())}" ) ;
TestContext . WriteLine ( $"GetImage: {WFClipboard.GetImage()}" ) ;
TestContext . WriteLine ( $"GetText: {WFClipboard.GetText()}" ) ;
}
[Test]
public void EnumFormatsTest ( )
{
using var cb = new Clipboard ( ) ;
var fmts = cb . EnumAvailableFormats ( ) ;
Assert . That ( fmts , Is . Not . Empty ) ;
2022-01-11 20:13:44 -05:00
TestContext . Write ( string . Join ( ", " , fmts . Select ( f = > Clipboard . GetFormatName ( f ) ) ) ) ;
2020-07-26 15:54:57 -04:00
var fmt = fmts . First ( ) ;
2022-01-11 20:13:44 -05:00
Assert . IsTrue ( Clipboard . IsFormatAvailable ( fmt ) ) ;
2020-07-26 15:54:57 -04:00
}
[Test]
public void GetNativeTextTest ( )
{
using var cb = new Clipboard ( ) ;
foreach ( TextDataFormat e in Enum . GetValues ( typeof ( TextDataFormat ) ) )
TestContext . WriteLine ( $"{e}: {cb.GetText(e)}" ) ;
}
[Test]
public void GetPriorityFormatTest ( )
{
2022-01-11 20:13:44 -05:00
var fmts = Clipboard . CurrentlySupportedFormats . ToArray ( ) ;
2020-07-26 15:54:57 -04:00
Assert . That ( Clipboard . GetFirstFormatAvailable ( fmts ) , Is . GreaterThan ( 0 ) ) ;
}
[Test]
public void SetNativeTextHtmlTest ( )
{
using ( var cb = new Clipboard ( ) )
cb . SetText ( html , TextDataFormat . Html ) ;
using ( var cb = new Clipboard ( ) )
{
var outVal = cb . GetText ( TextDataFormat . Html ) ;
Assert . That ( outVal , Is . EqualTo ( html ) ) ;
}
}
[Test]
public void SetNativeTextMultTest ( )
{
const string txt = @"“We’ ve been here”" ;
using var cb = new Clipboard ( ) ;
cb . SetText ( txt , html ) ;
}
2021-08-26 19:28:15 -04:00
2022-01-11 20:13:44 -05:00
//[Test]
2021-08-26 19:28:15 -04:00
public void ChangeEventTest ( )
{
2021-08-29 17:08:08 -04:00
var sawChange = new System . Threading . ManualResetEvent ( false ) ;
2021-08-26 19:28:15 -04:00
Clipboard . ClipboardUpdate + = OnUpdate ;
2021-08-29 17:08:08 -04:00
System . Threading . Thread . SpinWait ( 1000 ) ;
WFClipboard . SetText ( "Hello" ) ;
//using var cb = new Clipboard();
//cb.SetText("Hello");
Assert . IsTrue ( sawChange . WaitOne ( 5000 ) ) ;
2021-08-26 19:28:15 -04:00
Clipboard . ClipboardUpdate - = OnUpdate ;
2021-08-29 17:08:08 -04:00
void OnUpdate ( object sender , EventArgs e ) = > sawChange . Set ( ) ;
2021-08-26 19:28:15 -04:00
}
2020-07-26 15:54:57 -04:00
}
}