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.Collections.Generic;
6
using System.ComponentModel;
7
using System.ComponentModel.Design;
8
using System.ComponentModel.Design.Serialization;
10
using System.Windows.Forms;
12
using ICSharpCode.PythonBinding;
13
using ICSharpCode.Scripting.Tests.Utils;
14
using NUnit.Framework;
15
using PythonBinding.Tests.Utils;
17
namespace PythonBinding.Tests.Designer
20
/// Tests that a custom collection class generates the correct code.
21
/// The collection class should be a property of a custom component or user control
22
/// and it should be marked with DesignerSerializationVisibility.Content.
25
public class GenerateCustomCollectionItemsTestFixture
27
string generatedPythonCode;
30
public void SetUpFixture()
32
using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
33
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
34
IEventBindingService eventBindingService = new MockEventBindingService(host);
35
Form form = (Form)host.RootComponent;
36
form.ClientSize = new Size(200, 300);
38
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
39
PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
40
namePropertyDescriptor.SetValue(form, "MainForm");
43
CustomUserControl userControl = (CustomUserControl)host.CreateComponent(typeof(CustomUserControl), "userControl1");
44
userControl.Location = new Point(0, 0);
45
userControl.ClientSize = new Size(200, 100);
47
DesignerSerializationManager designerSerializationManager = new DesignerSerializationManager(host);
48
IDesignerSerializationManager serializationManager = (IDesignerSerializationManager)designerSerializationManager;
49
using (designerSerializationManager.CreateSession()) {
50
FooItem fooItem = (FooItem)serializationManager.CreateInstance(typeof(FooItem), new object[] {"aa"}, "fooItem1", false);
51
userControl.FooItems.Add(fooItem);
52
fooItem = (FooItem)serializationManager.CreateInstance(typeof(FooItem), new object[] {"bb"}, "fooItem2", false);
53
userControl.FooItems.Add(fooItem);
55
BarItem barItem = (BarItem)serializationManager.CreateInstance(typeof(BarItem), new object[] {"cc"}, "barItem1", false);
56
userControl.ParentComponent.ParentBarItems.Add(barItem);
57
barItem = (BarItem)serializationManager.CreateInstance(typeof(BarItem), new object[] {"dd"}, "barItem2", false);
58
userControl.ParentComponent.ParentBarItems.Add(barItem);
59
form.Controls.Add(userControl);
61
PythonCodeDomSerializer serializer = new PythonCodeDomSerializer(" ");
62
generatedPythonCode = serializer.GenerateInitializeComponentMethodBody(host, serializationManager, String.Empty, 1);
68
public void GeneratedCode()
70
string expectedCode = " fooItem1 = ICSharpCode.Scripting.Tests.Utils.FooItem()\r\n" +
71
" fooItem2 = ICSharpCode.Scripting.Tests.Utils.FooItem()\r\n" +
72
" barItem1 = ICSharpCode.Scripting.Tests.Utils.BarItem()\r\n" +
73
" barItem2 = ICSharpCode.Scripting.Tests.Utils.BarItem()\r\n" +
74
" self._userControl1 = ICSharpCode.Scripting.Tests.Utils.CustomUserControl()\r\n" +
75
" self.SuspendLayout()\r\n" +
77
" # userControl1\r\n" +
79
" fooItem1.Text = \"aa\"\r\n" +
80
" fooItem2.Text = \"bb\"\r\n" +
81
" self._userControl1.FooItems.AddRange(System.Array[ICSharpCode.Scripting.Tests.Utils.FooItem](\r\n" +
84
" self._userControl1.Location = System.Drawing.Point(0, 0)\r\n" +
85
" self._userControl1.Name = \"userControl1\"\r\n" +
89
" barItem1.Text = \"cc\"\r\n" +
90
" barItem2.Text = \"dd\"\r\n" +
91
" self._userControl1.ParentComponent.ParentBarItems.AddRange(System.Array[ICSharpCode.Scripting.Tests.Utils.BarItem](\r\n" +
94
" self._userControl1.Size = System.Drawing.Size(200, 100)\r\n" +
95
" self._userControl1.TabIndex = 0\r\n" +
99
" self.ClientSize = System.Drawing.Size(200, 300)\r\n" +
100
" self.Controls.Add(self._userControl1)\r\n" +
101
" self.Name = \"MainForm\"\r\n" +
102
" self.ResumeLayout(False)\r\n";
104
Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode);