1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
23 // Carlos Alberto Cortez <ccortes@novell.com>
27 using System.CodeDom.Compiler;
28 using System.Diagnostics;
31 using System.Windows.Forms;
32 using NUnit.Framework;
34 namespace MonoTests.System.Windows.Forms
37 public class ClipboardTest
40 public void UnicodeTextTest ()
42 // Put some unicode chars
43 string text = "hello \u1000 mono!";
44 Clipboard.SetText (text);
46 Assert.AreEqual (true, Clipboard.ContainsText (TextDataFormat.UnicodeText), "#A1");
47 Assert.AreEqual (text, Clipboard.GetText (TextDataFormat.UnicodeText), "#A2");
53 public void RtfTextTest ()
55 string rtf_text = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}}{\*\generator Mono RichTextBox;}\pard\f0\fs16 hola\par}";
56 string plain_text = "hola";
58 Clipboard.SetText (rtf_text, TextDataFormat.Rtf);
60 Assert.AreEqual (false, Clipboard.ContainsText (TextDataFormat.Text), "#A1");
61 Assert.AreEqual (false, Clipboard.ContainsText (TextDataFormat.UnicodeText), "#A2");
62 Assert.AreEqual (true, Clipboard.ContainsText (TextDataFormat.Rtf), "#A3");
63 Assert.AreEqual (rtf_text, Clipboard.GetText (TextDataFormat.Rtf), "#A4");
65 // Now use a IDataObject, so we can have more than one format at the time
66 DataObject data = new DataObject ();
67 data.SetData (DataFormats.Rtf, rtf_text);
68 data.SetData (DataFormats.UnicodeText, plain_text);
70 Clipboard.SetDataObject (data);
72 Assert.AreEqual (true, Clipboard.ContainsText (TextDataFormat.Text), "#B1");
73 Assert.AreEqual (true, Clipboard.ContainsText (TextDataFormat.UnicodeText), "#B2");
74 Assert.AreEqual (true, Clipboard.ContainsText (TextDataFormat.Rtf), "#B3");
75 Assert.AreEqual (rtf_text, Clipboard.GetText (TextDataFormat.Rtf), "#B4");
76 Assert.AreEqual (plain_text, Clipboard.GetText (), "#B5");
82 public void CustomSerializableData ()
84 CustomSerializableClass obj = new CustomSerializableClass ();
88 Clipboard.SetData ("CustomSerializable", obj);
90 //Assert.AreEqual (true, Clipboard.ContainsData ("CustomSerializable"), "#A1");
92 CustomSerializableClass obj2 = (CustomSerializableClass)Clipboard.GetData ("CustomSerializable");
94 Assert.AreEqual ("mono101", obj2.Name, "#B1");
95 Assert.AreEqual (-3, obj2.Id, "#B2");
99 private class CustomSerializableClass
106 [Category ("NotWorking")] // Doesn't work under Xvfb.
107 public void DataRemainsOnClipboard_Xamarin4959 ()
109 // Compile an app that puts something on the clipboard
112 using System.Windows.Forms;
113 public static class MainClass
115 public static void Main ()
117 Clipboard.SetDataObject (""testing bug 4959"", true, 10, 100);
121 var exeName = Path.GetTempFileName ();
123 var parameters = new CompilerParameters ();
124 parameters.GenerateExecutable = true;
125 parameters.ReferencedAssemblies.Add ("System.Windows.Forms.dll");
126 parameters.OutputAssembly = exeName;
127 var compiler = CodeDomProvider.CreateProvider ("CSharp");
128 var compilerResults = compiler.CompileAssemblyFromSource (parameters, source);
129 Assert.AreEqual (0, compilerResults.Errors.Count);
132 using (var app = Process.Start (exeName)) {
136 // Text should still be on the clipboard
137 Assert.AreEqual ("testing bug 4959", Clipboard.GetText ());
139 File.Delete (exeName);
144 public void DataGetsCleared_Xamarin4959 ()
146 // This is the reverse of the previous test
148 // Compile an app that puts something on the clipboard
151 using System.Windows.Forms;
152 public static class MainClass
154 public static void Main ()
156 Clipboard.SetDataObject (""testing bug 4959"", false, 10, 100);
160 var exeName = Path.GetTempFileName ();
162 var parameters = new CompilerParameters ();
163 parameters.GenerateExecutable = true;
164 parameters.ReferencedAssemblies.Add ("System.Windows.Forms.dll");
165 parameters.OutputAssembly = exeName;
166 var compiler = CodeDomProvider.CreateProvider ("CSharp");
167 var compilerResults = compiler.CompileAssemblyFromSource (parameters, source);
168 Assert.AreEqual (0, compilerResults.Errors.Count);
171 using (var app = Process.Start (exeName)) {
175 // Text should no longer be on the clipboard
176 Assert.IsTrue (string.IsNullOrEmpty (Clipboard.GetText ()));
178 File.Delete (exeName);