diff --git a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FileNameEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FileNameEditor.cs index ba9202bf67c..e6e75c10d09 100644 --- a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FileNameEditor.cs +++ b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FileNameEditor.cs @@ -2,49 +2,45 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.ComponentModel; -using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Drawing.Design; namespace System.Windows.Forms.Design { /// > - /// Provides an - /// editor for filenames. + /// Provides an editor for filenames. /// [CLSCompliant(false)] public class FileNameEditor : UITypeEditor { - private OpenFileDialog openFileDialog; + private OpenFileDialog _openFileDialog; /// - /// Edits the given object value using the editor style provided by - /// GetEditorStyle. A service provider is provided so that any - /// required editing services can be obtained. + /// Edits the given object value using the editor style provided by GetEditorStyle. + /// A service provider is provided so that any required editing services can be obtained. /// - [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { - Debug.Assert(provider != null, "No service provider; we cannot edit the value"); if (provider != null) { - IWindowsFormsEditorService edSvc = - (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); - - Debug.Assert(edSvc != null, "No editor service; we cannot edit the value"); - if (edSvc != null) + if (provider.GetService(typeof(IWindowsFormsEditorService)) is IWindowsFormsEditorService edSvc) { - if (openFileDialog == null) + if (_openFileDialog == null) { - openFileDialog = new OpenFileDialog(); - InitializeDialog(openFileDialog); + _openFileDialog = new OpenFileDialog(); + InitializeDialog(_openFileDialog); } - if (value is string) openFileDialog.FileName = (string)value; + if (value is string stringValue) + { + _openFileDialog.FileName = stringValue; + } - if (openFileDialog.ShowDialog() == DialogResult.OK) value = openFileDialog.FileName; + if (_openFileDialog.ShowDialog() == DialogResult.OK) + { + return _openFileDialog.FileName; + } } } @@ -52,24 +48,26 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide } /// > - /// Gets the editing style of the Edit method. If the method - /// is not supported, this will return None. + /// Gets the editing style of the Edit method. /// - [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] - // everything in this assembly is full trust. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } /// - /// Initializes the open file dialog when it is created. This gives you - /// an opportunity to configure the dialog as you please. The default - /// implementation provides a generic file filter and title. + /// Initializes the open file dialog when it is created. This gives you an opportunity to + /// configure the dialog as you please. The default implementation provides a generic file + /// filter and title. /// [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] protected virtual void InitializeDialog(OpenFileDialog openFileDialog) { + if (openFileDialog == null) + { + throw new ArgumentNullException(nameof(openFileDialog)); + } + openFileDialog.Filter = SR.GenericFileFilter; openFileDialog.Title = SR.GenericOpenFile; } diff --git a/src/System.Windows.Forms.Design.Editors/tests/UnitTests/System/Windows/Forms/Design/FileNameEditorTests.cs b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/System/Windows/Forms/Design/FileNameEditorTests.cs new file mode 100644 index 00000000000..90a245d1b92 --- /dev/null +++ b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/System/Windows/Forms/Design/FileNameEditorTests.cs @@ -0,0 +1,73 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Drawing.Design; +using System.Windows.Forms; +using System.Windows.Forms.Design; +using Moq; +using Xunit; + +namespace System.Windows.Forms.Design.Tests +{ + public class FileNameEditorTests + { + public static IEnumerable EditValue_InvalidProvider_TestData() + { + yield return new object[] { null }; + + var nullServiceProviderMock = new Mock(MockBehavior.Strict); + nullServiceProviderMock + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) + .Returns(null); + yield return new object[] { nullServiceProviderMock.Object }; + + var invalidServiceProviderMock = new Mock(MockBehavior.Strict); + invalidServiceProviderMock + .Setup(p => p.GetService(typeof(IWindowsFormsEditorService))) + .Returns(new object()); + yield return new object[] { invalidServiceProviderMock.Object }; + } + + [Theory] + [MemberData(nameof(EditValue_InvalidProvider_TestData))] + public void EditValue_InvalidProvider_ReturnsValue(IServiceProvider provider) + { + var editor = new FileNameEditor(); + var value = new object(); + Assert.Same(value, editor.EditValue(null, provider, value)); + } + + [Fact] + public void GetEditStyle_Invoke_ReturnsModal() + { + var editor = new FileNameEditor(); + Assert.Equal(UITypeEditorEditStyle.Modal, editor.GetEditStyle(null)); + } + + [Fact] + public void InitializeDialog_Invoke_Success() + { + var editor = new SubFileNameEditor(); + using (var openFileDialog = new OpenFileDialog()) + { + editor.InitializeDialog(openFileDialog); + Assert.Equal("All Files(*.*)|*.*", openFileDialog.Filter); + Assert.Equal("Open File", openFileDialog.Title); + } + } + + [Fact] + public void InitializeDialog_NullOpenFileDialog_ThrowsArgumentNullException() + { + var editor = new SubFileNameEditor(); + Assert.Throws("openFileDialog", () => editor.InitializeDialog(null)); + } + + private class SubFileNameEditor : FileNameEditor + { + public new void InitializeDialog(OpenFileDialog openFileDialog) => base.InitializeDialog(openFileDialog); + } + } +}