Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,72 @@
// 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
{
/// <summary>>
/// Provides an
/// editor for filenames.
/// Provides an editor for filenames.
/// </summary>
[CLSCompliant(false)]
public class FileNameEditor : UITypeEditor
{
private OpenFileDialog openFileDialog;
private OpenFileDialog _openFileDialog;

/// <summary>
/// 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.
/// </summary>
[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;
}
}
}

return value;
}

/// <summary>>
/// 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.
/// </summary>
[SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
// everything in this assembly is full trust.
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}

/// <summary>
/// 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.
/// </summary>
[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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<object[]> EditValue_InvalidProvider_TestData()
{
yield return new object[] { null };

var nullServiceProviderMock = new Mock<IServiceProvider>(MockBehavior.Strict);
nullServiceProviderMock
.Setup(p => p.GetService(typeof(IWindowsFormsEditorService)))
.Returns(null);
yield return new object[] { nullServiceProviderMock.Object };

var invalidServiceProviderMock = new Mock<IServiceProvider>(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<ArgumentNullException>("openFileDialog", () => editor.InitializeDialog(null));
}

private class SubFileNameEditor : FileNameEditor
{
public new void InitializeDialog(OpenFileDialog openFileDialog) => base.InitializeDialog(openFileDialog);
}
}
}