SaveFileDialogTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 SaveFileDialogTest : TestHelper
  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. sfd.FilterIndex = -5;
  223. Assert.AreEqual (-5, sfd.FilterIndex, "#3");
  224. }
  225. [Test]
  226. public void InitialDirectory ()
  227. {
  228. SaveFileDialog sfd = new SaveFileDialog ();
  229. Assert.IsNotNull (sfd.InitialDirectory, "#A1");
  230. Assert.AreEqual (string.Empty, sfd.InitialDirectory, "#A2");
  231. sfd.InitialDirectory = Path.GetTempPath ();
  232. Assert.IsNotNull (sfd.InitialDirectory, "#B1");
  233. Assert.AreEqual (Path.GetTempPath (), sfd.InitialDirectory, "#B2");
  234. sfd.InitialDirectory = null;
  235. Assert.IsNotNull (sfd.InitialDirectory, "#C1");
  236. Assert.AreEqual (string.Empty, sfd.InitialDirectory, "#C2");
  237. string initialDir = Path.Combine (Path.GetTempPath (),
  238. "doesnotexistforsure");
  239. sfd.InitialDirectory = initialDir;
  240. Assert.IsNotNull (sfd.InitialDirectory, "#D1");
  241. Assert.AreEqual (initialDir, sfd.InitialDirectory, "#D2");
  242. }
  243. [Test]
  244. public void OverwritePrompt ()
  245. {
  246. SaveFileDialog sfd = new SaveFileDialog ();
  247. Assert.IsTrue (sfd.OverwritePrompt, "#1");
  248. sfd.OverwritePrompt = false;
  249. Assert.IsFalse (sfd.OverwritePrompt, "#2");
  250. }
  251. [Test]
  252. public void Reset ()
  253. {
  254. SaveFileDialog sfd = new SaveFileDialog ();
  255. sfd.AddExtension = false;
  256. sfd.CheckFileExists = true;
  257. sfd.CheckPathExists = false;
  258. sfd.CreatePrompt = true;
  259. sfd.DefaultExt = "txt";
  260. sfd.DereferenceLinks = false;
  261. sfd.FileName = "default.build";
  262. sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
  263. sfd.FilterIndex = 5;
  264. sfd.InitialDirectory = Path.GetTempPath ();
  265. sfd.OverwritePrompt = false;
  266. sfd.RestoreDirectory = true;
  267. sfd.ShowHelp = true;
  268. sfd.Title = "Saving";
  269. sfd.ValidateNames = false;
  270. sfd.Reset ();
  271. Assert.IsTrue (sfd.AddExtension, "#1");
  272. Assert.IsFalse (sfd.CheckFileExists, "#2");
  273. Assert.IsTrue (sfd.CheckPathExists, "#3");
  274. Assert.IsFalse (sfd.CreatePrompt, "#4");
  275. Assert.IsNotNull (sfd.DefaultExt, "#5");
  276. Assert.AreEqual (string.Empty, sfd.DefaultExt, "#6");
  277. Assert.IsTrue (sfd.DereferenceLinks, "#7");
  278. Assert.IsNotNull (sfd.FileName, "#8");
  279. Assert.AreEqual (string.Empty, sfd.FileName, "#9");
  280. Assert.IsNotNull (sfd.FileNames, "#10");
  281. Assert.AreEqual (0, sfd.FileNames.Length, "#11");
  282. Assert.IsNotNull (sfd.Filter, "#12");
  283. Assert.AreEqual (string.Empty, sfd.Filter, "#13");
  284. Assert.AreEqual (1, sfd.FilterIndex, "#14");
  285. Assert.IsNotNull (sfd.InitialDirectory, "#15");
  286. Assert.AreEqual (string.Empty, sfd.InitialDirectory, "#16");
  287. Assert.IsTrue (sfd.OverwritePrompt, "#17");
  288. Assert.IsFalse (sfd.RestoreDirectory, "#18");
  289. Assert.IsFalse (sfd.ShowHelp, "#19");
  290. Assert.IsNotNull (sfd.Title, "#20");
  291. Assert.AreEqual (string.Empty, sfd.Title, "#21");
  292. Assert.IsTrue (sfd.ValidateNames, "#22");
  293. }
  294. [Test]
  295. public void RestoreDirectory ()
  296. {
  297. SaveFileDialog sfd = new SaveFileDialog ();
  298. Assert.IsFalse (sfd.RestoreDirectory, "#1");
  299. sfd.RestoreDirectory = true;
  300. Assert.IsTrue (sfd.RestoreDirectory, "#2");
  301. }
  302. [Test]
  303. public void ShowHelp ()
  304. {
  305. SaveFileDialog sfd = new SaveFileDialog ();
  306. Assert.IsFalse (sfd.ShowHelp, "#1");
  307. sfd.ShowHelp = true;
  308. Assert.IsTrue (sfd.ShowHelp, "#2");
  309. }
  310. [Test]
  311. public void Title ()
  312. {
  313. SaveFileDialog sfd = new SaveFileDialog ();
  314. Assert.IsNotNull (sfd.Title, "#A1");
  315. Assert.AreEqual (string.Empty, sfd.Title, "#A2");
  316. sfd.Title = "Saving";
  317. Assert.IsNotNull (sfd.Title, "#B1");
  318. Assert.AreEqual ("Saving", sfd.Title, "#B2");
  319. sfd.Title = null;
  320. Assert.IsNotNull (sfd.Title, "#C1");
  321. Assert.AreEqual (string.Empty, sfd.Title, "#C2");
  322. }
  323. [Test]
  324. public void ToStringTest ()
  325. {
  326. SaveFileDialog sfd = new SaveFileDialog ();
  327. sfd.CheckFileExists = true;
  328. sfd.CreatePrompt = true;
  329. sfd.DefaultExt = "txt";
  330. sfd.DereferenceLinks = false;
  331. sfd.FileName = "default.build";
  332. sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
  333. sfd.FilterIndex = 5;
  334. sfd.InitialDirectory = Path.GetTempPath ();
  335. sfd.OverwritePrompt = false;
  336. sfd.RestoreDirectory = true;
  337. sfd.ShowHelp = true;
  338. sfd.Title = "Saving";
  339. sfd.ValidateNames = false;
  340. StringBuilder sb = new StringBuilder ();
  341. sb.Append (typeof (SaveFileDialog).FullName);
  342. sb.Append (": Title: ");
  343. sb.Append (sfd.Title);
  344. sb.Append (", FileName: ");
  345. sb.Append (sfd.FileName);
  346. Assert.AreEqual (sb.ToString (), sfd.ToString (), "#1");
  347. sfd.FileName = null;
  348. sfd.Title = null;
  349. sb.Length = 0;
  350. sb.Append (typeof (SaveFileDialog).FullName);
  351. sb.Append (": Title: ");
  352. sb.Append (sfd.Title);
  353. sb.Append (", FileName: ");
  354. sb.Append (sfd.FileName);
  355. Assert.AreEqual (sb.ToString (), sfd.ToString (), "#2");
  356. }
  357. [Test]
  358. public void ValidateNames ()
  359. {
  360. SaveFileDialog sfd = new SaveFileDialog ();
  361. Assert.IsTrue (sfd.ValidateNames, "#1");
  362. sfd.ValidateNames = false;
  363. Assert.IsFalse (sfd.ValidateNames, "#2");
  364. }
  365. }
  366. }