2 // ToolStripControlHostTests.cs
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
30 using System.Collections.Generic;
32 using NUnit.Framework;
34 using System.Windows.Forms;
35 using System.ComponentModel;
37 namespace MonoTests.System.Windows.Forms
40 public class ToolStripControlHostTests : TestHelper
43 public void Constructor ()
45 Control t = new Control ();
46 ToolStripControlHost tsi = new ToolStripControlHost (t);
48 Assert.AreEqual (SystemColors.Control, tsi.BackColor, "A1");
49 Assert.AreEqual (null, tsi.BackgroundImage, "A2");
50 Assert.AreEqual (ImageLayout.Tile, tsi.BackgroundImageLayout, "A3");
51 Assert.AreEqual (true, tsi.CanSelect, "A4");
52 Assert.AreEqual (true, tsi.CausesValidation, "A5");
53 Assert.AreSame (t, tsi.Control, "A6");
54 Assert.AreEqual (ContentAlignment.MiddleCenter, tsi.ControlAlign, "A7");
55 Assert.AreEqual (true, tsi.Enabled, "A8");
56 Assert.AreEqual (false, tsi.Focused, "A9");
57 Assert.AreEqual (t.Font, tsi.Font, "A10");
58 Assert.AreEqual (SystemColors.ControlText, tsi.ForeColor, "A11");
59 Assert.AreEqual (RightToLeft.No, tsi.RightToLeft, "A12");
60 Assert.AreEqual (false, tsi.Selected, "A13");
61 Assert.AreEqual (null, tsi.Site, "A14");
62 Assert.AreEqual (new Size (0, 0), tsi.Size, "A15");
63 Assert.AreEqual (string.Empty, tsi.Text, "A16");
65 tsi = new ToolStripControlHost (t, "Bob");
66 Assert.AreEqual ("Bob", tsi.Name, "A17");
67 Assert.AreSame (t, tsi.Control, "A18");
68 Assert.AreEqual (string.Empty, tsi.Control.Name, "A19");
72 [ExpectedException (typeof (ArgumentNullException))]
73 public void ConstructorANE ()
75 new ToolStripControlHost (null);
79 [ExpectedException (typeof (ArgumentNullException))]
80 public void ConstructorANE2 ()
82 new ToolStripControlHost (null, string.Empty);
86 public void ProtectedProperties ()
88 ExposeProtectedProperties epp = new ExposeProtectedProperties ();
90 Assert.AreEqual (new Size (0, 0), epp.DefaultSize, "C1");
94 public void PropertyBackColor ()
96 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
97 EventWatcher ew = new EventWatcher (tsi);
99 tsi.BackColor = Color.BurlyWood;
100 Assert.AreEqual (Color.BurlyWood, tsi.BackColor, "B1");
101 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
104 tsi.BackColor = Color.BurlyWood;
105 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
107 // BackColor initially comes from hosted control
108 tsi = new ToolStripControlHost (new Button ());
109 Assert.AreEqual (SystemColors.Control, tsi.BackColor, "B4");
111 tsi = new ToolStripControlHost (new TextBox ());
112 Assert.AreEqual (SystemColors.Window, tsi.BackColor, "B5");
114 tsi = new ToolStripControlHost (new ProgressBar ());
115 Assert.AreEqual (SystemColors.Control, tsi.BackColor, "B6");
119 public void PropertyBackgroundImage ()
121 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
122 EventWatcher ew = new EventWatcher (tsi);
124 Image i = new Bitmap (1, 1);
125 tsi.BackgroundImage = i;
126 Assert.AreSame (i, tsi.BackgroundImage, "B1");
127 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
130 tsi.BackgroundImage = i;
131 Assert.AreSame (string.Empty, ew.ToString (), "B3");
135 public void PropertyBackgroundImageLayout ()
137 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
138 EventWatcher ew = new EventWatcher (tsi);
140 tsi.BackgroundImageLayout = ImageLayout.Zoom;
141 Assert.AreEqual (ImageLayout.Zoom, tsi.BackgroundImageLayout, "B1");
142 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
145 tsi.BackgroundImageLayout = ImageLayout.Zoom;
146 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
150 public void PropertyCausesValidation ()
152 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
153 EventWatcher ew = new EventWatcher (tsi);
155 tsi.CausesValidation = false;
156 Assert.AreEqual (false, tsi.CausesValidation, "B1");
157 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
160 tsi.CausesValidation = false;
161 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
165 public void PropertyControlAlign ()
167 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
168 EventWatcher ew = new EventWatcher (tsi);
170 tsi.ControlAlign = ContentAlignment.TopRight ;
171 Assert.AreEqual (ContentAlignment.TopRight, tsi.ControlAlign, "B1");
172 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
175 tsi.ControlAlign = ContentAlignment.TopRight;
176 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
180 public void PropertyEnabled ()
182 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
183 EventWatcher ew = new EventWatcher (tsi);
186 Assert.AreEqual (false, tsi.Enabled, "B1");
187 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
191 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
195 public void PropertyFont ()
197 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
198 EventWatcher ew = new EventWatcher (tsi);
200 Font f = new Font ("Arial", 12);
203 Assert.AreSame (f, tsi.Font, "B1");
204 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
208 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
212 public void PropertyForeColor ()
214 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
215 EventWatcher ew = new EventWatcher (tsi);
217 tsi.ForeColor = Color.BurlyWood;
218 Assert.AreEqual (Color.BurlyWood, tsi.ForeColor, "B1");
219 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
222 tsi.ForeColor = Color.BurlyWood;
223 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
225 // CausesValidation initially comes from hosted control
226 tsi = new ToolStripControlHost (new Button ());
227 Assert.AreEqual (SystemColors.ControlText, tsi.ForeColor, "B4");
229 tsi = new ToolStripControlHost (new TextBox ());
230 Assert.AreEqual (SystemColors.WindowText, tsi.ForeColor, "B5");
234 public void PropertyRightToLeft ()
236 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
237 EventWatcher ew = new EventWatcher (tsi);
239 tsi.RightToLeft = RightToLeft.Yes;
240 Assert.AreEqual (RightToLeft.Yes, tsi.RightToLeft, "B1");
241 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
244 tsi.RightToLeft = RightToLeft.Yes;
245 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
249 public void PropertySite ()
251 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
252 EventWatcher ew = new EventWatcher (tsi);
254 ISite i = new Form ().Site;
256 Assert.AreSame (i, tsi.Site, "B1");
257 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
261 Assert.AreSame (string.Empty, ew.ToString (), "B3");
265 public void PropertySize ()
267 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
268 EventWatcher ew = new EventWatcher (tsi);
270 tsi.Size = new Size (42, 42);
271 Assert.AreEqual (new Size (42, 42), tsi.Size, "B1");
272 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
275 tsi.Size = new Size (42, 42);
276 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
280 public void PropertyText ()
282 ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
283 EventWatcher ew = new EventWatcher (tsi);
286 Assert.AreEqual ("Text", tsi.Text, "B1");
287 Assert.AreEqual (string.Empty, ew.ToString (), "B2");
291 Assert.AreEqual (string.Empty, ew.ToString (), "B3");
296 //public void PropertyAnchorAndDocking ()
298 // ToolStripItem ts = new NullToolStripItem ();
300 // ts.Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
302 // Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Bottom, ts.Anchor, "A1");
303 // Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
305 // ts.Anchor = AnchorStyles.Left | AnchorStyles.Right;
307 // Assert.AreEqual (AnchorStyles.Left | AnchorStyles.Right, ts.Anchor, "A1");
308 // Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
310 // ts.Dock = DockStyle.Left;
312 // Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
313 // Assert.AreEqual (DockStyle.Left, ts.Dock, "A2");
315 // ts.Dock = DockStyle.None;
317 // Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
318 // Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
320 // ts.Dock = DockStyle.Top;
322 // Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
323 // Assert.AreEqual (DockStyle.Top, ts.Dock, "A2");
327 [Ignore ("Accessibility still needs some work")]
328 public void Accessibility ()
330 ToolStripControlHost tsi = new ToolStripControlHost (new Button ());
331 AccessibleObject ao = tsi.AccessibilityObject;
333 Assert.AreEqual ("Press", ao.DefaultAction, "L2");
334 Assert.AreEqual (null, ao.Description, "L3");
335 Assert.AreEqual (null, ao.Help, "L4");
336 Assert.AreEqual (null, ao.KeyboardShortcut, "L5");
337 Assert.AreEqual (null, ao.Name, "L6");
338 Assert.AreEqual (AccessibleRole.PushButton, ao.Role, "L8");
339 Assert.AreEqual (AccessibleStates.None, ao.State, "L9");
340 Assert.AreEqual (null, ao.Value, "L10");
343 tsi.Text = "Test Label";
344 tsi.AccessibleDescription = "Label Desc";
346 Assert.AreEqual ("Press", ao.DefaultAction, "L12");
347 Assert.AreEqual ("Label Desc", ao.Description, "L13");
348 Assert.AreEqual (null, ao.Help, "L14");
349 Assert.AreEqual (null, ao.KeyboardShortcut, "L15");
350 //Assert.AreEqual ("Test Label", ao.Name, "L16");
351 Assert.AreEqual (AccessibleRole.PushButton, ao.Role, "L18");
352 Assert.AreEqual (AccessibleStates.None, ao.State, "L19");
353 Assert.AreEqual (null, ao.Value, "L20");
355 tsi.AccessibleName = "Access Label";
356 Assert.AreEqual ("Access Label", ao.Name, "L21");
358 tsi.Text = "Test Label";
359 Assert.AreEqual ("Access Label", ao.Name, "L22");
361 tsi.AccessibleDefaultActionDescription = "AAA";
362 Assert.AreEqual ("AAA", tsi.AccessibleDefaultActionDescription, "L23");
366 public void BehaviorBackColor ()
368 ToolStrip ts = new ToolStrip ();
369 ToolStripItem tsi = new ToolStripControlHost (new Control ());
373 Assert.AreEqual (SystemColors.Control, ts.BackColor, "C1");
374 Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C2");
376 ts.BackColor = Color.BlueViolet;
378 Assert.AreEqual (Color.BlueViolet, ts.BackColor, "C3");
379 Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C4");
381 tsi.BackColor = Color.Snow;
383 Assert.AreEqual (Color.BlueViolet, ts.BackColor, "C5");
384 Assert.AreEqual (Color.Snow, tsi.BackColor, "C6");
386 tsi.ResetBackColor ();
388 Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C7");
392 [Ignore ("Need some AutoSize work done in ToolStripControlHost")]
393 public void BehaviorSize ()
395 Control c = new Control ();
396 ToolStripControlHost tsi = new ToolStripControlHost (c);
397 ToolStrip ts = new ToolStrip ();
399 Assert.AreEqual (new Size (0, 0), c.Size, "H1");
400 Assert.AreEqual (new Size (0, 0), tsi.Size, "H2");
403 tsi = new ToolStripControlHost (c);
405 Assert.AreEqual (new Size (100, 20), c.Size, "H3");
406 Assert.AreEqual (new Size (100, 20), tsi.Size, "H4");
409 tsi = new ToolStripControlHost (c);
411 Assert.AreEqual (new Size (121, 21), c.Size, "H5");
412 Assert.AreEqual (new Size (121, 21), tsi.Size, "H6");
414 c = new ProgressBar ();
415 tsi = new ToolStripControlHost (c);
417 Assert.AreEqual (new Size (100, 23), c.Size, "H7");
418 Assert.AreEqual (new Size (100, 23), tsi.Size, "H8");
420 c = new PictureBox ();
421 tsi = new ToolStripControlHost (c);
423 Assert.AreEqual (new Size (100, 50), c.Size, "H7");
424 Assert.AreEqual (new Size (100, 50), tsi.Size, "H8");
426 Form f = new Form ();
427 f.ShowInTaskbar = false;
431 Assert.AreEqual (new Size (100, 50), c.Size, "H9");
432 Assert.AreEqual (new Size (100, 50), tsi.Size, "H10");
433 Assert.AreEqual (new Size (292, 53), ts.Size, "H11");
436 private class EventWatcher
438 private string events = string.Empty;
440 public EventWatcher (ToolStripControlHost tsi)
442 tsi.Enter += new EventHandler (delegate (Object obj, EventArgs e) { events += ("Enter;"); });
443 tsi.GotFocus += new EventHandler (delegate (Object obj, EventArgs e) { events += ("GotFocus;"); });
444 tsi.KeyDown += new KeyEventHandler (delegate (Object obj, KeyEventArgs e) { events += ("KeyDown;"); });
445 tsi.KeyPress += new KeyPressEventHandler (delegate (Object obj, KeyPressEventArgs e) { events += ("KeyPress;"); });
446 tsi.KeyUp += new KeyEventHandler (delegate (Object obj, KeyEventArgs e) { events += ("KeyUp;"); });
447 tsi.Leave += new EventHandler (delegate (Object obj, EventArgs e) { events += ("Leave;"); });
448 tsi.LostFocus += new EventHandler (delegate (Object obj, EventArgs e) { events += ("LostFocus;"); });
449 tsi.Validated += new EventHandler (delegate (Object obj, EventArgs e) { events += ("Validated;"); });
450 tsi.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { events += ("Validating;"); });
453 public override string ToString ()
455 return events.TrimEnd (';');
460 events = string.Empty;
464 private class ExposeProtectedProperties : ToolStripControlHost
466 public ExposeProtectedProperties () : base (new Control ()) {}
468 public new Size DefaultSize { get { return base.DefaultSize; } }