Merge pull request #217 from QuickJack/master
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / CommonDialogsTest.cs
1 //
2 // CommonDialogsTest.cs: Tests for common dialogs.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
24 //
25 // Authors:
26 //      Alexander Olk <alex.olk@googlemail.com>
27 //
28
29 using System;
30 using System.Collections;
31 using System.Drawing;
32 using System.Drawing.Printing;
33 using System.Windows.Forms;
34 using System.IO;
35 using System.ComponentModel;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Windows.Forms
39 {
40         [TestFixture]
41         public class CommonDialogsTest : TestHelper
42         {
43                 OpenFileDialog ofd;
44                 SaveFileDialog sfd;
45                 FontDialog fd;
46                 FolderBrowserDialog fbd;
47                 ColorDialog cd;
48                 
49                 [Test]
50                 public void ColorDialogTest ()
51                 {
52                         cd = new ColorDialog ();
53                         
54                         Assert.AreEqual (Color.Black, cd.Color, "#1");
55                         Assert.IsTrue (cd.AllowFullOpen, "#2");
56                         Assert.IsFalse (cd.AnyColor, "#3");
57                         Assert.IsFalse (cd.FullOpen, "#4");
58                         Assert.IsNotNull (cd.CustomColors, "#5");
59                         Assert.IsFalse (cd.ShowHelp, "#6");
60                         Assert.IsFalse (cd.SolidColorOnly, "#7");
61                         Assert.AreEqual ("System.Windows.Forms.ColorDialog,  Color: Color [Black]", cd.ToString (), "#8");
62                         
63                         cd.Color = Color.Red;
64                         Assert.AreEqual (Color.Red, cd.Color, "#9");
65                         
66                         cd.AllowFullOpen = false;
67                         cd.FullOpen = true;
68                         Assert.IsTrue (cd.FullOpen, "#10");
69                         
70                         int[] custom_colors = new int[] {Color.Yellow.ToArgb (), Color.Red.ToArgb ()};
71                         cd.CustomColors = custom_colors;
72                         Assert.IsNotNull (cd.CustomColors, "#10a");
73                         Assert.AreEqual (16, cd.CustomColors.Length, "#10aa");
74                         Assert.AreEqual (Color.Red.ToArgb (), cd.CustomColors[1], "#10ab");
75                         Assert.AreEqual (Color.FromArgb(0, 255, 255, 255).ToArgb (), cd.CustomColors[15], "#10ac");
76                         
77                         cd.CustomColors = null;
78                         Assert.IsNotNull (cd.CustomColors, "#10b");
79                         Assert.AreEqual (16, cd.CustomColors.Length, "#10bb");
80                         Assert.AreEqual (Color.FromArgb(0, 255, 255, 255).ToArgb (), cd.CustomColors[0], "#10bc");
81                         
82                         cd.AllowFullOpen = true;
83                         cd.CustomColors = custom_colors;
84                         Assert.IsNotNull (cd.CustomColors, "#10c");
85                         Assert.AreEqual (16, cd.CustomColors.Length, "#10cc");
86                 }
87                 
88                 [Test]
89                 public void OpenFileDialogTest ()
90                 {
91                         ofd = new OpenFileDialog ();
92                         
93                         Assert.IsTrue (ofd.AddExtension, "#11");
94                         Assert.IsTrue (ofd.CheckFileExists, "#12");
95                         Assert.IsTrue (ofd.CheckPathExists, "#13");
96                         Assert.AreEqual ("", ofd.DefaultExt, "#14");
97                         Assert.IsTrue (ofd.DereferenceLinks, "#15");
98                         Assert.AreEqual ("", ofd.FileName, "#16");
99                         Assert.IsNotNull (ofd.FileNames, "#17");
100                         Assert.AreEqual (0, ofd.FileNames.Length, "#17a");
101                         Assert.AreEqual ("", ofd.Filter, "#18");
102                         Assert.AreEqual (1, ofd.FilterIndex, "#19");
103                         Assert.AreEqual ("", ofd.InitialDirectory, "#20");
104                         Assert.IsFalse (ofd.Multiselect, "#21");
105                         Assert.IsFalse (ofd.ReadOnlyChecked, "#22");
106                         Assert.IsFalse (ofd.RestoreDirectory, "#23");
107                         Assert.IsFalse (ofd.ShowHelp, "#24");
108                         Assert.IsFalse (ofd.ShowReadOnly, "#25");
109                         Assert.AreEqual ("", ofd.Title, "#26");
110                         Assert.IsTrue (ofd.ValidateNames, "#27");
111                         Assert.AreEqual ("System.Windows.Forms.OpenFileDialog: Title: , FileName: ", ofd.ToString (), "#28");
112                         
113                         ofd.DefaultExt = ".TXT";
114                         Assert.AreEqual ("TXT", ofd.DefaultExt, "#29");
115                         
116                         ofd.Filter = null;
117                         Assert.AreEqual ("", ofd.Filter, "#30");
118                         
119                         ofd.Filter = "Text (*.txt)|*.txt|All (*.*)|*.*";
120                         
121                         try {
122                                 ofd.Filter = "abcd";
123                         } catch (Exception) {
124                         }
125                         
126                         Assert.AreEqual ("Text (*.txt)|*.txt|All (*.*)|*.*", ofd.Filter, "#30a");
127                         
128                         ofd.FilterIndex = 10;
129                         Assert.AreEqual (10, ofd.FilterIndex, "#30aa");
130                         
131                         ofd.Filter = null;
132                         Assert.AreEqual ("", ofd.Filter, "#30b");
133                         Assert.AreEqual (10, ofd.FilterIndex, "#30ba");
134                         
135                         string current_path = Environment.CurrentDirectory;
136                         string current_file = Path.Combine(current_path, "test_file");
137                         if (!File.Exists (current_file))
138                                 File.Create (current_file);
139                         
140                         ofd.FileName = current_file;
141                         
142                         Assert.AreEqual (current_file, ofd.FileName, "#31");
143                         
144                         string[] file_names = ofd.FileNames;
145                         Assert.AreEqual (current_file, file_names [0], "#32");
146                         
147                         ofd.Title = "Test";
148                         Assert.AreEqual ("System.Windows.Forms.OpenFileDialog: Title: Test, FileName: " + current_file, ofd.ToString (), "#33");
149                         
150                         ofd.FileName = null;
151                         Assert.AreEqual ("", ofd.FileName, "#33a");
152                         Assert.IsNotNull (ofd.FileNames, "#33b");
153                         Assert.AreEqual (0, ofd.FileNames.Length, "#33c");
154                         
155                         ofd.Reset ();
156                         
157                         // check again
158                         Assert.IsTrue (ofd.AddExtension, "#34");
159                         Assert.IsTrue (ofd.CheckFileExists, "#35");
160                         Assert.IsTrue (ofd.CheckPathExists, "#36");
161                         Assert.AreEqual ("", ofd.DefaultExt, "#37");
162                         Assert.IsTrue (ofd.DereferenceLinks, "#38");
163                         Assert.AreEqual ("", ofd.FileName, "#39");
164                         Assert.IsNotNull (ofd.FileNames, "#40");
165                         Assert.AreEqual ("", ofd.Filter, "#41");
166                         Assert.AreEqual (1, ofd.FilterIndex, "#42");
167                         Assert.AreEqual ("", ofd.InitialDirectory, "#43");
168                         Assert.IsFalse (ofd.Multiselect, "#44");
169                         Assert.IsFalse (ofd.ReadOnlyChecked, "#45");
170                         Assert.IsFalse (ofd.RestoreDirectory, "#46");
171                         Assert.IsFalse (ofd.ShowHelp, "#47");
172                         Assert.IsFalse (ofd.ShowReadOnly, "#48");
173                         Assert.AreEqual ("", ofd.Title, "#49");
174                         Assert.IsTrue (ofd.ValidateNames, "#50");
175                         Assert.AreEqual ("System.Windows.Forms.OpenFileDialog: Title: , FileName: ", ofd.ToString (), "#60");
176                 }
177                 
178                 [Test]
179                 [ExpectedException(typeof(ArgumentException))]
180                 public void FileDialogFilterArgumentException () {
181                         if (ofd == null)
182                                 ofd = new OpenFileDialog ();
183                         
184                         ofd.Filter = "xyafj";
185                 }
186                 
187                 [Test]
188                 public void SaveFileDialogTest ()
189                 {
190                         // most of the OpenFileDialogTest are also valid for SaveFileDialg
191                         sfd = new SaveFileDialog ();
192                         
193                         Assert.IsFalse (sfd.CreatePrompt, "#61");
194                         
195                         Assert.IsTrue (sfd.OverwritePrompt, "#62");
196                 }
197                 
198                 [Test]
199                 public void FontDialogTest ()
200                 {
201                         fd = new FontDialog ();
202                         
203                         Assert.IsTrue (fd.AllowScriptChange, "#63");
204                         Assert.IsTrue (fd.AllowSimulations, "#64");
205                         Assert.IsTrue (fd.AllowVectorFonts, "#65");
206                         Assert.IsTrue (fd.AllowVerticalFonts, "#66");
207                         
208                         Assert.AreEqual (Color.Black, fd.Color, "#67");
209                         Assert.IsFalse (fd.FixedPitchOnly, "#68");
210                         
211                         //Assert.AreEqual ("[Font: Name=Microsoft Sans Serif, Size=8,25, Units=3, GdiCharSet=0, GdiVerticalFont=False]", fd.Font.ToString (), "#69");
212                         
213                         Assert.IsFalse (fd.FontMustExist, "#70");
214                         
215                         Assert.AreEqual (0, fd.MaxSize, "#71");
216                         Assert.AreEqual (0, fd.MinSize, "#72");
217                         
218                         Assert.IsFalse (fd.ScriptsOnly, "#73");
219                         
220                         Assert.IsFalse (fd.ShowApply, "#74");
221                         
222                         Assert.IsFalse (fd.ShowColor, "#75");
223                         
224                         Assert.IsTrue (fd.ShowEffects, "#76");
225                         
226                         Assert.IsFalse (fd.ShowHelp, "#77");
227                         
228                         //Assert.AreEqual ("System.Windows.Forms.FontDialog,  Font: [Font: Name=Microsoft Sans Serif, Size=8,25, Units=3, GdiCharSet=0, GdiVerticalFont=False]", fd.ToString (), "#78");
229                         
230                         fd.MaxSize = -1;
231                         Assert.AreEqual (0, fd.MaxSize, "#79");
232                         
233                         fd.MinSize = -1;
234                         Assert.AreEqual (0, fd.MinSize, "#80");
235                         
236                         fd.MinSize = 24;
237                         fd.MaxSize = 10;
238                         Assert.AreEqual (10, fd.MinSize, "#81");
239                         
240                         fd.MinSize = 48;
241                         Assert.AreEqual (48, fd.MaxSize, "#82");
242                 }
243                 
244                 [Test]
245                 [NUnit.Framework.Category ("NotWorking")]
246                 /* a few people have been seeing the following:
247 Failures:
248 1) MonoTests.System.Windows.Forms.CommonDialogsTest.FolderBrowserDialogTest : System.ArgumentException : oldValue is the empty string.
249   at System.String.Replace (System.String oldValue, System.String newValue) [0x00054] in /tmp/scratch/BUILD/mono-1.2.70507/mcs/class/corlib/System/String.cs:1285 
250   at System.Windows.Forms.FolderBrowserDialog+FolderBrowserTreeView.SetSelectedPath (System.String path) [0x00061] in /tmp/scratch/BUILD/mono-1.2.70507/mcs/class/Managed.Windows.Forms/System.Windows.Forms/FolderBrowserDialog.cs:460 
251   at System.Windows.Forms.FolderBrowserDialog+FolderBrowserTreeView.set_SelectedPath (System.String value) [0x0000c] in /tmp/scratch/BUILD/mono-1.2.70507/mcs/class/Managed.Windows.Forms/System.Windows.Forms/FolderBrowserDialog.cs:363 
252   at (wrapper remoting-invoke-with-check) FolderBrowserTreeView:set_SelectedPath (string)
253   at System.Windows.Forms.FolderBrowserDialog.set_SelectedPath (System.String value) [0x0001a] in /tmp/scratch/BUILD/mono-1.2.70507/mcs/class/Managed.Windows.Forms/System.Windows.Forms/FolderBrowserDialog.cs:223 
254   at (wrapper remoting-invoke-with-check) System.Windows.Forms.FolderBrowserDialog:set_SelectedPath (string)
255   at MonoTests.System.Windows.Forms.CommonDialogsTest.FolderBrowserDialogTest () [0x00094] in /tmp/scratch/BUILD/mono-1.2.70507/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/CommonDialogsTest.cs:260 
256   at <0x00000> <unknown method>
257   at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[])
258   at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00040] in /tmp/scratch/BUILD/mono-1.2.70507/mcs/class/corlib/System.Reflection/MonoMethod.cs:143 
259                 */
260                 public void FolderBrowserDialogTest ()
261                 {
262                         fbd = new FolderBrowserDialog ();
263                         
264                         Assert.AreEqual ("", fbd.Description, "#83");
265                         
266                         Assert.AreEqual (Environment.SpecialFolder.Desktop, fbd.RootFolder, "#84");
267                         
268                         Assert.AreEqual ("", fbd.SelectedPath, "#85");
269                         
270                         Assert.IsTrue (fbd.ShowNewFolderButton, "#86");
271                         
272                         Assert.AreEqual ("System.Windows.Forms.FolderBrowserDialog", fbd.ToString (), "#87");
273                         
274                         string current_path = Environment.CurrentDirectory;
275                         fbd.SelectedPath = current_path;
276                         
277                         Assert.AreEqual (current_path, fbd.SelectedPath, "#89");
278                 }
279                 
280                 [Test]
281                 [ExpectedException(typeof(InvalidEnumArgumentException))]
282                 public void FolderBrowserDialogInvalidEnumArgumentExceptionTest () {
283                         if (fbd == null)
284                                 fbd = new FolderBrowserDialog ();
285                         
286                         fbd.RootFolder = (Environment.SpecialFolder)12;
287                 }
288
289 #if NET_2_0
290                 [Test]
291                 public void CommonDialogPropertyTag ()
292                 {
293                         MyDialog md = new MyDialog ();
294                         object s = "MyString";
295                         
296                         Assert.AreEqual (null, md.Tag, "A1");
297                         
298                         md.Tag = s;
299                         Assert.AreSame (s, md.Tag, "A2");
300                 }
301                 
302                 private class MyDialog : CommonDialog
303                 {
304                         public override void Reset ()
305                         {
306                                 throw new Exception ("The method or operation is not implemented.");
307                         }
308
309                         protected override bool RunDialog (IntPtr hwndOwner)
310                         {
311                                 throw new Exception ("The method or operation is not implemented.");
312                         }
313                 }
314 #endif
315         }
316 }
317