~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GeneratePanelFormTestFixture.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.ComponentModel;
 
6
using System.ComponentModel.Design;
 
7
using System.ComponentModel.Design.Serialization;
 
8
using System.Drawing;
 
9
using System.Windows.Forms;
 
10
 
 
11
using ICSharpCode.PythonBinding;
 
12
using ICSharpCode.Scripting.Tests.Utils;
 
13
using NUnit.Framework;
 
14
using PythonBinding.Tests.Utils;
 
15
 
 
16
namespace PythonBinding.Tests.Designer
 
17
{
 
18
        [TestFixture]
 
19
         public class GeneratePanelFormTestFixture
 
20
        {
 
21
                string generatedPythonCode;
 
22
                
 
23
                [TestFixtureSetUp]
 
24
                public void SetUpFixture()
 
25
                {
 
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);
 
30
 
 
31
                                Form form = (Form)host.RootComponent;
 
32
                                form.ClientSize = new Size(284, 264);
 
33
                                
 
34
                                PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
 
35
                                PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
 
36
                                namePropertyDescriptor.SetValue(form, "MainForm");
 
37
                                
 
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;
 
41
                                panel.TabIndex = 0;
 
42
                                panel.Size = new Size(100, 120);
 
43
                                TextBox textBox = (TextBox)host.CreateComponent(typeof(TextBox), "textBox1");
 
44
                                textBox.Location = new Point(5, 5);
 
45
                                textBox.TabIndex = 0;
 
46
                                textBox.Size = new Size(110, 20);
 
47
                                panel.Controls.Add(textBox);
 
48
                                
 
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");
 
55
 
 
56
                                form.Controls.Add(panel);
 
57
                                
 
58
                                DesignerSerializationManager serializationManager = new DesignerSerializationManager(host);
 
59
                                using (serializationManager.CreateSession()) {                                  
 
60
                                        PythonCodeDomSerializer serializer = new PythonCodeDomSerializer("    ");
 
61
                                        generatedPythonCode = serializer.GenerateInitializeComponentMethodBody(host, serializationManager, String.Empty, 1);
 
62
                                }
 
63
                        }
 
64
                }
 
65
                                
 
66
                [Test]
 
67
                public void GeneratedCode()
 
68
                {
 
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" +
 
73
                                                                "    # \r\n" +
 
74
                                                                "    # panel1\r\n" +
 
75
                                                                "    # \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" +
 
83
                                                                "    # \r\n" +
 
84
                                                                "    # textBox1\r\n" +
 
85
                                                                "    # \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" +
 
90
                                                                "    # \r\n" +
 
91
                                                                "    # MainForm\r\n" +
 
92
                                                                "    # \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";
 
99
                        
 
100
                        Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode);
 
101
                }
 
102
        }
 
103
}