Unit test for bug Xamarin-4959
authorEberhard Beilharz <eb1@sil.org>
Wed, 9 May 2012 08:49:42 +0000 (10:49 +0200)
committerEberhard Beilharz <eb1@sil.org>
Mon, 21 May 2012 09:12:23 +0000 (11:12 +0200)
Because the clipboard is a shared resource between applications
it is possible that the test sometimes fails for wrong reasons
if another app manipulates the clipboard just at the right time.

Change-Id: I1a52b62d2958ece35c8927172d94ab6ccb3183b0

mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ClipboardTest.cs

index 66832bc9131f25a8912e3bbe6d9f86cac102fd47..f90a1b98c1274b9676dba4f8550b28333a30fc36 100644 (file)
@@ -24,6 +24,9 @@
 //
 
 using System;
+using System.CodeDom.Compiler;
+using System.Diagnostics;
+using System.IO;
 using System.Text;
 using System.Windows.Forms;
 using NUnit.Framework;
@@ -99,6 +102,82 @@ namespace MonoTests.System.Windows.Forms
                        public string Name;
                        public int Id;
                }
+
+               [Test]
+               public void DataRemainsOnClipboard_Xamarin4959 ()
+               {
+                       // Compile an app that puts something on the clipboard
+                       var source = @"
+using System;
+using System.Windows.Forms;
+public static class MainClass
+{
+       public static void Main ()
+       {
+               Clipboard.SetDataObject (""testing bug 4959"", true, 10, 100);
+       }
+}
+";
+                       var exeName = Path.GetTempFileName ();
+                       try {
+                               var parameters = new CompilerParameters ();
+                               parameters.GenerateExecutable = true;
+                               parameters.ReferencedAssemblies.Add ("System.Windows.Forms.dll");
+                               parameters.OutputAssembly = exeName;
+                               var compiler = CodeDomProvider.CreateProvider ("CSharp");
+                               var compilerResults = compiler.CompileAssemblyFromSource (parameters, source);
+                               Assert.AreEqual (0, compilerResults.Errors.Count);
+
+                               // Execute the app
+                               using (var app = Process.Start (exeName)) {
+                                       app.WaitForExit ();
+                               }
+
+                               // Text should still be on the clipboard
+                               Assert.AreEqual ("testing bug 4959", Clipboard.GetText ());
+                       } finally {
+                               File.Delete (exeName);
+                       }
+               }
+
+               [Test]
+               public void DataGetsCleared_Xamarin4959 ()
+               {
+                       // This is the reverse of the previous test
+
+                       // Compile an app that puts something on the clipboard
+                       var source = @"
+using System;
+using System.Windows.Forms;
+public static class MainClass
+{
+       public static void Main ()
+       {
+               Clipboard.SetDataObject (""testing bug 4959"", false, 10, 100);
+       }
+}
+";
+                       var exeName = Path.GetTempFileName ();
+                       try {
+                               var parameters = new CompilerParameters ();
+                               parameters.GenerateExecutable = true;
+                               parameters.ReferencedAssemblies.Add ("System.Windows.Forms.dll");
+                               parameters.OutputAssembly = exeName;
+                               var compiler = CodeDomProvider.CreateProvider ("CSharp");
+                               var compilerResults = compiler.CompileAssemblyFromSource (parameters, source);
+                               Assert.AreEqual (0, compilerResults.Errors.Count);
+
+                               // Execute the app
+                               using (var app = Process.Start (exeName)) {
+                                       app.WaitForExit ();
+                               }
+
+                               // Text should no longer be on the clipboard
+                               Assert.IsTrue (string.IsNullOrEmpty (Clipboard.GetText ()));
+                       } finally {
+                               File.Delete (exeName);
+                       }
+               }
 #endif
        }
 }