Author: gert
Date: 2007-02-18 14:09:22 -0500 (Sun, 18 Feb 2007)
New Revision: 73090

Added:
   
trunk/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/SaveFileDialogTest.cs
Modified:
   trunk/mcs/class/Managed.Windows.Forms/ChangeLog
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/FileDialog.cs
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/OpenFileDialog.cs
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/SaveFileDialog.cs
   trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms_test.dll.sources
   trunk/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ChangeLog
Log:
* FileDialog.cs: Removed need for separate fileName field. On 2.0
profile, do not check filename(s) for illegal character if filename(s)
were set non-interactively but always check on 1.0 profile. Fixed NRE
in DefaultExt and only strip off first leading dot. Improve exception
message when invalid Filter is set. Do not ignore InitialDirectory if
it does no exist. Store specified Title, and if empty use default
title (depending on type of dialog). Added an internal DialogTitle
property for retrieving dialog title. Fixed logic of displayed dir to
more closely match MS. Avoid setting ComboBox.Text to a zero-length
string as its buggy.
* OpenFileDialog.cs: In OpenFile, throw ArgumentNullException if
FileName is a zero-length string (it can never be null). Override
DialogTitle property to set default title of dialog box.
* SaveFileDialog.cs: Override DialogTitle property to set default
title of dialog box.
* SaveFileDialogTest.cs: Added tests for SaveFileDialog.
* System.Windows.Forms_test.dll.sources: add SaveFileDialogTest.cs.


Modified: trunk/mcs/class/Managed.Windows.Forms/ChangeLog
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/ChangeLog     2007-02-18 17:21:53 UTC 
(rev 73089)
+++ trunk/mcs/class/Managed.Windows.Forms/ChangeLog     2007-02-18 19:09:22 UTC 
(rev 73090)
@@ -1,3 +1,7 @@
+2007-02-18  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * System.Windows.Forms_test.dll.sources: add SaveFileDialogTest.cs.
+
 2007-02-14  Rolf Bjarne Kvinge  <[EMAIL PROTECTED]>
 
        * System.Windows.Forms_test.dll.sources: add EventLogger.cs

Modified: trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog        
2007-02-18 17:21:53 UTC (rev 73089)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog        
2007-02-18 19:09:22 UTC (rev 73090)
@@ -1,5 +1,23 @@
 2007-02-18  Gert Driesen  <[EMAIL PROTECTED]>
 
+       * FileDialog.cs: Removed need for separate fileName field. On 2.0
+       profile, do not check filename(s) for illegal character if filename(s)
+       were set non-interactively but always check on 1.0 profile. Fixed NRE
+        in DefaultExt and only strip off first leading dot. Improve exception
+       message when invalid Filter is set. Do not ignore InitialDirectory if
+       it does no exist. Store specified Title, and if empty use default
+       title (depending on type of dialog). Added an internal DialogTitle 
+       property for retrieving dialog title. Fixed logic of displayed dir to
+       more closely match MS. Avoid setting ComboBox.Text to a zero-length 
+       string as its buggy.
+       * OpenFileDialog.cs: In OpenFile, throw ArgumentNullException if
+       FileName is a zero-length string (it can never be null). Override 
+       DialogTitle property to set default title of dialog box.
+       * SaveFileDialog.cs: Override DialogTitle property to set default
+       title of dialog box.
+
+2007-02-18  Gert Driesen  <[EMAIL PROTECTED]>
+
        * FileDialog.cs: Modify default text of filename and filetype labels
        to match that of MS. Reset do_not_call_OnSelectedIndexChanged...
        after we've updated the SelectedIndex. Fixes part of bug #80887.

Modified: 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/FileDialog.cs
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/FileDialog.cs    
2007-02-18 17:21:53 UTC (rev 73089)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/FileDialog.cs    
2007-02-18 19:09:22 UTC (rev 73090)
@@ -43,6 +43,7 @@
        public abstract class FileDialog : CommonDialog
        {
                protected static readonly object EventFileOk = new object ();
+               private static int MaxFileNameItems = 10;
                
                internal enum FileDialogType
                {
@@ -55,7 +56,6 @@
                private bool checkPathExists = true;
                private string defaultExt = String.Empty;
                private bool dereferenceLinks = true;
-               private string fileName = String.Empty;
                private string[] fileNames;
                private string filter = "";
                private int filterIndex = 1;
@@ -65,6 +65,9 @@
                private bool showHelp = false;
                private string title = String.Empty;
                private bool validateNames = true;
+#if NET_2_0
+               private bool checkForIllegalChars = true;
+#endif
                
                private Button cancelButton;
                private ToolBarButton upToolBarButton;
@@ -236,16 +239,19 @@
                        fileNameComboBox.Location = new Point (195, 330);
                        fileNameComboBox.Size = new Size (245, 21);
                        fileNameComboBox.TabIndex = 1;
-                       fileNameComboBox.MaxDropDownItems = 10;
+                       fileNameComboBox.MaxDropDownItems = MaxFileNameItems;
                        fileNameComboBox.Items.Add (" ");
                        
                        if (configFileNames != null) {
                                fileNameComboBox.Items.Clear ();
                                
                                foreach (string configFileName in 
configFileNames) {
-                                       if (configFileName != null)
-                                               if (configFileName.Trim 
().Length > 0)
-                                                       
fileNameComboBox.Items.Add (configFileName);
+                                       if (configFileName == null || 
configFileName.Trim ().Length == 0)
+                                               continue;
+                                       // add no more than 10 items
+                                       if (fileNameComboBox.Items.Count >= 
MaxFileNameItems)
+                                               break;
+                                       fileNameComboBox.Items.Add 
(configFileName);
                                }
                        }
                        
@@ -414,14 +420,15 @@
                        get {
                                return defaultExt;
                        }
-                       
+
                        set {
-                               defaultExt = value;
-                               
-                               // if there is a dot remove it and everything 
before it
-                               if (defaultExt.LastIndexOf ('.') != - 1) {
-                                       string[] split = defaultExt.Split (new 
char [] { '.' });
-                                       defaultExt = split [split.Length - 1];
+                               if (value != null && value.Length > 0) {
+                                       // remove leading dot
+                                       if (value [0] == '.')
+                                               value = value.Substring (1);
+                                       defaultExt = value;
+                               } else {
+                                       defaultExt = string.Empty;
                                }
                        }
                }
@@ -444,25 +451,38 @@
                [DefaultValue("")]
                public string FileName {
                        get {
-                               return fileName;
+                               if (fileNames == null || fileNames.Length == 0)
+                                       return string.Empty;
+
+                               if (fileNames [0].Length == 0)
+                                       return string.Empty;
+
+#if NET_2_0
+                               // skip check for illegal characters if the 
filename was set
+                               // through FileDialog API
+                               if (!checkForIllegalChars)
+                                       return fileNames [0];
+#endif
+
+                               // ensure filename contains only valid 
characters
+                               Path.GetFullPath (fileNames [0]);
+                               // but return filename as is
+                               return fileNames [0];
+
                        }
                        
                        set {
                                if (value != null) {
-                                       if (SetFileName (value)) {
-                                               fileName = value;
-                                               if (fileNames == null) {
-                                                       fileNames = new string 
[1];
-                                                       fileNames [0] = value;
-                                               }
-                                       }
+                                       fileNames = new string [1] { 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 = " ";
                                }
+
+#if NET_2_0
+                               // skip check for illegal characters if the 
filename was set
+                               // through FileDialog API
+                               checkForIllegalChars = false;
+#endif
                        }
                }
                
@@ -471,12 +491,23 @@
                public string[] FileNames {
                        get {
                                if (fileNames == null || fileNames.Length == 0) 
{
-                                       string[] null_nada_nothing_filenames = 
new string [0];
-                                       return null_nada_nothing_filenames;
+                                       return new string [0];
                                }
                                
                                string[] new_filenames = new string 
[fileNames.Length];
                                fileNames.CopyTo (new_filenames, 0);
+
+#if NET_2_0
+                               // skip check for illegal characters if the 
filename was set
+                               // through FileDialog API
+                               if (!checkForIllegalChars)
+                                       return new_filenames;
+#endif
+
+                               foreach (string fileName in new_filenames) {
+                                       // ensure filename contains only valid 
characters
+                                       Path.GetFullPath (fileName);
+                               }
                                return new_filenames;
                        }
                }
@@ -499,7 +530,13 @@
                                                
                                                fileFilter = new FileFilter 
(filter);
                                        } else
-                                               throw new ArgumentException ();
+                                               throw new ArgumentException 
("The provided filter string"
+                                                       + " is invalid. The 
filter string should contain a"
+                                                       + " description of the 
filter, followed by the "
+                                                       + " vertical bar (|) 
and the filter pattern. The"
+                                                       + " strings for 
different filtering options should"
+                                                       + " also be separated 
by the vertical bar. Example:"
+                                                       + " Text files 
(*.txt)|*.txt|All files (*.*)|*.*");
                                }
                                
                                UpdateFilters ();
@@ -531,10 +568,10 @@
                        }
                        
                        set {
-                               if (Directory.Exists (value)) {
+                               if (value == null) {
+                                       initialDirectory = string.Empty;
+                               } else {
                                        initialDirectory = value;
-                                       
-                                       mwfFileView.ChangeDirectory (null, 
initialDirectory);
                                }
                        }
                }
@@ -570,9 +607,11 @@
                        }
                        
                        set {
-                               title = value;
-                               
-                               form.Text = title;
+                               if (value == null) {
+                                       title = string.Empty;
+                               } else {
+                                       title = value;
+                               }
                        }
                }
                
@@ -605,7 +644,7 @@
                        checkPathExists = true;
                        defaultExt = String.Empty;
                        dereferenceLinks = true;
-                       FileName = String.Empty;
+                       FileName = null;
                        Filter = String.Empty;
                        FilterIndex = 1;
                        initialDirectory = String.Empty;
@@ -619,7 +658,7 @@
                
                public override string ToString ()
                {
-                       return String.Format("{0}: Title: {1}, FileName: {2}", 
base.ToString (), Title, fileName);
+                       return String.Format("{0}: Title: {1}, FileName: {2}", 
base.ToString (), Title, FileName);
                }
                
                public event CancelEventHandler FileOk {
@@ -640,6 +679,12 @@
                protected int Options {
                        get { return -1; }
                }
+
+               internal virtual string DialogTitle {
+                       get {
+                               return Title;
+                       }
+               }
                
                [MonoTODO]
                protected  override IntPtr HookProc (IntPtr hWnd, int msg, 
IntPtr wparam, IntPtr lparam)
@@ -660,14 +705,22 @@
                        disable_form_closed_event = true;
                }
                
-               protected  override bool RunDialog (IntPtr hWndOwner)
+               protected override bool RunDialog (IntPtr hWndOwner)
                {
                        ReadConfigValues ();
-                       
+                       form.Text = DialogTitle;
+
+                       // avoid using the FileNames property to skip the 
invalid characters
+                       // check
+                       string fileName = null;
+                       if (fileNames != null && fileNames.Length != 0)
+                               fileName = fileNames [0];
+                       else
+                               fileName = string.Empty;
+
                        form.Refresh ();
-                       
-                       mwfFileView.ChangeDirectory (null, lastFolder);
-                       
+
+                       SetFileAndDirectory (fileName);
                        fileNameComboBox.Select ();
                        
                        return true;
@@ -736,37 +789,38 @@
                        do_not_call_OnSelectedIndexChangedFileTypeComboBox = 
false;
                        mwfFileView.FilterIndex = filterIndex;
                }
-               
-               private bool SetFileName (string fname)
+
+               private void SetFileAndDirectory (string fname)
                {
-                       bool rooted = Path.IsPathRooted (fname);
-                       
-                       if (!rooted) {
-                               string dir = mwfFileView.CurrentRealFolder ;
-                               if (dir == null) {
-                                       dir = Environment.CurrentDirectory;
-                               }
-                               if (File.Exists (Path.Combine (dir, fname))) {
+                       if (fname.Length != 0) {
+                               bool rooted = Path.IsPathRooted (fname);
+                               if (!rooted) {
+                                       mwfFileView.ChangeDirectory (null, 
lastFolder);
                                        fileNameComboBox.Text = fname;
-                                       mwfFileView.SetSelectedIndexTo (fname);
-                                       
-                                       return true;
+                               } else {
+                                       string dirname = Path.GetDirectoryName 
(fname);
+                                       if (dirname != null && dirname.Length > 
0 && Directory.Exists (dirname)) {
+                                               fileNameComboBox.Text = 
Path.GetFileName (fname);
+                                               mwfFileView.ChangeDirectory 
(null, dirname);
+                                       } else {
+                                               fileNameComboBox.Text = fname;
+                                               mwfFileView.ChangeDirectory 
(null, lastFolder);
+                                       }
                                }
                        } else {
-                               if (File.Exists (fname)) {
-                                       fileNameComboBox.Text = 
Path.GetFileName (fname);
-                                       mwfFileView.ChangeDirectory (null, 
Path.GetDirectoryName (fname));
-                                       mwfFileView.SetSelectedIndexTo (fname);
-                                       
-                                       return true;
-                               }
+                               mwfFileView.ChangeDirectory (null, lastFolder);
+                               fileNameComboBox.Text = " ";
                        }
-                       
-                       return false;
                }
                
                void OnClickOpenSaveButton (object sender, EventArgs e)
                {
+#if NET_2_0
+                       // for filenames typed or selected by user, enable 
check for 
+                       // illegal characters in filename(s)
+                       checkForIllegalChars = true;
+#endif
+
                        if (fileDialogType == FileDialogType.OpenFileDialog) {
                                ListView.SelectedListViewItemCollection sl = 
mwfFileView.SelectedItems;
                                if (sl.Count > 0 && sl [0] != null) {
@@ -816,12 +870,11 @@
                                                DirectoryInfo dirInfo = new 
DirectoryInfo (fileFromComboBox);
                                                if (dirInfo.Exists) {
                                                        
mwfFileView.ChangeDirectory (null, dirInfo.FullName);
-                                                       
                                                        fileNameComboBox.Text = 
" ";
                                                        return;
                                                } else {
                                                        internalfullfilename = 
fileFromComboBox;
-                                               }                               
                        
+                                               }
                                        }
                                } else
                                        return;
@@ -889,13 +942,12 @@
                                                        internalfullfilename += 
extension_to_use;
                                        }
                                }
+
+                               // do not use the FileName setter internally, 
as this will 
+                               // cause the illegal character check to be 
skipped, and we
+                               // do not want this for filename users have 
entered
+                               fileNames = new string [1] { 
internalfullfilename };
                                
-                               fileNames = new string [1];
-                               
-                               fileNames [0] = internalfullfilename;
-                               
-                               fileName = internalfullfilename;
-                               
                                mwfFileView.WriteRecentlyUsed 
(internalfullfilename);
                        } else // multiSelect = true
                        if (fileDialogType != FileDialogType.SaveFileDialog) {
@@ -911,8 +963,6 @@
                                                }
                                        }
                                        
-                                       fileName = ((FSEntry)al [0]).FullName;
-                                       
                                        fileNames = new string [al.Count];
                                        
                                        for (int i = 0; i < al.Count; i++) {
@@ -926,7 +976,7 @@
                                        string message = "\"" + 
mwfFileView.CurrentRealFolder + "\" doesn't exist. Please verify that you have 
entered the correct directory name.";
                                        MessageBox.Show (message, 
openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                        
-                                       if (initialDirectory == String.Empty)
+                                       if (initialDirectory == String.Empty || 
!Directory.Exists (initialDirectory))
                                                mwfFileView.ChangeDirectory 
(null, lastFolder);
                                        else
                                                mwfFileView.ChangeDirectory 
(null, initialDirectory);
@@ -940,17 +990,20 @@
                        } else {
                                lastFolder = mwfFileView.CurrentFolder;
                        }
-                       
-                       if (fileNameComboBox.Items.Count > 0) {
-                               if (fileNameComboBox.Items.IndexOf (fileName) 
== -1) {
+
+                       foreach (string fileName in fileNames) {
+                               if (!File.Exists (fileName))
+                                       // ignore files that do not exist
+                                       continue;
+
+                               if (fileNameComboBox.Items.IndexOf (fileName) 
== -1)
                                        fileNameComboBox.Items.Insert (0, 
fileName);
-                               }
-                       } else
-                               fileNameComboBox.Items.Add (fileName);
+                       }
+
+                       // remove items above the maximum items that we want to 
display
+                       while (fileNameComboBox.Items.Count > MaxFileNameItems)
+                               fileNameComboBox.Items.RemoveAt 
(MaxFileNameItems);
                        
-                       if (fileNameComboBox.Items.Count == 11)
-                               fileNameComboBox.Items.RemoveAt (10);
-                       
                        CancelEventArgs cancelEventArgs = new CancelEventArgs 
();
                        
                        cancelEventArgs.Cancel = false;
@@ -1011,7 +1064,9 @@
                
                void OnSelectedFilesChangedFileView (object sender, EventArgs e)
                {
-                       fileNameComboBox.Text = mwfFileView.SelectedFilesString;
+                       string selectedFiles = mwfFileView.SelectedFilesString;
+                       if (selectedFiles != null && selectedFiles.Length != 0)
+                               fileNameComboBox.Text = selectedFiles;
                }
                
                void OnForceDialogEndFileView (object sender, EventArgs e)
@@ -1140,11 +1195,11 @@
                                }
                        }
                        
-                       if (initialDirectory != String.Empty)
+                       if (initialDirectory != String.Empty && 
Directory.Exists (initialDirectory))
                                lastFolder = initialDirectory;
                        else
-                       if (lastFolder == null || lastFolder == String.Empty)
-                               lastFolder = Environment.CurrentDirectory;
+                               if (lastFolder == null || lastFolder.Length == 
0)
+                                       lastFolder = 
Environment.CurrentDirectory;
                        
                        if (RestoreDirectory)
                                restoreDirectoryString = lastFolder;
@@ -2480,23 +2535,23 @@
                                        FileViewListViewItem listViewItem = 
SelectedItems [0] as FileViewListViewItem;
                                        
                                        FSEntry fsEntry = listViewItem.FSEntry;
-                                       
+
                                        if (fsEntry.Attributes != 
FileAttributes.Directory)
                                                selectedFilesString = 
SelectedItems [0].Text;
                                } else {
                                        foreach (FileViewListViewItem lvi in 
SelectedItems) {
                                                FSEntry fsEntry = lvi.FSEntry;
-                                               
+
                                                if (fsEntry.Attributes != 
FileAttributes.Directory)
-                                                       selectedFilesString = 
selectedFilesString + "\"" + lvi.Text + " ";
+                                                       selectedFilesString = 
selectedFilesString + "\"" + lvi.Text + "\" ";
                                        }
                                }
-                               
+
                                EventHandler eh = (EventHandler)(Events 
[MSelectedFilesChangedEvent]);
                                if (eh != null)
                                        eh (this, EventArgs.Empty);
                        }
-                       
+
                        base.OnSelectedIndexChanged (e);
                }
                

Modified: 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/OpenFileDialog.cs
===================================================================
--- 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/OpenFileDialog.cs    
    2007-02-18 17:21:53 UTC (rev 73089)
+++ 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/OpenFileDialog.cs    
    2007-02-18 19:09:22 UTC (rev 73090)
@@ -103,7 +103,7 @@
                #region Public Instance Methods
                public Stream OpenFile ()
                {
-                       if (FileName == null)
+                       if (FileName.Length == 0)
                                throw new ArgumentNullException ("OpenFile", 
"FileName is null");
                        
                        return new FileStream (FileName, FileMode.Open, 
FileAccess.Read);
@@ -118,6 +118,15 @@
                        base.ReadOnlyChecked = false;
                        base.ShowReadOnly = false;
                }
+
+               internal override string DialogTitle {
+                       get {
+                               string title = base.DialogTitle;
+                               if (title.Length == 0)
+                                       title = "Open";
+                               return title;
+                       }
+               }
        }
 }
 

Modified: 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/SaveFileDialog.cs
===================================================================
--- 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/SaveFileDialog.cs    
    2007-02-18 17:21:53 UTC (rev 73089)
+++ 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/SaveFileDialog.cs    
    2007-02-18 19:09:22 UTC (rev 73090)
@@ -98,6 +98,14 @@
                        overwritePrompt = true;
                        createPrompt = false;
                }
+
+               internal override string DialogTitle {
+                       get {
+                               string title = base.DialogTitle;
+                               if (title.Length == 0)
+                                       title = "Save As";
+                               return title;
+                       }
+               }
        }
 }
-

Modified: 
trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms_test.dll.sources
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms_test.dll.sources 
2007-02-18 17:21:53 UTC (rev 73089)
+++ trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms_test.dll.sources 
2007-02-18 19:09:22 UTC (rev 73090)
@@ -83,6 +83,7 @@
 System.Windows.Forms/RadioButtonTest.cs
 System.Windows.Forms/RowStyleTest.cs
 System.Windows.Forms/RichTextBoxTest.cs
+System.Windows.Forms/SaveFileDialogTest.cs
 System.Windows.Forms/ScrollableControlTest.cs
 System.Windows.Forms/ScrollBarTest.cs
 System.Windows.Forms/SendKeysTest.cs

Modified: 
trunk/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ChangeLog
===================================================================
--- trunk/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ChangeLog   
2007-02-18 17:21:53 UTC (rev 73089)
+++ trunk/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ChangeLog   
2007-02-18 19:09:22 UTC (rev 73090)
@@ -1,3 +1,7 @@
+2007-02-18  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * SaveFileDialogTest.cs: Added tests for SaveFileDialog.
+
 2007-02-15  Jonathan Pobst  <[EMAIL PROTECTED]>
 
        * ToolStripItemTest.cs: BehaviorImageList fails on *nix due to a bug in

Added: 
trunk/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/SaveFileDialogTest.cs
===================================================================
--- 
trunk/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/SaveFileDialogTest.cs
       2007-02-18 17:21:53 UTC (rev 73089)
+++ 
trunk/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/SaveFileDialogTest.cs
       2007-02-18 19:09:22 UTC (rev 73090)
@@ -0,0 +1,414 @@
+//
+// SaveFileDialogTest.cs: Tests for SaveFileDialog class.
+//
+// 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) 2007 Gert Driesen
+//
+// Authors:
+//     Gert Driesen ([EMAIL PROTECTED])
+//
+
+using System;
+using System.IO;
+using System.Text;
+using System.Windows.Forms;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Windows.Forms
+{
+       [TestFixture]
+       public class SaveDialogTest
+       {
+               [Test]
+               public void AddExtension ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsTrue (sfd.AddExtension, "#1");
+                       sfd.AddExtension = false;
+                       Assert.IsFalse (sfd.AddExtension, "#2");
+               }
+
+               [Test]
+               public void CheckFileExists ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsFalse (sfd.CheckFileExists, "#1");
+                       sfd.CheckFileExists = true;
+                       Assert.IsTrue (sfd.CheckFileExists, "#2");
+               }
+
+               [Test]
+               public void CheckPathExists ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsTrue (sfd.CheckPathExists, "#1");
+                       sfd.CheckPathExists = false;
+                       Assert.IsFalse (sfd.CheckPathExists, "#2");
+               }
+
+               [Test]
+               public void CreatePrompt ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsFalse (sfd.CreatePrompt, "#1");
+                       sfd.CreatePrompt = true;
+                       Assert.IsTrue (sfd.CreatePrompt, "#2");
+               }
+
+               [Test]
+               public void DefaultExt ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsNotNull (sfd.DefaultExt, "#A1");
+                       Assert.AreEqual (string.Empty, sfd.DefaultExt, "#A2");
+
+                       sfd.DefaultExt = "txt";
+                       Assert.IsNotNull (sfd.DefaultExt, "#B1");
+                       Assert.AreEqual ("txt", sfd.DefaultExt, "#B2");
+
+                       sfd.DefaultExt = null;
+                       Assert.IsNotNull (sfd.DefaultExt, "#C1");
+                       Assert.AreEqual (string.Empty, sfd.DefaultExt, "#C2");
+
+                       sfd.DefaultExt = ".Xml";
+                       Assert.IsNotNull (sfd.DefaultExt, "#D1");
+                       Assert.AreEqual ("Xml", sfd.DefaultExt, "#D2");
+
+                       sfd.DefaultExt = ".tar.gz";
+                       Assert.IsNotNull (sfd.DefaultExt, "#E1");
+                       Assert.AreEqual ("tar.gz", sfd.DefaultExt, "#E2");
+
+                       sfd.DefaultExt = "..Xml";
+                       Assert.IsNotNull (sfd.DefaultExt, "#F1");
+                       Assert.AreEqual (".Xml", sfd.DefaultExt, "#F2");
+
+                       sfd.DefaultExt = "tar.gz";
+                       Assert.IsNotNull (sfd.DefaultExt, "#G1");
+                       Assert.AreEqual ("tar.gz", sfd.DefaultExt, "#G2");
+
+                       sfd.DefaultExt = ".";
+                       Assert.IsNotNull (sfd.DefaultExt, "#H1");
+                       Assert.AreEqual (string.Empty, sfd.DefaultExt, "#H2");
+               }
+
+               [Test]
+               public void DereferenceLinks ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsTrue (sfd.DereferenceLinks, "#1");
+                       sfd.DereferenceLinks = false;
+                       Assert.IsFalse (sfd.DereferenceLinks, "#2");
+               }
+
+               [Test]
+               public void FileName ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsNotNull (sfd.FileName, "#A1");
+                       Assert.AreEqual (string.Empty, sfd.FileName, "#A2");
+
+                       sfd.FileName = "default.build";
+                       Assert.IsNotNull (sfd.FileName, "#B1");
+                       Assert.AreEqual ("default.build", sfd.FileName, "#B2");
+
+                       sfd.FileName = null;
+                       Assert.IsNotNull (sfd.FileName, "#C1");
+                       Assert.AreEqual (string.Empty, sfd.FileName, "#C2");
+
+                       sfd.FileName = string.Empty;
+                       Assert.IsNotNull (sfd.FileName, "#D1");
+                       Assert.AreEqual (string.Empty, sfd.FileName, "#D2");
+               }
+
+               [Test]
+               public void FileName_InvalidPathCharacter ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       sfd.FileName = Path.InvalidPathChars [0] + "file";
+#if NET_2_0
+                       Assert.IsNotNull (sfd.FileName, "#1");
+                       Assert.AreEqual (Path.InvalidPathChars [0] + "file", 
sfd.FileName, "#2");
+#else
+                       try {
+                               string fileName = sfd.FileName;
+                               Assert.Fail ("#1: " + fileName);
+                       } catch (ArgumentException ex) {
+                               // The path contains illegal characters
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               //Assert.IsNull (ex.ParamName, "#5");
+                       }
+#endif
+               }
+
+               [Test]
+               public void FileNames ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsNotNull (sfd.FileNames, "#A1");
+                       Assert.AreEqual (0, sfd.FileNames.Length, "#A2");
+
+                       sfd.FileName = "default.build";
+                       Assert.IsNotNull (sfd.FileNames, "#B1");
+                       Assert.AreEqual (1, sfd.FileNames.Length, "#B2");
+                       Assert.AreEqual ("default.build", sfd.FileNames [0], 
"#B3");
+
+                       sfd.FileName = null;
+                       Assert.IsNotNull (sfd.FileNames, "#C1");
+                       Assert.AreEqual (0, sfd.FileNames.Length, "#C2");
+               }
+
+               [Test]
+               public void FileNames_InvalidPathCharacter ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       sfd.FileName = Path.InvalidPathChars [0] + "file";
+#if NET_2_0
+                       Assert.IsNotNull (sfd.FileNames, "#1");
+                       Assert.AreEqual (1, sfd.FileNames.Length, "#2");
+                       Assert.AreEqual (Path.InvalidPathChars [0] + "file", 
sfd.FileNames [0], "#3");
+#else
+                       try {
+                               string [] fileNames = sfd.FileNames;
+                               Assert.Fail ("#1: " + fileNames.Length);
+                       } catch (ArgumentException ex) {
+                               // The path contains illegal characters
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               //Assert.IsNull (ex.ParamName, "#5");
+                       }
+#endif
+               }
+
+               [Test]
+               public void Filter ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsNotNull (sfd.Filter, "#A1");
+                       Assert.AreEqual (string.Empty, sfd.Filter, "#A2");
+
+                       sfd.Filter = "Text files (*.txt)|*.txt|All files 
(*.*)|*.*";
+                       Assert.IsNotNull (sfd.Filter, "#B1");
+                       Assert.AreEqual ("Text files (*.txt)|*.txt|All files 
(*.*)|*.*", sfd.Filter, "#B2");
+
+                       sfd.Filter = null;
+                       Assert.IsNotNull (sfd.Filter, "#C1");
+                       Assert.AreEqual (string.Empty, sfd.Filter, "#C2");
+
+                       sfd.Filter = string.Empty;
+                       Assert.IsNotNull (sfd.Filter, "#D1");
+                       Assert.AreEqual (string.Empty, sfd.Filter, "#D2");
+               }
+
+               [Test]
+               public void Filter_InvalidFormat ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       try {
+                               sfd.Filter = "Text files (*.txt)|*.txt|All 
files (*.*)";
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // The provided filter string is invalid. The 
filter string
+                               // should contain a description of the filter, 
followed by the
+                               // vertical bar (|) and the filter pattern. The 
strings for
+                               // different filtering options should also be 
separated by the
+                               // vertical bar. Example: "Text files 
(*.txt)|*.txt|All files
+                               // (*.*)|*.*"
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
+               }
+
+               [Test]
+               public void FilterIndex ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.AreEqual (1, sfd.FilterIndex, "#1");
+                       sfd.FilterIndex = 99;
+                       Assert.AreEqual (99, sfd.FilterIndex, "#2");
+               }
+
+               [Test]
+               public void InitialDirectory ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsNotNull (sfd.InitialDirectory, "#A1");
+                       Assert.AreEqual (string.Empty, sfd.InitialDirectory, 
"#A2");
+
+                       sfd.InitialDirectory = Path.GetTempPath ();
+                       Assert.IsNotNull (sfd.InitialDirectory, "#B1");
+                       Assert.AreEqual (Path.GetTempPath (), 
sfd.InitialDirectory, "#B2");
+
+                       sfd.InitialDirectory = null;
+                       Assert.IsNotNull (sfd.InitialDirectory, "#C1");
+                       Assert.AreEqual (string.Empty, sfd.InitialDirectory, 
"#C2");
+
+                       string initialDir = Path.Combine (Path.GetTempPath (), 
+                               "doesnotexistforsure");
+                       sfd.InitialDirectory = initialDir;
+                       Assert.IsNotNull (sfd.InitialDirectory, "#D1");
+                       Assert.AreEqual (initialDir, sfd.InitialDirectory, 
"#D2");
+
+               }
+
+               [Test]
+               public void OverwritePrompt ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsTrue (sfd.OverwritePrompt, "#1");
+                       sfd.OverwritePrompt = false;
+                       Assert.IsFalse (sfd.OverwritePrompt, "#2");
+               }
+
+               [Test]
+               public void Reset ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       sfd.AddExtension = false;
+                       sfd.CheckFileExists = true;
+                       sfd.CheckPathExists = false;
+                       sfd.CreatePrompt = true;
+                       sfd.DefaultExt = "txt";
+                       sfd.DereferenceLinks = false;
+                       sfd.FileName = "default.build";
+                       sfd.Filter = "Text files (*.txt)|*.txt|All files 
(*.*)|*.*";
+                       sfd.FilterIndex = 5;
+                       sfd.InitialDirectory = Path.GetTempPath ();
+                       sfd.OverwritePrompt = false;
+                       sfd.RestoreDirectory = true;
+                       sfd.ShowHelp = true;
+                       sfd.Title = "Saving";
+                       sfd.ValidateNames = false;
+                       sfd.Reset ();
+
+                       Assert.IsTrue (sfd.AddExtension, "#1");
+                       Assert.IsFalse (sfd.CheckFileExists, "#2");
+                       Assert.IsTrue (sfd.CheckPathExists, "#3");
+                       Assert.IsFalse (sfd.CreatePrompt, "#4");
+                       Assert.IsNotNull (sfd.DefaultExt, "#5");
+                       Assert.AreEqual (string.Empty, sfd.DefaultExt, "#6");
+                       Assert.IsTrue (sfd.DereferenceLinks, "#7");
+                       Assert.IsNotNull (sfd.FileName, "#8");
+                       Assert.AreEqual (string.Empty, sfd.FileName, "#9");
+                       Assert.IsNotNull (sfd.FileNames, "#10");
+                       Assert.AreEqual (0, sfd.FileNames.Length, "#11");
+                       Assert.IsNotNull (sfd.Filter, "#12");
+                       Assert.AreEqual (string.Empty, sfd.Filter, "#13");
+                       Assert.AreEqual (1, sfd.FilterIndex, "#14");
+                       Assert.IsNotNull (sfd.InitialDirectory, "#15");
+                       Assert.AreEqual (string.Empty, sfd.InitialDirectory, 
"#16");
+                       Assert.IsTrue (sfd.OverwritePrompt, "#17");
+                       Assert.IsFalse (sfd.RestoreDirectory, "#18");
+                       Assert.IsFalse (sfd.ShowHelp, "#19");
+                       Assert.IsNotNull (sfd.Title, "#20");
+                       Assert.AreEqual (string.Empty, sfd.Title, "#21");
+                       Assert.IsTrue (sfd.ValidateNames, "#22");
+               }
+
+               [Test]
+               public void RestoreDirectory ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsFalse (sfd.RestoreDirectory, "#1");
+                       sfd.RestoreDirectory = true;
+                       Assert.IsTrue (sfd.RestoreDirectory, "#2");
+               }
+
+               [Test]
+               public void ShowHelp ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsFalse (sfd.ShowHelp, "#1");
+                       sfd.ShowHelp = true;
+                       Assert.IsTrue (sfd.ShowHelp, "#2");
+               }
+
+               [Test]
+               public void Title ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsNotNull (sfd.Title, "#A1");
+                       Assert.AreEqual (string.Empty, sfd.Title, "#A2");
+
+                       sfd.Title = "Saving";
+                       Assert.IsNotNull (sfd.Title, "#B1");
+                       Assert.AreEqual ("Saving", sfd.Title, "#B2");
+
+                       sfd.Title = null;
+                       Assert.IsNotNull (sfd.Title, "#C1");
+                       Assert.AreEqual (string.Empty, sfd.Title, "#C2");
+               }
+
+               [Test]
+               public void ToStringTest ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       sfd.CheckFileExists = true;
+                       sfd.CreatePrompt = true;
+                       sfd.DefaultExt = "txt";
+                       sfd.DereferenceLinks = false;
+                       sfd.FileName = "default.build";
+                       sfd.Filter = "Text files (*.txt)|*.txt|All files 
(*.*)|*.*";
+                       sfd.FilterIndex = 5;
+                       sfd.InitialDirectory = Path.GetTempPath ();
+                       sfd.OverwritePrompt = false;
+                       sfd.RestoreDirectory = true;
+                       sfd.ShowHelp = true;
+                       sfd.Title = "Saving";
+                       sfd.ValidateNames = false;
+
+                       StringBuilder sb = new StringBuilder ();
+                       sb.Append (typeof (SaveFileDialog).FullName);
+                       sb.Append (": Title: ");
+                       sb.Append (sfd.Title);
+                       sb.Append (", FileName: ");
+                       sb.Append (sfd.FileName);
+
+                       Assert.AreEqual (sb.ToString (), sfd.ToString (), "#1");
+
+                       sfd.FileName = null;
+                       sfd.Title = null;
+
+                       sb.Length = 0;
+                       sb.Append (typeof (SaveFileDialog).FullName);
+                       sb.Append (": Title: ");
+                       sb.Append (sfd.Title);
+                       sb.Append (", FileName: ");
+                       sb.Append (sfd.FileName);
+
+                       Assert.AreEqual (sb.ToString (), sfd.ToString (), "#2");
+               }
+
+               [Test]
+               public void ValidateNames ()
+               {
+                       SaveFileDialog sfd = new SaveFileDialog ();
+                       Assert.IsTrue (sfd.ValidateNames, "#1");
+                       sfd.ValidateNames = false;
+                       Assert.IsFalse (sfd.ValidateNames, "#2");
+               }
+       }
+}


Property changes on: 
trunk/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/SaveFileDialogTest.cs
___________________________________________________________________
Name: svn:eol-style
   + native

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to