importing messaging-2008 branch to trunk.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / UserControlTest.cs
1 //
2 // UserControlTest.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Daniel Nauck
24 //
25 // Authors:
26 //      Daniel Nauck    (dna(at)mono-project(dot)de)
27
28
29 using System;
30 using System.Windows.Forms;
31 using System.Drawing;
32 using System.Reflection;
33 using NUnit.Framework;
34 using System.Collections;
35 using System.ComponentModel;
36
37 namespace MonoTests.System.Windows.Forms
38 {
39         [TestFixture]
40         public class UserControlTest : TestHelper
41         {
42                 UserControl uc = null;
43
44                 [SetUp]
45                 protected override void SetUp () {
46                         uc = new UserControl();
47                         base.SetUp ();
48                 }
49
50                 [Test]
51                 public void PropertyTest()
52                 {
53                         Assert.AreEqual(string.Empty, uc.Text, "#A1");
54
55 #if NET_2_0
56                         Assert.AreEqual(BorderStyle.None, uc.BorderStyle, "#A2");
57                         uc.BorderStyle = BorderStyle.Fixed3D;
58                         Assert.AreEqual(BorderStyle.Fixed3D, uc.BorderStyle, "#A3");
59                         uc.BorderStyle = BorderStyle.FixedSingle;
60                         Assert.AreEqual(BorderStyle.FixedSingle, uc.BorderStyle, "#A4");
61                         uc.BorderStyle = BorderStyle.None;
62                         Assert.AreEqual(BorderStyle.None, uc.BorderStyle, "#A5");
63 #endif
64                 }
65
66 #if NET_2_0
67                 [Test]
68                 [ExpectedException(typeof(InvalidEnumArgumentException))]
69                 public void BorderStyleInvalidEnumArgumentException()
70                 {
71                         uc.BorderStyle = (BorderStyle) 9999;
72                 }
73                 
74                 [Test]
75                 public void MethodCreateParams ()
76                 {
77                         ExposeProtectedProperties uc = new ExposeProtectedProperties ();
78
79                         Assert.AreEqual (WindowStyles.WS_TILED | WindowStyles.WS_MAXIMIZEBOX | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD, (WindowStyles)uc.CreateParams.Style, "D1");
80                         Assert.AreEqual (WindowExStyles.WS_EX_CONTROLPARENT, (WindowExStyles)uc.CreateParams.ExStyle, "D2");
81                 }
82
83                 private class ExposeProtectedProperties : UserControl
84                 {
85                         public new CreateParams CreateParams { get { return base.CreateParams; } }
86                 }
87
88                 [Test]
89                 public void AutoSize ()
90                 {
91                         Form f = new Form ();
92                         f.ShowInTaskbar = false;
93
94                         Panel p = new Panel ();
95                         p.AutoSize = true;
96                         f.Controls.Add (p);
97
98                         Button b = new Button ();
99                         b.Size = new Size (200, 200);
100                         b.Location = new Point (200, 200);
101                         p.Controls.Add (b);
102
103                         f.Show ();
104
105                         Assert.AreEqual (new Size (403, 403), p.ClientSize, "A1");
106
107                         p.Controls.Remove (b);
108                         Assert.AreEqual (new Size (200, 100), p.ClientSize, "A2");
109
110                         p.AutoSizeMode = AutoSizeMode.GrowAndShrink;
111                         Assert.AreEqual (new Size (0, 0), p.ClientSize, "A3");
112                         
113                         f.Close ();
114                 }
115
116                 [Test]
117                 public void PreferredSize ()
118                 {
119                         Form f = new Form ();
120                         f.ShowInTaskbar = false;
121
122                         UserControl p = new UserControl ();
123                         f.Controls.Add (p);
124
125                         Button b1 = new Button ();
126                         b1.Size = new Size (200, 200);
127                         b1.Dock = DockStyle.Fill;
128                         p.Controls.Add (b1);
129
130                         Button b = new Button ();
131                         b.Size = new Size (100, 100);
132                         b.Dock = DockStyle.Top;
133                         p.Controls.Add (b);
134                         
135                         f.Show ();
136                         
137                         Assert.AreEqual (new Size (0, 100), p.PreferredSize, "A1");
138                         
139                         b1.Dock = DockStyle.Left;
140                         Assert.AreEqual (new Size (200, 100), p.PreferredSize, "A2");
141
142                         b1.Dock = DockStyle.None;
143                         Assert.AreEqual (new Size (203, 203), p.PreferredSize, "A3");
144
145                         b1.Dock = DockStyle.Fill;
146                         b.Dock = DockStyle.Fill;
147                         Assert.AreEqual (new Size (0, 0), p.PreferredSize, "A4");
148                         
149                         b1.Dock = DockStyle.Top;
150                         b.Dock = DockStyle.Left;
151
152                         Assert.AreEqual (new Size (100, 200), p.PreferredSize, "A5");
153                 
154                         Button b2 = new Button ();
155                         b2.Size = new Size (50, 50);
156                         p.Controls.Add (b2);
157
158                         Assert.AreEqual (new Size (100, 200), p.PreferredSize, "A6");
159                         
160                         b2.Left = 300;
161                         Assert.AreEqual (new Size (353, 200), p.PreferredSize, "A7");
162
163                         b2.Top = 300;
164                         Assert.AreEqual (new Size (353, 353), p.PreferredSize, "A8");
165
166                         b2.Anchor = AnchorStyles.Bottom;
167                         Assert.AreEqual (new Size (100, 200), p.PreferredSize, "A9");
168
169                         b2.Anchor = AnchorStyles.Left;
170                         Assert.AreEqual (new Size (353, 353), p.PreferredSize, "A10");
171                         
172                         f.Dispose ();
173                 }
174         
175 #endif  
176         }
177 }