diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionItem.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionItem.cs index 545959df806..34d64277245 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionItem.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionItem.cs @@ -15,61 +15,25 @@ namespace System.ComponentModel.Design /// public abstract class DesignerActionItem { - private bool _allowAssociate; - private readonly string _displayName; - private readonly string _description; - private readonly string _category; private IDictionary _properties; - private bool _showInSourceView = true; public DesignerActionItem(string displayName, string category, string description) { - _category = category; - _description = description; - _displayName = displayName == null ? null : Regex.Replace(displayName, @"\(\&.\)", ""); + DisplayName = displayName == null ? null : Regex.Replace(displayName, @"\(\&.\)", ""); + Category = category; + Description = description; } - internal DesignerActionItem() - { - } + public bool AllowAssociate { get; set; } - public bool AllowAssociate - { - get => _allowAssociate; - set => _allowAssociate = value; - } + public virtual string Category { get; } - public virtual string Category - { - get => _category; - } + public virtual string Description { get; } - public virtual string Description - { - get => _description; - } + public virtual string DisplayName { get; } - public virtual string DisplayName - { - get => _displayName; - } + public IDictionary Properties => _properties ?? (_properties = new HybridDictionary()); - public IDictionary Properties - { - get - { - if (_properties == null) - { - _properties = new HybridDictionary(); - } - return _properties; - } - } - - public bool ShowInSourceView - { - get => _showInSourceView; - set => _showInSourceView = value; - } + public bool ShowInSourceView { get; set; } = true; } } diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionList.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionList.cs index 15789da42e8..0b11c725058 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionList.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionList.cs @@ -14,53 +14,23 @@ namespace System.ComponentModel.Design /// public class DesignerActionList { - private bool _autoShow = false; - private readonly IComponent _component; - - /// - /// takes the related component as a parameter - /// public DesignerActionList(IComponent component) { - _component = component; + Component = component; } - public virtual bool AutoShow - { - get => _autoShow; - set - { - if (_autoShow != value) - { - _autoShow = value; - } - } - } + public virtual bool AutoShow { get; set; } - /// - /// this will be null for list created from upgraded verbs collection... - /// - public IComponent Component - { - get => _component; - } + public IComponent Component { get; } public object GetService(Type serviceType) { - if (_component != null && _component.Site != null) - { - return _component.Site.GetService(serviceType); - } - else - { - return null; - } + return Component?.Site?.GetService(serviceType); } public virtual DesignerActionItemCollection GetSortedActionItems() { - string dispName, desc, cat; - SortedList items = new SortedList(); + var items = new SortedList(); // we want to ignore the public methods and properties for THIS class (only take the inherited ones) IList originalMethods = Array.AsReadOnly(typeof(DesignerActionList).GetMethods(BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public)); @@ -71,11 +41,14 @@ public virtual DesignerActionItemCollection GetSortedActionItems() foreach (MethodInfo info in methods) { if (originalMethods.Contains(info)) + { continue; + } + // Make sure there are only methods that take no parameters if (info.GetParameters().Length == 0 && !info.IsSpecialName) { - GetMemberDisplayProperties(info, out dispName, out desc, out cat); + GetMemberDisplayProperties(info, out string dispName, out string desc, out string cat); items.Add(info.Name, new DesignerActionMethodItem(this, info.Name, dispName, cat, desc)); } } @@ -85,43 +58,46 @@ public virtual DesignerActionItemCollection GetSortedActionItems() foreach (PropertyInfo info in properties) { if (originalProperties.Contains(info)) + { continue; - GetMemberDisplayProperties(info, out dispName, out desc, out cat); + } + + GetMemberDisplayProperties(info, out string dispName, out string desc, out string cat); items.Add(dispName, new DesignerActionPropertyItem(info.Name, dispName, cat, desc)); } - DesignerActionItemCollection returnValue = new DesignerActionItemCollection(); + var returnValue = new DesignerActionItemCollection(); foreach (DesignerActionItem dai in items.Values) { returnValue.Add(dai); } + return returnValue; } + private object GetCustomAttribute(MemberInfo info, Type attributeType) { object[] attributes = info.GetCustomAttributes(attributeType, true); - if (attributes.Length > 0) - { - return attributes[0]; - } - else - { - return null; - } + return attributes.Length > 0 ? attributes[0] : null; } private void GetMemberDisplayProperties(MemberInfo info, out string displayName, out string description, out string category) { - displayName = description = category = ""; + displayName = string.Empty; + description = string.Empty; + category = string.Empty; + if (GetCustomAttribute(info, typeof(DescriptionAttribute)) is DescriptionAttribute descAttr) { description = descAttr.Description; } + DisplayNameAttribute dispNameAttr = GetCustomAttribute(info, typeof(DisplayNameAttribute)) as DisplayNameAttribute; if (dispNameAttr != null) { displayName = dispNameAttr.DisplayName; } + CategoryAttribute catAttr = GetCustomAttribute(info, typeof(CategoryAttribute)) as CategoryAttribute; if (dispNameAttr != null) { diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionListCollection.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionListCollection.cs index 29c32c4aea4..8d708ab5032 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionListCollection.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionListCollection.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System.Collections; -using System.Diagnostics; using System.Runtime.InteropServices; namespace System.ComponentModel.Design @@ -26,10 +25,7 @@ public DesignerActionList this[int index] set => List[index] = value; } - public int Add(DesignerActionList value) - { - return List.Add(value); - } + public int Add(DesignerActionList value) => List.Add(value); public void AddRange(DesignerActionList[] value) { @@ -37,6 +33,7 @@ public void AddRange(DesignerActionList[] value) { throw new ArgumentNullException(nameof(value)); } + for (int i = 0; i < value.Length; i++) { Add(value[i]); @@ -49,6 +46,7 @@ public void AddRange(DesignerActionListCollection value) { throw new ArgumentNullException(nameof(value)); } + int currentCount = value.Count; for (int i = 0; i < currentCount; i++) { @@ -66,22 +64,9 @@ public void AddRange(DesignerActionListCollection value) public void CopyTo(DesignerActionList[] array, int index) => List.CopyTo(array, index); - protected override void OnSet(int index, object oldValue, object newValue) - { - } - - protected override void OnInsert(int index, object value) + protected override void OnValidate(object value) { + // Don't perform any validation. } - - protected override void OnClear() - { - } - - protected override void OnRemove(int index, object value) - { - } - - protected override void OnValidate(object value) => Debug.Assert(value != null, "Don't add null actionlist!"); } } diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ExceptionCollection.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ExceptionCollection.cs index af19d3b4f67..65e09f954b2 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ExceptionCollection.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ExceptionCollection.cs @@ -4,43 +4,26 @@ using System.Collections; using System.Runtime.Serialization; -using System.Security; namespace System.ComponentModel.Design { [Serializable] public sealed class ExceptionCollection : Exception { - readonly ArrayList _exceptions; + private readonly ArrayList _exceptions; public ExceptionCollection(ArrayList exceptions) { _exceptions = exceptions; } - /// - /// Need this constructor since Exception implements ISerializable. - /// private ExceptionCollection(SerializationInfo info, StreamingContext context) : base(info, context) { _exceptions = (ArrayList)info.GetValue("exceptions", typeof(ArrayList)); } - public ArrayList Exceptions - { - get - { - if (_exceptions != null) - { - return (ArrayList)_exceptions.Clone(); - } - return null; - } - } + public ArrayList Exceptions => (ArrayList)_exceptions?.Clone(); - /// - /// Need this since Exception implements ISerializable and we have fields to save out. - /// public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ProjectTargetFrameworkAttribute.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ProjectTargetFrameworkAttribute.cs index 24956e289c2..7a0c4917196 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ProjectTargetFrameworkAttribute.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/ProjectTargetFrameworkAttribute.cs @@ -7,15 +7,11 @@ namespace System.ComponentModel.Design [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct)] public sealed class ProjectTargetFrameworkAttribute : Attribute { - private string _targetFrameworkMoniker; public ProjectTargetFrameworkAttribute(string targetFrameworkMoniker) { - _targetFrameworkMoniker = targetFrameworkMoniker; + TargetFrameworkMoniker = targetFrameworkMoniker; } - public string TargetFrameworkMoniker - { - get => _targetFrameworkMoniker; - } + public string TargetFrameworkMoniker { get; } } } diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/CodeDomSerializerException.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/CodeDomSerializerException.cs index c714c72095e..02b56516b6e 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/CodeDomSerializerException.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/CodeDomSerializerException.cs @@ -9,68 +9,47 @@ namespace System.ComponentModel.Design.Serialization { /// - /// The exception that is thrown when the code dom serializer experiences an error. + /// The exception that is thrown when the code dom serializer experiences an error. /// [Serializable] [SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors")] public class CodeDomSerializerException : SystemException { - - private readonly CodeLinePragma _linePragma; - - /// - /// Initializes a new instance of the CodeDomSerializerException class. - /// public CodeDomSerializerException(string message, CodeLinePragma linePragma) : base(message) { - _linePragma = linePragma; + LinePragma = linePragma; } - /// - /// Initializes a new instance of the CodeDomSerializerException class. - /// - public CodeDomSerializerException(Exception ex, CodeLinePragma linePragma) : base(ex.Message, ex) + public CodeDomSerializerException(Exception ex, CodeLinePragma linePragma) : base(ex?.Message, ex) { - _linePragma = linePragma; + LinePragma = linePragma; } - /// - /// Initializes a new instance of the CodeDomSerializerException class. - /// public CodeDomSerializerException(string message, IDesignerSerializationManager manager) : base(message) { - FillLinePragmaFromContext(manager); + if (manager == null) + { + throw new ArgumentNullException(nameof(manager)); + } } - /// - /// Initializes a new instance of the CodeDomSerializerException class. - /// - public CodeDomSerializerException(Exception ex, IDesignerSerializationManager manager) : base(ex.Message, ex) + public CodeDomSerializerException(Exception ex, IDesignerSerializationManager manager) : base(ex?.Message, ex) { - FillLinePragmaFromContext(manager); + if (manager == null) + { + throw new ArgumentNullException(nameof(manager)); + } } protected CodeDomSerializerException(SerializationInfo info, StreamingContext context) : base(info, context) { - _linePragma = (CodeLinePragma)info.GetValue("linePragma", typeof(CodeLinePragma)); + LinePragma = (CodeLinePragma)info.GetValue("linePragma", typeof(CodeLinePragma)); } /// /// Gets the line pragma object that is related to this error. /// - public CodeLinePragma LinePragma - { - get => _linePragma; - } - - /// - /// Sniffs around in the context looking for a code statement. if it finds one, it will add the statement's line # information to the exception. - /// - private void FillLinePragmaFromContext(IDesignerSerializationManager manager) - { - if (manager == null) - throw new ArgumentNullException(nameof(manager)); - } + public CodeLinePragma LinePragma { get; } public override void GetObjectData(SerializationInfo info, StreamingContext context) { @@ -78,7 +57,8 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont { throw new ArgumentNullException(nameof(info)); } - info.AddValue("linePragma", _linePragma); + + info.AddValue("linePragma", LinePragma); base.GetObjectData(info, context); } } diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/ExpressionContext.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/ExpressionContext.cs index 37118a94e1d..051de753676 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/ExpressionContext.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/ExpressionContext.cs @@ -16,27 +16,14 @@ namespace System.ComponentModel.Design.Serialization /// public sealed class ExpressionContext { - private readonly CodeExpression _expression; - private readonly Type _expressionType; - private readonly object _owner; - private readonly object _presetValue; - - /// - /// Creates a new expression context. - /// public ExpressionContext(CodeExpression expression, Type expressionType, object owner, object presetValue) { - // To make this public, we cannot have random special cases for what the args mean. - Debug.Assert(expression != null && expressionType != null && owner != null, "Obsolete use of expression context."); - _expression = expression ?? throw new ArgumentNullException(nameof(expression)); - _expressionType = expressionType ?? throw new ArgumentNullException(nameof(expressionType)); - _owner = owner ?? throw new ArgumentNullException(nameof(owner)); - _presetValue = presetValue; + Expression = expression ?? throw new ArgumentNullException(nameof(expression)); + ExpressionType = expressionType ?? throw new ArgumentNullException(nameof(expressionType)); + Owner = owner ?? throw new ArgumentNullException(nameof(owner)); + PresetValue = presetValue; } - /// - /// Creates a new expression context. - /// public ExpressionContext(CodeExpression expression, Type expressionType, object owner) : this(expression, expressionType, owner, null) { } @@ -44,38 +31,42 @@ public ExpressionContext(CodeExpression expression, Type expressionType, object /// /// The expression this context represents. /// - public CodeExpression Expression - { - get => _expression; - } + public CodeExpression Expression { get; } /// - /// The type of the expression. This can be used to determine if a cast is needed when assigning to the expression. + /// The type of the expression. This can be used to determine if a + /// cast is needed when assigning to the expression. /// - public Type ExpressionType - { - get => _expressionType; - } + public Type ExpressionType { get; } /// - /// The object owning this expression. For example, if the expression was a property reference to button1's Text property, Owner would return button1. + /// The object owning this expression. For example, if the expression + /// was a property reference to button1's Text property, Owner would + /// return button1. /// - public object Owner - { - get => _owner; - } + public object Owner { get; } /// - /// Contains the preset value of an expression, should one exist. For example, if the expression is a property reference expression referring to the Controls property of a button, PresetValue will contain the instance of Controls property because the property is read-only and preset by the object to contain a value. On the other hand, a property such as Text or Visible does not have a preset value and therefore the PresetValue property will return null. Serializers can use this information to guide serialization. For example, take the following two snippts of code: + /// Contains the preset value of an expression, should one exist. + /// For example, if the expression is a property reference expression + /// referring to the Controls property of a button, PresetValue will + /// contain the instance of Controls property because the property is + /// read-only and preset by the object to contain a value. On the other + /// hand, a property such as Text or Visible does not have a preset + /// value and therefore the PresetValue property will return null. + /// Serializers can use this information to guide serialization. + /// For example, take the following two snippts of code: /// Padding p = new Padding(); /// p.Left = 5; /// button1.Padding = p; /// button1.Padding.Left = 5; - /// The serializer of the Padding class needs to know if it should generate the first or second form. The first form would be generated by default. The second form will only be generated if there is an ExpressionContext on the stack that contains a PresetValue equal to the value of the Padding object currently being serialized. + /// The serializer of the Padding class needs to know if it should + /// generate the first or second form. The first form would be + /// generated by default. The second form will only be generated + /// if there is an ExpressionContext on the stack that contains a + /// PresetValue equal to the value of the Padding object currently + /// being serialized. /// - public object PresetValue - { - get => _presetValue; - } + public object PresetValue { get; } } } diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/RootContext.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/RootContext.cs index b5bae9ae838..123c7f8096c 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/RootContext.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/RootContext.cs @@ -3,39 +3,35 @@ // See the LICENSE file in the project root for more information. using System.CodeDom; + namespace System.ComponentModel.Design.Serialization { /// - /// The root context is added by a type code dom serailizier to provide a definiton of the "root" object. + /// The root context is added by a type code dom serailizier to provide a + /// definiton of the "root" object. /// public sealed class RootContext { - private readonly CodeExpression _expression; - private readonly object _value; - /// - /// This object can be placed on the context stack to represent the object that is the root of the serialization hierarchy. In addition to this instance, the RootContext also contains an expression that can be used to reference the RootContext. + /// This object can be placed on the context stack to represent the + /// object that is the root of the serialization hierarchy. In addition + /// to this instance, the RootContext also contains an expression that + /// can be used to reference the RootContext. /// public RootContext(CodeExpression expression, object value) { - _expression = expression ?? throw new ArgumentNullException(nameof(expression)); - _value = value ?? throw new ArgumentNullException(nameof(value)); + Expression = expression ?? throw new ArgumentNullException(nameof(expression)); + Value = value ?? throw new ArgumentNullException(nameof(value)); } /// - /// The expression representing the root object in the object graph. + /// The expression representing the root object in the object graph. /// - public CodeExpression Expression - { - get => _expression; - } + public CodeExpression Expression { get; } /// - /// The root object of the object graph. + /// The root object of the object graph. /// - public object Value - { - get => _value; - } + public object Value { get; } } } diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/SerializeAbsoluteContext.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/SerializeAbsoluteContext.cs index a9d99a9ba96..05ad465d3db 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/SerializeAbsoluteContext.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/SerializeAbsoluteContext.cs @@ -5,40 +5,47 @@ namespace System.ComponentModel.Design.Serialization { /// - /// The ComponentSerializationService supports "absolute" serialization, where instead of just serializing values that differ from an object's default values, all values are serialized in such a way as to be able to reset values to their defaults for objects that may have already been initialized. When a component serialization service wishes to indicate this to CodeDomSerializer objects, it will place a SerializeAbsoluteContext on the context stack. The member in this context may be null, to indicate that all members are serialized, or a member indicating that only a specific member is being serialized at this time. + /// The ComponentSerializationService supports "absolute" serialization, + /// where instead of just serializing values that differ from an object's + // default values, all values are serialized in such a way as to be able + /// to reset values to their defaults for objects that may have already + /// been initialized. When a component serialization service wishes to + /// indicate this to CodeDomSerializer objects, it will place a + /// SerializeAbsoluteContext on the context stack. The member in this + /// context may be null, to indicate that all members are serialized, or + /// a member indicating that only a specific member is being serialized at + /// this time. /// public sealed class SerializeAbsoluteContext { - private readonly MemberDescriptor _member; /// - /// Creeates a new SerializeAbsoluteContext. Member can be null or omitted to indicate this context should be used for all members. + /// Creeates a new SerializeAbsoluteContext. Member can be null or + /// omitted to indicate this context should be used for all members. /// public SerializeAbsoluteContext() { } /// - /// Creeates a new SerializeAbsoluteContext. Member can be null or omitted to indicate this context should be used for all members. + /// Creeates a new SerializeAbsoluteContext. Member can be null or + /// omitted to indicate this context should be used for all members. /// public SerializeAbsoluteContext(MemberDescriptor member) { - _member = member; + Member = member; } /// /// This property returns the member this context is bound to. It may be null to indicate the context is bound to all members of an object. /// - public MemberDescriptor Member - { - get => _member; - } + public MemberDescriptor Member { get; } /// /// Returns true if the given member should be serialized in this context. /// public bool ShouldSerialize(MemberDescriptor member) { - return (_member == null || _member == member); + return Member == null || Member == member; } } } diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/StatementContext.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/StatementContext.cs index cfdd12703a6..659ceb462c7 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/StatementContext.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/Serialization/StatementContext.cs @@ -9,7 +9,16 @@ namespace System.ComponentModel.Design.Serialization { /// - /// This object can be placed on the context stack to provide a place for statements to be serialized into. Normally, statements are serialized into whatever statement collection that is on the context stack. You can modify this behavior by creating a statement context and calling Populate with a collection of objects whose statements you would like stored in the statement table. As each object is serialized in SerializeToExpression it will have its contents placed in the statement table. saved in a table within the context. If you push this object on the stack it is your responsibility to integrate the statements added to it into your own collection of statements. + /// This object can be placed on the context stack to provide a place for + /// statements to be serialized into. Normally, statements are serialized + /// into whatever statement collection that is on the context stack. You + /// can modify this behavior by creating a statement context and calling + /// Populate with a collection of objects whose statements you would like + /// stored in the statement table. As each object is serialized in + /// SerializeToExpression it will have its contents placed in the statement + /// table. saved in a table within the context. If you push this object on + /// the stack it is your responsibility to integrate the statements added + /// to it into your own collection of statements. /// public sealed class StatementContext { @@ -20,15 +29,7 @@ public sealed class StatementContext /// public ObjectStatementCollection StatementCollection { - get - { - if (_statements == null) - { - _statements = new ObjectStatementCollection(); - } - - return _statements; - } + get => _statements ?? (_statements = new ObjectStatementCollection()); } } diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionMethodItem.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionMethodItem.cs index bfcc3c1f36a..4a541b92421 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionMethodItem.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionMethodItem.cs @@ -8,8 +8,6 @@ namespace System.ComponentModel.Design { public class DesignerActionMethodItem : DesignerActionItem { - private readonly string _memberName; - private readonly bool _includeAsDesignerVerb; private readonly DesignerActionList _actionList; private MethodInfo _methodInfo; @@ -17,8 +15,8 @@ public DesignerActionMethodItem(DesignerActionList actionList, string memberName : base(displayName, category, description) { _actionList = actionList; - _memberName = memberName; - _includeAsDesignerVerb = includeAsDesignerVerb; + MemberName = memberName; + IncludeAsDesignerVerb = includeAsDesignerVerb; } public DesignerActionMethodItem(DesignerActionList actionList, string memberName, string displayName) : this(actionList, memberName, displayName, null, null, false) @@ -41,45 +39,25 @@ public DesignerActionMethodItem(DesignerActionList actionList, string memberName { } - internal DesignerActionMethodItem() - { - } - - public virtual string MemberName - { - get => _memberName; - } + public virtual string MemberName { get; } public IComponent RelatedComponent { get; set; } - public virtual bool IncludeAsDesignerVerb - { - get => _includeAsDesignerVerb; - } - - // this is only use for verbs so that a designer action method item can be converted to a verb. - // Verbs use an EventHandler to call their invoke so we need a way to translate the EventHandler Invoke into ou own Invoke - internal void Invoke(object sender, EventArgs args) - { - Invoke(); - } + public virtual bool IncludeAsDesignerVerb { get; } public virtual void Invoke() { if (_methodInfo == null) { - // we look public AND private or protected methods - _methodInfo = _actionList.GetType().GetMethod(_memberName, BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + _methodInfo = _actionList?.GetType()?.GetMethod(MemberName, BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); } - if (_methodInfo != null) - { - _methodInfo.Invoke(_actionList, null); - } - else + if (_methodInfo == null) { throw new InvalidOperationException(string.Format(SR.DesignerActionPanel_CouldNotFindMethod, MemberName)); } + + _methodInfo.Invoke(_actionList, null); } } } diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionPropertyItem.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionPropertyItem.cs index 706ab1a6dc1..c2efd9b22f2 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionPropertyItem.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionPropertyItem.cs @@ -6,12 +6,9 @@ namespace System.ComponentModel.Design { public sealed class DesignerActionPropertyItem : DesignerActionItem { - private readonly string _memberName; - private IComponent _relatedComponent; - public DesignerActionPropertyItem(string memberName, string displayName, string category, string description) : base(displayName, category, description) { - _memberName = memberName; + MemberName = memberName; } public DesignerActionPropertyItem(string memberName, string displayName) : this(memberName, displayName, null, null) @@ -22,16 +19,8 @@ public DesignerActionPropertyItem(string memberName, string displayName, string { } - public string MemberName - { - get => _memberName; - } - - public IComponent RelatedComponent - { - get => _relatedComponent; - set => _relatedComponent = value; - } + public string MemberName { get; } + public IComponent RelatedComponent { get; set; } } } diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionVerbItem.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionVerbItem.cs index d771812b3e0..9ae7300020c 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionVerbItem.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionVerbItem.cs @@ -8,32 +8,14 @@ internal class DesignerActionVerbItem : DesignerActionMethodItem { private readonly DesignerVerb _targetVerb; - public DesignerActionVerbItem(DesignerVerb verb) + public DesignerActionVerbItem(DesignerVerb verb) : base(null, null, null) { - _targetVerb = verb ?? throw new ArgumentNullException(); + _targetVerb = verb ?? throw new ArgumentNullException(nameof(verb)); } - public override string Category - { - get => "Verbs"; - } - - public override string Description { get; } - - public override string DisplayName - { - get => _targetVerb.Text; - } + public override string Category => "Verbs"; - public override string MemberName - { - get => null; - } - - public override bool IncludeAsDesignerVerb - { - get => false; - } + public override string DisplayName => _targetVerb.Text; public override void Invoke() => _targetVerb.Invoke(); } diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionVerbList.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionVerbList.cs index 6ef7876dfe0..2837b836b23 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionVerbList.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionVerbList.cs @@ -13,10 +13,7 @@ public DesignerActionVerbList(DesignerVerb[] verbs) : base(null) _verbs = verbs; } - public override bool AutoShow - { - get => false; - } + public override bool AutoShow => false; public override DesignerActionItemCollection GetSortedActionItems() { diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/UndoEngine.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/UndoEngine.cs index 120d2c1146a..37249235932 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/UndoEngine.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/UndoEngine.cs @@ -578,8 +578,6 @@ public ReferencingComponent(IComponent component, MemberDescriptor member) /// protected class UndoUnit { - private readonly string _name; // the name of the undo unit - private readonly UndoEngine _engine; // the undo engine we're tied to private ArrayList _events; // the list of events we've captured private ArrayList _changeEvents; // the list of change events we're currently capturing. Only valid until Commit is called. private ArrayList _removeEvents; // the list of remove events we're currently capturing. Only valid until a matching Removed is encountered. @@ -588,22 +586,19 @@ protected class UndoUnit private bool _reverse; // if true, we walk the events list from the bottom up private readonly Hashtable _lastSelection; // the selection as it was before we gathered undo info - /// - /// Creates a new UndoUnit. - /// public UndoUnit(UndoEngine engine, string name) { if (name == null) { - Debug.Fail("Null name passed to new undo unit"); name = string.Empty; } + UndoEngine.Trace("Creating undo unit '{0}'", name); - _name = name; - _engine = engine ?? throw new ArgumentNullException(nameof(engine)); + Name = name; + UndoEngine = engine ?? throw new ArgumentNullException(nameof(engine)); _reverse = true; - if (_engine.GetService(typeof(ISelectionService)) is ISelectionService ss) + if (UndoEngine.GetService(typeof(ISelectionService)) is ISelectionService ss) { ICollection selection = ss.GetSelectedComponents(); Hashtable selectedNames = new Hashtable(); @@ -618,29 +613,14 @@ public UndoUnit(UndoEngine engine, string name) } } - /// - /// The name of the unit. - /// - public string Name - { - get => _name; - } + public string Name { get; } /// /// This returns true if the undo unit has nothing in it to undo. The unit will be discarded. /// - public virtual bool IsEmpty - { - get => _events == null || _events.Count == 0; - } + public virtual bool IsEmpty => _events == null || _events.Count == 0; - /// - /// The undo engine that was passed into the constructor. - /// - protected UndoEngine UndoEngine - { - get => _engine; - } + protected UndoEngine UndoEngine { get; } /// /// Adds the given event to our event list. @@ -651,6 +631,7 @@ private void AddEvent(UndoEvent e) { _events = new ArrayList(); } + _events.Add(e); } @@ -663,7 +644,7 @@ public virtual void Close() { foreach (ChangeUndoEvent e in _changeEvents) { - e.Commit(_engine); + e.Commit(UndoEngine); } } @@ -671,7 +652,7 @@ public virtual void Close() { foreach (AddRemoveUndoEvent e in _removeEvents) { - e.Commit(_engine); + e.Commit(UndoEngine); } } @@ -693,7 +674,7 @@ public virtual void ComponentAdded(ComponentEventArgs e) // do nothing } else - AddEvent(new AddRemoveUndoEvent(_engine, e.Component, true)); + AddEvent(new AddRemoveUndoEvent(UndoEngine, e.Component, true)); if (_ignoreAddingList != null) { @@ -795,7 +776,7 @@ public virtual void ComponentChanging(ComponentChangingEventArgs e) } // The site check here is done because the data team is calling us for components that are not yet sited. We end up writing them out as Guid-named locals. That's fine, except that we cannot capture after state for these types of things so we assert. - if (_engine != null && _engine.GetName(e.Component, false) != null) + if (UndoEngine.GetName(e.Component, false) != null) { // The caller provided us with a component. This is the common case. We will add a new change event provided there is not already one open for this component. bool hasChange = false; @@ -814,7 +795,7 @@ public virtual void ComponentChanging(ComponentChangingEventArgs e) (e.Member != null && e.Member.Attributes != null && e.Member.Attributes.Contains(DesignerSerializationVisibilityAttribute.Content))) { #if DEBUG - string name = _engine.GetName(e.Component, false); + string name = UndoEngine.GetName(e.Component, false); string memberName = "(none)"; if (e.Member != null && e.Member.Name != null) { memberName = e.Member.Name; @@ -836,7 +817,7 @@ public virtual void ComponentChanging(ComponentChangingEventArgs e) if (e.Component is IComponent comp && comp.Site != null) { - changeEvent = new ChangeUndoEvent(_engine, e, serializeBeforeState); + changeEvent = new ChangeUndoEvent(UndoEngine, e, serializeBeforeState); } else if (e.Component != null) { @@ -846,7 +827,7 @@ public virtual void ComponentChanging(ComponentChangingEventArgs e) if (owningComp != null) { - changeEvent = new ChangeUndoEvent(_engine, new ComponentChangingEventArgs(owningComp, null), serializeBeforeState); + changeEvent = new ChangeUndoEvent(UndoEngine, new ComponentChangingEventArgs(owningComp, null), serializeBeforeState); } } } @@ -881,7 +862,7 @@ public virtual void ComponentRemoved(ComponentEventArgs e) if (_events[idx] is AddRemoveUndoEvent evt && evt.OpenComponent == e.Component) { - evt.Commit(_engine); + evt.Commit(UndoEngine); // We should only reorder events if there are change events coming between OnRemoving and OnRemoved. // If there are other events (such as AddRemoving), the serialization done in OnComponentRemoving might refer to components that aren't available. if (idx != _events.Count - 1 && changeEvt != null) @@ -927,7 +908,7 @@ public virtual void ComponentRemoving(ComponentEventArgs e) } try { - AddRemoveUndoEvent evt = new AddRemoveUndoEvent(_engine, e.Component, false); + AddRemoveUndoEvent evt = new AddRemoveUndoEvent(UndoEngine, e.Component, false); AddEvent(evt); _removeEvents.Add(evt); } @@ -947,7 +928,7 @@ public virtual void ComponentRename(ComponentRenameEventArgs e) /// protected object GetService(Type serviceType) { - return _engine.GetService(serviceType); + return UndoEngine.GetService(serviceType); } /// @@ -964,17 +945,17 @@ public override string ToString() public void Undo() { UndoEngine.Trace("Performing undo '{0}'", Name); - UndoUnit savedUnit = _engine._executingUnit; - _engine._executingUnit = this; + UndoUnit savedUnit = UndoEngine._executingUnit; + UndoEngine._executingUnit = this; DesignerTransaction transaction = null; try { if (savedUnit == null) { - _engine.OnUndoing(EventArgs.Empty); + UndoEngine.OnUndoing(EventArgs.Empty); } // create a transaction here so things that do work on componentchanged can ignore that while the transaction is opened...big perf win. - transaction = _engine._host.CreateTransaction(); + transaction = UndoEngine._host.CreateTransaction(); UndoCore(); } catch (CheckoutException) @@ -990,10 +971,10 @@ public void Undo() transaction.Commit(); } - _engine._executingUnit = savedUnit; + UndoEngine._executingUnit = savedUnit; if (savedUnit == null) { - _engine.OnUndone(EventArgs.Empty); + UndoEngine.OnUndone(EventArgs.Empty); } } } @@ -1028,12 +1009,12 @@ protected virtual void UndoCore() for (int beforeIdx = idx; beforeIdx >= groupEndIdx; beforeIdx--) { - ((UndoEvent)_events[beforeIdx]).BeforeUndo(_engine); + ((UndoEvent)_events[beforeIdx]).BeforeUndo(UndoEngine); } for (int undoIdx = idx; undoIdx >= groupEndIdx; undoIdx--) { - ((UndoEvent)_events[undoIdx]).Undo(_engine); + ((UndoEvent)_events[undoIdx]).Undo(UndoEngine); } Debug.Assert(idx >= groupEndIdx, "We're going backwards"); @@ -1043,7 +1024,7 @@ protected virtual void UndoCore() // Now, if we have a selection, apply it. if (_lastSelection != null) { - if (_engine.GetService(typeof(ISelectionService)) is ISelectionService ss) + if (UndoEngine.GetService(typeof(ISelectionService)) is ISelectionService ss) { string[] names = new string[_lastSelection.Keys.Count]; _lastSelection.Keys.CopyTo(names, 0); @@ -1084,12 +1065,12 @@ protected virtual void UndoCore() for (int beforeIdx = idx; beforeIdx <= groupEndIdx; beforeIdx++) { - ((UndoEvent)_events[beforeIdx]).BeforeUndo(_engine); + ((UndoEvent)_events[beforeIdx]).BeforeUndo(UndoEngine); } for (int undoIdx = idx; undoIdx <= groupEndIdx; undoIdx++) { - ((UndoEvent)_events[undoIdx]).Undo(_engine); + ((UndoEvent)_events[undoIdx]).Undo(UndoEngine); } Debug.Assert(idx <= groupEndIdx, "We're going backwards"); diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionItemCollectionTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionItemCollectionTests.cs deleted file mode 100644 index ea258c17671..00000000000 --- a/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionItemCollectionTests.cs +++ /dev/null @@ -1,68 +0,0 @@ -// 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.ComponentModel.Design; -using Xunit; - -namespace System.Windows.Forms.Design.Tests -{ - public class DesignerActionItemCollectionTests - { - [Fact] - public void DesignerActionItemCollection_Constructor() - { - DesignerActionItemCollection underTest = new DesignerActionItemCollection(); - Assert.NotNull(underTest); - Assert.Empty(underTest); - } - - [Fact] - public void DesignerActionItemCollection_Add_Contains_IndexOf() - { - DesignerActionItemCollection underTest = new DesignerActionItemCollection(); - - DesignerActionItem item1 = new DesignerActionItemTest("name", "category", "description"); - underTest.Add(item1); - Assert.True(underTest.Contains(item1)); - Assert.Equal(0, underTest.IndexOf(item1)); - - DesignerActionItem item2 = new DesignerActionItemTest("name1", "category1", "description1"); - underTest.Add(item2); - Assert.True(underTest.Contains(item2)); - Assert.Equal(1, underTest.IndexOf(item2)); - } - - [Fact] - public void DesignerActionItemCollection_Insert_Remove_Count() - { - DesignerActionItemCollection underTest = new DesignerActionItemCollection(); - DesignerActionItem item1 = new DesignerActionItemTest("name", "category", "description"); - DesignerActionItem item2 = new DesignerActionItemTest("name1", "category1", "description1"); - DesignerActionItem item3 = new DesignerActionItemTest("name2", "category2", "description2"); - DesignerActionItem item4 = new DesignerActionItemTest("name3", "category3", "description3"); - - underTest.Add(item1); - underTest.Add(item2); - - underTest.Add(item3); - Assert.Equal(2, underTest.IndexOf(item3)); - - underTest.Insert(2, item4); - Assert.Equal(3, underTest.IndexOf(item3)); - Assert.Equal(2, underTest.IndexOf(item4)); - - underTest.Remove(item4); - Assert.False(underTest.Contains(item4)); - Assert.Equal(2, underTest.IndexOf(item3)); - Assert.Equal(3, underTest.Count); - } - - private class DesignerActionItemTest : DesignerActionItem - { - public DesignerActionItemTest(string displayName, string category, string description) : base(displayName, category, description) - { - } - } - } -} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionListCollectionTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionListCollectionTests.cs deleted file mode 100644 index e871d4719e4..00000000000 --- a/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionListCollectionTests.cs +++ /dev/null @@ -1,83 +0,0 @@ -// 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.ComponentModel.Design; -using WinForms.Common.Tests; -using Xunit; - -namespace System.Windows.Forms.Design.Tests -{ - public class DesignerActionListCollectionTests - { - [Fact] - public void DesignerActionListCollection_Constructor() - { - DesignerActionListCollection underTest = new DesignerActionListCollection(); - Assert.NotNull(underTest); - } - - [Fact] - public void DesignerActionListCollection_Constructor_DesignerActionList() - { - DesignerActionListCollection underTest = CreateNewCollectionOfItems(); - Assert.NotNull(underTest); - } - - [Fact] - public void DesignerActionItemCollection_Add_AddRange() - { - DesignerActionListCollection underTest = CreateNewCollectionOfItems(5); - Assert.Equal(5, underTest.Count); - - Button button = new Button(); - DesignerActionList list = new DesignerActionList(button); - underTest.Add(list); - Assert.Equal(6, underTest.Count); - - DesignerActionListCollection other = CreateNewCollectionOfItems(3); - underTest.AddRange(other); - Assert.Equal(9, underTest.Count); - } - - - [Fact] - public void DesignerActionItemCollection_Insert_Contains_IndexOf() - { - DesignerActionListCollection underTest = CreateNewCollectionOfItems(5); - - Button button = new Button(); - DesignerActionList list = new DesignerActionList(button); - underTest.Insert(3, list); - Assert.True(underTest.Contains(list)); - Assert.Equal(3, underTest.IndexOf(list)); - } - - [Fact] - public void DesignerActionItemCollection_Remove() - { - DesignerActionListCollection underTest = CreateNewCollectionOfItems(5); - - Button button = new Button(); - DesignerActionList list = new DesignerActionList(button); - underTest.Insert(3, list); - underTest.Remove(list); - Assert.False(underTest.Contains(list)); - Assert.Equal(5, underTest.Count); - } - - private DesignerActionListCollection CreateNewCollectionOfItems(int numberOfItems = 1) - { - Button button = new Button(); - DesignerActionList[] list = new DesignerActionList[] { new DesignerActionList(button) }; - DesignerActionListCollection underTest = new DesignerActionListCollection(list); - - for (int i = 1; i < numberOfItems; i++) - { - underTest.Add(new DesignerActionList(button)); - } - - return underTest; - } - } -} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionMethodItemTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionMethodItemTests.cs deleted file mode 100644 index eee7ac462eb..00000000000 --- a/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionMethodItemTests.cs +++ /dev/null @@ -1,94 +0,0 @@ -// 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.ComponentModel.Design; -using WinForms.Common.Tests; -using Xunit; - -namespace System.Windows.Forms.Design.Tests -{ - public class DesignerActionMethodItemTests - { - [Theory] - [InlineData("memberName", "displayName", "category", "description", true)] - [InlineData("memberName", "displayName", "category", "", true)] - [InlineData("memberName", "displayName", "", "", false)] - [InlineData("memberName", "", "", "", false)] - public void DesignerActionMethodItem_Constructor(string memberName, string displayName, string category, string description, bool includeAsDesignerVerb) - { - DesignerActionMethodItem underTest = CreateDesignerActionMethodItem(memberName, displayName, category, description, includeAsDesignerVerb); - Assert.NotNull(underTest); - Assert.Equal(memberName, underTest.MemberName); - Assert.Equal(displayName, underTest.DisplayName); - Assert.Equal(category, underTest.Category); - Assert.Equal(description, underTest.Description); - Assert.Equal(includeAsDesignerVerb, underTest.IncludeAsDesignerVerb); - } - - [Fact] - public void DesignerActionMethodItem_Constructor2() - { - DesignerActionMethodItem underTest = CreateDesignerActionMethodItem("memberName", "displayName", "category"); - Assert.NotNull(underTest); - Assert.Equal("memberName", underTest.MemberName); - Assert.Equal("displayName", underTest.DisplayName); - Assert.Equal("category", underTest.Category); - Assert.Null(underTest.Description); - } - - [Fact] - public void DesignerActionMethodItem_Constructor3() - { - DesignerActionMethodItem underTest = CreateDesignerActionMethodItem("memberName", "displayName"); - Assert.NotNull(underTest); - Assert.Equal("memberName", underTest.MemberName); - Assert.Equal("displayName", underTest.DisplayName); - Assert.Null(underTest.Category); - Assert.Null(underTest.Description); - } - - [Fact] - public void DesignerActionMethodItem_RelatedComponent_getter_setter() - { - DesignerActionMethodItem underTest = CreateDesignerActionMethodItem("memberName", "displayName"); - - Button button = new Button(); - underTest.RelatedComponent = button; - Assert.Equal(button, underTest.RelatedComponent); - } - - private DesignerActionMethodItem CreateDesignerActionMethodItem(string memberName, string displayName, string category, string description, bool includeAsDesignerVerb) - { - Button button = new Button(); - DesignerActionList actionList = new DesignerActionList(button); - - if (category == null && description == null) - { - return new DesignerActionMethodItem(actionList, memberName, displayName, includeAsDesignerVerb); - } - else if (description == null) - { - return new DesignerActionMethodItem(actionList, memberName, displayName, category, includeAsDesignerVerb); - } - else - { - return new DesignerActionMethodItem(actionList, memberName, displayName, category, description, includeAsDesignerVerb); - } - } - - private DesignerActionMethodItem CreateDesignerActionMethodItem(string memberName, string displayName, string category, string description) - { - return CreateDesignerActionMethodItem(memberName, displayName, category, description, false); - } - - private DesignerActionMethodItem CreateDesignerActionMethodItem(string memberName, string displayName, string category) - { - return CreateDesignerActionMethodItem(memberName, displayName, category, null, false); - } - private DesignerActionMethodItem CreateDesignerActionMethodItem(string memberName, string displayName) - { - return CreateDesignerActionMethodItem(memberName, displayName, null, null, false); - } - } -} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionPropertyItemTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionPropertyItemTests.cs deleted file mode 100644 index 4ce807673e4..00000000000 --- a/src/System.Windows.Forms.Design/tests/UnitTests/DesignerActionPropertyItemTests.cs +++ /dev/null @@ -1,61 +0,0 @@ -// 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.ComponentModel.Design; -using WinForms.Common.Tests; -using Xunit; - -namespace System.Windows.Forms.Design.Tests -{ - public class DesignerActionPropertyItemTests - { - [Theory] - [InlineData("memberName", "displayName", "category", "description")] - [InlineData("memberName", "displayName", "category", "")] - [InlineData("memberName", "displayName", "", "")] - [InlineData("memberName", "", "", "")] - public void DesignerActionPropertyItem_Constructor(string memberName, string displayName, string category, string description) - { - DesignerActionPropertyItem underTest = new DesignerActionPropertyItem(memberName, displayName, category, description); - Assert.NotNull(underTest); - Assert.Equal(memberName, underTest.MemberName); - Assert.Equal(displayName, underTest.DisplayName); - Assert.Equal(category, underTest.Category); - Assert.Equal(description, underTest.Description); - } - - [Fact] - public void DesignerActionPropertyItem_Constructor2() - { - DesignerActionPropertyItem underTest = new DesignerActionPropertyItem("memberName", "displayName", "category"); - Assert.NotNull(underTest); - Assert.Equal("memberName", underTest.MemberName); - Assert.Equal("displayName", underTest.DisplayName); - Assert.Equal("category", underTest.Category); - Assert.Null(underTest.Description); - } - - [Fact] - public void DesignerActionPropertyItem_Constructor3() - { - DesignerActionPropertyItem underTest = new DesignerActionPropertyItem("memberName", "displayName"); - Assert.NotNull(underTest); - Assert.Equal("memberName", underTest.MemberName); - Assert.Equal("displayName", underTest.DisplayName); - Assert.Null(underTest.Category); - Assert.Null(underTest.Description); - } - - [Fact] - public void DesignerActionPropertyItem_RelatedComponent_getter_setter() - { - DesignerActionPropertyItem underTest = new DesignerActionPropertyItem("name", "displayname"); - - Button button = new Button(); - underTest.RelatedComponent = button; - Assert.Equal(button, underTest.RelatedComponent); - - } - } -} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/Serialization/CodeDomSerializerExceptionTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/Serialization/CodeDomSerializerExceptionTests.cs deleted file mode 100644 index eb09161140c..00000000000 --- a/src/System.Windows.Forms.Design/tests/UnitTests/Serialization/CodeDomSerializerExceptionTests.cs +++ /dev/null @@ -1,52 +0,0 @@ -// 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.CodeDom; -using System.ComponentModel.Design.Serialization; -using Moq; -using Xunit; - -namespace System.Windows.Forms.Design.Serialization.Tests -{ - public class CodeDomSerializerExceptionTests - { - Mock _mockDesignerSerializationManager; - - [Fact] - public void CodeDomSerializerException_Constructor_Message_Pragma() - { - var pragma = new CodeLinePragma(); - var underTest = new CodeDomSerializerException("message", pragma); - Assert.NotNull(underTest); - Assert.Equal(pragma, underTest.LinePragma); - } - - [Fact] - public void CodeDomSerializerException_Constructor_Exception_Pragma() - { - var ex = new Exception(); - var pragma = new CodeLinePragma(); - var underTest = new CodeDomSerializerException(ex, pragma); - Assert.NotNull(underTest); - Assert.Equal(pragma, underTest.LinePragma); - } - - [Fact] - public void CodeDomSerializerException_Constructor_SerializationManager() - { - _mockDesignerSerializationManager = new Mock(MockBehavior.Strict); - var underTest = new CodeDomSerializerException("message", _mockDesignerSerializationManager.Object); - Assert.NotNull(underTest); - } - - [Fact] - public void CodeDomSerializerException_Constructor_Exception_SerializationManager() - { - var ex = new Exception(); - _mockDesignerSerializationManager = new Mock(MockBehavior.Strict); - var underTest = new CodeDomSerializerException(ex, _mockDesignerSerializationManager.Object); - Assert.NotNull(underTest); - } - } -} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/Serialization/ExpressionContextTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/Serialization/ExpressionContextTests.cs deleted file mode 100644 index d03fa06e376..00000000000 --- a/src/System.Windows.Forms.Design/tests/UnitTests/Serialization/ExpressionContextTests.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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.CodeDom; -using System.ComponentModel.Design.Serialization; -using Xunit; - -namespace System.Windows.Forms.Design.Serialization.Tests -{ - public class ExpressionContextTests - { - [Fact] - public void ExpressionContext_Constructor() - { - CodeExpression expression = new CodeExpression(); - Type type = expression.GetType(); - object owner = "owner"; - object presetValue = null; - - var underTest = new ExpressionContext(expression, type, owner, presetValue); - Assert.NotNull(underTest); - Assert.Equal(expression, underTest.Expression); - } - } -} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System.Windows.Forms.Design.Tests.csproj b/src/System.Windows.Forms.Design/tests/UnitTests/System.Windows.Forms.Design.Tests.csproj index 2db090483e3..58d353fd39d 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/System.Windows.Forms.Design.Tests.csproj +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System.Windows.Forms.Design.Tests.csproj @@ -14,6 +14,7 @@ + diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionHeaderItemTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionHeaderItemTests.cs new file mode 100644 index 00000000000..93c48db9808 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionHeaderItemTests.cs @@ -0,0 +1,48 @@ +// 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.Specialized; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerActionHeaderItemTests + { + [Theory] + [InlineData("displayName", "category", "displayName")] + [InlineData("displa(&a)yName", "cate(&a)gory", "displayName")] + [InlineData("", "", "")] + [InlineData(null, null, null)] + public void DesignerActionItem_Ctor_String_String(string displayName, string category, string expectedDisplayName) + { + var item = new DesignerActionHeaderItem(displayName, category); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(category, item.Category); + Assert.Null(item.Description); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + } + + [Theory] + [InlineData("displayName", "displayName")] + [InlineData("displa(&a)yName", "displayName")] + [InlineData("", "")] + [InlineData(null, null)] + public void DesignerActionItem_Ctor_String(string displayName, string expectedDisplayName) + { + var item = new DesignerActionHeaderItem(displayName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(displayName, item.Category); + Assert.Null(item.Description); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionItemCollectionTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionItemCollectionTests.cs new file mode 100644 index 00000000000..453e955f5b8 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionItemCollectionTests.cs @@ -0,0 +1,175 @@ +// 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.Linq; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerActionItemCollectionTests + { + [Fact] + public void DesignerActionItemCollection_Ctor_Default() + { + var collection = new DesignerActionItemCollection(); + Assert.Empty(collection); + } + + [Fact] + public void DesignerActionItemCollection_Add_DesignerActionItem_Success() + { + var collection = new DesignerActionItemCollection(); + + var value1 = new SubDesignerActionItem("displayName", "category", "description"); + collection.Add(value1); + Assert.Same(value1, Assert.Single(collection)); + Assert.Same(value1, collection[0]); + Assert.True(collection.Contains(value1)); + Assert.Equal(0, collection.IndexOf(value1)); + + var value2 = new SubDesignerActionItem("displayName", "category", "description"); + collection.Add(value2); + Assert.Equal(new object[] { value1, value2 }, collection.Cast()); + Assert.True(collection.Contains(value2)); + Assert.Equal(1, collection.IndexOf(value2)); + } + + [Fact] + public void DesignerActionItemCollection_Add_NullValue_ThrowsArgumentNullException() + { + var collection = new DesignerActionItemCollection(); + Assert.Throws("value", () => collection.Add(null)); + } + + [Fact] + public void DesignerActionItemCollection_Insert_DesignerActionItem_Success() + { + var collection = new DesignerActionItemCollection(); + + var value1 = new SubDesignerActionItem("displayName", "category", "description"); + collection.Insert(0, value1); + Assert.Same(value1, Assert.Single(collection)); + Assert.Same(value1, collection[0]); + Assert.True(collection.Contains(value1)); + Assert.Equal(0, collection.IndexOf(value1)); + + var value2 = new SubDesignerActionItem("displayName", "category", "description"); + collection.Insert(0, value2); + Assert.Equal(new object[] { value2, value1 }, collection.Cast()); + Assert.True(collection.Contains(value2)); + Assert.Equal(0, collection.IndexOf(value2)); + } + + [Fact] + public void DesignerActionItemCollection_Insert_NullValue_ThrowsArgumentNullException() + { + var collection = new DesignerActionItemCollection(); + Assert.Throws("value", () => collection.Insert(0, null)); + } + + [Fact] + public void DesignerActionItemCollection_Remove_Invoke_Success() + { + var collection = new DesignerActionItemCollection(); + var value = new SubDesignerActionItem("displayName", "category", "description"); + collection.Add(value); + Assert.Same(value, Assert.Single(collection)); + + collection.Remove(value); + Assert.Empty(collection); + Assert.False(collection.Contains(value)); + Assert.Equal(-1, collection.IndexOf(value)); + } + + [Fact] + public void DesignerActionItemCollection_Remove_NullValue_ThrowsArgumentNullException() + { + var collection = new DesignerActionItemCollection(); + Assert.Throws("value", () => collection.Remove(null)); + } + + [Fact] + public void DesignerActionItemCollection_Item_Set_GetReturnsExpected() + { + var collection = new DesignerActionItemCollection(); + var value1 = new SubDesignerActionItem("displayName", "category", "description"); + var value2 = new SubDesignerActionItem("displayName", "category", "description"); + collection.Add(value1); + Assert.Same(value1, Assert.Single(collection)); + + collection[0] = value2; + Assert.Same(value2, Assert.Single(collection)); + Assert.Same(value2, collection[0]); + Assert.False(collection.Contains(value1)); + Assert.Equal(-1, collection.IndexOf(value1)); + Assert.True(collection.Contains(value2)); + Assert.Equal(0, collection.IndexOf(value2)); + } + + [Fact] + public void DesignerActionItemCollection_Item_SetNull_ThrowsArgumentNullException() + { + var collection = new DesignerActionItemCollection(); + var value = new SubDesignerActionItem("displayName", "category", "description"); + collection.Add(value); + Assert.Throws("value", () => collection[0] = null); + } + + [Fact] + public void DesignerActionItemCollection_CopyTo_Invoke_Success() + { + var collection = new DesignerActionItemCollection(); + var value = new SubDesignerActionItem("displayName", "category", "description"); + collection.Add(value); + + var array = new DesignerActionItem[3]; + collection.CopyTo(array, 1); + Assert.Equal(new DesignerActionItem[] { null, value, null }, array); + } + + [Fact] + public void DesignerActionItemCollection_Contains_NoSuchValue_ReturnsFalse() + { + var collection = new DesignerActionItemCollection(); + var value = new SubDesignerActionItem("displayName", "category", "description"); + collection.Add(value); + + Assert.False(collection.Contains(new SubDesignerActionItem("displayName", "category", "description"))); + Assert.False(collection.Contains(null)); + } + + [Fact] + public void DesignerActionItemCollection_IndexOf_NoSuchValue_ReturnsNegativeOne() + { + var collection = new DesignerActionItemCollection(); + var value = new SubDesignerActionItem("displayName", "category", "description"); + collection.Add(value); + + Assert.Equal(-1, collection.IndexOf(new SubDesignerActionItem("displayName", "category", "description"))); + Assert.Equal(-1, collection.IndexOf(null)); + } + + [Fact] + public void DesignerActionItemCollection_Clear_Success() + { + var collection = new DesignerActionItemCollection(); + var value = new SubDesignerActionItem("displayName", "category", "description"); + collection.Add(value); + + collection.Clear(); + Assert.Empty(collection); + + // Clear again. + collection.Clear(); + Assert.Empty(collection); + } + + private class SubDesignerActionItem : DesignerActionItem + { + public SubDesignerActionItem(string displayName, string category, string description) : base(displayName, category, description) + { + } + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionItemTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionItemTests.cs new file mode 100644 index 00000000000..1d45ff3ac0b --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionItemTests.cs @@ -0,0 +1,70 @@ +// 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.Collections.Specialized; +using System.ComponentModel; +using WinForms.Common.Tests; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerActionItemTests + { + [Theory] + [InlineData("displayName", "category", "description", "displayName")] + [InlineData("displa(&a)yName", "cate(&a)gory", "descr(&a)iption", "displayName")] + [InlineData("", "", "", "")] + [InlineData(null, null, null, null)] + public void DesignerActionItem_Ctor_String_String_String(string displayName, string category, string description, string expectedDisplayName) + { + var item = new SubDesignerActionItem(displayName, category, description); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(category, item.Category); + Assert.Equal(description, item.Description); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + } + + [Theory] + [CommonMemberData(nameof(CommonTestHelper.GetBoolTheoryData))] + public void DesignerActionItem_AllowAssociate_Set_GetReturnsExpected(bool value) + { + var item = new SubDesignerActionItem("displayName", "category", "description") + { + AllowAssociate = value + }; + Assert.Equal(value, item.AllowAssociate); + + // Set same. + item.AllowAssociate = value; + Assert.Equal(value, item.AllowAssociate); + } + + [Theory] + [CommonMemberData(nameof(CommonTestHelper.GetBoolTheoryData))] + public void DesignerActionItem_ShowInSourceView_Set_GetReturnsExpected(bool value) + { + var item = new SubDesignerActionItem("displayName", "category", "description") + { + ShowInSourceView = value + }; + Assert.Equal(value, item.ShowInSourceView); + + // Set same. + item.ShowInSourceView = value; + Assert.Equal(value, item.ShowInSourceView); + } + + private class SubDesignerActionItem : DesignerActionItem + { + public SubDesignerActionItem(string displayName, string category, string description) : base(displayName, category, description) + { + } + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionListCollectionTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionListCollectionTests.cs new file mode 100644 index 00000000000..2a5faee57c8 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionListCollectionTests.cs @@ -0,0 +1,209 @@ +// 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.Linq; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerActionListCollectionTests + { + [Fact] + public void DesignerActionListCollection_Ctor_Default() + { + var collection = new DesignerActionListCollection(); + Assert.Empty(collection); + } + + public static IEnumerable Ctor_DesignerActionListArray_TestData() + { + yield return new object[] { new DesignerActionList[0] }; + yield return new object[] { new DesignerActionList[] { new DesignerActionList(null), null } }; + } + + [Theory] + [MemberData(nameof(Ctor_DesignerActionListArray_TestData))] + public void DesignerActionListCollection_Ctor_DesignerActionListArray(DesignerActionList[] value) + { + var collection = new DesignerActionListCollection(value); + Assert.Equal(value, collection.Cast()); + } + + [Fact] + public void DesignerActionListCollection_Ctor_NullValue_ThrowsArgumentNullException() + { + Assert.Throws("value", () => new DesignerActionListCollection(null)); + } + + [Fact] + public void DesignerActionListCollection_Add_DesignerActionList_Success() + { + var collection = new DesignerActionListCollection(); + + var value1 = new DesignerActionList(null); + collection.Add(value1); + Assert.Same(value1, Assert.Single(collection)); + Assert.Same(value1, collection[0]); + Assert.True(collection.Contains(value1)); + Assert.Equal(0, collection.IndexOf(value1)); + + var value2 = new DesignerActionList(null); + collection.Add(value2); + Assert.Equal(new object[] { value1, value2 }, collection.Cast()); + Assert.True(collection.Contains(value2)); + Assert.Equal(1, collection.IndexOf(value2)); + + collection.Add(null); + Assert.Equal(new object[] { value1, value2, null }, collection.Cast()); + Assert.True(collection.Contains(null)); + Assert.Equal(2, collection.IndexOf(null)); + } + + [Theory] + [MemberData(nameof(Ctor_DesignerActionListArray_TestData))] + public void DesignerActionListCollection_AddRange_DesignerActionListArray_Success(DesignerActionList[] value) + { + var collection = new DesignerActionListCollection(); + collection.AddRange(value); + Assert.Equal(value, collection.Cast()); + + // Add again. + collection.AddRange(value); + Assert.Equal(value.Concat(value), collection.Cast()); + } + + [Theory] + [MemberData(nameof(Ctor_DesignerActionListArray_TestData))] + public void DesignerActionListCollection_AddRange_DesignerActionListCollection_Success(DesignerActionList[] value) + { + var collection = new DesignerActionListCollection(); + collection.AddRange(new DesignerActionListCollection(value)); + Assert.Equal(value, collection.Cast()); + + // Add again. + collection.AddRange(new DesignerActionListCollection(value)); + Assert.Equal(value.Concat(value), collection.Cast()); + } + + [Fact] + public void DesignerActionListCollection_Insert_DesignerActionList_Success() + { + var collection = new DesignerActionListCollection(); + + var value1 = new DesignerActionList(null); + collection.Insert(0, value1); + Assert.Same(value1, Assert.Single(collection)); + Assert.Same(value1, collection[0]); + Assert.True(collection.Contains(value1)); + Assert.Equal(0, collection.IndexOf(value1)); + + var value2 = new DesignerActionList(null); + collection.Insert(0, value2); + Assert.Equal(new object[] { value2, value1 }, collection.Cast()); + Assert.True(collection.Contains(value2)); + Assert.Equal(0, collection.IndexOf(value2)); + + collection.Insert(1, null); + Assert.Equal(new object[] { value2, null, value1 }, collection.Cast()); + Assert.True(collection.Contains(null)); + Assert.Equal(1, collection.IndexOf(null)); + } + + [Fact] + public void DesignerActionListCollection_Remove_Invoke_Success() + { + var collection = new DesignerActionListCollection(); + var value = new DesignerActionList(null); + collection.Add(value); + Assert.Same(value, Assert.Single(collection)); + + collection.Remove(value); + Assert.Empty(collection); + Assert.False(collection.Contains(value)); + Assert.Equal(-1, collection.IndexOf(value)); + + collection.Add(null); + collection.Remove(null); + Assert.Empty(collection); + Assert.False(collection.Contains(null)); + Assert.Equal(-1, collection.IndexOf(null)); + } + + [Fact] + public void DesignerActionListCollection_Item_Set_GetReturnsExpected() + { + var collection = new DesignerActionListCollection(); + var value1 = new DesignerActionList(null); + var value2 = new DesignerActionList(null); + collection.Add(value1); + Assert.Same(value1, Assert.Single(collection)); + + collection[0] = value2; + Assert.Same(value2, Assert.Single(collection)); + Assert.Same(value2, collection[0]); + Assert.False(collection.Contains(value1)); + Assert.Equal(-1, collection.IndexOf(value1)); + Assert.True(collection.Contains(value2)); + Assert.Equal(0, collection.IndexOf(value2)); + + collection[0] = null; + Assert.Null(Assert.Single(collection)); + Assert.Null(collection[0]); + Assert.False(collection.Contains(value2)); + Assert.Equal(-1, collection.IndexOf(value2)); + Assert.True(collection.Contains(null)); + Assert.Equal(0, collection.IndexOf(null)); + } + + [Fact] + public void DesignerActionListCollection_CopyTo_Invoke_Success() + { + var collection = new DesignerActionListCollection(); + var value = new DesignerActionList(null); + collection.Add(value); + + var array = new DesignerActionList[3]; + collection.CopyTo(array, 1); + Assert.Equal(new DesignerActionList[] { null, value, null }, array); + } + + [Fact] + public void DesignerActionListCollection_Contains_NoSuchValue_ReturnsFalse() + { + var collection = new DesignerActionListCollection(); + var value = new DesignerActionList(null); + collection.Add(value); + + Assert.False(collection.Contains(new DesignerActionList(null))); + Assert.False(collection.Contains(null)); + } + + [Fact] + public void DesignerActionListCollection_IndexOf_NoSuchValue_ReturnsNegativeOne() + { + var collection = new DesignerActionListCollection(); + var value = new DesignerActionList(null); + collection.Add(value); + + Assert.Equal(-1, collection.IndexOf(new DesignerActionList(null))); + Assert.Equal(-1, collection.IndexOf(null)); + } + + [Fact] + public void DesignerActionListCollection_Clear_Success() + { + var collection = new DesignerActionListCollection(); + var value = new DesignerActionList(null); + collection.Add(value); + + collection.Clear(); + Assert.Empty(collection); + + // Clear again. + collection.Clear(); + Assert.Empty(collection); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionListTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionListTests.cs new file mode 100644 index 00000000000..50973392849 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionListTests.cs @@ -0,0 +1,205 @@ +// 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.Collections.Specialized; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using Moq; +using WinForms.Common.Tests; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerActionListTests + { + public static IEnumerable Ctor_IComponent_TestDat() + { + yield return new object[] { null }; + yield return new object[] { new Component() }; + } + + [Theory] + [MemberData(nameof(Ctor_IComponent_TestDat))] + public void DesignerActionList_Ctor_IComponent(IComponent component) + { + var list = new DesignerActionList(component); + Assert.Equal(component, list.Component); + Assert.False(list.AutoShow); + } + + [Theory] + [CommonMemberData(nameof(CommonTestHelper.GetBoolTheoryData))] + public void DesignerActionList_AutoShow_Set_GetReturnsExpected(bool value) + { + var list = new DesignerActionList(new Component()) + { + AutoShow = value + }; + Assert.Equal(value, list.AutoShow); + + // Set same. + list.AutoShow = value; + Assert.Equal(value, list.AutoShow); + } + + public static IEnumerable GetService_TestData() + { + yield return new object[] { null, null }; + yield return new object[] { new Component(), null }; + + var o = new object(); + var mockSite = new Mock(MockBehavior.Strict); + mockSite + .Setup(s => s.GetService(typeof(int))) + .Returns(o); + mockSite + .Setup(s => s.Container) + .Returns((IContainer)null); + yield return new object[] { new Component { Site = mockSite.Object }, o }; + } + + [Theory] + [MemberData(nameof(GetService_TestData))] + public void DesignerActionList_GetService_Invoke_ReturnsExpected(Component component, object expected) + { + var list = new DesignerActionList(component); + Assert.Equal(expected, list.GetService(typeof(int))); + } + + [Fact] + public void DesignerActionList_GetSortedActionItems_CustomClass_ReturnsExpected() + { + var list = new SubDesignerActionList(); + DesignerActionItemCollection items = list.GetSortedActionItems(); + Assert.Equal(8, items.Count); + + DesignerActionMethodItem item1 = Assert.IsType(items[0]); + Assert.Equal("AnnotatedMethod", item1.MemberName); + Assert.Equal("DisplayName", item1.DisplayName); + Assert.Equal("Description", item1.Description); + Assert.Equal("Category", item1.Category); + + DesignerActionPropertyItem item2 = Assert.IsType(items[1]); + Assert.Equal("AnnotatedProperty", item2.MemberName); + Assert.Equal("DisplayName", item2.DisplayName); + Assert.Equal("Description", item2.Description); + Assert.Equal("Category", item2.Category); + + DesignerActionMethodItem item3 = Assert.IsType(items[2]); + Assert.Equal("EmptyAnnotatedMethod", item3.MemberName); + Assert.Equal("EmptyAnnotatedMethod", item3.DisplayName); + Assert.Empty(item3.Description); + Assert.Empty(item3.Category); + + DesignerActionPropertyItem item4 = Assert.IsType(items[3]); + Assert.Equal("EmptyAnnotatedProperty", item4.MemberName); + Assert.Equal("EmptyAnnotatedProperty", item4.DisplayName); + Assert.Empty(item4.Description); + Assert.Empty(item4.Category); + + DesignerActionMethodItem item5 = Assert.IsType(items[4]); + Assert.Equal("NullAnnotatedMethod", item5.MemberName); + Assert.Equal("NullAnnotatedMethod", item5.DisplayName); + Assert.Null(item5.Description); + Assert.Null(item5.Category); + + DesignerActionPropertyItem item6 = Assert.IsType(items[5]); + Assert.Equal("NullAnnotatedProperty", item6.MemberName); + Assert.Equal("NullAnnotatedProperty", item6.DisplayName); + Assert.Null(item6.Description); + Assert.Null(item6.Category); + + DesignerActionMethodItem item7 = Assert.IsType(items[6]); + Assert.Equal("PublicMethod", item7.MemberName); + Assert.Equal("PublicMethod", item7.DisplayName); + Assert.Empty(item7.Description); + Assert.Empty(item7.Category); + + DesignerActionPropertyItem item8 = Assert.IsType(items[7]); + Assert.Equal("PublicProperty", item8.MemberName); + Assert.Equal("PublicProperty", item8.DisplayName); + Assert.Empty(item8.Description); + Assert.Empty(item8.Category); + } + + [Fact] + public void DesignerActionList_GetSortedActionItems_BaseClass_ReturnsEmpty() + { + var list = new DesignerActionList(null); + DesignerActionItemCollection items = list.GetSortedActionItems(); + Assert.Empty(items); + } + + private class SubDesignerActionList : DesignerActionList + { + public SubDesignerActionList() : base(null) + { + } + + public void PublicMethod() + { + } + + [Description("Description")] + [DisplayName("DisplayName")] + [Category("Category")] + public void AnnotatedMethod() + { + } + + [Description("")] + [DisplayName("")] + [Category("")] + public void EmptyAnnotatedMethod() + { + } + + [Description(null)] + [DisplayName(null)] + [Category(null)] + public void NullAnnotatedMethod() + { + } + + private void PrivateMethod() + { + } + + public void MethodWithParameters(object o) + { + } + + [SpecialName] + public void MethodWithSpecialName() + { + } + + public static void StaticMethod() + { + } + + public int PublicProperty { get; set; } + + [Description("Description")] + [DisplayName("DisplayName")] + [Category("Category")] + public int AnnotatedProperty { get; set; } + + [Description("")] + [DisplayName("")] + [Category("")] + public int EmptyAnnotatedProperty { get; set; } + + [Description(null)] + [DisplayName(null)] + [Category(null)] + public int NullAnnotatedProperty { get; set; } + + private int PrivateProperty { get; set; } + + public static int StaticProperty { get; set; } + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionMethodItemTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionMethodItemTests.cs new file mode 100644 index 00000000000..a07245949e6 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionMethodItemTests.cs @@ -0,0 +1,274 @@ +// 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.Collections.Specialized; +using System.ComponentModel; +using System.Reflection; +using WinForms.Common.Tests; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerActionMethodItemTests + { + public static IEnumerable Ctor_DesignerActionList_String_String_String_String_Bool_TestData() + { + yield return new object[] { new DesignerActionList(null), "memberName", "displayName", "category", "description", false, "displayName" }; + yield return new object[] { new DesignerActionList(null), "member(&a)Name", "displa(&a)yName", "cate(&a)gory", "description", true, "displayName" }; + yield return new object[] { null, string.Empty, string.Empty, string.Empty, string.Empty, false, string.Empty }; + yield return new object[] { null, null, null, null, null, false, null }; + } + + [Theory] + [MemberData(nameof(Ctor_DesignerActionList_String_String_String_String_Bool_TestData))] + public void DesignerActionMethodItem_Ctor_DesignerActionList_String_String_String_String_Bool(DesignerActionList actionList, string memberName, string displayName, string category, string description, bool includeAsDesignerVerb, string expectedDisplayName) + { + var item = new DesignerActionMethodItem(actionList, memberName, displayName, category, description, includeAsDesignerVerb); + Assert.Equal(memberName, item.MemberName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(category, item.Category); + Assert.Equal(description, item.Description); + Assert.Equal(includeAsDesignerVerb, item.IncludeAsDesignerVerb); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + public static IEnumerable Ctor_DesignerActionList_String_String_String_String_TestData() + { + yield return new object[] { new DesignerActionList(null), "memberName", "displayName", "category", "description", "displayName" }; + yield return new object[] { new DesignerActionList(null), "member(&a)Name", "displa(&a)yName", "cate(&a)gory", "description", "displayName" }; + yield return new object[] { null, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty }; + yield return new object[] { null, null, null, null, null, null }; + } + + [Theory] + [MemberData(nameof(Ctor_DesignerActionList_String_String_String_String_TestData))] + public void DesignerActionMethodItem_Ctor_DesignerActionList_String_String_String_String(DesignerActionList actionList, string memberName, string displayName, string category, string description, string expectedDisplayName) + { + var item = new DesignerActionMethodItem(actionList, memberName, displayName, category, description); + Assert.Equal(memberName, item.MemberName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(category, item.Category); + Assert.Equal(description, item.Description); + Assert.False(item.IncludeAsDesignerVerb); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + public static IEnumerable Ctor_DesignerActionList_String_String_String_Bool_TestData() + { + yield return new object[] { new DesignerActionList(null), "memberName", "displayName", "category", false, "displayName" }; + yield return new object[] { new DesignerActionList(null), "member(&a)Name", "displa(&a)yName", "cate(&a)gory", true, "displayName" }; + yield return new object[] { null, string.Empty, string.Empty, string.Empty, false, string.Empty }; + yield return new object[] { null, null, null, null, false, null }; + } + + [Theory] + [MemberData(nameof(Ctor_DesignerActionList_String_String_String_Bool_TestData))] + public void DesignerActionMethodItem_Ctor_DesignerActionList_String_String_String_Bool(DesignerActionList actionList, string memberName, string displayName, string category, bool includeAsDesignerVerb, string expectedDisplayName) + { + var item = new DesignerActionMethodItem(actionList, memberName, displayName, category, includeAsDesignerVerb); + Assert.Equal(memberName, item.MemberName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(category, item.Category); + Assert.Null(item.Description); + Assert.Equal(includeAsDesignerVerb, item.IncludeAsDesignerVerb); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + public static IEnumerable Ctor_DesignerActionList_String_String_String_TestData() + { + yield return new object[] { new DesignerActionList(null), "memberName", "displayName", "category", "displayName" }; + yield return new object[] { new DesignerActionList(null), "member(&a)Name", "displa(&a)yName", "cate(&a)gory", "displayName" }; + yield return new object[] { null, string.Empty, string.Empty, string.Empty, string.Empty }; + yield return new object[] { null, null, null, null, null }; + } + + [Theory] + [MemberData(nameof(Ctor_DesignerActionList_String_String_String_TestData))] + public void DesignerActionMethodItem_Ctor_DesignerActionList_String_String_String(DesignerActionList actionList, string memberName, string displayName, string category, string expectedDisplayName) + { + var item = new DesignerActionMethodItem(actionList, memberName, displayName, category); + Assert.Equal(memberName, item.MemberName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(category, item.Category); + Assert.Null(item.Description); + Assert.False(item.IncludeAsDesignerVerb); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + public static IEnumerable Ctor_DesignerActionList_String_String_Bool_TestData() + { + yield return new object[] { new DesignerActionList(null), "memberName", "displayName", false, "displayName" }; + yield return new object[] { new DesignerActionList(null), "member(&a)Name", "displa(&a)yName", true, "displayName" }; + yield return new object[] { null, string.Empty, string.Empty, false, string.Empty }; + yield return new object[] { null, null, null, false, null }; + } + + [Theory] + [MemberData(nameof(Ctor_DesignerActionList_String_String_Bool_TestData))] + public void DesignerActionMethodItem_Ctor_DesignerActionList_String_String_Bool(DesignerActionList actionList, string memberName, string displayName, bool includeAsDesignerVerb, string expectedDisplayName) + { + var item = new DesignerActionMethodItem(actionList, memberName, displayName, includeAsDesignerVerb); + Assert.Equal(memberName, item.MemberName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Null(item.Category); + Assert.Null(item.Description); + Assert.Equal(includeAsDesignerVerb, item.IncludeAsDesignerVerb); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + public static IEnumerable Ctor_DesignerActionList_String_String_TestData() + { + yield return new object[] { new DesignerActionList(null), "memberName", "displayName", "displayName" }; + yield return new object[] { new DesignerActionList(null), "member(&a)Name", "displa(&a)yName", "displayName" }; + yield return new object[] { null, string.Empty, string.Empty, string.Empty }; + yield return new object[] { null, null, null, null }; + } + + [Theory] + [MemberData(nameof(Ctor_DesignerActionList_String_String_TestData))] + public void DesignerActionMethodItem_Ctor_DesignerActionList_String_String(DesignerActionList actionList, string memberName, string displayName, string expectedDisplayName) + { + var item = new DesignerActionMethodItem(actionList, memberName, displayName); + Assert.Equal(memberName, item.MemberName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Null(item.Category); + Assert.Null(item.Description); + Assert.False(item.IncludeAsDesignerVerb); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + public static IEnumerable RelatedComponent_Set_TestData() + { + yield return new object[] { new Component() }; + yield return new object[] { null }; + } + + [Theory] + [MemberData(nameof(RelatedComponent_Set_TestData))] + public void DesignerActionMethodItem_RelatedComponent_Set_GetReturnsExpected(IComponent value) + { + var item = new DesignerActionMethodItem(null, "memberName", "displayName", "category", "description") + { + RelatedComponent = value + }; + Assert.Same(value, item.RelatedComponent); + + // Set same. + item.RelatedComponent = value; + Assert.Same(value, item.RelatedComponent); + } + + [Theory] + [InlineData(nameof(SubDesignerActionList.PublicMethod))] + [InlineData("PrivateMethod")] + public void Invoke_ValidMemberName_ReturnsExpected(string memberName) + { + var list = new SubDesignerActionList(); + var item = new DesignerActionMethodItem(list, memberName, "displayName", "category", "description"); + item.Invoke(); + Assert.Equal(memberName, list.CalledMethod); + + // Call again to test caching behaviour. + list.CalledMethod = null; + item.Invoke(); + Assert.Equal(memberName, list.CalledMethod); + } + + [Fact] + public void Invoke_NullActionList_ThrowsInvalidOperationException() + { + var item = new DesignerActionMethodItem(null, "memberName", "displayName", "category", "description"); + Assert.Throws(() => item.Invoke()); + } + + [Theory] + [InlineData("")] + [InlineData("NoSuchMember")] + [InlineData(nameof(SubDesignerActionList.StaticMethod))] + public void Invoke_NoSuchMemberName_ThrowsInvalidOperationException(string memberName) + { + var list = new SubDesignerActionList(); + var item = new DesignerActionMethodItem(list, memberName, "displayName", "category", "description"); + Assert.Throws(() => item.Invoke()); + } + + [Fact] + public void Invoke_NullMemberName_ThrowsArgumentNullException() + { + var list = new SubDesignerActionList(); + var item = new DesignerActionMethodItem(list, null, "displayName", "category", "description"); + Assert.Throws("name", () => item.Invoke()); + } + + [Fact] + public void Invoke_MemberWithParameters_ThrowsTargetParameterCountException() + { + var list = new SubDesignerActionList(); + var item = new DesignerActionMethodItem(list, nameof(SubDesignerActionList.MethodWithParameters), "displayName", "category", "description"); + Assert.Throws(() => item.Invoke()); + } + + private class SubDesignerActionList : DesignerActionList + { + public SubDesignerActionList() : base(null) + { + } + + public string CalledMethod { get; set; } + + public void PublicMethod() + { + Assert.Null(CalledMethod); + CalledMethod = nameof(PublicMethod); + } + + private void PrivateMethod() + { + Assert.Null(CalledMethod); + CalledMethod = nameof(PrivateMethod); + } + + public static void StaticMethod() + { + throw new InvalidOperationException(); + } + + public void MethodWithParameters(object o) + { + throw new InvalidOperationException(); + } + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionPropertyItemTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionPropertyItemTests.cs new file mode 100644 index 00000000000..088cf9a16e4 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionPropertyItemTests.cs @@ -0,0 +1,96 @@ +// 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.Collections.Specialized; +using System.ComponentModel; +using WinForms.Common.Tests; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerActionPropertyItemTests + { + [Theory] + [InlineData("memberName", "displayName", "category", "description", "displayName")] + [InlineData("member(&a)Name", "displa(&a)yName", "cate(&a)gory", "descr(&a)iption", "displayName")] + [InlineData("", "", "", "", "")] + [InlineData(null, null, null, null, null)] + public void DesignerActionPropertyItem_Ctor_String_String_String_String(string memberName, string displayName, string category, string description, string expectedDisplayName) + { + var item = new DesignerActionPropertyItem(memberName, displayName, category, description); + Assert.Equal(memberName, item.MemberName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(category, item.Category); + Assert.Equal(description, item.Description); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + [Theory] + [InlineData("memberName", "displayName", "category", "displayName")] + [InlineData("member(&a)Name", "displa(&a)yName", "cate(&a)gory", "displayName")] + [InlineData("", "", "", "")] + [InlineData(null, null, null, null)] + public void DesignerActionPropertyItem_Ctor_String_String_String(string memberName, string displayName, string category, string expectedDisplayName) + { + var item = new DesignerActionPropertyItem(memberName, displayName, category); + Assert.Equal(memberName, item.MemberName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(category, item.Category); + Assert.Null(item.Description); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + [Theory] + [InlineData("memberName", "displayName", "displayName")] + [InlineData("member(&a)Name", "displa(&a)yName", "displayName")] + [InlineData("", "", "")] + [InlineData(null, null, null)] + public void DesignerActionPropertyItem_Ctor_String_String(string memberName, string displayName, string expectedDisplayName) + { + var item = new DesignerActionPropertyItem(memberName, displayName); + Assert.Equal(memberName, item.MemberName); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Null(item.Category); + Assert.Null(item.Description); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + public static IEnumerable RelatedComponent_Set_TestData() + { + yield return new object[] { new Component() }; + yield return new object[] { null }; + } + + [Theory] + [MemberData(nameof(RelatedComponent_Set_TestData))] + public void DesignerActionPropertyItem_RelatedComponent_Set_GetReturnsExpected(IComponent value) + { + var item = new DesignerActionPropertyItem("memberName", "displayName", "category", "description") + { + RelatedComponent = value + }; + Assert.Same(value, item.RelatedComponent); + + // Set same. + item.RelatedComponent = value; + Assert.Same(value, item.RelatedComponent); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionTextItemTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionTextItemTests.cs new file mode 100644 index 00000000000..c65d1756b83 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionTextItemTests.cs @@ -0,0 +1,30 @@ +// 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.Specialized; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerActionTextItemTests + { + [Theory] + [InlineData("displayName", "category", "displayName")] + [InlineData("displa(&a)yName", "cate(&a)gory", "displayName")] + [InlineData("", "", "")] + [InlineData(null, null, null)] + public void DesignerActionItem_Ctor_String_String(string displayName, string category, string expectedDisplayName) + { + var item = new DesignerActionTextItem(displayName, category); + Assert.Equal(expectedDisplayName, item.DisplayName); + Assert.Equal(category, item.Category); + Assert.Null(item.Description); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionVerbItemTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionVerbItemTests.cs new file mode 100644 index 00000000000..38e4aebe072 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerActionVerbItemTests.cs @@ -0,0 +1,53 @@ +// 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.Specialized; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerActionVerbItemTests + { + [Fact] + public void DesignerActionVerbItem_Ctor_DesignerVerb() + { + var verb = new DesignerVerb("text", null); + var item = new DesignerActionVerbItem(verb); + Assert.Null(item.MemberName); + Assert.Equal("text", item.DisplayName); + Assert.Equal("Verbs", item.Category); + Assert.Null(item.Description); + Assert.False(item.IncludeAsDesignerVerb); + Assert.False(item.AllowAssociate); + Assert.Empty(item.Properties); + Assert.Same(item.Properties, item.Properties); + Assert.IsType(item.Properties); + Assert.True(item.ShowInSourceView); + Assert.Null(item.RelatedComponent); + } + + [Fact] + public void DesignerActionVerbItem_Ctor_NullVerb_ThrowsArgumentNullException() + { + Assert.Throws("verb", () => new DesignerActionVerbItem(null)); + } + + [Fact] + public void DesignerActionVerbItem_Invoke_Invoke_CallsVerbInvoke() + { + DesignerVerb verb = null; + int callCount = 0; + EventHandler handler = (sender, e) => + { + Assert.Same(verb, sender); + Assert.Same(EventArgs.Empty, e); + callCount++; + }; + verb = new DesignerVerb("text", handler); + var item = new DesignerActionVerbItem(verb); + item.Invoke(); + Assert.Equal(1, callCount); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerCommandSetTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerCommandSetTests.cs new file mode 100644 index 00000000000..a2a1c93d2a0 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/DesignerCommandSetTests.cs @@ -0,0 +1,71 @@ +// 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 Moq; +using WinForms.Common.Tests; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class DesignerCommandSetTests + { + [Fact] + public void DesignerCommandSet_Ctor_Default() + { + var set = new DesignerCommandSet(); + Assert.Null(set.ActionLists); + Assert.Null(set.Verbs); + } + + [Theory] + [CommonMemberData(nameof(CommonTestHelper.GetStringWithNullTheoryData))] + public void DesignerCommandSet_GetCommands_Invoke_ReturnsNull(string name) + { + var set = new DesignerCommandSet(); + Assert.Null(set.GetCommands(name)); + } + + [Fact] + public void DesignerCommandSet_Verbs_OverridenGetCommands_ReturnsExpected() + { + var collection = new DesignerVerbCollection(); + var mockSet = new Mock(MockBehavior.Strict); + mockSet + .Setup(s => s.GetCommands("Verbs")) + .Returns(collection); + Assert.Same(collection, mockSet.Object.Verbs); + } + + [Fact] + public void DesignerCommandSet_Verbs_InvalidOverridenGetCommands_ThrowsInvalidCastException() + { + var mockSet = new Mock(MockBehavior.Strict); + mockSet + .Setup(s => s.GetCommands("Verbs")) + .Returns(new object[0]); + Assert.Throws(() => mockSet.Object.Verbs); + } + + [Fact] + public void DesignerCommandSet_ActionLists_OverridenGetCommands_ReturnsExpected() + { + var collection = new DesignerActionListCollection(); + var mockSet = new Mock(MockBehavior.Strict); + mockSet + .Setup(s => s.GetCommands("ActionLists")) + .Returns(collection); + Assert.Same(collection, mockSet.Object.ActionLists); + } + + [Fact] + public void DesignerCommandSet_ActionLists_InvalidOverridenGetCommands_ThrowsInvalidCastException() + { + var mockSet = new Mock(MockBehavior.Strict); + mockSet + .Setup(s => s.GetCommands("ActionLists")) + .Returns(new object[0]); + Assert.Throws(() => mockSet.Object.ActionLists); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/ExceptionCollectionTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/ExceptionCollectionTests.cs new file mode 100644 index 00000000000..eb0908474c0 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/ExceptionCollectionTests.cs @@ -0,0 +1,64 @@ +// 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; +using System.Collections.Generic; +using System.IO; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class ExceptionCollectionTests + { + public static IEnumerable Ctor_ArrayList_TestData() + { + yield return new object[] { null }; + yield return new object[] { new ArrayList() }; + yield return new object[] { new ArrayList { 1, 2, 3 } }; + } + + [Theory] + [MemberData(nameof(Ctor_ArrayList_TestData))] + public void ExceptionCollection_Ctor_ArrayList(ArrayList exceptions) + { + var collection = new ExceptionCollection(exceptions); + if (exceptions == null) + { + Assert.Null(collection.Exceptions); + } + else + { + Assert.Equal(exceptions, collection.Exceptions); + Assert.NotSame(exceptions, collection.Exceptions); + Assert.Equal(collection.Exceptions, collection.Exceptions); + Assert.NotSame(collection.Exceptions, collection.Exceptions); + } + } + + [Theory] + [MemberData(nameof(Ctor_ArrayList_TestData))] + public void ExceptionCollection_Serialize_Deserialize_Success(ArrayList exceptions) + { + using (var stream = new MemoryStream()) + { + var formatter = new BinaryFormatter(); + var collection = new ExceptionCollection(exceptions); + formatter.Serialize(stream, collection); + + stream.Position = 0; + ExceptionCollection deserialized = Assert.IsType(formatter.Deserialize(stream)); + Assert.Equal(exceptions, deserialized.Exceptions); + } + } + + [Fact] + public void ExceptionCollection_GetObjectData_NullInfo_ThrowsArgumentNullException() + { + var collection = new ExceptionCollection(new ArrayList()); + Assert.Throws("info", () => collection.GetObjectData(null, new StreamingContext())); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/ProjectTargetFrameworkAttributeTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/ProjectTargetFrameworkAttributeTests.cs new file mode 100644 index 00000000000..a49b73e3e1d --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/ProjectTargetFrameworkAttributeTests.cs @@ -0,0 +1,20 @@ +// 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 WinForms.Common.Tests; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class ProjectTargetFrameworkAttributeTests + { + [Theory] + [CommonMemberData(nameof(CommonTestHelper.GetStringWithNullTheoryData))] + public void ProjectTargetFrameworkAttribute_Ctor_String(string targetFrameworkMoniker) + { + var attribute = new ProjectTargetFrameworkAttribute(targetFrameworkMoniker); + Assert.Equal(targetFrameworkMoniker, attribute.TargetFrameworkMoniker); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/CodeDomSerializerExceptionTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/CodeDomSerializerExceptionTests.cs new file mode 100644 index 00000000000..ebb9a521f4d --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/CodeDomSerializerExceptionTests.cs @@ -0,0 +1,125 @@ +// 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.CodeDom; +using System.Collections.Generic; +using System.ComponentModel.Design.Serialization; +using System.IO; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; +using Moq; +using Xunit; + +namespace System.Windows.Forms.Design.Serialization.Tests +{ + public class CodeDomSerializerExceptionTests + { + public static IEnumerable Ctor_String_CodeLinePragma_TestData() + { + yield return new object[] { "message", new CodeLinePragma() }; + yield return new object[] { null, null }; + } + + [Theory] + [MemberData(nameof(Ctor_String_CodeLinePragma_TestData))] + public void CodeDomSerializerException_Ctor_String_CodeLinePragma(string message, CodeLinePragma linePragma) + { + var exception = new CodeDomSerializerException(message, linePragma); + Assert.NotEmpty(exception.Message); + Assert.Null(exception.InnerException); + Assert.Same(linePragma, exception.LinePragma); + } + + public static IEnumerable Ctor_Exception_CodeLinePragma_TestData() + { + yield return new object[] { new Exception(), new CodeLinePragma() }; + yield return new object[] { null, null }; + } + + [Theory] + [MemberData(nameof(Ctor_Exception_CodeLinePragma_TestData))] + public void CodeDomSerializerException_Ctor_Exception_CodeLinePragma(Exception innerException, CodeLinePragma linePragma) + { + var exception = new CodeDomSerializerException(innerException, linePragma); + Assert.NotEmpty(exception.Message); + Assert.Same(innerException, exception.InnerException); + Assert.Same(linePragma, exception.LinePragma); + } + + public static IEnumerable Ctor_String_IDesignerSerializationManager_TestData() + { + var mockDesignerSerializationManager = new Mock(MockBehavior.Strict); + yield return new object[] { "message", mockDesignerSerializationManager.Object }; + yield return new object[] { null, mockDesignerSerializationManager.Object }; + } + + [Theory] + [MemberData(nameof(Ctor_String_IDesignerSerializationManager_TestData))] + public void CodeDomSerializerException_Ctor_String_IDesignerSerializationManager(string message, IDesignerSerializationManager manager) + { + var exception = new CodeDomSerializerException(message, manager); + Assert.NotEmpty(exception.Message); + Assert.Null(exception.InnerException); + Assert.Null(exception.LinePragma); + } + + public static IEnumerable Ctor_Exception_IDesignerSerializationManager_TestData() + { + var mockDesignerSerializationManager = new Mock(MockBehavior.Strict); + yield return new object[] { new Exception(), mockDesignerSerializationManager.Object }; + yield return new object[] { null, mockDesignerSerializationManager.Object }; + } + + [Theory] + [MemberData(nameof(Ctor_Exception_IDesignerSerializationManager_TestData))] + public void CodeDomSerializerException_Ctor_Exception_IDesignerSerializationManager(Exception innerException, IDesignerSerializationManager manager) + { + var exception = new CodeDomSerializerException(innerException, manager); + Assert.NotEmpty(exception.Message); + Assert.Same(innerException, exception.InnerException); + Assert.Null(exception.LinePragma); + } + + [Fact] + public void CodeDomSerializerException_NullManager_ThrowsArgumentNullException() + { + Assert.Throws("manager", () => new CodeDomSerializerException("message", (IDesignerSerializationManager)null)); + Assert.Throws("manager", () => new CodeDomSerializerException(new Exception(), (IDesignerSerializationManager)null)); + } + + [Fact] + public void CodeDomSerializerException_SerializeWithoutPragma_Deserialize_Success() + { + using (var stream = new MemoryStream()) + { + var formatter = new BinaryFormatter(); + var exception = new CodeDomSerializerException("message", (CodeLinePragma)null); + formatter.Serialize(stream, exception); + + stream.Position = 0; + CodeDomSerializerException deserialized = Assert.IsType(formatter.Deserialize(stream)); + Assert.Equal(exception.Message, deserialized.Message); + Assert.Null(deserialized.LinePragma); + } + } + + [Fact] + public void CodeDomSerializerException_SerializeWithPragma_Deserialize_Success() + { + using (var stream = new MemoryStream()) + { + var formatter = new BinaryFormatter(); + var exception = new CodeDomSerializerException("message", new CodeLinePragma()); + Assert.Throws(() => formatter.Serialize(stream, exception)); + } + } + + [Fact] + public void CodeDomSerializerException_GetObjectData_NullInfo_ThrowsArgumentNullException() + { + var exception = new CodeDomSerializerException("message", new CodeLinePragma()); + Assert.Throws("info", () => exception.GetObjectData(null, new StreamingContext())); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/ExpressionContextTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/ExpressionContextTests.cs new file mode 100644 index 00000000000..cea66d7c28f --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/ExpressionContextTests.cs @@ -0,0 +1,63 @@ +// 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.CodeDom; +using System.Collections.Generic; +using Xunit; + +namespace System.ComponentModel.Design.Serialization.Tests +{ + public class ExpressionContextTests + { + public static IEnumerable Ctor_CodeExpression_Type_Object_Object_TestData() + { + yield return new object[] { new CodeExpression(), typeof(int), new object(), new object() }; + yield return new object[] { new CodeExpression(), typeof(int), new object(), null }; + } + + [Theory] + [MemberData(nameof(Ctor_CodeExpression_Type_Object_Object_TestData))] + public void ExpressionContext_Ctor_CodeExpression_Type_Object_Object_TestData(CodeExpression expression, Type expressionType, object owner, object presetValue) + { + var context = new ExpressionContext(expression, expressionType, owner, presetValue); + Assert.Same(expression, context.Expression); + Assert.Same(expressionType, context.ExpressionType); + Assert.Same(owner, context.Owner); + Assert.Same(presetValue, context.PresetValue); + } + public static IEnumerable Ctor_CodeExpression_Type_Object_TestData() + { + yield return new object[] { new CodeExpression(), typeof(int), new object() }; + } + + [Theory] + [MemberData(nameof(Ctor_CodeExpression_Type_Object_TestData))] + public void ExpressionContext_Ctor_CodeExpression_Type_Object_TestData(CodeExpression expression, Type expressionType, object owner) + { + var context = new ExpressionContext(expression, expressionType, owner); + Assert.Same(expression, context.Expression); + Assert.Same(expressionType, context.ExpressionType); + Assert.Same(owner, context.Owner); + Assert.Null(context.PresetValue); + } + + [Fact] + public void ExpressionContext_Ctor_NullExpression_ThrowsArgumentNullException() + { + Assert.Throws("expression", () => new ExpressionContext(null, typeof(int), new object(), new object())); + } + + [Fact] + public void RootContext_Ctor_NullExpressionType_ThrowsArgumentNullException() + { + Assert.Throws("expressionType", () => new ExpressionContext(new CodeExpression(), null, new object(), new object())); + } + + [Fact] + public void RootContext_Ctor_NullOwner_ThrowsArgumentNullException() + { + Assert.Throws("owner", () => new ExpressionContext(new CodeExpression(), typeof(int), null, new object())); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/RootContextTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/RootContextTests.cs new file mode 100644 index 00000000000..467568420dd --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/RootContextTests.cs @@ -0,0 +1,35 @@ +// 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.CodeDom; +using Xunit; + +namespace System.ComponentModel.Design.Serialization.Tests +{ + public class RootContextTests + { + [Fact] + public void RootContext_Ctor_CodeExpression_Object() + { + var expression = new CodeExpression(); + var value = new object(); + var context = new RootContext(expression, value); + Assert.Same(expression, context.Expression); + Assert.Same(value, context.Value); + } + + [Fact] + public void RootContext_Ctor_NullExpression_ThrowsArgumentNullException() + { + Assert.Throws("expression", () => new RootContext(null, new object())); + } + + [Fact] + public void RootContext_Ctor_NullValue_ThrowsArgumentNullException() + { + var expression = new CodeExpression(); + Assert.Throws("value", () => new RootContext(expression, null)); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/SerializeAbsoluteContextTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/SerializeAbsoluteContextTests.cs new file mode 100644 index 00000000000..d301475c386 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/SerializeAbsoluteContextTests.cs @@ -0,0 +1,56 @@ +// 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 Xunit; + +namespace System.ComponentModel.Design.Serialization.Tests +{ + public class SerializeAbsoluteContextTests + { + [Fact] + public void SerializeAbsoluteContext_Ctor_Default() + { + var context = new SerializeAbsoluteContext(); + Assert.Null(context.Member); + } + + public static IEnumerable Ctor_MemberDescriptor_TestData() + { + yield return new object[] { null }; + + PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(RootContext)); + yield return new object[] { properties[nameof(RootContext.Expression)] }; + } + + [Theory] + [MemberData(nameof(Ctor_MemberDescriptor_TestData))] + public void SerializeAbsoluteContext_Ctor_MemberDescriptor(MemberDescriptor member) + { + var context = new SerializeAbsoluteContext(member); + Assert.Same(member, context.Member); + } + + public static IEnumerable ShouldSerialize_TestData() + { + PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(RootContext)); + MemberDescriptor member1 = properties[nameof(RootContext.Expression)]; + MemberDescriptor member2 = properties[nameof(RootContext.Value)]; + + yield return new object[] { new SerializeAbsoluteContext(null), null, true }; + yield return new object[] { new SerializeAbsoluteContext(null), member1, true }; + + yield return new object[] { new SerializeAbsoluteContext(member1), null, false }; + yield return new object[] { new SerializeAbsoluteContext(member1), member1, true }; + yield return new object[] { new SerializeAbsoluteContext(member2), member2, true }; + } + + [Theory] + [MemberData(nameof(ShouldSerialize_TestData))] + public void SerializeAbsoluteContext_ShouldSerialize_Invoke_ReturnsExpected(SerializeAbsoluteContext context, MemberDescriptor member, bool expected) + { + Assert.Equal(expected, context.ShouldSerialize(member)); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/StatementContextTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/StatementContextTests.cs new file mode 100644 index 00000000000..8b32fb2343c --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/Serialization/StatementContextTests.cs @@ -0,0 +1,19 @@ +// 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 Xunit; + +namespace System.ComponentModel.Design.Serialization.Tests +{ + public class StatementContextTests + { + [Fact] + public void StatementContext_Ctor_Default() + { + var context = new StatementContext(); + Assert.Empty(context.StatementCollection); + Assert.Same(context.StatementCollection, context.StatementCollection); + } + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/UndoUnitTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/UndoUnitTests.cs new file mode 100644 index 00000000000..cd1ba09ed6f --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/ComponentModel/Design/UndoUnitTests.cs @@ -0,0 +1,64 @@ +// 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.ComponentModel.Design.Serialization; +using Moq; +using WinForms.Common.Tests; +using Xunit; + +namespace System.ComponentModel.Design.Tests +{ + public class UndoUnitTests: UndoEngine + { + public UndoUnitTests() : base(GetServiceProvider()) + { + } + + private static IServiceProvider GetServiceProvider() + { + var mockServiceProvider = new Mock(); + var mockDesignerHost = new Mock(); + var mockComponentChangeService = new Mock(); + mockServiceProvider + .Setup(p => p.GetService(typeof(IDesignerHost))) + .Returns(mockDesignerHost.Object); + mockServiceProvider + .Setup(p => p.GetService(typeof(IComponentChangeService))) + .Returns(mockComponentChangeService.Object); + mockServiceProvider + .Setup(p => p.GetService(typeof(ComponentSerializationService))) + .Returns(new object()); + return mockServiceProvider.Object; + } + + [Theory] + [CommonMemberData(nameof(CommonTestHelper.GetStringWithNullTheoryData))] + public void UndoUnit_Ctor_UndoEngine_String(string name) + { + var unit = new SubUndoUnit(this, name); + Assert.Same(this, unit.UndoEngine); + Assert.Equal(name ?? string.Empty, unit.Name); + Assert.True(unit.IsEmpty); + } + + [Fact] + public void UndoUnit_NullEngine_ThrowsArgumentNullException() + { + Assert.Throws("engine", () => new UndoUnit(null, "name")); + } + + protected override void AddUndoUnit(UndoUnit unit) + { + } + + protected class SubUndoUnit : UndoUnit + { + public SubUndoUnit(UndoEngine engine, string name) : base(engine, name) + { + } + + public new UndoEngine UndoEngine => base.UndoEngine; + } + } +}