2 // SaveFileDialogTest.cs: Tests for SaveFileDialog class.
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2007 Gert Driesen
26 // Gert Driesen (drieseng@user.sourceforge.net)
32 using System.Windows.Forms;
34 using NUnit.Framework;
36 namespace MonoTests.System.Windows.Forms
39 public class SaveFileDialogTest : TestHelper
42 public void AddExtension ()
44 SaveFileDialog sfd = new SaveFileDialog ();
45 Assert.IsTrue (sfd.AddExtension, "#1");
46 sfd.AddExtension = false;
47 Assert.IsFalse (sfd.AddExtension, "#2");
51 public void CheckFileExists ()
53 SaveFileDialog sfd = new SaveFileDialog ();
54 Assert.IsFalse (sfd.CheckFileExists, "#1");
55 sfd.CheckFileExists = true;
56 Assert.IsTrue (sfd.CheckFileExists, "#2");
60 public void CheckPathExists ()
62 SaveFileDialog sfd = new SaveFileDialog ();
63 Assert.IsTrue (sfd.CheckPathExists, "#1");
64 sfd.CheckPathExists = false;
65 Assert.IsFalse (sfd.CheckPathExists, "#2");
69 public void CreatePrompt ()
71 SaveFileDialog sfd = new SaveFileDialog ();
72 Assert.IsFalse (sfd.CreatePrompt, "#1");
73 sfd.CreatePrompt = true;
74 Assert.IsTrue (sfd.CreatePrompt, "#2");
78 public void DefaultExt ()
80 SaveFileDialog sfd = new SaveFileDialog ();
81 Assert.IsNotNull (sfd.DefaultExt, "#A1");
82 Assert.AreEqual (string.Empty, sfd.DefaultExt, "#A2");
84 sfd.DefaultExt = "txt";
85 Assert.IsNotNull (sfd.DefaultExt, "#B1");
86 Assert.AreEqual ("txt", sfd.DefaultExt, "#B2");
88 sfd.DefaultExt = null;
89 Assert.IsNotNull (sfd.DefaultExt, "#C1");
90 Assert.AreEqual (string.Empty, sfd.DefaultExt, "#C2");
92 sfd.DefaultExt = ".Xml";
93 Assert.IsNotNull (sfd.DefaultExt, "#D1");
94 Assert.AreEqual ("Xml", sfd.DefaultExt, "#D2");
96 sfd.DefaultExt = ".tar.gz";
97 Assert.IsNotNull (sfd.DefaultExt, "#E1");
98 Assert.AreEqual ("tar.gz", sfd.DefaultExt, "#E2");
100 sfd.DefaultExt = "..Xml";
101 Assert.IsNotNull (sfd.DefaultExt, "#F1");
102 Assert.AreEqual (".Xml", sfd.DefaultExt, "#F2");
104 sfd.DefaultExt = "tar.gz";
105 Assert.IsNotNull (sfd.DefaultExt, "#G1");
106 Assert.AreEqual ("tar.gz", sfd.DefaultExt, "#G2");
108 sfd.DefaultExt = ".";
109 Assert.IsNotNull (sfd.DefaultExt, "#H1");
110 Assert.AreEqual (string.Empty, sfd.DefaultExt, "#H2");
114 public void DereferenceLinks ()
116 SaveFileDialog sfd = new SaveFileDialog ();
117 Assert.IsTrue (sfd.DereferenceLinks, "#1");
118 sfd.DereferenceLinks = false;
119 Assert.IsFalse (sfd.DereferenceLinks, "#2");
123 public void FileName ()
125 SaveFileDialog sfd = new SaveFileDialog ();
126 Assert.IsNotNull (sfd.FileName, "#A1");
127 Assert.AreEqual (string.Empty, sfd.FileName, "#A2");
129 sfd.FileName = "default.build";
130 Assert.IsNotNull (sfd.FileName, "#B1");
131 Assert.AreEqual ("default.build", sfd.FileName, "#B2");
134 Assert.IsNotNull (sfd.FileName, "#C1");
135 Assert.AreEqual (string.Empty, sfd.FileName, "#C2");
137 sfd.FileName = string.Empty;
138 Assert.IsNotNull (sfd.FileName, "#D1");
139 Assert.AreEqual (string.Empty, sfd.FileName, "#D2");
143 public void FileName_InvalidPathCharacter ()
145 SaveFileDialog sfd = new SaveFileDialog ();
146 sfd.FileName = Path.InvalidPathChars [0] + "file";
148 Assert.IsNotNull (sfd.FileName, "#1");
149 Assert.AreEqual (Path.InvalidPathChars [0] + "file", sfd.FileName, "#2");
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");
165 public void FileNames ()
167 SaveFileDialog sfd = new SaveFileDialog ();
168 Assert.IsNotNull (sfd.FileNames, "#A1");
169 Assert.AreEqual (0, sfd.FileNames.Length, "#A2");
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");
177 Assert.IsNotNull (sfd.FileNames, "#C1");
178 Assert.AreEqual (0, sfd.FileNames.Length, "#C2");
182 public void FileNames_InvalidPathCharacter ()
184 SaveFileDialog sfd = new SaveFileDialog ();
185 sfd.FileName = Path.InvalidPathChars [0] + "file";
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");
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");
205 public void Filter ()
207 SaveFileDialog sfd = new SaveFileDialog ();
208 Assert.IsNotNull (sfd.Filter, "#A1");
209 Assert.AreEqual (string.Empty, sfd.Filter, "#A2");
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");
216 Assert.IsNotNull (sfd.Filter, "#C1");
217 Assert.AreEqual (string.Empty, sfd.Filter, "#C2");
219 sfd.Filter = string.Empty;
220 Assert.IsNotNull (sfd.Filter, "#D1");
221 Assert.AreEqual (string.Empty, sfd.Filter, "#D2");
225 public void Filter_InvalidFormat ()
227 SaveFileDialog sfd = new SaveFileDialog ();
229 sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)";
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
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");
246 public void FilterIndex ()
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");
257 public void InitialDirectory ()
259 SaveFileDialog sfd = new SaveFileDialog ();
260 Assert.IsNotNull (sfd.InitialDirectory, "#A1");
261 Assert.AreEqual (string.Empty, sfd.InitialDirectory, "#A2");
263 sfd.InitialDirectory = Path.GetTempPath ();
264 Assert.IsNotNull (sfd.InitialDirectory, "#B1");
265 Assert.AreEqual (Path.GetTempPath (), sfd.InitialDirectory, "#B2");
267 sfd.InitialDirectory = null;
268 Assert.IsNotNull (sfd.InitialDirectory, "#C1");
269 Assert.AreEqual (string.Empty, sfd.InitialDirectory, "#C2");
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");
280 public void OverwritePrompt ()
282 SaveFileDialog sfd = new SaveFileDialog ();
283 Assert.IsTrue (sfd.OverwritePrompt, "#1");
284 sfd.OverwritePrompt = false;
285 Assert.IsFalse (sfd.OverwritePrompt, "#2");
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 (*.*)|*.*";
301 sfd.InitialDirectory = Path.GetTempPath ();
302 sfd.OverwritePrompt = false;
303 sfd.RestoreDirectory = true;
305 sfd.Title = "Saving";
306 sfd.ValidateNames = false;
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");
334 public void RestoreDirectory ()
336 SaveFileDialog sfd = new SaveFileDialog ();
337 Assert.IsFalse (sfd.RestoreDirectory, "#1");
338 sfd.RestoreDirectory = true;
339 Assert.IsTrue (sfd.RestoreDirectory, "#2");
343 public void ShowHelp ()
345 SaveFileDialog sfd = new SaveFileDialog ();
346 Assert.IsFalse (sfd.ShowHelp, "#1");
348 Assert.IsTrue (sfd.ShowHelp, "#2");
354 SaveFileDialog sfd = new SaveFileDialog ();
355 Assert.IsNotNull (sfd.Title, "#A1");
356 Assert.AreEqual (string.Empty, sfd.Title, "#A2");
358 sfd.Title = "Saving";
359 Assert.IsNotNull (sfd.Title, "#B1");
360 Assert.AreEqual ("Saving", sfd.Title, "#B2");
363 Assert.IsNotNull (sfd.Title, "#C1");
364 Assert.AreEqual (string.Empty, sfd.Title, "#C2");
368 public void ToStringTest ()
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 (*.*)|*.*";
378 sfd.InitialDirectory = Path.GetTempPath ();
379 sfd.OverwritePrompt = false;
380 sfd.RestoreDirectory = true;
382 sfd.Title = "Saving";
383 sfd.ValidateNames = false;
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);
392 Assert.AreEqual (sb.ToString (), sfd.ToString (), "#1");
398 sb.Append (typeof (SaveFileDialog).FullName);
399 sb.Append (": Title: ");
400 sb.Append (sfd.Title);
401 sb.Append (", FileName: ");
402 sb.Append (sfd.FileName);
404 Assert.AreEqual (sb.ToString (), sfd.ToString (), "#2");
408 public void ValidateNames ()
410 SaveFileDialog sfd = new SaveFileDialog ();
411 Assert.IsTrue (sfd.ValidateNames, "#1");
412 sfd.ValidateNames = false;
413 Assert.IsFalse (sfd.ValidateNames, "#2");