SaveFileDialogTest.cs 12 KB

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