From ccddc2cd65f291a9d080c254256a8bf9024e0bb3 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Tue, 28 Jul 2009 18:59:09 +0000 Subject: [PATCH] Python forms designer now creates an instance of the form's base class instead of just a Form or UserControl when loading a form in the designer. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4561 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/PythonComponentWalker.cs | 8 +++- .../FormBaseClassCreatedOnLoadTestFixture.cs | 47 +++++++++++++++++++ .../Designer/LoadSimpleFormTestFixture.cs | 11 ++++- .../Test/PythonBinding.Tests.csproj | 1 + .../Test/Utils/MockComponentCreator.cs | 12 +++++ 5 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/FormBaseClassCreatedOnLoadTestFixture.cs diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs index 6940909df8..23e645bf49 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs @@ -33,8 +33,7 @@ namespace ICSharpCode.PythonBinding PythonCodeDeserializer deserializer; ClassDefinition classDefinition; - public PythonComponentWalker(IComponentCreator componentCreator) - { + public PythonComponentWalker(IComponentCreator componentCreator) { this.componentCreator = componentCreator; deserializer = new PythonCodeDeserializer(componentCreator); } @@ -232,6 +231,11 @@ namespace ICSharpCode.PythonBinding Type GetComponentType() { string baseClass = GetBaseClassName(classDefinition); + Type type = componentCreator.GetType(baseClass); + if (type != null) { + return type; + } + if (baseClass.Contains("UserControl")) { return typeof(UserControl); } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/FormBaseClassCreatedOnLoadTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/FormBaseClassCreatedOnLoadTestFixture.cs new file mode 100644 index 0000000000..63e2d83152 --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/FormBaseClassCreatedOnLoadTestFixture.cs @@ -0,0 +1,47 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.Design; +using System.Drawing; +using System.IO; +using System.Windows.Forms; + +using ICSharpCode.PythonBinding; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + [TestFixture] + public class FormBaseClassCreatedOnLoadTestFixture : LoadFormTestFixtureBase + { + public override string PythonCode { + get { + ComponentCreator.AddType("FormBase.FormBase", typeof(Component)); + + return "class TestForm(FormBase.FormBase):\r\n" + + " def InitializeComponent(self):\r\n" + + " pass"; + } + } + + [Test] + public void BaseClassNamePassedAsGetTypeParam() + { + Assert.AreEqual("FormBase.FormBase", ComponentCreator.TypeNames[0]); + } + + [Test] + public void BaseClassTypePassedToCreateComponent() + { + Assert.AreEqual(typeof(Component).FullName, ComponentCreator.CreatedComponents[0].TypeName); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs index 4869067357..d50ca01005 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs @@ -74,15 +74,22 @@ namespace PythonBinding.Tests.Designer Size size = new Size(300, 400); Assert.AreEqual(size, Form.ClientSize); } + + [Test] + public void BaseClassTypeNameLookedUp() + { + Assert.AreEqual("System.Windows.Forms.Form", ComponentCreator.TypeNames[0]); + } /// /// The System.Drawing.Size type name should have been looked up by the PythonFormWalker when - /// parsing the InitializeComponent method. + /// parsing the InitializeComponent method. Note that this is the second type that is looked up. + /// The first lookup is the base class type. /// [Test] public void TypeNameLookedUp() { - Assert.AreEqual("System.Drawing.Size", ComponentCreator.TypeNames[0]); + Assert.AreEqual("System.Drawing.Size", ComponentCreator.TypeNames[1]); } [Test] diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index 807dbdb439..b68712c825 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -175,6 +175,7 @@ + diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs index 6c8c7d6c08..e5dae079e9 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockComponentCreator.cs @@ -34,6 +34,7 @@ namespace PythonBinding.Tests.Utils bool getResourceWriterCalled; CultureInfo cultureInfoPassedToGetResourceWriter; IResourceReader resourceReader; + Dictionary types = new Dictionary(); public MockComponentCreator() { @@ -109,6 +110,14 @@ namespace PythonBinding.Tests.Utils return o; } + /// + /// Adds a type that can be returned from the GetType method. + /// + public void AddType(string name, Type type) + { + types.Add(name, type); + } + public Type GetType(string typeName) { typeNames.Add(typeName); @@ -125,6 +134,9 @@ namespace PythonBinding.Tests.Utils if (type == null) { type = typeof(Component).Assembly.GetType(typeName); } + if (type == null) { + types.TryGetValue(typeName, out type); + } return type; }