using System; using System.Runtime.InteropServices; using System.Threading; using UnitTestSharp; namespace Foundations.UnitTests { public class CrashDialogTests : TestFixture { public class CtorTests : TestFixture { public void NoReport_Throws() { CheckThrow(typeof(ArgumentNullException)); new CrashDialog(null); } public void ShowsTheReport() { using (var dialog = new CrashDialog("boom")) { CheckEqual("boom", dialog.ReportBox.Text); CheckTrue(dialog.ReportBox.ReadOnly); } } public void GivesEveryLineBreakTheSystemsOwn() { using (var dialog = new CrashDialog("one\ntwo\r\nthree")) { CheckEqual($"one{Environment.NewLine}two{Environment.NewLine}three", dialog.ReportBox.Text); } } public void OffersToCopy() { using (var dialog = new CrashDialog("boom")) { CheckEqual(CrashDialog.CopyCaption, dialog.CopyButton.Text); } } } public class CopyTests : TestFixture { public void PutsTheReportOnTheClipboard() { string copied = null; using (var dialog = new CrashDialog("boom", text => copied = text)) { dialog.Copy(); CheckEqual("boom", copied); CheckEqual(CrashDialog.CopiedCaption, dialog.CopyButton.Text); } } public void AClipboardThatIsHeld_SaysItCouldNot() { using (var dialog = new CrashDialog("boom", text => throw new ExternalException())) { dialog.Copy(); CheckEqual(CrashDialog.CopyFailedCaption, dialog.CopyButton.Text); } } } public class RunOnStaThreadTests : TestFixture { [IgnoreTest] private void RunOn(ApartmentState apartment, System.Action action) { var thread = new Thread(() => action()); thread.SetApartmentState(apartment); thread.Start(); thread.Join(); } public void FromAnStaThread_RunsOnIt() { int caller = 0; int ran = -1; RunOn(ApartmentState.STA, () => { caller = Thread.CurrentThread.ManagedThreadId; CrashDialog.RunOnStaThread(() => ran = Thread.CurrentThread.ManagedThreadId); }); CheckEqual(caller, ran); } public void FromAnMtaThread_HasRunOnAnStaThreadByTheTimeItReturns() { ApartmentState? ranOn = null; ApartmentState? seenAfterwards = null; RunOn(ApartmentState.MTA, () => { CrashDialog.RunOnStaThread(() => ranOn = Thread.CurrentThread.GetApartmentState()); seenAfterwards = ranOn; }); CheckEqual((ApartmentState?)ApartmentState.STA, seenAfterwards); } } } }