Bläddra i källkod

In .:
2006-11-09 Alexander Olk <[email protected]>

* System.Windows.Forms_test.dll.sources: Added CommonDialogsTest.cs.

In Test/System.Windows.Forms:
2006-11-09 Alexander Olk <[email protected]>

* CommonDialogsTest.cs: Added

In System.Windows.Forms:
2006-11-09 Alexander Olk <[email protected]>

* FileDialog.cs:
- Fix ToString ()
- An ArgumentException is now thrown if a wrong filter
is applied (matches ms). The previous filter doesn't change
anymore if an exception is thrown.
- Changing the FileName property also affects FileNames
* ColorDialog.cs: The length of the CustomColors array is always
16. It doesn't matter if we use a smaller array or null to update
or change the custom colors property.
* FolderBrowserDialog.cs: Throw an InvalidEnumArgumentException if
for RootFolder if we get a undefined value.



svn path=/trunk/mcs/; revision=67618

Alexander Olk 19 år sedan
förälder
incheckning
54860094ac

+ 4 - 0
mcs/class/Managed.Windows.Forms/ChangeLog

@@ -1,3 +1,7 @@
+2006-11-09  Alexander Olk  <[email protected]>
+
+	* System.Windows.Forms_test.dll.sources: Added CommonDialogsTest.cs.
+
 2006-11-08  Chris Toshok  <[email protected]>
 
 	* System.Windows.Forms.dll.sources: add RootGridEntry.cs

+ 15 - 0
mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog

@@ -1,3 +1,18 @@
+2006-11-09  Alexander Olk  <[email protected]>
+
+	* FileDialog.cs:
+	  - Fix ToString ()
+	  - An ArgumentException is now thrown if a wrong filter
+	    is applied (matches ms). The previous filter doesn't change
+	    anymore if an exception is thrown.
+	  - Changing the FileName property also affects FileNames
+	* ColorDialog.cs: The length of the CustomColors array is always
+	  16. It doesn't matter if we use a smaller array or null to update
+	  or change the custom colors property.
+	* FolderBrowserDialog.cs: Throw an InvalidEnumArgumentException if
+	  for RootFolder if we get a undefined value.
+
+
 2006-11-09  Rolf Bjarne Kvinge  <[email protected]>
 
 	* StatusBarPanel.cs: 

+ 23 - 13
mcs/class/Managed.Windows.Forms/System.Windows.Forms/ColorDialog.cs

@@ -312,6 +312,8 @@ namespace System.Windows.Forms {
 			redTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
 			greenTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
 			blueTextBox.LostFocus += new EventHandler (OnLostFocusTextBoxes);
+			
+			ResetCustomColors ();
 		}
 		#endregion	// Public Constructors
 		
@@ -398,11 +400,15 @@ namespace System.Windows.Forms {
 			}
 			
 			set {
-				if (allowFullOpen) {
-					customColors = value;
+				if (value == null)
+					ResetCustomColors ();
+				else {
+					int[] tmp_colors = value;
 					
-					baseColorControl.SetCustomColors ();
+					Array.Copy (tmp_colors, customColors, tmp_colors.Length);
 				}
+					
+				baseColorControl.SetCustomColors ();
 			}
 		}
 		
@@ -442,6 +448,7 @@ namespace System.Windows.Forms {
 			anyColor = false;
 			Color = Color.Black;
 			CustomColors = null;
+			ResetCustomColors ();
 			FullOpen = false;
 			ShowHelp = false;
 			solidColorOnly = false;
@@ -449,7 +456,7 @@ namespace System.Windows.Forms {
 		
 		public override string ToString ()
 		{
-			return base.ToString () + ", Color: " + Color.ToString ();
+			return base.ToString () + ",  Color: " + Color.ToString ();
 		}
 		#endregion	// Public Instance Methods
 		
@@ -711,6 +718,18 @@ namespace System.Windows.Forms {
 			
 			UpdateControls (col);
 		}
+		
+		void ResetCustomColors ()
+		{
+			// check if this.customColors already exists
+			if (customColors == null)
+				customColors = new int [16];
+			
+			int default_color = Color.FromArgb(0, 255, 255, 255).ToArgb ();
+				
+			for (int i = 0; i < customColors.Length; i++)
+				customColors [i] = default_color;
+		}
 		#endregion
 		
 		#region Internal structs and classes
@@ -1411,15 +1430,6 @@ namespace System.Windows.Forms {
 			{
 				userSmallColorControl [currentlyUsedUserSmallColorControl].InternalColor = col;
 				
-				// check if this.customColors already exists
-				if (customColors == null) {
-					customColors = new int [16];
-					int white = Color.White.ToArgb ();
-					
-					for (int i = 0; i < customColors.Length; i++)
-						customColors [i] = white;
-				}
-				
 				customColors [currentlyUsedUserSmallColorControl] = col.ToArgb ();
 				
 				// update ColorDialog dialog property

+ 47 - 23
mcs/class/Managed.Windows.Forms/System.Windows.Forms/FileDialog.cs

@@ -57,7 +57,7 @@ namespace System.Windows.Forms {
 		private bool dereferenceLinks = true;
 		private string fileName = String.Empty;
 		private string[] fileNames;
-		private string filter;
+		private string filter = "";
 		private int filterIndex = 1;
 		private string initialDirectory = String.Empty;
 		private bool restoreDirectory = false;
@@ -441,8 +441,19 @@ namespace System.Windows.Forms {
 			
 			set {
 				if (value != null) {
-					if (SetFileName (value))
+					if (SetFileName (value)) {
 						fileName = value;
+						if (fileNames == null) {
+							fileNames = new string [1];
+							fileNames [0] = value;
+						}
+					}
+				} else {
+					fileName = String.Empty;
+					fileNames = new string [0];
+					// this does not match ms exactly,
+					// but there is no other way to clear that %#&?§ combobox
+					fileNameComboBox.Text = " ";
 				}
 			}
 		}
@@ -470,12 +481,18 @@ namespace System.Windows.Forms {
 			}
 			
 			set {
-				if (value == null)
-					throw new NullReferenceException ("Filter");
-				
-				filter = value;
-				
-				fileFilter = new FileFilter (filter);
+				if (value == null) {
+					filter = "";
+					if (fileFilter != null)
+						fileFilter.FilterArrayList.Clear ();
+				} else {
+					if (FileFilter.CheckFilter (value)) {
+						filter = value;
+						
+						fileFilter = new FileFilter (filter);
+					} else
+						throw new ArgumentException ();
+				}
 				
 				UpdateFilters ();
 			}
@@ -488,9 +505,6 @@ namespace System.Windows.Forms {
 			}
 			
 			set {
-				if (fileFilter != null && fileFilter.FilterArrayList.Count > value)
-					return;  // FIXME: throw an exception ?
-				
 				filterIndex = value;
 				
 				SelectFilter ();
@@ -578,8 +592,7 @@ namespace System.Windows.Forms {
 			checkPathExists = true;
 			defaultExt = String.Empty;
 			dereferenceLinks = true;
-			fileName = String.Empty;
-			fileNames = null;
+			FileName = String.Empty;
 			Filter = String.Empty;
 			filterIndex = 1;
 			initialDirectory = String.Empty;
@@ -593,7 +606,7 @@ namespace System.Windows.Forms {
 		
 		public override string ToString ()
 		{
-			return base.ToString ();
+			return String.Format("{0}: Title: {1}, FileName: {2}", base.ToString (), Title, fileName);
 		}
 		
 		public event CancelEventHandler FileOk {
@@ -690,7 +703,7 @@ namespace System.Windows.Forms {
 		
 		private void SelectFilter ()
 		{
-			if (FilterIndex > mwfFileView.FilterArrayList.Count)
+			if (mwfFileView.FilterArrayList == null || FilterIndex > mwfFileView.FilterArrayList.Count)
 				return;
 			
 			do_not_call_OnSelectedIndexChangedFileTypeComboBox = true;
@@ -999,6 +1012,9 @@ namespace System.Windows.Forms {
 		
 		private void UpdateFilters ()
 		{
+			if (fileFilter == null)
+				fileFilter = new FileFilter ();
+			
 			ArrayList filters = fileFilter.FilterArrayList;
 			
 			fileTypeComboBox.BeginUpdate ();
@@ -1009,7 +1025,8 @@ namespace System.Windows.Forms {
 				fileTypeComboBox.Items.Add (fs.filterName);
 			}
 			
-			fileTypeComboBox.SelectedIndex = FilterIndex - 1;
+			if (filters.Count > 0 && FilterIndex <= filters.Count)
+				fileTypeComboBox.SelectedIndex = FilterIndex - 1;
 			
 			fileTypeComboBox.EndUpdate ();
 			
@@ -1800,13 +1817,26 @@ namespace System.Windows.Forms {
 		public FileFilter ()
 		{}
 		
-		public FileFilter (string filter)
+		public FileFilter (string filter) : base ()
 		{
 			this.filter = filter;
 			
 			SplitFilter ();
 		}
 		
+		public static bool CheckFilter (string val)
+		{
+			if (val.Length == 0)
+				return true;
+			
+			string[] filters = val.Split (new char [] {'|'});
+			
+			if ((filters.Length % 2) != 0)
+				return false;
+			
+			return true;
+		}
+		
 		public ArrayList FilterArrayList {
 			set {
 				filterArrayList = value;
@@ -1833,17 +1863,11 @@ namespace System.Windows.Forms {
 		{
 			filterArrayList.Clear ();
 			
-			if (filter == null)
-				throw new NullReferenceException ("Filter");
-			
 			if (filter.Length == 0)
 				return;
 			
 			string[] filters = filter.Split (new char [] {'|'});
 			
-			if ((filters.Length % 2) != 0)
-				throw new ArgumentException ("Filter");
-			
 			for (int i = 0; i < filters.Length; i += 2) {
 				FilterStruct filterStruct = new FilterStruct (filters [i], filters [i + 1]);
 				

+ 5 - 0
mcs/class/Managed.Windows.Forms/System.Windows.Forms/FolderBrowserDialog.cs

@@ -193,6 +193,11 @@ namespace System.Windows.Forms {
 		[Localizable(false)]
 		public Environment.SpecialFolder RootFolder {
 			set {
+				int v = (int)value;
+				
+				if (!Enum.IsDefined(typeof(Environment.SpecialFolder), v))
+					throw new InvalidEnumArgumentException ();
+				
 				if (rootFolder != value)
 					rootFolder = value;
 				

+ 1 - 0
mcs/class/Managed.Windows.Forms/System.Windows.Forms_test.dll.sources

@@ -9,6 +9,7 @@ System.Windows.Forms/CheckedListBoxTest.cs
 System.Windows.Forms/ComboBoxTest.cs
 System.Windows.Forms/ComboBoxTests.cs
 System.Windows.Forms/Common.cs
+System.Windows.Forms/CommonDialogsTest.cs
 System.Windows.Forms/ContainerControlTest.cs
 System.Windows.Forms/ControlBindingsConverterTest.cs
 System.Windows.Forms/ControlEventTest.cs

+ 4 - 0
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ChangeLog

@@ -1,3 +1,7 @@
+2006-11-09  Alexander Olk  <[email protected]>
+
+	* CommonDialogsTest.cs: Added
+
 2006-11-09  Rolf Bjarne Kvinge  <[email protected]>
 
 	 * StatusBarPanelTest.cs: Added tests for bug #79842

+ 275 - 0
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/CommonDialogsTest.cs

@@ -0,0 +1,275 @@
+//
+// CommonDialogsTest.cs: Tests for common dialogs.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+// Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
+//
+// Authors:
+//	Alexander Olk <[email protected]>
+//
+
+using System;
+using System.Collections;
+using System.Drawing;
+using System.Drawing.Printing;
+using System.Windows.Forms;
+using System.IO;
+using System.ComponentModel;
+using NUnit.Framework;
+
+namespace MonoTests.System.Windows.Forms
+{
+	[TestFixture]
+	public class CommonDialogsTest
+	{
+		OpenFileDialog ofd;
+		SaveFileDialog sfd;
+		FontDialog fd;
+		FolderBrowserDialog fbd;
+		ColorDialog cd;
+		
+		[Test]
+		public void ColorDialogTest ()
+		{
+			cd = new ColorDialog ();
+			
+			Assert.AreEqual (Color.Black, cd.Color, "#1");
+			Assert.IsTrue (cd.AllowFullOpen, "#2");
+			Assert.IsFalse (cd.AnyColor, "#3");
+			Assert.IsFalse (cd.FullOpen, "#4");
+			Assert.IsNotNull (cd.CustomColors, "#5");
+			Assert.IsFalse (cd.ShowHelp, "#6");
+			Assert.IsFalse (cd.SolidColorOnly, "#7");
+			Assert.AreEqual ("System.Windows.Forms.ColorDialog,  Color: Color [Black]", cd.ToString (), "#8");
+			
+			cd.Color = Color.Red;
+			Assert.AreEqual (Color.Red, cd.Color, "#9");
+			
+			cd.AllowFullOpen = false;
+			cd.FullOpen = true;
+			Assert.IsTrue (cd.FullOpen, "#10");
+			
+			int[] custom_colors = new int[] {Color.Yellow.ToArgb (), Color.Red.ToArgb ()};
+			cd.CustomColors = custom_colors;
+			Assert.IsNotNull (cd.CustomColors, "#10a");
+			Assert.AreEqual (16, cd.CustomColors.Length, "#10aa");
+			Assert.AreEqual (Color.Red.ToArgb (), cd.CustomColors[1], "#10ab");
+			Assert.AreEqual (Color.FromArgb(0, 255, 255, 255).ToArgb (), cd.CustomColors[15], "#10ac");
+			
+			cd.CustomColors = null;
+			Assert.IsNotNull (cd.CustomColors, "#10b");
+			Assert.AreEqual (16, cd.CustomColors.Length, "#10bb");
+			Assert.AreEqual (Color.FromArgb(0, 255, 255, 255).ToArgb (), cd.CustomColors[0], "#10bc");
+			
+			cd.AllowFullOpen = true;
+			cd.CustomColors = custom_colors;
+			Assert.IsNotNull (cd.CustomColors, "#10c");
+			Assert.AreEqual (16, cd.CustomColors.Length, "#10cc");
+		}
+		
+		[Test]
+		public void OpenFileDialogTest ()
+		{
+			ofd = new OpenFileDialog ();
+			
+			Assert.IsTrue (ofd.AddExtension, "#11");
+			Assert.IsTrue (ofd.CheckFileExists, "#12");
+			Assert.IsTrue (ofd.CheckPathExists, "#13");
+			Assert.AreEqual ("", ofd.DefaultExt, "#14");
+			Assert.IsTrue (ofd.DereferenceLinks, "#15");
+			Assert.AreEqual ("", ofd.FileName, "#16");
+			Assert.IsNotNull (ofd.FileNames, "#17");
+			Assert.AreEqual (0, ofd.FileNames.Length, "#17a");
+			Assert.AreEqual ("", ofd.Filter, "#18");
+			Assert.AreEqual (1, ofd.FilterIndex, "#19");
+			Assert.AreEqual ("", ofd.InitialDirectory, "#20");
+			Assert.IsFalse (ofd.Multiselect, "#21");
+			Assert.IsFalse (ofd.ReadOnlyChecked, "#22");
+			Assert.IsFalse (ofd.RestoreDirectory, "#23");
+			Assert.IsFalse (ofd.ShowHelp, "#24");
+			Assert.IsFalse (ofd.ShowReadOnly, "#25");
+			Assert.AreEqual ("", ofd.Title, "#26");
+			Assert.IsTrue (ofd.ValidateNames, "#27");
+			Assert.AreEqual ("System.Windows.Forms.OpenFileDialog: Title: , FileName: ", ofd.ToString (), "#28");
+			
+			ofd.DefaultExt = ".TXT";
+			Assert.AreEqual ("TXT", ofd.DefaultExt, "#29");
+			
+			ofd.Filter = null;
+			Assert.AreEqual ("", ofd.Filter, "#30");
+			
+			ofd.Filter = "Text (*.txt)|*.txt|All (*.*)|*.*";
+			
+			try {
+				ofd.Filter = "abcd";
+			} catch (Exception) {
+			}
+			
+			Assert.AreEqual ("Text (*.txt)|*.txt|All (*.*)|*.*", ofd.Filter, "#30a");
+			
+			ofd.FilterIndex = 10;
+			Assert.AreEqual (10, ofd.FilterIndex, "#30aa");
+			
+			ofd.Filter = null;
+			Assert.AreEqual ("", ofd.Filter, "#30b");
+			Assert.AreEqual (10, ofd.FilterIndex, "#30ba");
+			
+			string current_path = Environment.CurrentDirectory;
+			string current_file = Path.Combine(current_path, "test_file");
+			if (!File.Exists (current_file))
+				File.Create (current_file);
+			
+			ofd.FileName = current_file;
+			
+			Assert.AreEqual (current_file, ofd.FileName, "#31");
+			
+			string[] file_names = ofd.FileNames;
+			Assert.AreEqual (current_file, file_names [0], "#32");
+			
+			ofd.Title = "Test";
+			Assert.AreEqual ("System.Windows.Forms.OpenFileDialog: Title: Test, FileName: " + current_file, ofd.ToString (), "#33");
+			
+			ofd.FileName = null;
+			Assert.AreEqual ("", ofd.FileName, "#33a");
+			Assert.IsNotNull (ofd.FileNames, "#33b");
+			Assert.AreEqual (0, ofd.FileNames.Length, "#33c");
+			
+			ofd.Reset ();
+			
+			// check again
+			Assert.IsTrue (ofd.AddExtension, "#34");
+			Assert.IsTrue (ofd.CheckFileExists, "#35");
+			Assert.IsTrue (ofd.CheckPathExists, "#36");
+			Assert.AreEqual ("", ofd.DefaultExt, "#37");
+			Assert.IsTrue (ofd.DereferenceLinks, "#38");
+			Assert.AreEqual ("", ofd.FileName, "#39");
+			Assert.IsNotNull (ofd.FileNames, "#40");
+			Assert.AreEqual ("", ofd.Filter, "#41");
+			Assert.AreEqual (1, ofd.FilterIndex, "#42");
+			Assert.AreEqual ("", ofd.InitialDirectory, "#43");
+			Assert.IsFalse (ofd.Multiselect, "#44");
+			Assert.IsFalse (ofd.ReadOnlyChecked, "#45");
+			Assert.IsFalse (ofd.RestoreDirectory, "#46");
+			Assert.IsFalse (ofd.ShowHelp, "#47");
+			Assert.IsFalse (ofd.ShowReadOnly, "#48");
+			Assert.AreEqual ("", ofd.Title, "#49");
+			Assert.IsTrue (ofd.ValidateNames, "#50");
+			Assert.AreEqual ("System.Windows.Forms.OpenFileDialog: Title: , FileName: ", ofd.ToString (), "#60");
+		}
+		
+		[Test]
+		[ExpectedException(typeof(ArgumentException))]
+		public void FileDialogFilterArgumentException () {
+			if (ofd == null)
+				ofd = new OpenFileDialog ();
+			
+			ofd.Filter = "xyafj";
+		}
+		
+		[Test]
+		public void SaveFileDialogTest ()
+		{
+			// most of the OpenFileDialogTest are also valid for SaveFileDialg
+			sfd = new SaveFileDialog ();
+			
+			Assert.IsFalse (sfd.CreatePrompt, "#61");
+			
+			Assert.IsTrue (sfd.OverwritePrompt, "#62");
+		}
+		
+		[Test]
+		public void FontDialogTest ()
+		{
+			fd = new FontDialog ();
+			
+			Assert.IsTrue (fd.AllowScriptChange, "#63");
+			Assert.IsTrue (fd.AllowSimulations, "#64");
+			Assert.IsTrue (fd.AllowVectorFonts, "#65");
+			Assert.IsTrue (fd.AllowVerticalFonts, "#66");
+			
+			Assert.AreEqual (Color.Black, fd.Color, "#67");
+			Assert.IsFalse (fd.FixedPitchOnly, "#68");
+			
+			//Assert.AreEqual ("[Font: Name=Microsoft Sans Serif, Size=8,25, Units=3, GdiCharSet=0, GdiVerticalFont=False]", fd.Font.ToString (), "#69");
+			
+			Assert.IsFalse (fd.FontMustExist, "#70");
+			
+			Assert.AreEqual (0, fd.MaxSize, "#71");
+			Assert.AreEqual (0, fd.MinSize, "#72");
+			
+			Assert.IsFalse (fd.ScriptsOnly, "#73");
+			
+			Assert.IsFalse (fd.ShowApply, "#74");
+			
+			Assert.IsFalse (fd.ShowColor, "#75");
+			
+			Assert.IsTrue (fd.ShowEffects, "#76");
+			
+			Assert.IsFalse (fd.ShowHelp, "#77");
+			
+			//Assert.AreEqual ("System.Windows.Forms.FontDialog,  Font: [Font: Name=Microsoft Sans Serif, Size=8,25, Units=3, GdiCharSet=0, GdiVerticalFont=False]", fd.ToString (), "#78");
+			
+			fd.MaxSize = -1;
+			Assert.AreEqual (0, fd.MaxSize, "#79");
+			
+			fd.MinSize = -1;
+			Assert.AreEqual (0, fd.MinSize, "#80");
+			
+			fd.MinSize = 24;
+			fd.MaxSize = 10;
+			Assert.AreEqual (10, fd.MinSize, "#81");
+			
+			fd.MinSize = 48;
+			Assert.AreEqual (48, fd.MaxSize, "#82");
+		}
+		
+		[Test]
+		public void FolderBrowserDialogTest ()
+		{
+			fbd = new FolderBrowserDialog ();
+			
+			Assert.AreEqual ("", fbd.Description, "#83");
+			
+			Assert.AreEqual (Environment.SpecialFolder.Desktop, fbd.RootFolder, "#84");
+			
+			Assert.AreEqual ("", fbd.SelectedPath, "#85");
+			
+			Assert.IsTrue (fbd.ShowNewFolderButton, "#86");
+			
+			Assert.AreEqual ("System.Windows.Forms.FolderBrowserDialog", fbd.ToString (), "#87");
+			
+			string current_path = Environment.CurrentDirectory;
+			fbd.SelectedPath = current_path;
+			
+			Assert.AreEqual (current_path, fbd.SelectedPath, "#89");
+		}
+		
+		[Test]
+		[ExpectedException(typeof(InvalidEnumArgumentException))]
+		public void FolderBrowserDialogInvalidEnumArgumentExceptionTest () {
+			if (fbd == null)
+				fbd = new FolderBrowserDialog ();
+			
+			fbd.RootFolder = (Environment.SpecialFolder)12;
+		}
+	}
+}
+