~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateCustomCollectionItemsTestFixture.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.Collections.Generic;
 
6
using System.ComponentModel;
 
7
using System.ComponentModel.Design;
 
8
using System.ComponentModel.Design.Serialization;
 
9
using System.Drawing;
 
10
using System.Windows.Forms;
 
11
 
 
12
using ICSharpCode.PythonBinding;
 
13
using ICSharpCode.Scripting.Tests.Utils;
 
14
using NUnit.Framework;
 
15
using PythonBinding.Tests.Utils;
 
16
 
 
17
namespace PythonBinding.Tests.Designer
 
18
{
 
19
        /// <summary>
 
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.
 
23
        /// </summary>
 
24
        [TestFixture]
 
25
        public class GenerateCustomCollectionItemsTestFixture
 
26
        {
 
27
                string generatedPythonCode;
 
28
                
 
29
                [TestFixtureSetUp]
 
30
                public void SetUpFixture()
 
31
                {
 
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);
 
37
 
 
38
                                PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
 
39
                                PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
 
40
                                namePropertyDescriptor.SetValue(form, "MainForm");
 
41
                                
 
42
                                // Add custom control
 
43
                                CustomUserControl userControl = (CustomUserControl)host.CreateComponent(typeof(CustomUserControl), "userControl1");
 
44
                                userControl.Location = new Point(0, 0);
 
45
                                userControl.ClientSize = new Size(200, 100);
 
46
                                
 
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);
 
54
                                        
 
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);
 
60
                                
 
61
                                        PythonCodeDomSerializer serializer = new PythonCodeDomSerializer("    ");
 
62
                                        generatedPythonCode = serializer.GenerateInitializeComponentMethodBody(host, serializationManager, String.Empty, 1);
 
63
                                }       
 
64
                        }
 
65
                }
 
66
                
 
67
                [Test]
 
68
                public void GeneratedCode()
 
69
                {
 
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" +
 
76
                                                                "    # \r\n" +
 
77
                                                                "    # userControl1\r\n" +
 
78
                                                                "    # \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" +
 
82
                                                                "        [fooItem1,\r\n" +
 
83
                                                                "        fooItem2]))\r\n" +
 
84
                                                                "    self._userControl1.Location = System.Drawing.Point(0, 0)\r\n" +
 
85
                                                                "    self._userControl1.Name = \"userControl1\"\r\n" +
 
86
                                                                "    # \r\n" +
 
87
                                                                "    # \r\n" +
 
88
                                                                "    # \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" +
 
92
                                                                "        [barItem1,\r\n" +
 
93
                                                                "        barItem2]))\r\n" +
 
94
                                                                "    self._userControl1.Size = System.Drawing.Size(200, 100)\r\n" +
 
95
                                                                "    self._userControl1.TabIndex = 0\r\n" +
 
96
                                                                "    # \r\n" +
 
97
                                                                "    # MainForm\r\n" +
 
98
                                                                "    # \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";
 
103
                        
 
104
                        Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode);
 
105
                }
 
106
        }
 
107
}