CommonDialogsTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <[email protected]>
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Drawing;
  31. using System.Drawing.Printing;
  32. using System.Windows.Forms;
  33. using System.IO;
  34. using System.ComponentModel;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Windows.Forms
  37. {
  38. [TestFixture]
  39. public class CommonDialogsTest
  40. {
  41. OpenFileDialog ofd;
  42. SaveFileDialog sfd;
  43. FontDialog fd;
  44. FolderBrowserDialog fbd;
  45. ColorDialog cd;
  46. [Test]
  47. public void ColorDialogTest ()
  48. {
  49. cd = new ColorDialog ();
  50. Assert.AreEqual (Color.Black, cd.Color, "#1");
  51. Assert.IsTrue (cd.AllowFullOpen, "#2");
  52. Assert.IsFalse (cd.AnyColor, "#3");
  53. Assert.IsFalse (cd.FullOpen, "#4");
  54. Assert.IsNotNull (cd.CustomColors, "#5");
  55. Assert.IsFalse (cd.ShowHelp, "#6");
  56. Assert.IsFalse (cd.SolidColorOnly, "#7");
  57. Assert.AreEqual ("System.Windows.Forms.ColorDialog, Color: Color [Black]", cd.ToString (), "#8");
  58. cd.Color = Color.Red;
  59. Assert.AreEqual (Color.Red, cd.Color, "#9");
  60. cd.AllowFullOpen = false;
  61. cd.FullOpen = true;
  62. Assert.IsTrue (cd.FullOpen, "#10");
  63. int[] custom_colors = new int[] {Color.Yellow.ToArgb (), Color.Red.ToArgb ()};
  64. cd.CustomColors = custom_colors;
  65. Assert.IsNotNull (cd.CustomColors, "#10a");
  66. Assert.AreEqual (16, cd.CustomColors.Length, "#10aa");
  67. Assert.AreEqual (Color.Red.ToArgb (), cd.CustomColors[1], "#10ab");
  68. Assert.AreEqual (Color.FromArgb(0, 255, 255, 255).ToArgb (), cd.CustomColors[15], "#10ac");
  69. cd.CustomColors = null;
  70. Assert.IsNotNull (cd.CustomColors, "#10b");
  71. Assert.AreEqual (16, cd.CustomColors.Length, "#10bb");
  72. Assert.AreEqual (Color.FromArgb(0, 255, 255, 255).ToArgb (), cd.CustomColors[0], "#10bc");
  73. cd.AllowFullOpen = true;
  74. cd.CustomColors = custom_colors;
  75. Assert.IsNotNull (cd.CustomColors, "#10c");
  76. Assert.AreEqual (16, cd.CustomColors.Length, "#10cc");
  77. }
  78. [Test]
  79. public void OpenFileDialogTest ()
  80. {
  81. ofd = new OpenFileDialog ();
  82. Assert.IsTrue (ofd.AddExtension, "#11");
  83. Assert.IsTrue (ofd.CheckFileExists, "#12");
  84. Assert.IsTrue (ofd.CheckPathExists, "#13");
  85. Assert.AreEqual ("", ofd.DefaultExt, "#14");
  86. Assert.IsTrue (ofd.DereferenceLinks, "#15");
  87. Assert.AreEqual ("", ofd.FileName, "#16");
  88. Assert.IsNotNull (ofd.FileNames, "#17");
  89. Assert.AreEqual (0, ofd.FileNames.Length, "#17a");
  90. Assert.AreEqual ("", ofd.Filter, "#18");
  91. Assert.AreEqual (1, ofd.FilterIndex, "#19");
  92. Assert.AreEqual ("", ofd.InitialDirectory, "#20");
  93. Assert.IsFalse (ofd.Multiselect, "#21");
  94. Assert.IsFalse (ofd.ReadOnlyChecked, "#22");
  95. Assert.IsFalse (ofd.RestoreDirectory, "#23");
  96. Assert.IsFalse (ofd.ShowHelp, "#24");
  97. Assert.IsFalse (ofd.ShowReadOnly, "#25");
  98. Assert.AreEqual ("", ofd.Title, "#26");
  99. Assert.IsTrue (ofd.ValidateNames, "#27");
  100. Assert.AreEqual ("System.Windows.Forms.OpenFileDialog: Title: , FileName: ", ofd.ToString (), "#28");
  101. ofd.DefaultExt = ".TXT";
  102. Assert.AreEqual ("TXT", ofd.DefaultExt, "#29");
  103. ofd.Filter = null;
  104. Assert.AreEqual ("", ofd.Filter, "#30");
  105. ofd.Filter = "Text (*.txt)|*.txt|All (*.*)|*.*";
  106. try {
  107. ofd.Filter = "abcd";
  108. } catch (Exception) {
  109. }
  110. Assert.AreEqual ("Text (*.txt)|*.txt|All (*.*)|*.*", ofd.Filter, "#30a");
  111. ofd.FilterIndex = 10;
  112. Assert.AreEqual (10, ofd.FilterIndex, "#30aa");
  113. ofd.Filter = null;
  114. Assert.AreEqual ("", ofd.Filter, "#30b");
  115. Assert.AreEqual (10, ofd.FilterIndex, "#30ba");
  116. string current_path = Environment.CurrentDirectory;
  117. string current_file = Path.Combine(current_path, "test_file");
  118. if (!File.Exists (current_file))
  119. File.Create (current_file);
  120. ofd.FileName = current_file;
  121. Assert.AreEqual (current_file, ofd.FileName, "#31");
  122. string[] file_names = ofd.FileNames;
  123. Assert.AreEqual (current_file, file_names [0], "#32");
  124. ofd.Title = "Test";
  125. Assert.AreEqual ("System.Windows.Forms.OpenFileDialog: Title: Test, FileName: " + current_file, ofd.ToString (), "#33");
  126. ofd.FileName = null;
  127. Assert.AreEqual ("", ofd.FileName, "#33a");
  128. Assert.IsNotNull (ofd.FileNames, "#33b");
  129. Assert.AreEqual (0, ofd.FileNames.Length, "#33c");
  130. ofd.Reset ();
  131. // check again
  132. Assert.IsTrue (ofd.AddExtension, "#34");
  133. Assert.IsTrue (ofd.CheckFileExists, "#35");
  134. Assert.IsTrue (ofd.CheckPathExists, "#36");
  135. Assert.AreEqual ("", ofd.DefaultExt, "#37");
  136. Assert.IsTrue (ofd.DereferenceLinks, "#38");
  137. Assert.AreEqual ("", ofd.FileName, "#39");
  138. Assert.IsNotNull (ofd.FileNames, "#40");
  139. Assert.AreEqual ("", ofd.Filter, "#41");
  140. Assert.AreEqual (1, ofd.FilterIndex, "#42");
  141. Assert.AreEqual ("", ofd.InitialDirectory, "#43");
  142. Assert.IsFalse (ofd.Multiselect, "#44");
  143. Assert.IsFalse (ofd.ReadOnlyChecked, "#45");
  144. Assert.IsFalse (ofd.RestoreDirectory, "#46");
  145. Assert.IsFalse (ofd.ShowHelp, "#47");
  146. Assert.IsFalse (ofd.ShowReadOnly, "#48");
  147. Assert.AreEqual ("", ofd.Title, "#49");
  148. Assert.IsTrue (ofd.ValidateNames, "#50");
  149. Assert.AreEqual ("System.Windows.Forms.OpenFileDialog: Title: , FileName: ", ofd.ToString (), "#60");
  150. }
  151. [Test]
  152. [ExpectedException(typeof(ArgumentException))]
  153. public void FileDialogFilterArgumentException () {
  154. if (ofd == null)
  155. ofd = new OpenFileDialog ();
  156. ofd.Filter = "xyafj";
  157. }
  158. [Test]
  159. public void SaveFileDialogTest ()
  160. {
  161. // most of the OpenFileDialogTest are also valid for SaveFileDialg
  162. sfd = new SaveFileDialog ();
  163. Assert.IsFalse (sfd.CreatePrompt, "#61");
  164. Assert.IsTrue (sfd.OverwritePrompt, "#62");
  165. }
  166. [Test]
  167. public void FontDialogTest ()
  168. {
  169. fd = new FontDialog ();
  170. Assert.IsTrue (fd.AllowScriptChange, "#63");
  171. Assert.IsTrue (fd.AllowSimulations, "#64");
  172. Assert.IsTrue (fd.AllowVectorFonts, "#65");
  173. Assert.IsTrue (fd.AllowVerticalFonts, "#66");
  174. Assert.AreEqual (Color.Black, fd.Color, "#67");
  175. Assert.IsFalse (fd.FixedPitchOnly, "#68");
  176. //Assert.AreEqual ("[Font: Name=Microsoft Sans Serif, Size=8,25, Units=3, GdiCharSet=0, GdiVerticalFont=False]", fd.Font.ToString (), "#69");
  177. Assert.IsFalse (fd.FontMustExist, "#70");
  178. Assert.AreEqual (0, fd.MaxSize, "#71");
  179. Assert.AreEqual (0, fd.MinSize, "#72");
  180. Assert.IsFalse (fd.ScriptsOnly, "#73");
  181. Assert.IsFalse (fd.ShowApply, "#74");
  182. Assert.IsFalse (fd.ShowColor, "#75");
  183. Assert.IsTrue (fd.ShowEffects, "#76");
  184. Assert.IsFalse (fd.ShowHelp, "#77");
  185. //Assert.AreEqual ("System.Windows.Forms.FontDialog, Font: [Font: Name=Microsoft Sans Serif, Size=8,25, Units=3, GdiCharSet=0, GdiVerticalFont=False]", fd.ToString (), "#78");
  186. fd.MaxSize = -1;
  187. Assert.AreEqual (0, fd.MaxSize, "#79");
  188. fd.MinSize = -1;
  189. Assert.AreEqual (0, fd.MinSize, "#80");
  190. fd.MinSize = 24;
  191. fd.MaxSize = 10;
  192. Assert.AreEqual (10, fd.MinSize, "#81");
  193. fd.MinSize = 48;
  194. Assert.AreEqual (48, fd.MaxSize, "#82");
  195. }
  196. [Test]
  197. [NUnit.Framework.Category ("NotWorking")]
  198. /* a few people have been seeing the following:
  199. Failures:
  200. 1) MonoTests.System.Windows.Forms.CommonDialogsTest.FolderBrowserDialogTest : System.ArgumentException : oldValue is the empty string.
  201. 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
  202. 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
  203. 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
  204. at (wrapper remoting-invoke-with-check) FolderBrowserTreeView:set_SelectedPath (string)
  205. 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
  206. at (wrapper remoting-invoke-with-check) System.Windows.Forms.FolderBrowserDialog:set_SelectedPath (string)
  207. 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
  208. at <0x00000> <unknown method>
  209. at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[])
  210. 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
  211. */
  212. public void FolderBrowserDialogTest ()
  213. {
  214. fbd = new FolderBrowserDialog ();
  215. Assert.AreEqual ("", fbd.Description, "#83");
  216. Assert.AreEqual (Environment.SpecialFolder.Desktop, fbd.RootFolder, "#84");
  217. Assert.AreEqual ("", fbd.SelectedPath, "#85");
  218. Assert.IsTrue (fbd.ShowNewFolderButton, "#86");
  219. Assert.AreEqual ("System.Windows.Forms.FolderBrowserDialog", fbd.ToString (), "#87");
  220. string current_path = Environment.CurrentDirectory;
  221. fbd.SelectedPath = current_path;
  222. Assert.AreEqual (current_path, fbd.SelectedPath, "#89");
  223. }
  224. [Test]
  225. [ExpectedException(typeof(InvalidEnumArgumentException))]
  226. public void FolderBrowserDialogInvalidEnumArgumentExceptionTest () {
  227. if (fbd == null)
  228. fbd = new FolderBrowserDialog ();
  229. fbd.RootFolder = (Environment.SpecialFolder)12;
  230. }
  231. #if NET_2_0
  232. [Test]
  233. public void CommonDialogPropertyTag ()
  234. {
  235. MyDialog md = new MyDialog ();
  236. object s = "MyString";
  237. Assert.AreEqual (null, md.Tag, "A1");
  238. md.Tag = s;
  239. Assert.AreSame (s, md.Tag, "A2");
  240. }
  241. private class MyDialog : CommonDialog
  242. {
  243. public override void Reset ()
  244. {
  245. throw new Exception ("The method or operation is not implemented.");
  246. }
  247. protected override bool RunDialog (IntPtr hwndOwner)
  248. {
  249. throw new Exception ("The method or operation is not implemented.");
  250. }
  251. }
  252. #endif
  253. }
  254. }