[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ClipboardTest.cs
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
19 //
20 // Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Carlos Alberto Cortez <ccortes@novell.com>
24 //
25
26 using System;
27 using System.CodeDom.Compiler;
28 using System.Diagnostics;
29 using System.IO;
30 using System.Text;
31 using System.Windows.Forms;
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Windows.Forms
35 {
36         [TestFixture]
37         public class ClipboardTest
38         {
39                 [Test]
40                 public void UnicodeTextTest ()
41                 {
42                         // Put some unicode chars
43                         string text = "hello \u1000 mono!";
44                         Clipboard.SetText (text);
45
46                         Assert.AreEqual (true, Clipboard.ContainsText (TextDataFormat.UnicodeText), "#A1");
47                         Assert.AreEqual (text, Clipboard.GetText (TextDataFormat.UnicodeText), "#A2");
48
49                         Clipboard.Clear ();
50                 }
51
52                 [Test]
53                 public void RtfTextTest ()
54                 {
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";
57
58                         Clipboard.SetText (rtf_text, TextDataFormat.Rtf);
59
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");
64
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);
69
70                         Clipboard.SetDataObject (data);
71
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");
77
78                         Clipboard.Clear ();
79                 }
80
81                 [Test]
82                 public void CustomSerializableData ()
83                 {
84                         CustomSerializableClass obj = new CustomSerializableClass ();
85                         obj.Name = "mono101";
86                         obj.Id = -3;
87
88                         Clipboard.SetData ("CustomSerializable", obj);
89
90                         //Assert.AreEqual (true, Clipboard.ContainsData ("CustomSerializable"), "#A1");
91
92                         CustomSerializableClass obj2 = (CustomSerializableClass)Clipboard.GetData ("CustomSerializable");
93
94                         Assert.AreEqual ("mono101", obj2.Name, "#B1");
95                         Assert.AreEqual (-3, obj2.Id, "#B2");
96                 }
97
98                 [Serializable]
99                 private class CustomSerializableClass 
100                 {
101                         public string Name;
102                         public int Id;
103                 }
104
105                 [Test]
106                 [Category ("NotWorking")] // Doesn't work under Xvfb.
107                 public void DataRemainsOnClipboard_Xamarin4959 ()
108                 {
109                         // Compile an app that puts something on the clipboard
110                         var source = @"
111 using System;
112 using System.Windows.Forms;
113 public static class MainClass
114 {
115         public static void Main ()
116         {
117                 Clipboard.SetDataObject (""testing bug 4959"", true, 10, 100);
118         }
119 }
120 ";
121                         var exeName = Path.GetTempFileName ();
122                         try {
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);
130
131                                 // Execute the app
132                                 using (var app = Process.Start (exeName)) {
133                                         app.WaitForExit ();
134                                 }
135
136                                 // Text should still be on the clipboard
137                                 Assert.AreEqual ("testing bug 4959", Clipboard.GetText ());
138                         } finally {
139                                 File.Delete (exeName);
140                         }
141                 }
142
143                 [Test]
144                 public void DataGetsCleared_Xamarin4959 ()
145                 {
146                         // This is the reverse of the previous test
147
148                         // Compile an app that puts something on the clipboard
149                         var source = @"
150 using System;
151 using System.Windows.Forms;
152 public static class MainClass
153 {
154         public static void Main ()
155         {
156                 Clipboard.SetDataObject (""testing bug 4959"", false, 10, 100);
157         }
158 }
159 ";
160                         var exeName = Path.GetTempFileName ();
161                         try {
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);
169
170                                 // Execute the app
171                                 using (var app = Process.Start (exeName)) {
172                                         app.WaitForExit ();
173                                 }
174
175                                 // Text should no longer be on the clipboard
176                                 Assert.IsTrue (string.IsNullOrEmpty (Clipboard.GetText ()));
177                         } finally {
178                                 File.Delete (exeName);
179                         }
180                 }
181         }
182 }
183