2007-05-24 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ContainerControlTest.cs
1 //
2 // ContainerControl class testing unit
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using C=System.ComponentModel;
31 using System.Security.Permissions;
32 using System.Windows.Forms;
33 using System.Collections;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Windows.Forms {
37
38         public class IContainerControlTest : Control, IContainerControl {
39
40                 public bool ActivateControl (Control active)
41                 {
42                         return true;
43                 }
44
45                 public Control  ActiveControl {
46                         get { return null; }
47                         set { ; }
48                 }
49         }
50
51         public class FormCustom: Form {
52                 public bool record;
53                 public Queue events;
54
55                 public FormCustom(string name, bool record, Queue events) {
56                         base.Name = name;
57                         this.record = record;
58                         this.events = events;
59                 }
60
61                 protected override void OnValidating(C.CancelEventArgs e) {
62                         if (this.record)
63                                 events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
64                         base.OnValidating (e);
65                 }
66
67                 protected override void OnValidated(EventArgs e) {
68                         if (this.record)
69                                 events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
70                         base.OnValidated (e);
71                 }
72
73                 protected override void OnGotFocus(EventArgs e) {
74                         if (this.record)
75                                 events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
76                         base.OnGotFocus (e);
77                 }
78
79         }
80
81         public class ContainerControlCustom: ContainerControl {
82                 public bool record;
83                 public Queue events;
84
85                 public ContainerControlCustom(string name, bool record, Queue events) {
86                         base.Name = name;
87                         this.record = record;
88                         this.events = events;
89                 }
90
91                 protected override void OnValidating(C.CancelEventArgs e) {
92                         if (this.record)
93                                 events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
94                         base.OnValidating (e);
95                 }
96
97                 protected override void OnValidated(EventArgs e) {
98                         if (this.record)
99                                 events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
100                         base.OnValidated (e);
101                 }
102                 
103                 protected override void OnGotFocus(EventArgs e) {
104                         if (this.record)
105                                 events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
106                         base.OnGotFocus (e);
107                 }
108
109                 protected override void Select(bool directed, bool forward) {
110                         if (this.record)
111                                 events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
112                         base.Select (directed, forward);
113                 }
114         }
115
116         public class UserControlCustom: UserControl {
117                 public bool record;
118                 public Queue events;
119
120                 public UserControlCustom(string name, bool record, Queue events) {
121                         base.Name = name;
122                         this.record = record;
123                         this.events = events;
124                 }
125                 protected override void OnValidating(C.CancelEventArgs e) {
126                         if (this.record)
127                                 events.Enqueue(String.Format("{0}:{1}:OnValidating", this, this.Name));
128                         base.OnValidating (e);
129                 }
130
131                 protected override void OnValidated(EventArgs e) {
132                         if (this.record)
133                                 events.Enqueue(String.Format("{0}:{1}:OnValidated", this, this.Name));
134                         base.OnValidated (e);
135                 }
136
137                 protected override void OnGotFocus(EventArgs e) {
138                         if (this.record)
139                                 events.Enqueue(String.Format("{0}:{1}:OnGotFocus", this, this.Name));
140                         base.OnGotFocus (e);
141                 }
142
143                 protected override void Select(bool directed, bool forward) {
144                         if (this.record)
145                                 events.Enqueue(String.Format("{0}:{1}:Select", this, this.Name));
146                         base.Select (directed, forward);
147                 }
148         }
149
150         [TestFixture]
151         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
152         public class ContainerControlTest {
153                 [Test]
154                 public void RemoveActiveControlTest ()
155                 {
156                         ContainerControl cc = new ContainerControl();
157                         TextBox txt = new TextBox ();
158                         cc.Controls.Add (txt);
159                         Assert.IsFalse (cc.ActiveControl == txt, "#01");
160                         cc.ActiveControl = txt;
161                         Assert.AreSame (cc.ActiveControl, txt, "#02");
162                         cc.Controls.Remove (txt);
163                         Assert.IsNull (cc.ActiveControl, "#03");
164                 }
165                 [Test]
166                 public void GetContainerControl ()
167                 {
168                         ContainerControl cc = new ContainerControl ();
169                         Assert.IsTrue (Object.ReferenceEquals (cc, cc.GetContainerControl ()), "ContainerControl.GetContainerControl");
170
171                         Button b = new Button ();
172                         Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
173                         b.Parent = cc;
174                         Assert.IsTrue (Object.ReferenceEquals (cc, b.GetContainerControl ()), "Button.GetContainerControl");
175                 }
176
177                 [Test]
178                 public void GetContainerControl_WithoutStyle ()
179                 {
180                         IContainerControlTest cct = new IContainerControlTest ();
181                         Assert.IsNull (cct.GetContainerControl (), "IContainerControlTest.GetContainerControl");
182
183                         Button b = new Button ();
184                         b.Parent = cct;
185                         Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
186                 }
187
188                 [Test]
189                 [ExpectedException (typeof (ArgumentException))]
190                 public void ActiveControlNotChildTest ()
191                 {
192                         ContainerControl c = new ContainerControl ();
193                         c.ActiveControl = new Control ();
194                 }
195
196                 [Test]
197                 public void Validation() {
198                         Queue events = new Queue();
199
200                         FormCustom form = new FormCustom("form1", true, events);
201                         ContainerControlCustom container1 = new ContainerControlCustom("container1", true, events);
202                         ContainerControlCustom container2 = new ContainerControlCustom("container2", true, events);
203                         ContainerControlCustom container3 = new ContainerControlCustom("container3", true, events);
204                         UserControlCustom userctl1 = new UserControlCustom("userctl1", true, events);
205                         UserControlCustom userctl2 = new UserControlCustom("userctl2", true, events);
206                         UserControlCustom userctl3 = new UserControlCustom("userctl3", true, events);
207
208                         container2.Controls.Add(userctl2);
209                         container2.Controls.Add(userctl3);
210                         container1.Controls.Add(userctl1);
211                         form.Controls.Add(container1);
212                         form.Controls.Add(container2);
213                         form.Controls.Add(container3);
214
215                         form.Show();
216
217                         object s;
218
219                         events.Enqueue("START");
220                         container3.Select();
221                         events.Enqueue("END");
222                         events.Enqueue("START");
223                         container1.Select();
224                         events.Enqueue("END");
225                         events.Enqueue("START");
226                         container2.Select();
227                         events.Enqueue("END");
228                         events.Enqueue("START");
229                         userctl1.Select();
230                         events.Enqueue("END");
231                         events.Enqueue("START");
232                         userctl2.Select();
233                         events.Enqueue("END");
234                         events.Enqueue("START");
235                         userctl2.Select();
236                         events.Enqueue("END");
237
238
239                         while (events.Count > 0) {
240                                 s = events.Dequeue();
241                                 Console.WriteLine(s.ToString());
242                         }
243
244                         events.Clear();
245
246                         form.Close();
247                         userctl1.Dispose();
248                         userctl2.Dispose();
249                         userctl3.Dispose();
250                         container1.Dispose();
251                         container1.Dispose();
252                         form.Dispose();
253
254                 }
255                 
256                 [Test]
257                 public void MnemonicCalledWhenCanSelectFalse ()
258                 {
259                         MyForm f = new MyForm ();
260                         f.ShowInTaskbar = false;
261                         
262                         MyControl c = new MyControl ();
263                         
264                         f.Controls.Add (c);
265                         f.Show ();
266                         
267                         Assert.AreEqual (false, c.CanSelect, "A1");
268                         f.PublicProcessMnemonic ('b');
269                         
270                         Assert.AreEqual (true, c.mnemonic_called, "A2");
271                 }
272                 
273                 private class MyForm : Form
274                 {
275                         public bool PublicProcessMnemonic (char charCode)
276                         {
277                                 return this.ProcessMnemonic (charCode);
278                         }
279                 }
280                 
281                 private class MyControl : Control
282                 {
283                         public bool mnemonic_called;
284                         
285                         public MyControl ()
286                         {
287                                 SetStyle (ControlStyles.Selectable, false);
288                         }
289                         
290                         protected override bool ProcessMnemonic (char charCode)
291                         {
292                                 mnemonic_called = true;
293                                 return base.ProcessMnemonic (charCode);
294                         }
295                 }
296         }
297 }