~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateFormResourcesTestFixture.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.Globalization;
 
11
using System.Windows.Forms;
 
12
 
 
13
using ICSharpCode.PythonBinding;
 
14
using ICSharpCode.Scripting.Tests.Utils;
 
15
using NUnit.Framework;
 
16
using PythonBinding.Tests.Utils;
 
17
 
 
18
namespace PythonBinding.Tests.Designer
 
19
{
 
20
        [TestFixture]
 
21
        public class GenerateFormResourceTestFixture
 
22
        {
 
23
                MockResourceWriter resourceWriter;
 
24
                MockComponentCreator componentCreator;
 
25
                string generatedPythonCode;
 
26
                Bitmap bitmap;
 
27
                Icon icon;
 
28
                
 
29
                [TestFixtureSetUp]
 
30
                public void SetUpFixture()
 
31
                {
 
32
                        resourceWriter = new MockResourceWriter();
 
33
                        componentCreator = new MockComponentCreator();
 
34
                        componentCreator.SetResourceWriter(resourceWriter);
 
35
                        
 
36
                        using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
 
37
                                IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
 
38
                                IEventBindingService eventBindingService = new MockEventBindingService(host);
 
39
                                host.AddService(typeof(IResourceService), componentCreator);
 
40
                                
 
41
                                Form form = (Form)host.RootComponent;
 
42
                                form.ClientSize = new Size(200, 300);
 
43
 
 
44
                                PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
 
45
                                PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
 
46
                                namePropertyDescriptor.SetValue(form, "MainForm");
 
47
                                
 
48
                                // Set bitmap as form background image.
 
49
                                bitmap = new Bitmap(10, 10);
 
50
                                form.BackgroundImage = bitmap;
 
51
                                
 
52
                                icon = new Icon(typeof(GenerateFormResourceTestFixture), "App.ico");
 
53
                                form.Icon = icon;
 
54
                                
 
55
                                DesignerSerializationManager serializationManager = new DesignerSerializationManager(host);
 
56
                                using (serializationManager.CreateSession()) {                                  
 
57
                                        PythonCodeDomSerializer serializer = new PythonCodeDomSerializer("    ");
 
58
                                        generatedPythonCode = serializer.GenerateInitializeComponentMethodBody(host, serializationManager, "RootNamespace", 1);
 
59
                                }
 
60
                        }
 
61
                }
 
62
                
 
63
                [Test]
 
64
                public void TearDownFixture()
 
65
                {
 
66
                        bitmap.Dispose();
 
67
                        icon.Dispose();
 
68
                }
 
69
                
 
70
                [Test]
 
71
                public void GeneratedCode()
 
72
                {
 
73
                        string expectedCode = "    resources = System.Resources.ResourceManager(\"RootNamespace.MainForm\", System.Reflection.Assembly.GetEntryAssembly())\r\n" +
 
74
                                                                "    self.SuspendLayout()\r\n" +
 
75
                                                                "    # \r\n" +
 
76
                                                                "    # MainForm\r\n" +
 
77
                                                                "    # \r\n" +
 
78
                                                                "    self.BackgroundImage = resources.GetObject(\"$this.BackgroundImage\")\r\n" +
 
79
                                                                "    self.ClientSize = System.Drawing.Size(200, 300)\r\n" +
 
80
                                                                "    self.Icon = resources.GetObject(\"$this.Icon\")\r\n" +
 
81
                                                                "    self.Name = \"MainForm\"\r\n" +
 
82
                                                                "    self.ResumeLayout(False)\r\n";
 
83
                        
 
84
                        Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode);
 
85
                }
 
86
                
 
87
                [Test]
 
88
                public void BitmapAddedToResourceWriter()
 
89
                {
 
90
                        Assert.IsTrue(Object.ReferenceEquals(bitmap, resourceWriter.GetResource("$this.BackgroundImage")));
 
91
                }
 
92
                
 
93
                [Test]
 
94
                public void IconAddedToResourceWriter()
 
95
                {
 
96
                        Assert.IsTrue(Object.ReferenceEquals(icon, resourceWriter.GetResource("$this.Icon")));
 
97
                }
 
98
        }
 
99
}