Browse Source

Python forms designer now using more PropertyDescriptor information to determine whether a property should be serialized. Form properties now generated in alphabetical order.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3905 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 17 years ago
parent
commit
c19a0e9bb5
  1. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Project/PythonBinding.csproj
  2. 109
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlDefaultPropertyValues.cs
  3. 64
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs
  4. 3
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonPropertyValueAssignment.cs
  5. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/EnabledSetUsingPropertyDescriptorTestFixture.cs
  6. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateAccessibleRoleFormTestFixture.cs
  7. 22
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateAutoScaleModeFormTestFixture.cs
  8. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateDoubleBufferedFormTestFixture.cs
  9. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormPaddingTestFixture.cs
  10. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateImeModeFormTestFixture.cs
  11. 4
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateMinSizeFormTestFixture.cs
  12. 4
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateRightToLeftFormTestFixture.cs
  13. 84
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/IgnoreDesignTimePropertiesTestFixture.cs
  14. 271
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/IsDefaultPropertyValueTests.cs
  15. 7
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonPropertyAssignmentToStringTests.cs
  16. 76
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/ShouldSerializeTests.cs
  17. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

1
src/AddIns/BackendBindings/Python/PythonBinding/Project/PythonBinding.csproj

@ -87,7 +87,6 @@ @@ -87,7 +87,6 @@
<Compile Include="Src\PythonConsoleCompletionDataProvider.cs" />
<Compile Include="Src\PythonConsoleHost.cs" />
<Compile Include="Src\PythonConsolePad.cs" />
<Compile Include="Src\PythonControlDefaultPropertyValues.cs" />
<Compile Include="Src\PythonControlFieldExpression.cs" />
<Compile Include="Src\PythonDesignerGenerator.cs" />
<Compile Include="Src\PythonDesignerLoader.cs" />

109
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlDefaultPropertyValues.cs

@ -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;
}
}
}

64
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs

@ -26,7 +26,6 @@ namespace ICSharpCode.PythonBinding @@ -26,7 +26,6 @@ namespace ICSharpCode.PythonBinding
StringBuilder codeBuilder;
string indentString = String.Empty;
int indent;
PythonControlDefaultPropertyValues defaultPropertyValues = new PythonControlDefaultPropertyValues();
public PythonForm()
: this("\t")
@ -65,45 +64,31 @@ namespace ICSharpCode.PythonBinding @@ -65,45 +64,31 @@ namespace ICSharpCode.PythonBinding
return codeBuilder.ToString();
}
/// <summary>
/// Should serialize a property if:
///
/// 1) It has a different value to its default.
/// 2) DesignerSerializationVisibility is set to Hidden.
/// Gets a list of properties that should be serialized for the specified form.
/// </summary>
public bool ShouldSerialize(object obj, PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.DesignTimeOnly) {
return false;
}
// Is default value?
bool serialize = !defaultPropertyValues.IsDefaultValue(propertyDescriptor, obj);
// Is visible to designer?
if (serialize) {
serialize = propertyDescriptor.SerializationVisibility == DesignerSerializationVisibility.Visible;
if (!serialize) {
serialize = (propertyDescriptor.Name == "AutoScaleMode");
}
}
// Is browsable?
if (serialize) {
// Always serialize the Name.
if (propertyDescriptor.Name != "Name" && propertyDescriptor.Name != "AutoScaleMode" && propertyDescriptor.Name != "ClientSize") {
serialize = propertyDescriptor.IsBrowsable;
}
public PropertyDescriptorCollection GetSerializableProperties(object obj)
{
System.Console.WriteLine("GetSerializableProperties");
List<PropertyDescriptor> properties = new List<PropertyDescriptor>();
Attribute[] filter = new Attribute[] { DesignOnlyAttribute.No };
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(obj, filter).Sort()) {
if (property.SerializationVisibility == DesignerSerializationVisibility.Visible) {
if (property.ShouldSerializeValue(obj)) {
properties.Add(property);
System.Console.WriteLine("Property.Name: " + property.Name);
}
}
}
return serialize;
}
return new PropertyDescriptorCollection(properties.ToArray());
}
void GenerateInitializeComponentMethodBodyInternal(Form form)
{
// Add method body.
AppendIndentedLine("self.SuspendLayout()");
AppendForm(form);
AppendIndentedLine("self.ResumeLayout(False)");
@ -127,14 +112,9 @@ namespace ICSharpCode.PythonBinding @@ -127,14 +112,9 @@ namespace ICSharpCode.PythonBinding
void AppendForm(Form form)
{
AppendComment(form.Name);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(form);
foreach (PropertyDescriptor property in properties) {
if (property.SerializationVisibility == DesignerSerializationVisibility.Hidden && property.Name == "Name") {
// Skip the duplicate Name property.
} else {
AppendProperty(form, property);
}
foreach (PropertyDescriptor property in GetSerializableProperties(form)) {
AppendProperty(form, property);
}
}
@ -148,9 +128,7 @@ namespace ICSharpCode.PythonBinding @@ -148,9 +128,7 @@ namespace ICSharpCode.PythonBinding
return;
}
if (ShouldSerialize(obj, propertyDescriptor)) {
AppendIndentedLine("self." + propertyDescriptor.Name + " = " + PythonPropertyValueAssignment.ToString(propertyValue));
}
AppendIndentedLine("self." + propertyDescriptor.Name + " = " + PythonPropertyValueAssignment.ToString(propertyValue));
}
/// <summary>

3
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonPropertyValueAssignment.cs

@ -39,6 +39,9 @@ namespace ICSharpCode.PythonBinding @@ -39,6 +39,9 @@ namespace ICSharpCode.PythonBinding
} else if (propertyType == typeof(Size)) {
Size size = (Size)propertyValue;
return size.GetType().FullName + "(" + size.Width + ", " + size.Height + ")";
} else if (propertyType == typeof(SizeF)) {
SizeF size = (SizeF)propertyValue;
return size.GetType().FullName + "(" + size.Width + ", " + size.Height + ")";
} else if (propertyType.IsEnum) {
return propertyType.FullName + "." + propertyValue.ToString();
} else if (propertyType == typeof(Cursor)) {

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/EnabledSetUsingPropertyDescriptorTestFixture.cs

@ -57,9 +57,9 @@ namespace PythonBinding.Tests.Designer @@ -57,9 +57,9 @@ namespace PythonBinding.Tests.Designer
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.Enabled = False\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateAccessibleRoleFormTestFixture.cs

@ -41,8 +41,8 @@ namespace PythonBinding.Tests.Designer @@ -41,8 +41,8 @@ namespace PythonBinding.Tests.Designer
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.AccessibleRole = System.Windows.Forms.AccessibleRole.None\r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.Visible = False\r\n" +
" self.ResumeLayout(False)\r\n" +

22
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateAutoScaleModeFormTestFixture.cs

@ -6,6 +6,8 @@ @@ -6,6 +6,8 @@
// </file>
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
@ -34,11 +36,21 @@ namespace PythonBinding.Tests.Designer @@ -34,11 +36,21 @@ namespace PythonBinding.Tests.Designer
[TestFixtureSetUp]
public void SetUpFixture()
{
using (Form form = new Form()) {
form.Name = "MainForm";
using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
Form form = (Form)host.RootComponent;
form.ClientSize = new Size(284, 264);
form.AutoScaleMode = AutoScaleMode.Font;
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
namePropertyDescriptor.SetValue(form, "MainForm");
PropertyDescriptor autoScaleModeDescriptor = descriptors.Find("AutoScaleMode", false);
autoScaleModeDescriptor.SetValue(form, AutoScaleMode.Font);
PropertyDescriptor autoScaleDimensionsDescriptor = descriptors.Find("AutoScaleDimensions", false);
autoScaleDimensionsDescriptor.SetValue(form, new SizeF(6F, 13F));
string indentString = " ";
PythonForm pythonForm = new PythonForm(indentString);
generatedPythonCode = pythonForm.GenerateInitializeComponentMethod(form);
@ -53,10 +65,10 @@ namespace PythonBinding.Tests.Designer @@ -53,10 +65,10 @@ namespace PythonBinding.Tests.Designer
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.AutoScaleDimensions = System.Drawing.SizeF(6, 13)\r\n" +
" self.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font\r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.Visible = False\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateDoubleBufferedFormTestFixture.cs

@ -56,9 +56,9 @@ namespace PythonBinding.Tests.Designer @@ -56,9 +56,9 @@ namespace PythonBinding.Tests.Designer
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.DoubleBuffered = True\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormPaddingTestFixture.cs

@ -43,8 +43,8 @@ namespace PythonBinding.Tests.Designer @@ -43,8 +43,8 @@ namespace PythonBinding.Tests.Designer
" # \r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.Visible = False\r\n" +
" self.Padding = System.Windows.Forms.Padding(10, 20, 15, 18)\r\n" +
" self.Visible = False\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateImeModeFormTestFixture.cs

@ -50,9 +50,9 @@ namespace PythonBinding.Tests.Designer @@ -50,9 +50,9 @@ namespace PythonBinding.Tests.Designer
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.ImeMode = System.Windows.Forms.ImeMode.Alpha\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";

4
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateMinSizeFormTestFixture.cs

@ -48,10 +48,10 @@ namespace PythonBinding.Tests.Designer @@ -48,10 +48,10 @@ namespace PythonBinding.Tests.Designer
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.MinimumSize = System.Drawing.Size(100, 200)\r\n" +
" self.AutoScrollMargin = System.Drawing.Size(11, 22)\r\n" +
" self.AutoScrollMinSize = System.Drawing.Size(10, 20)\r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.MinimumSize = System.Drawing.Size(100, 200)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.Visible = False\r\n" +
" self.ResumeLayout(False)\r\n" +

4
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateRightToLeftFormTestFixture.cs

@ -50,9 +50,9 @@ namespace PythonBinding.Tests.Designer @@ -50,9 +50,9 @@ namespace PythonBinding.Tests.Designer
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.RightToLeft = System.Windows.Forms.RightToLeft.Yes\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.RightToLeft = System.Windows.Forms.RightToLeft.Yes\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";

84
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/IgnoreDesignTimePropertiesTestFixture.cs

@ -6,8 +6,10 @@ @@ -6,8 +6,10 @@
// </file>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
@ -27,22 +29,24 @@ namespace PythonBinding.Tests.Designer @@ -27,22 +29,24 @@ namespace PythonBinding.Tests.Designer
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.ClientSize = System.Drawing.Size(200, 300)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";
string generatedCode;
PropertyDescriptorCollection propertyDescriptors;
/// <summary>
/// Loads a form into a DesignSurface and checks that the PythonForm does not try to
/// After a form is loaded onto a DesignSurface this checks that the PythonForm does not try to
/// add design time properties and does not throw a null reference exception.
/// </summary>
[Test]
public void DesignTimePropertyIsIgnored()
[TestFixtureSetUp]
public void SetUpFixture()
{
using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
Form form = (Form)host.RootComponent;
form.AllowDrop = false;
form.ClientSize = new Size(200, 300);
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
@ -50,10 +54,74 @@ namespace PythonBinding.Tests.Designer @@ -50,10 +54,74 @@ namespace PythonBinding.Tests.Designer
namePropertyDescriptor.SetValue(form, "MainForm");
PythonForm pythonForm = new PythonForm(" ");
string code = pythonForm.GenerateInitializeComponentMethod(form);
Assert.AreEqual(expectedCode, code);
generatedCode = pythonForm.GenerateInitializeComponentMethod(form);
propertyDescriptors = pythonForm.GetSerializableProperties(form);
}
}
[Test]
public void DesignTimePropertyIsIgnoredInGeneratedCode()
{
Assert.AreEqual(expectedCode, generatedCode);
}
[Test]
public void AtLeastOneSerializableProperty()
{
Assert.IsTrue(propertyDescriptors.Count > 0);
}
[Test]
public void NoLockedProperty()
{
Assert.IsFalse(ContainsProperty(propertyDescriptors, "Locked"), "Locked property is not expected.");
}
[Test]
public void NoTopLevelPropertyWhichHasDesignTimeVisibilityHidden()
{
Assert.IsFalse(ContainsProperty(propertyDescriptors, "TopLevel"), "TopLevel property is not expected.");
}
[Test]
public void NoTagPropertyWhichHasDefaultValue()
{
Assert.IsFalse(ContainsProperty(propertyDescriptors, "Tag"), "Tag property is not expected.");
}
[Test]
public void PropertiesAreSorted()
{
List<string> strings = new List<string>();
List<string> unsortedStrings = new List<string>();
foreach (PropertyDescriptor p in propertyDescriptors) {
strings.Add(p.Name);
unsortedStrings.Add(p.Name);
}
strings.Sort();
Assert.AreEqual(strings, unsortedStrings);
}
static bool ContainsProperty(PropertyDescriptorCollection propertyDescriptors, string name)
{
foreach (PropertyDescriptor property in propertyDescriptors) {
if (property.Name == name) {
return true;
}
}
return false;
}
bool HasDesignOnlyAttribute(AttributeCollection attributes)
{
foreach (Attribute a in attributes) {
if (a is DesignOnlyAttribute) {
return true;
}
}
return false;
}
}
}

271
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/IsDefaultPropertyValueTests.cs

@ -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));
}
}
}

7
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/PythonPropertyAssignmentToStringTests.cs

@ -40,5 +40,12 @@ namespace PythonBinding.Tests.Designer @@ -40,5 +40,12 @@ namespace PythonBinding.Tests.Designer
System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
}
}
[Test]
public void SizeFToString()
{
SizeF sizeF = new SizeF(4, 10);
Assert.AreEqual("System.Drawing.SizeF(4, 10)", PythonPropertyValueAssignment.ToString(sizeF));
}
}
}

76
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/ShouldSerializeTests.cs

@ -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));
}
}
}

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

@ -165,7 +165,6 @@ @@ -165,7 +165,6 @@
<Compile Include="Designer\GeneratorMergeFindsInitializeComponentsTestFixture.cs" />
<Compile Include="Designer\IgnoreDesignTimePropertiesTestFixture.cs" />
<Compile Include="Designer\InsertNewEventHandlerTestFixture.cs" />
<Compile Include="Designer\IsDefaultPropertyValueTests.cs" />
<Compile Include="Designer\IsFullyQualifiedBaseClassFormDesignableTestFixture.cs" />
<Compile Include="Designer\LoadAccessibleRoleTestFixture.cs" />
<Compile Include="Designer\LoadColorFromArgbTestFixture.cs" />
@ -186,7 +185,6 @@ @@ -186,7 +185,6 @@
<Compile Include="Designer\PythonDesignerLoaderProviderTestFixture.cs" />
<Compile Include="Designer\PythonDesignerLoaderTestFixture.cs" />
<Compile Include="Designer\PythonPropertyAssignmentToStringTests.cs" />
<Compile Include="Designer\ShouldSerializeTests.cs" />
<Compile Include="Designer\TextBoxNotAddedToFormTestFixture.cs" />
<Compile Include="Designer\TextEditorIndentPassedToGeneratorTestFixture.cs" />
<Compile Include="Designer\UnknownTypeTestFixture.cs" />

Loading…
Cancel
Save