1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.ComponentModel;
6
using System.ComponentModel.Design;
7
using System.ComponentModel.Design.Serialization;
9
using System.Windows.Forms;
11
using ICSharpCode.PythonBinding;
12
using ICSharpCode.Scripting.Tests.Utils;
13
using NUnit.Framework;
14
using PythonBinding.Tests.Utils;
16
namespace PythonBinding.Tests.Designer
19
public class GeneratePanelFormTestFixture
21
string generatedPythonCode;
24
public void SetUpFixture()
26
using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
27
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
28
IEventBindingService eventBindingService = new MockEventBindingService(host);
29
host.AddService(typeof(IEventBindingService), eventBindingService);
31
Form form = (Form)host.RootComponent;
32
form.ClientSize = new Size(284, 264);
34
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
35
PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
36
namePropertyDescriptor.SetValue(form, "MainForm");
38
Panel panel = (Panel)host.CreateComponent(typeof(Panel), "panel1");
39
panel.Location = new Point(10, 15);
40
panel.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
42
panel.Size = new Size(100, 120);
43
TextBox textBox = (TextBox)host.CreateComponent(typeof(TextBox), "textBox1");
44
textBox.Location = new Point(5, 5);
46
textBox.Size = new Size(110, 20);
47
panel.Controls.Add(textBox);
49
// Add an event handler to the panel to check that this code is generated
50
// before the text box is initialized.
51
EventDescriptorCollection events = TypeDescriptor.GetEvents(panel);
52
EventDescriptor clickEvent = events.Find("Click", false);
53
PropertyDescriptor clickEventProperty = eventBindingService.GetEventProperty(clickEvent);
54
clickEventProperty.SetValue(panel, "Panel1Click");
56
form.Controls.Add(panel);
58
DesignerSerializationManager serializationManager = new DesignerSerializationManager(host);
59
using (serializationManager.CreateSession()) {
60
PythonCodeDomSerializer serializer = new PythonCodeDomSerializer(" ");
61
generatedPythonCode = serializer.GenerateInitializeComponentMethodBody(host, serializationManager, String.Empty, 1);
67
public void GeneratedCode()
69
string expectedCode = " self._panel1 = System.Windows.Forms.Panel()\r\n" +
70
" self._textBox1 = System.Windows.Forms.TextBox()\r\n" +
71
" self._panel1.SuspendLayout()\r\n" +
72
" self.SuspendLayout()\r\n" +
76
" self._panel1.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right\r\n" +
77
" self._panel1.Controls.Add(self._textBox1)\r\n" +
78
" self._panel1.Location = System.Drawing.Point(10, 15)\r\n" +
79
" self._panel1.Name = \"panel1\"\r\n" +
80
" self._panel1.Size = System.Drawing.Size(100, 120)\r\n" +
81
" self._panel1.TabIndex = 0\r\n" +
82
" self._panel1.Click += self.Panel1Click\r\n" +
86
" self._textBox1.Location = System.Drawing.Point(5, 5)\r\n" +
87
" self._textBox1.Name = \"textBox1\"\r\n" +
88
" self._textBox1.Size = System.Drawing.Size(110, 20)\r\n" +
89
" self._textBox1.TabIndex = 0\r\n" +
93
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
94
" self.Controls.Add(self._panel1)\r\n" +
95
" self.Name = \"MainForm\"\r\n" +
96
" self._panel1.ResumeLayout(False)\r\n" +
97
" self._panel1.PerformLayout()\r\n" +
98
" self.ResumeLayout(False)\r\n";
100
Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode);