Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3936 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
17 changed files with 479 additions and 16 deletions
@ -0,0 +1,78 @@
@@ -0,0 +1,78 @@
|
||||
// <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.ComponentModel.Design; |
||||
using System.ComponentModel.Design.Serialization; |
||||
using System.Drawing; |
||||
using System.Windows.Forms; |
||||
using ICSharpCode.PythonBinding; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Designer |
||||
{ |
||||
/// <summary>
|
||||
/// Tests that an event is wired to its event handler after the user specifies an event handler
|
||||
/// method in the property grid.
|
||||
/// </summary>
|
||||
[TestFixture] |
||||
public class GenerateEventHandlerFormTestFixture |
||||
{ |
||||
string generatedPythonCode; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void SetUpFixture() |
||||
{ |
||||
using (DesignSurface designSurface = new DesignSurface(typeof(Form))) { |
||||
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost)); |
||||
IEventBindingService eventBindingService = new MockEventBindingService(host); |
||||
Form form = (Form)host.RootComponent; |
||||
form.ClientSize = new Size(200, 300); |
||||
|
||||
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form); |
||||
PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false); |
||||
namePropertyDescriptor.SetValue(form, "MainForm"); |
||||
|
||||
// Simulate giving a name to the Load event handler in the property grid.
|
||||
EventDescriptorCollection events = TypeDescriptor.GetEvents(form); |
||||
EventDescriptor loadEvent = events.Find("Load", false); |
||||
PropertyDescriptor loadEventProperty = eventBindingService.GetEventProperty(loadEvent); |
||||
loadEventProperty.SetValue(form, "MainFormLoad"); |
||||
|
||||
// Add a second event handler method to check that the events are sorted alphabetically
|
||||
// before the InitializeComponent method is generated.
|
||||
EventDescriptor closedEvent = events.Find("FormClosed", false); |
||||
PropertyDescriptor closedEventProperty = eventBindingService.GetEventProperty(closedEvent); |
||||
closedEventProperty.SetValue(form, "MainFormClosed"); |
||||
|
||||
PythonForm pythonForm = new PythonForm(" "); |
||||
generatedPythonCode = pythonForm.GenerateInitializeComponentMethod(form); |
||||
} |
||||
} |
||||
|
||||
[Test] |
||||
public void GeneratedCode() |
||||
{ |
||||
string expectedCode = "def InitializeComponent(self):\r\n" + |
||||
" self.SuspendLayout()\r\n" + |
||||
" # \r\n" + |
||||
" # MainForm\r\n" + |
||||
" # \r\n" + |
||||
" self.ClientSize = System.Drawing.Size(200, 300)\r\n" + |
||||
" self.Name = \"MainForm\"\r\n" + |
||||
" self.FormClosed += self.MainFormClosed\r\n" + |
||||
" self.Load += self.MainFormLoad\r\n" + |
||||
" self.ResumeLayout(False)\r\n" + |
||||
" self.PerformLayout()\r\n"; |
||||
|
||||
Assert.AreEqual(expectedCode, generatedPythonCode); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,71 @@
@@ -0,0 +1,71 @@
|
||||
// <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 ICSharpCode.PythonBinding; |
||||
using ICSharpCode.FormsDesigner; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Designer |
||||
{ |
||||
/// <summary>
|
||||
/// Tests the PythonDesignerGenerator adds an extra new line between the previous event handler
|
||||
/// and the new one inserted.
|
||||
/// </summary>
|
||||
[TestFixture] |
||||
public class InsertSecondEventHandlerTestFixture |
||||
{ |
||||
string file; |
||||
int position; |
||||
bool insertedEventHandler; |
||||
MockTextEditorViewContent mockViewContent; |
||||
DerivedFormDesignerViewContent viewContent; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void SetUpFixture() |
||||
{ |
||||
DerivedPythonDesignerGenerator generator = new DerivedPythonDesignerGenerator(); |
||||
mockViewContent = new MockTextEditorViewContent(); |
||||
viewContent = new DerivedFormDesignerViewContent(mockViewContent, new MockOpenedFile(@"C:\Projects\Python\mainform.py")); |
||||
generator.Attach(viewContent); |
||||
viewContent.DesignerCodeFileContent = GetTextEditorCode(); |
||||
|
||||
MockEventDescriptor mockEventDescriptor = new MockEventDescriptor("Click"); |
||||
generator.InsertComponentEvent(null, mockEventDescriptor, "button1_click", String.Empty, out file, out position); |
||||
insertedEventHandler = generator.InsertComponentEvent(null, mockEventDescriptor, "button2_click", String.Empty, out file, out position); |
||||
} |
||||
|
||||
[Test] |
||||
public void ExpectedCodeAfterEventHandlerInserted() |
||||
{ |
||||
string expectedCode = GetTextEditorCode(); |
||||
string eventHandler = "\tdef button1_click(self, sender, e):\r\n" + |
||||
"\t\tpass\r\n" + |
||||
"\r\n" + |
||||
"\tdef button2_click(self, sender, e):\r\n" + |
||||
"\t\tpass"; |
||||
expectedCode = expectedCode + "\r\n" + eventHandler; |
||||
|
||||
Assert.AreEqual(expectedCode, viewContent.DesignerCodeFileContent); |
||||
} |
||||
|
||||
string GetTextEditorCode() |
||||
{ |
||||
return "from System.Windows.Forms import Form\r\n" + |
||||
"\r\n" + |
||||
"class MainForm(Form):\r\n" + |
||||
"\tdef __init__(self):\r\n" + |
||||
"\t\tself.InitializeComponents()\r\n" + |
||||
"\t\r\n" + |
||||
"\tdef InitializeComponents(self):\r\n" + |
||||
"\t\tself._button1 = System.Windows.Forms.Button()\r\n" + |
||||
"\t\tself.Controls.Add(self._button1)\r\n"; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
// <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.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 LoadEventHandlerTestFixture : LoadFormTestFixtureBase |
||||
{ |
||||
public override string PythonCode { |
||||
get { |
||||
return "class TestForm(System.Windows.Forms.Form):\r\n" + |
||||
" def InitializeComponent(self):\r\n" + |
||||
" self.SuspendLayout()\r\n" + |
||||
" # \r\n" + |
||||
" # TestForm\r\n" + |
||||
" # \r\n" + |
||||
" self.Name = \"TestForm\"\r\n" + |
||||
" self.Load += self.TestFormLoad\r\n" + |
||||
" self.ResumeLayout(False)\r\n"; |
||||
} |
||||
} |
||||
|
||||
public EventDescriptor GetLoadEventDescriptor() |
||||
{ |
||||
return TypeDescriptor.GetEvents(Form).Find("Load", true); |
||||
} |
||||
|
||||
[Test] |
||||
public void EventPropertyDescriptorValueSetToEventHandlerMethodName() |
||||
{ |
||||
EventDescriptor loadEventDescriptor = GetLoadEventDescriptor(); |
||||
PropertyDescriptor property = base.GetEventProperty(loadEventDescriptor); |
||||
Assert.AreEqual("TestFormLoad", property.GetValue(Form) as String); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
// <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; |
||||
using System.ComponentModel; |
||||
using System.ComponentModel.Design; |
||||
|
||||
namespace PythonBinding.Tests.Utils |
||||
{ |
||||
public class MockEventBindingService : EventBindingService |
||||
{ |
||||
public MockEventBindingService(IServiceProvider serviceProvider) |
||||
: base(serviceProvider) |
||||
{ |
||||
} |
||||
|
||||
public MockEventBindingService() |
||||
: this(new ServiceContainer()) |
||||
{ |
||||
} |
||||
|
||||
protected override string CreateUniqueMethodName(IComponent component, EventDescriptor e) |
||||
{ |
||||
return String.Empty; |
||||
} |
||||
|
||||
protected override ICollection GetCompatibleMethods(EventDescriptor e) |
||||
{ |
||||
return new ArrayList(); |
||||
} |
||||
|
||||
protected override bool ShowCode() |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
protected override bool ShowCode(int lineNumber) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
protected override bool ShowCode(IComponent component, EventDescriptor e, string methodName) |
||||
{ |
||||
return false; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
// <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.Windows.Forms; |
||||
|
||||
namespace PythonBinding.Tests.Utils |
||||
{ |
||||
public class MockPropertyDescriptor : PropertyDescriptor |
||||
{ |
||||
object propertyValue; |
||||
bool shouldSerializeValue; |
||||
|
||||
public MockPropertyDescriptor(string name, object value, bool shouldSerializeValue) |
||||
: base(name, null) |
||||
{ |
||||
this.propertyValue = value; |
||||
this.shouldSerializeValue = shouldSerializeValue; |
||||
} |
||||
|
||||
public override Type ComponentType { |
||||
get { return typeof(Form); } |
||||
} |
||||
|
||||
public override bool IsReadOnly { |
||||
get { return false; } |
||||
} |
||||
|
||||
public override Type PropertyType { |
||||
get { return typeof(String); } |
||||
} |
||||
|
||||
public override bool CanResetValue(object component) |
||||
{ |
||||
return true; |
||||
} |
||||
|
||||
public override object GetValue(object component) |
||||
{ |
||||
return this.propertyValue; |
||||
} |
||||
|
||||
public override void ResetValue(object component) |
||||
{ |
||||
} |
||||
|
||||
public override void SetValue(object component, object value) |
||||
{ |
||||
this.propertyValue = value; |
||||
} |
||||
|
||||
public override bool ShouldSerializeValue(object component) |
||||
{ |
||||
return shouldSerializeValue; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue