2007-01-10 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ContainerControlTest.cs
blob3741cd30720702f7a56431f83891dce755d0d995
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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;
36 namespace MonoTests.System.Windows.Forms {
38 public class IContainerControlTest : Control, IContainerControl {
40 public bool ActivateControl (Control active)
42 return true;
45 public Control ActiveControl {
46 get { return null; }
47 set { ; }
51 public class FormCustom: Form {
52 public bool record;
53 public Queue events;
55 public FormCustom(string name, bool record, Queue events) {
56 base.Name = name;
57 this.record = record;
58 this.events = events;
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);
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);
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);
81 public class ContainerControlCustom: ContainerControl {
82 public bool record;
83 public Queue events;
85 public ContainerControlCustom(string name, bool record, Queue events) {
86 base.Name = name;
87 this.record = record;
88 this.events = events;
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);
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);
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);
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);
116 public class UserControlCustom: UserControl {
117 public bool record;
118 public Queue events;
120 public UserControlCustom(string name, bool record, Queue events) {
121 base.Name = name;
122 this.record = record;
123 this.events = events;
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);
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);
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);
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);
150 [TestFixture]
151 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
152 public class ContainerControlTest {
154 [Test]
155 public void GetContainerControl ()
157 ContainerControl cc = new ContainerControl ();
158 Assert.IsTrue (Object.ReferenceEquals (cc, cc.GetContainerControl ()), "ContainerControl.GetContainerControl");
160 Button b = new Button ();
161 Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
162 b.Parent = cc;
163 Assert.IsTrue (Object.ReferenceEquals (cc, b.GetContainerControl ()), "Button.GetContainerControl");
166 [Test]
167 public void GetContainerControl_WithoutStyle ()
169 IContainerControlTest cct = new IContainerControlTest ();
170 Assert.IsNull (cct.GetContainerControl (), "IContainerControlTest.GetContainerControl");
172 Button b = new Button ();
173 b.Parent = cct;
174 Assert.IsNull (b.GetContainerControl (), "Button.GetContainerControl/without parent");
177 [Test]
178 [ExpectedException (typeof (ArgumentException))]
179 public void ActiveControlNotChildTest ()
181 ContainerControl c = new ContainerControl ();
182 c.ActiveControl = new Control ();
185 [Test]
186 public void Validation() {
187 Queue events = new Queue();
189 FormCustom form = new FormCustom("form1", true, events);
190 ContainerControlCustom container1 = new ContainerControlCustom("container1", true, events);
191 ContainerControlCustom container2 = new ContainerControlCustom("container2", true, events);
192 ContainerControlCustom container3 = new ContainerControlCustom("container3", true, events);
193 UserControlCustom userctl1 = new UserControlCustom("userctl1", true, events);
194 UserControlCustom userctl2 = new UserControlCustom("userctl2", true, events);
195 UserControlCustom userctl3 = new UserControlCustom("userctl3", true, events);
197 container2.Controls.Add(userctl2);
198 container2.Controls.Add(userctl3);
199 container1.Controls.Add(userctl1);
200 form.Controls.Add(container1);
201 form.Controls.Add(container2);
202 form.Controls.Add(container3);
204 form.Show();
206 object s;
208 events.Enqueue("START");
209 container3.Select();
210 events.Enqueue("END");
211 events.Enqueue("START");
212 container1.Select();
213 events.Enqueue("END");
214 events.Enqueue("START");
215 container2.Select();
216 events.Enqueue("END");
217 events.Enqueue("START");
218 userctl1.Select();
219 events.Enqueue("END");
220 events.Enqueue("START");
221 userctl2.Select();
222 events.Enqueue("END");
223 events.Enqueue("START");
224 userctl2.Select();
225 events.Enqueue("END");
228 while (events.Count > 0) {
229 s = events.Dequeue();
230 Console.WriteLine(s.ToString());
233 events.Clear();
235 form.Close();
236 userctl1.Dispose();
237 userctl2.Dispose();
238 userctl3.Dispose();
239 container1.Dispose();
240 container1.Dispose();
241 form.Dispose();