Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3905 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
17 changed files with 133 additions and 524 deletions
@ -1,109 +0,0 @@
@@ -1,109 +0,0 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.ComponentModel; |
||||
using System.Drawing; |
||||
using System.Reflection; |
||||
using System.Windows.Forms; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
/// <summary>
|
||||
/// Contains the default values for all properties on a Control, Form or UserControl.
|
||||
/// </summary>
|
||||
public class PythonControlDefaultPropertyValues |
||||
{ |
||||
Dictionary<string, object> defaultPropertyValues = new Dictionary<string, object>(); |
||||
|
||||
public PythonControlDefaultPropertyValues() |
||||
{ |
||||
defaultPropertyValues.Add("Text", String.Empty); |
||||
defaultPropertyValues.Add("AutoValidate", AutoValidate.EnablePreventFocusChange); |
||||
defaultPropertyValues.Add("Enabled", true); |
||||
defaultPropertyValues.Add("Visible", true); |
||||
defaultPropertyValues.Add("AutoScaleMode", AutoScaleMode.Inherit); |
||||
defaultPropertyValues.Add("DoubleBuffered", false); |
||||
defaultPropertyValues.Add("ImeMode", ImeMode.NoControl); |
||||
defaultPropertyValues.Add("RightToLeft", RightToLeft.No); |
||||
defaultPropertyValues.Add("Cursor", Cursors.Default); |
||||
defaultPropertyValues.Add("MinimumSize", new Size(0, 0)); |
||||
defaultPropertyValues.Add("AutoScrollMinSize", new Size(0, 0)); |
||||
defaultPropertyValues.Add("AutoScrollMargin", new Size(0, 0)); |
||||
defaultPropertyValues.Add("Location", new Point(0, 0)); |
||||
defaultPropertyValues.Add("Padding", Padding.Empty); |
||||
defaultPropertyValues.Add("BackColor", Control.DefaultBackColor); |
||||
defaultPropertyValues.Add("ForeColor", Control.DefaultForeColor); |
||||
defaultPropertyValues.Add("Font", Control.DefaultFont); |
||||
defaultPropertyValues.Add("TransparencyKey", Color.Empty); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Determines if the property value has its default value.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property.</param>
|
||||
/// <param name="obj">The object that has the property.</param>
|
||||
/// <returns>False if the property does not exist.</returns>
|
||||
public bool IsDefaultValue(string propertyName, object obj) |
||||
{ |
||||
PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); |
||||
if (propertyInfo != null) { |
||||
object propertyValue = propertyInfo.GetValue(obj, null); |
||||
return IsDefaultValue(propertyInfo, propertyValue); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Determines if the property value is the default value by checking the DefaultValueAttribute.
|
||||
/// </summary>
|
||||
/// <param name="propertyDescriptor">The property descriptor for the property.</param>
|
||||
/// <param name="obj">The object that has the property.</param>
|
||||
/// <remarks>
|
||||
/// For some properties such as Form.AutoValidate there is no default value specified by the
|
||||
/// DefaultValueAttribute.
|
||||
/// </remarks>
|
||||
public bool IsDefaultValue(PropertyDescriptor propertyDescriptor, object obj) |
||||
{ |
||||
PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyDescriptor.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); |
||||
if (propertyInfo != null) { |
||||
return IsDefaultValue(propertyInfo, propertyDescriptor.GetValue(obj)); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Determines if the property value is the default value by checking the DefaultValueAttribute.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// For some properties such as Form.AutoValidate there is no default value specified by the
|
||||
/// DefaultValueAttribute.
|
||||
/// </remarks>
|
||||
public bool IsDefaultValue(PropertyInfo propertyInfo, object propertyValue) |
||||
{ |
||||
// Check default attribute.
|
||||
DefaultValueAttribute defaultValueAttribute = (DefaultValueAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(DefaultValueAttribute), true); |
||||
if (defaultValueAttribute != null) { |
||||
if (defaultValueAttribute.Value != null) { |
||||
return defaultValueAttribute.Value.Equals(propertyValue); |
||||
} |
||||
return defaultValueAttribute.Value == propertyValue; |
||||
} |
||||
|
||||
object defaultPropertyValue = null; |
||||
if (defaultPropertyValues.TryGetValue(propertyInfo.Name, out defaultPropertyValue)) { |
||||
return defaultPropertyValue.Equals(propertyValue); |
||||
} |
||||
|
||||
if (propertyInfo.Name == "Icon") { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
} |
||||
} |
@ -1,271 +0,0 @@
@@ -1,271 +0,0 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Drawing; |
||||
using System.ComponentModel; |
||||
using System.ComponentModel.Design; |
||||
using System.Reflection; |
||||
using System.Windows.Forms; |
||||
|
||||
using ICSharpCode.PythonBinding; |
||||
using NUnit.Framework; |
||||
|
||||
namespace PythonBinding.Tests.Designer |
||||
{ |
||||
/// <summary>
|
||||
/// Tests the PythonControlDefaultPropertyValues.IsDefaultValue method.
|
||||
/// This checks the property and returns true if the value of the property is the same as its default.
|
||||
/// </summary>
|
||||
[TestFixture] |
||||
public class IsDefaultPropertyValueTests |
||||
{ |
||||
Form form; |
||||
PythonControlDefaultPropertyValues defaultPropertyValues; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void InitFixture() |
||||
{ |
||||
form = new Form(); |
||||
defaultPropertyValues = new PythonControlDefaultPropertyValues(); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void TearDownFixture() |
||||
{ |
||||
form.Dispose(); |
||||
} |
||||
|
||||
[Test] |
||||
public void TextPropertyDefaultIsEmptyString() |
||||
{ |
||||
form.Text = String.Empty; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("Text", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void TextPropertyIsNotEmptyString() |
||||
{ |
||||
form.Text = "abc"; |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("Text", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void AutoValidatePropertyDefaultIsEnablePreventFocusChange() |
||||
{ |
||||
form.AutoValidate = AutoValidate.EnablePreventFocusChange; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("AutoValidate", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void AutoValidatePropertyIsDisable() |
||||
{ |
||||
form.AutoValidate = AutoValidate.Disable; |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("AutoValidate", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void EnabledPropertyDefaultIsTrue() |
||||
{ |
||||
form.Enabled = true; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("Enabled", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void EnabledPropertyIsFalse() |
||||
{ |
||||
form.Enabled = false; |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("Enabled", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void AccessibleDescriptionDefaultValueIsNull() |
||||
{ |
||||
form.AccessibleDescription = null; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("AccessibleDescription", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void AutoScaleModeDefaultIsInherit() |
||||
{ |
||||
form.AutoScaleMode = AutoScaleMode.Inherit; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("AutoScaleMode", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void PropertyDoesNotExist() |
||||
{ |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("PropertyDoesNotExist", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void DoubleBufferedDefaultIsFalse() |
||||
{ |
||||
PropertyInfo property = form.GetType().GetProperty("DoubleBuffered", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); |
||||
property.SetValue(form, false, null); |
||||
|
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("DoubleBuffered", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void CursorDefaultIsCursorsDefault() |
||||
{ |
||||
form.Cursor = Cursors.Default; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("Cursor", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void HelpCursorIsNotDefaultValue() |
||||
{ |
||||
form.Cursor = Cursors.Help; |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("Cursor", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void VisiblePropertyDefaultIsTrue() |
||||
{ |
||||
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form); |
||||
PropertyDescriptor namePropertyDescriptor = descriptors.Find("Visible", false); |
||||
namePropertyDescriptor.SetValue(form, true); |
||||
|
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("Visible", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void VisiblePropertyIsFalse() |
||||
{ |
||||
form.Visible = false; |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("Visible", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void MinFormSizeDefaultIsEmpty() |
||||
{ |
||||
form.MinimumSize = new Size(0, 0); |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("MinimumSize", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void NonDefaultMinFormSize() |
||||
{ |
||||
form.MinimumSize = new Size(100, 100); |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("MinimumSize", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void AutoScrollSizeDefaultIsEmpty() |
||||
{ |
||||
form.AutoScrollMinSize = new Size(0, 0); |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("AutoScrollMinSize", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void NonDefaultAutoScrollMinSize() |
||||
{ |
||||
form.AutoScrollMinSize = new Size(100, 100); |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("AutoScrollMinSize", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void AutoScrollMarginDefaultIsEmpty() |
||||
{ |
||||
form.AutoScrollMargin = new Size(0, 0); |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("AutoScrollMargin", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void NonDefaultAutoScrollMargin() |
||||
{ |
||||
form.AutoScrollMargin = new Size(100, 100); |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("AutoScrollMargin", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void LocationDefaultIsEmpty() |
||||
{ |
||||
form.Location = new Point(0, 0); |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("Location", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void NonDefaultLocation() |
||||
{ |
||||
form.Location = new Point(10, 20); |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("Location", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void PaddingPropertyDefaultIsPaddingEmpty() |
||||
{ |
||||
form.Padding = Padding.Empty; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("Padding", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void NonDefaultPaddingProperty() |
||||
{ |
||||
form.Padding = new Padding(10, 10, 10, 10); |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("Padding", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void BackColorPropertyDefaultIsControlDefaultBackColor() |
||||
{ |
||||
form.BackColor = Control.DefaultBackColor; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("BackColor", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void NonDefaultBackColorProperty() |
||||
{ |
||||
form.BackColor = Color.Blue; |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("BackColor", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void ForeColorPropertyDefaultIsControlDefaultForeColor() |
||||
{ |
||||
form.ForeColor = Control.DefaultForeColor; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("ForeColor", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void NonDefaultForeColorProperty() |
||||
{ |
||||
form.ForeColor = Color.Blue; |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("ForeColor", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void FontDefaultIsControlDefaultFont() |
||||
{ |
||||
form.Font = Control.DefaultFont; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("Font", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void NonDefaultFont() |
||||
{ |
||||
form.Font = new Font("Times New Roman", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))); |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("Font", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void TransparencyKeyDefaultIsEmptyColor() |
||||
{ |
||||
form.TransparencyKey = Color.Empty; |
||||
Assert.IsTrue(defaultPropertyValues.IsDefaultValue("TransparencyKey", form)); |
||||
} |
||||
|
||||
[Test] |
||||
public void NonDefaultTransparencyKey() |
||||
{ |
||||
form.TransparencyKey = Color.White; |
||||
Assert.IsFalse(defaultPropertyValues.IsDefaultValue("TransparencyKey", form)); |
||||
} |
||||
} |
||||
} |
@ -1,76 +0,0 @@
@@ -1,76 +0,0 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.ComponentModel; |
||||
using System.ComponentModel.Design; |
||||
using System.Reflection; |
||||
using System.Windows.Forms; |
||||
|
||||
using ICSharpCode.PythonBinding; |
||||
using NUnit.Framework; |
||||
|
||||
namespace PythonBinding.Tests.Designer |
||||
{ |
||||
/// <summary>
|
||||
/// Tests the PythonForm.ShouldSerialize method which determines whether a form's property
|
||||
/// should be serialized.
|
||||
/// </summary>
|
||||
[TestFixture] |
||||
public class ShouldSerializeTests |
||||
{ |
||||
PythonForm pythonForm; |
||||
Form form; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void SetUpFixture() |
||||
{ |
||||
form = new Form(); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void TearDownFixture() |
||||
{ |
||||
form.Dispose(); |
||||
} |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
pythonForm = new PythonForm(); |
||||
} |
||||
|
||||
[Test] |
||||
public void ShouldNotSerializePropertyWithFalseBrowseAttribute() |
||||
{ |
||||
AssertShouldNotSerialize("LayoutEngine", form); |
||||
} |
||||
|
||||
[Test] |
||||
public void ShouldNotSerializeEmptyStringTextProperty() |
||||
{ |
||||
form.Text = String.Empty; |
||||
AssertShouldNotSerialize("Text", form); |
||||
} |
||||
|
||||
void AssertShouldSerialize(string propertyName, Form form) |
||||
{ |
||||
AssertShouldSerialize(propertyName, form, true); |
||||
} |
||||
|
||||
void AssertShouldNotSerialize(string propertyName, Form form) |
||||
{ |
||||
AssertShouldSerialize(propertyName, form, false); |
||||
} |
||||
|
||||
void AssertShouldSerialize(string propertyName, Form form, bool isTrue) |
||||
{ |
||||
PropertyDescriptor property = TypeDescriptor.GetProperties(form).Find(propertyName, true); |
||||
Assert.AreEqual(isTrue, pythonForm.ShouldSerialize(form, property)); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue