2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / SaveFileDialogTest.cs
1 //
2 // SaveFileDialogTest.cs: Tests for SaveFileDialog class.
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) 2007 Gert Driesen
24 //
25 // Authors:
26 //      Gert Driesen (drieseng@user.sourceforge.net)
27 //
28
29 using System;
30 using System.IO;
31 using System.Text;
32 using System.Windows.Forms;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Windows.Forms
37 {
38         [TestFixture]
39         public class SaveFileDialogTest : TestHelper
40         {
41                 [Test]
42                 public void AddExtension ()
43                 {
44                         SaveFileDialog sfd = new SaveFileDialog ();
45                         Assert.IsTrue (sfd.AddExtension, "#1");
46                         sfd.AddExtension = false;
47                         Assert.IsFalse (sfd.AddExtension, "#2");
48                 }
49
50                 [Test]
51                 public void CheckFileExists ()
52                 {
53                         SaveFileDialog sfd = new SaveFileDialog ();
54                         Assert.IsFalse (sfd.CheckFileExists, "#1");
55                         sfd.CheckFileExists = true;
56                         Assert.IsTrue (sfd.CheckFileExists, "#2");
57                 }
58
59                 [Test]
60                 public void CheckPathExists ()
61                 {
62                         SaveFileDialog sfd = new SaveFileDialog ();
63                         Assert.IsTrue (sfd.CheckPathExists, "#1");
64                         sfd.CheckPathExists = false;
65                         Assert.IsFalse (sfd.CheckPathExists, "#2");
66                 }
67
68                 [Test]
69                 public void CreatePrompt ()
70                 {
71                         SaveFileDialog sfd = new SaveFileDialog ();
72                         Assert.IsFalse (sfd.CreatePrompt, "#1");
73                         sfd.CreatePrompt = true;
74                         Assert.IsTrue (sfd.CreatePrompt, "#2");
75                 }
76
77                 [Test]
78                 public void DefaultExt ()
79                 {
80                         SaveFileDialog sfd = new SaveFileDialog ();
81                         Assert.IsNotNull (sfd.DefaultExt, "#A1");
82                         Assert.AreEqual (string.Empty, sfd.DefaultExt, "#A2");
83
84                         sfd.DefaultExt = "txt";
85                         Assert.IsNotNull (sfd.DefaultExt, "#B1");
86                         Assert.AreEqual ("txt", sfd.DefaultExt, "#B2");
87
88                         sfd.DefaultExt = null;
89                         Assert.IsNotNull (sfd.DefaultExt, "#C1");
90                         Assert.AreEqual (string.Empty, sfd.DefaultExt, "#C2");
91
92                         sfd.DefaultExt = ".Xml";
93                         Assert.IsNotNull (sfd.DefaultExt, "#D1");
94                         Assert.AreEqual ("Xml", sfd.DefaultExt, "#D2");
95
96                         sfd.DefaultExt = ".tar.gz";
97                         Assert.IsNotNull (sfd.DefaultExt, "#E1");
98                         Assert.AreEqual ("tar.gz", sfd.DefaultExt, "#E2");
99
100                         sfd.DefaultExt = "..Xml";
101                         Assert.IsNotNull (sfd.DefaultExt, "#F1");
102                         Assert.AreEqual (".Xml", sfd.DefaultExt, "#F2");
103
104                         sfd.DefaultExt = "tar.gz";
105                         Assert.IsNotNull (sfd.DefaultExt, "#G1");
106                         Assert.AreEqual ("tar.gz", sfd.DefaultExt, "#G2");
107
108                         sfd.DefaultExt = ".";
109                         Assert.IsNotNull (sfd.DefaultExt, "#H1");
110                         Assert.AreEqual (string.Empty, sfd.DefaultExt, "#H2");
111                 }
112
113                 [Test]
114                 public void DereferenceLinks ()
115                 {
116                         SaveFileDialog sfd = new SaveFileDialog ();
117                         Assert.IsTrue (sfd.DereferenceLinks, "#1");
118                         sfd.DereferenceLinks = false;
119                         Assert.IsFalse (sfd.DereferenceLinks, "#2");
120                 }
121
122                 [Test]
123                 public void FileName ()
124                 {
125                         SaveFileDialog sfd = new SaveFileDialog ();
126                         Assert.IsNotNull (sfd.FileName, "#A1");
127                         Assert.AreEqual (string.Empty, sfd.FileName, "#A2");
128
129                         sfd.FileName = "default.build";
130                         Assert.IsNotNull (sfd.FileName, "#B1");
131                         Assert.AreEqual ("default.build", sfd.FileName, "#B2");
132
133                         sfd.FileName = null;
134                         Assert.IsNotNull (sfd.FileName, "#C1");
135                         Assert.AreEqual (string.Empty, sfd.FileName, "#C2");
136
137                         sfd.FileName = string.Empty;
138                         Assert.IsNotNull (sfd.FileName, "#D1");
139                         Assert.AreEqual (string.Empty, sfd.FileName, "#D2");
140                 }
141
142                 [Test]
143                 public void FileName_InvalidPathCharacter ()
144                 {
145                         SaveFileDialog sfd = new SaveFileDialog ();
146                         sfd.FileName = Path.InvalidPathChars [0] + "file";
147 #if NET_2_0
148                         Assert.IsNotNull (sfd.FileName, "#1");
149                         Assert.AreEqual (Path.InvalidPathChars [0] + "file", sfd.FileName, "#2");
150 #else
151                         try {
152                                 string fileName = sfd.FileName;
153                                 Assert.Fail ("#1: " + fileName);
154                         } catch (ArgumentException ex) {
155                                 // The path contains illegal characters
156                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
157                                 Assert.IsNull (ex.InnerException, "#3");
158                                 Assert.IsNotNull (ex.Message, "#4");
159                                 //Assert.IsNull (ex.ParamName, "#5");
160                         }
161 #endif
162                 }
163
164                 [Test]
165                 public void FileNames ()
166                 {
167                         SaveFileDialog sfd = new SaveFileDialog ();
168                         Assert.IsNotNull (sfd.FileNames, "#A1");
169                         Assert.AreEqual (0, sfd.FileNames.Length, "#A2");
170
171                         sfd.FileName = "default.build";
172                         Assert.IsNotNull (sfd.FileNames, "#B1");
173                         Assert.AreEqual (1, sfd.FileNames.Length, "#B2");
174                         Assert.AreEqual ("default.build", sfd.FileNames [0], "#B3");
175
176                         sfd.FileName = null;
177                         Assert.IsNotNull (sfd.FileNames, "#C1");
178                         Assert.AreEqual (0, sfd.FileNames.Length, "#C2");
179                 }
180
181                 [Test]
182                 public void FileNames_InvalidPathCharacter ()
183                 {
184                         SaveFileDialog sfd = new SaveFileDialog ();
185                         sfd.FileName = Path.InvalidPathChars [0] + "file";
186 #if NET_2_0
187                         Assert.IsNotNull (sfd.FileNames, "#1");
188                         Assert.AreEqual (1, sfd.FileNames.Length, "#2");
189                         Assert.AreEqual (Path.InvalidPathChars [0] + "file", sfd.FileNames [0], "#3");
190 #else
191                         try {
192                                 string [] fileNames = sfd.FileNames;
193                                 Assert.Fail ("#1: " + fileNames.Length);
194                         } catch (ArgumentException ex) {
195                                 // The path contains illegal characters
196                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
197                                 Assert.IsNull (ex.InnerException, "#3");
198                                 Assert.IsNotNull (ex.Message, "#4");
199                                 //Assert.IsNull (ex.ParamName, "#5");
200                         }
201 #endif
202                 }
203
204                 [Test]
205                 public void Filter ()
206                 {
207                         SaveFileDialog sfd = new SaveFileDialog ();
208                         Assert.IsNotNull (sfd.Filter, "#A1");
209                         Assert.AreEqual (string.Empty, sfd.Filter, "#A2");
210
211                         sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
212                         Assert.IsNotNull (sfd.Filter, "#B1");
213                         Assert.AreEqual ("Text files (*.txt)|*.txt|All files (*.*)|*.*", sfd.Filter, "#B2");
214
215                         sfd.Filter = null;
216                         Assert.IsNotNull (sfd.Filter, "#C1");
217                         Assert.AreEqual (string.Empty, sfd.Filter, "#C2");
218
219                         sfd.Filter = string.Empty;
220                         Assert.IsNotNull (sfd.Filter, "#D1");
221                         Assert.AreEqual (string.Empty, sfd.Filter, "#D2");
222                 }
223
224                 [Test]
225                 public void Filter_InvalidFormat ()
226                 {
227                         SaveFileDialog sfd = new SaveFileDialog ();
228                         try {
229                                 sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)";
230                                 Assert.Fail ("#1");
231                         } catch (ArgumentException ex) {
232                                 // The provided filter string is invalid. The filter string
233                                 // should contain a description of the filter, followed by the
234                                 // vertical bar (|) and the filter pattern. The strings for
235                                 // different filtering options should also be separated by the
236                                 // vertical bar. Example: "Text files (*.txt)|*.txt|All files
237                                 // (*.*)|*.*"
238                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
239                                 Assert.IsNull (ex.InnerException, "#3");
240                                 Assert.IsNotNull (ex.Message, "#4");
241                                 Assert.IsNull (ex.ParamName, "#5");
242                         }
243                 }
244
245                 [Test]
246                 public void FilterIndex ()
247                 {
248                         SaveFileDialog sfd = new SaveFileDialog ();
249                         Assert.AreEqual (1, sfd.FilterIndex, "#1");
250                         sfd.FilterIndex = 99;
251                         Assert.AreEqual (99, sfd.FilterIndex, "#2");
252                         sfd.FilterIndex = -5;
253                         Assert.AreEqual (-5, sfd.FilterIndex, "#3");
254                 }
255
256                 [Test]
257                 public void InitialDirectory ()
258                 {
259                         SaveFileDialog sfd = new SaveFileDialog ();
260                         Assert.IsNotNull (sfd.InitialDirectory, "#A1");
261                         Assert.AreEqual (string.Empty, sfd.InitialDirectory, "#A2");
262
263                         sfd.InitialDirectory = Path.GetTempPath ();
264                         Assert.IsNotNull (sfd.InitialDirectory, "#B1");
265                         Assert.AreEqual (Path.GetTempPath (), sfd.InitialDirectory, "#B2");
266
267                         sfd.InitialDirectory = null;
268                         Assert.IsNotNull (sfd.InitialDirectory, "#C1");
269                         Assert.AreEqual (string.Empty, sfd.InitialDirectory, "#C2");
270
271                         string initialDir = Path.Combine (Path.GetTempPath (), 
272                                 "doesnotexistforsure");
273                         sfd.InitialDirectory = initialDir;
274                         Assert.IsNotNull (sfd.InitialDirectory, "#D1");
275                         Assert.AreEqual (initialDir, sfd.InitialDirectory, "#D2");
276
277                 }
278
279                 [Test]
280                 public void OverwritePrompt ()
281                 {
282                         SaveFileDialog sfd = new SaveFileDialog ();
283                         Assert.IsTrue (sfd.OverwritePrompt, "#1");
284                         sfd.OverwritePrompt = false;
285                         Assert.IsFalse (sfd.OverwritePrompt, "#2");
286                 }
287
288                 [Test]
289                 public void Reset ()
290                 {
291                         SaveFileDialog sfd = new SaveFileDialog ();
292                         sfd.AddExtension = false;
293                         sfd.CheckFileExists = true;
294                         sfd.CheckPathExists = false;
295                         sfd.CreatePrompt = true;
296                         sfd.DefaultExt = "txt";
297                         sfd.DereferenceLinks = false;
298                         sfd.FileName = "default.build";
299                         sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
300                         sfd.FilterIndex = 5;
301                         sfd.InitialDirectory = Path.GetTempPath ();
302                         sfd.OverwritePrompt = false;
303                         sfd.RestoreDirectory = true;
304                         sfd.ShowHelp = true;
305                         sfd.Title = "Saving";
306                         sfd.ValidateNames = false;
307                         sfd.Reset ();
308
309                         Assert.IsTrue (sfd.AddExtension, "#1");
310                         Assert.IsFalse (sfd.CheckFileExists, "#2");
311                         Assert.IsTrue (sfd.CheckPathExists, "#3");
312                         Assert.IsFalse (sfd.CreatePrompt, "#4");
313                         Assert.IsNotNull (sfd.DefaultExt, "#5");
314                         Assert.AreEqual (string.Empty, sfd.DefaultExt, "#6");
315                         Assert.IsTrue (sfd.DereferenceLinks, "#7");
316                         Assert.IsNotNull (sfd.FileName, "#8");
317                         Assert.AreEqual (string.Empty, sfd.FileName, "#9");
318                         Assert.IsNotNull (sfd.FileNames, "#10");
319                         Assert.AreEqual (0, sfd.FileNames.Length, "#11");
320                         Assert.IsNotNull (sfd.Filter, "#12");
321                         Assert.AreEqual (string.Empty, sfd.Filter, "#13");
322                         Assert.AreEqual (1, sfd.FilterIndex, "#14");
323                         Assert.IsNotNull (sfd.InitialDirectory, "#15");
324                         Assert.AreEqual (string.Empty, sfd.InitialDirectory, "#16");
325                         Assert.IsTrue (sfd.OverwritePrompt, "#17");
326                         Assert.IsFalse (sfd.RestoreDirectory, "#18");
327                         Assert.IsFalse (sfd.ShowHelp, "#19");
328                         Assert.IsNotNull (sfd.Title, "#20");
329                         Assert.AreEqual (string.Empty, sfd.Title, "#21");
330                         Assert.IsTrue (sfd.ValidateNames, "#22");
331                 }
332
333                 [Test]
334                 public void RestoreDirectory ()
335                 {
336                         SaveFileDialog sfd = new SaveFileDialog ();
337                         Assert.IsFalse (sfd.RestoreDirectory, "#1");
338                         sfd.RestoreDirectory = true;
339                         Assert.IsTrue (sfd.RestoreDirectory, "#2");
340                 }
341
342                 [Test]
343                 public void ShowHelp ()
344                 {
345                         SaveFileDialog sfd = new SaveFileDialog ();
346                         Assert.IsFalse (sfd.ShowHelp, "#1");
347                         sfd.ShowHelp = true;
348                         Assert.IsTrue (sfd.ShowHelp, "#2");
349                 }
350
351                 [Test]
352                 public void Title ()
353                 {
354                         SaveFileDialog sfd = new SaveFileDialog ();
355                         Assert.IsNotNull (sfd.Title, "#A1");
356                         Assert.AreEqual (string.Empty, sfd.Title, "#A2");
357
358                         sfd.Title = "Saving";
359                         Assert.IsNotNull (sfd.Title, "#B1");
360                         Assert.AreEqual ("Saving", sfd.Title, "#B2");
361
362                         sfd.Title = null;
363                         Assert.IsNotNull (sfd.Title, "#C1");
364                         Assert.AreEqual (string.Empty, sfd.Title, "#C2");
365                 }
366
367                 [Test]
368                 public void ToStringTest ()
369                 {
370                         SaveFileDialog sfd = new SaveFileDialog ();
371                         sfd.CheckFileExists = true;
372                         sfd.CreatePrompt = true;
373                         sfd.DefaultExt = "txt";
374                         sfd.DereferenceLinks = false;
375                         sfd.FileName = "default.build";
376                         sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
377                         sfd.FilterIndex = 5;
378                         sfd.InitialDirectory = Path.GetTempPath ();
379                         sfd.OverwritePrompt = false;
380                         sfd.RestoreDirectory = true;
381                         sfd.ShowHelp = true;
382                         sfd.Title = "Saving";
383                         sfd.ValidateNames = false;
384
385                         StringBuilder sb = new StringBuilder ();
386                         sb.Append (typeof (SaveFileDialog).FullName);
387                         sb.Append (": Title: ");
388                         sb.Append (sfd.Title);
389                         sb.Append (", FileName: ");
390                         sb.Append (sfd.FileName);
391
392                         Assert.AreEqual (sb.ToString (), sfd.ToString (), "#1");
393
394                         sfd.FileName = null;
395                         sfd.Title = null;
396
397                         sb.Length = 0;
398                         sb.Append (typeof (SaveFileDialog).FullName);
399                         sb.Append (": Title: ");
400                         sb.Append (sfd.Title);
401                         sb.Append (", FileName: ");
402                         sb.Append (sfd.FileName);
403
404                         Assert.AreEqual (sb.ToString (), sfd.ToString (), "#2");
405                 }
406
407                 [Test]
408                 public void ValidateNames ()
409                 {
410                         SaveFileDialog sfd = new SaveFileDialog ();
411                         Assert.IsTrue (sfd.ValidateNames, "#1");
412                         sfd.ValidateNames = false;
413                         Assert.IsFalse (sfd.ValidateNames, "#2");
414                 }
415         }
416 }