New test for ListView.Dispose firing extra Layout events.
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / ToolStripControlHostTest.cs
1 //
2 // ToolStripControlHostTests.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 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28 #if NET_2_0
29 using System;
30 using System.Collections.Generic;
31 using System.Text;
32 using NUnit.Framework;
33 using System.Drawing;
34 using System.Windows.Forms;
35 using System.ComponentModel;
36
37 namespace MonoTests.System.Windows.Forms
38 {
39         [TestFixture]
40         public class ToolStripControlHostTests : TestHelper
41         {
42                 [Test]
43                 public void Constructor ()
44                 {
45                         Control t = new Control ();
46                         ToolStripControlHost tsi = new ToolStripControlHost (t);
47
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");
64
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");
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (ArgumentNullException))]
73                 public void ConstructorANE ()
74                 {
75                         new ToolStripControlHost (null); 
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (ArgumentNullException))]
80                 public void ConstructorANE2 ()
81                 {
82                         new ToolStripControlHost (null, string.Empty);
83                 }
84         
85                 [Test]
86                 public void ProtectedProperties ()
87                 {
88                         ExposeProtectedProperties epp = new ExposeProtectedProperties ();
89
90                         Assert.AreEqual (new Size (0, 0), epp.DefaultSize, "C1");
91                 }
92
93                 [Test]
94                 public void PropertyBackColor ()
95                 {
96                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
97                         EventWatcher ew = new EventWatcher (tsi);
98
99                         tsi.BackColor = Color.BurlyWood;
100                         Assert.AreEqual (Color.BurlyWood, tsi.BackColor, "B1");
101                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
102
103                         ew.Clear ();
104                         tsi.BackColor = Color.BurlyWood;
105                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
106                         
107                         // BackColor initially comes from hosted control
108                         tsi = new ToolStripControlHost (new Button ());
109                         Assert.AreEqual (SystemColors.Control, tsi.BackColor, "B4");
110
111                         tsi = new ToolStripControlHost (new TextBox ());
112                         Assert.AreEqual (SystemColors.Window, tsi.BackColor, "B5");
113
114                         tsi = new ToolStripControlHost (new ProgressBar ());
115                         Assert.AreEqual (SystemColors.Control, tsi.BackColor, "B6");
116                 }
117
118                 [Test]
119                 public void PropertyBackgroundImage ()
120                 {
121                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
122                         EventWatcher ew = new EventWatcher (tsi);
123
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");
128
129                         ew.Clear ();
130                         tsi.BackgroundImage = i;
131                         Assert.AreSame (string.Empty, ew.ToString (), "B3");
132                 }
133
134                 [Test]
135                 public void PropertyBackgroundImageLayout ()
136                 {
137                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
138                         EventWatcher ew = new EventWatcher (tsi);
139
140                         tsi.BackgroundImageLayout = ImageLayout.Zoom;
141                         Assert.AreEqual (ImageLayout.Zoom, tsi.BackgroundImageLayout, "B1");
142                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
143
144                         ew.Clear ();
145                         tsi.BackgroundImageLayout = ImageLayout.Zoom;
146                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
147                 }
148
149                 [Test]
150                 public void PropertyCausesValidation ()
151                 {
152                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
153                         EventWatcher ew = new EventWatcher (tsi);
154
155                         tsi.CausesValidation = false;
156                         Assert.AreEqual (false, tsi.CausesValidation, "B1");
157                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
158
159                         ew.Clear ();
160                         tsi.CausesValidation = false;
161                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
162                 }
163
164                 [Test]
165                 public void PropertyControlAlign ()
166                 {
167                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
168                         EventWatcher ew = new EventWatcher (tsi);
169
170                         tsi.ControlAlign = ContentAlignment.TopRight ;
171                         Assert.AreEqual (ContentAlignment.TopRight, tsi.ControlAlign, "B1");
172                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
173
174                         ew.Clear ();
175                         tsi.ControlAlign = ContentAlignment.TopRight;
176                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
177                 }
178
179                 [Test]
180                 public void PropertyDefaultSize ()
181                 {
182                         VariableSizeControl c = new VariableSizeControl ();
183                         ExposeProtectedProperties epp = new ExposeProtectedProperties (c);
184
185                         Assert.AreEqual (new Size (-1, -1), epp.DefaultSize, "#A1");
186
187                         c.Size = new Size (666, 666);
188                         Assert.AreEqual (new Size (666, 666), c.Size, "#B99");
189                         Assert.AreEqual (new Size (666, 666), epp.DefaultSize, "#B1");
190                 }
191
192                 [Test]
193                 public void PropertyEnabled ()
194                 {
195                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
196                         EventWatcher ew = new EventWatcher (tsi);
197
198                         tsi.Enabled = false;
199                         Assert.AreEqual (false, tsi.Enabled, "B1");
200                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
201
202                         ew.Clear ();
203                         tsi.Enabled = false;
204                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
205                 }
206
207                 [Test]
208                 public void PropertyFont ()
209                 {
210                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
211                         EventWatcher ew = new EventWatcher (tsi);
212
213                         Font f = new Font ("Arial", 12);
214
215                         tsi.Font = f;
216                         Assert.AreSame (f, tsi.Font, "B1");
217                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
218
219                         ew.Clear ();
220                         tsi.Font = f;
221                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
222                 }
223
224                 [Test]
225                 public void PropertyForeColor ()
226                 {
227                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
228                         EventWatcher ew = new EventWatcher (tsi);
229
230                         tsi.ForeColor = Color.BurlyWood;
231                         Assert.AreEqual (Color.BurlyWood, tsi.ForeColor, "B1");
232                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
233
234                         ew.Clear ();
235                         tsi.ForeColor = Color.BurlyWood;
236                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
237
238                         // CausesValidation initially comes from hosted control
239                         tsi = new ToolStripControlHost (new Button ());
240                         Assert.AreEqual (SystemColors.ControlText, tsi.ForeColor, "B4");
241
242                         tsi = new ToolStripControlHost (new TextBox ());
243                         Assert.AreEqual (SystemColors.WindowText, tsi.ForeColor, "B5");
244                 }
245
246                 [Test]
247                 public void PropertyRightToLeft ()
248                 {
249                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
250                         EventWatcher ew = new EventWatcher (tsi);
251
252                         tsi.RightToLeft = RightToLeft.Yes;
253                         Assert.AreEqual (RightToLeft.Yes, tsi.RightToLeft, "B1");
254                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
255
256                         ew.Clear ();
257                         tsi.RightToLeft = RightToLeft.Yes;
258                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
259                 }
260
261                 [Test]
262                 public void PropertySite ()
263                 {
264                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
265                         EventWatcher ew = new EventWatcher (tsi);
266
267                         ISite i = new Form ().Site;
268                         tsi.Site = i;
269                         Assert.AreSame (i, tsi.Site, "B1");
270                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
271
272                         ew.Clear ();
273                         tsi.Site = i;
274                         Assert.AreSame (string.Empty, ew.ToString (), "B3");
275                 }
276
277                 [Test]
278                 public void PropertySize ()
279                 {
280                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
281                         EventWatcher ew = new EventWatcher (tsi);
282
283                         tsi.Size = new Size (42, 42);
284                         Assert.AreEqual (new Size (42, 42), tsi.Size, "B1");
285                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
286
287                         ew.Clear ();
288                         tsi.Size = new Size (42, 42);
289                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
290                 }
291
292                 [Test]
293                 public void PropertyText ()
294                 {
295                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
296                         EventWatcher ew = new EventWatcher (tsi);
297
298                         tsi.Text = "Text";
299                         Assert.AreEqual ("Text", tsi.Text, "B1");
300                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
301
302                         ew.Clear ();
303                         tsi.Text = "Text";
304                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
305                 }
306
307
308                 //[Test]
309                 //public void PropertyAnchorAndDocking ()
310                 //{
311                 //        ToolStripItem ts = new NullToolStripItem ();
312
313                 //        ts.Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
314
315                 //        Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Bottom, ts.Anchor, "A1");
316                 //        Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
317
318                 //        ts.Anchor = AnchorStyles.Left | AnchorStyles.Right;
319
320                 //        Assert.AreEqual (AnchorStyles.Left | AnchorStyles.Right, ts.Anchor, "A1");
321                 //        Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
322
323                 //        ts.Dock = DockStyle.Left;
324
325                 //        Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
326                 //        Assert.AreEqual (DockStyle.Left, ts.Dock, "A2");
327
328                 //        ts.Dock = DockStyle.None;
329
330                 //        Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
331                 //        Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
332
333                 //        ts.Dock = DockStyle.Top;
334
335                 //        Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
336                 //        Assert.AreEqual (DockStyle.Top, ts.Dock, "A2");
337                 //}
338                 
339                 [Test]
340                 [Ignore ("Accessibility still needs some work")]
341                 public void Accessibility ()
342                 {
343                         ToolStripControlHost tsi = new ToolStripControlHost (new Button ());
344                         AccessibleObject ao = tsi.AccessibilityObject;
345
346                         Assert.AreEqual ("Press", ao.DefaultAction, "L2");
347                         Assert.AreEqual (null, ao.Description, "L3");
348                         Assert.AreEqual (null, ao.Help, "L4");
349                         Assert.AreEqual (null, ao.KeyboardShortcut, "L5");
350                         Assert.AreEqual (null, ao.Name, "L6");
351                         Assert.AreEqual (AccessibleRole.PushButton, ao.Role, "L8");
352                         Assert.AreEqual (AccessibleStates.None, ao.State, "L9");
353                         Assert.AreEqual (null, ao.Value, "L10");
354
355                         tsi.Name = "Label1";
356                         tsi.Text = "Test Label";
357                         tsi.AccessibleDescription = "Label Desc";
358
359                         Assert.AreEqual ("Press", ao.DefaultAction, "L12");
360                         Assert.AreEqual ("Label Desc", ao.Description, "L13");
361                         Assert.AreEqual (null, ao.Help, "L14");
362                         Assert.AreEqual (null, ao.KeyboardShortcut, "L15");
363                         //Assert.AreEqual ("Test Label", ao.Name, "L16");
364                         Assert.AreEqual (AccessibleRole.PushButton, ao.Role, "L18");
365                         Assert.AreEqual (AccessibleStates.None, ao.State, "L19");
366                         Assert.AreEqual (null, ao.Value, "L20");
367
368                         tsi.AccessibleName = "Access Label";
369                         Assert.AreEqual ("Access Label", ao.Name, "L21");
370
371                         tsi.Text = "Test Label";
372                         Assert.AreEqual ("Access Label", ao.Name, "L22");
373
374                         tsi.AccessibleDefaultActionDescription = "AAA";
375                         Assert.AreEqual ("AAA", tsi.AccessibleDefaultActionDescription, "L23");
376                 }
377
378                 [Test]
379                 public void BehaviorBackColor ()
380                 {
381                         ToolStrip ts = new ToolStrip ();
382                         ToolStripItem tsi = new ToolStripControlHost (new Control ());
383
384                         ts.Items.Add (tsi);
385
386                         Assert.AreEqual (SystemColors.Control, ts.BackColor, "C1");
387                         Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C2");
388
389                         ts.BackColor = Color.BlueViolet;
390
391                         Assert.AreEqual (Color.BlueViolet, ts.BackColor, "C3");
392                         Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C4");
393
394                         tsi.BackColor = Color.Snow;
395
396                         Assert.AreEqual (Color.BlueViolet, ts.BackColor, "C5");
397                         Assert.AreEqual (Color.Snow, tsi.BackColor, "C6");
398
399                         tsi.ResetBackColor ();
400
401                         Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C7");
402                 }
403
404                 [Test]
405                 [Ignore ("Need some AutoSize work done in ToolStripControlHost")]
406                 public void BehaviorSize ()
407                 {
408                         Control c = new Control ();
409                         ToolStripControlHost tsi = new ToolStripControlHost (c);
410                         ToolStrip ts = new ToolStrip ();
411                         
412                         Assert.AreEqual (new Size (0, 0), c.Size, "H1");
413                         Assert.AreEqual (new Size (0, 0), tsi.Size, "H2");
414
415                         c = new TextBox ();
416                         tsi = new ToolStripControlHost (c);
417
418                         Assert.AreEqual (new Size (100, 20), c.Size, "H3");
419                         Assert.AreEqual (new Size (100, 20), tsi.Size, "H4");
420
421                         c = new ComboBox ();
422                         tsi = new ToolStripControlHost (c);
423
424                         Assert.AreEqual (new Size (121, 21), c.Size, "H5");
425                         Assert.AreEqual (new Size (121, 21), tsi.Size, "H6");
426
427                         c = new ProgressBar ();
428                         tsi = new ToolStripControlHost (c);
429
430                         Assert.AreEqual (new Size (100, 23), c.Size, "H7");
431                         Assert.AreEqual (new Size (100, 23), tsi.Size, "H8");
432                         
433                         c = new PictureBox ();
434                         tsi = new ToolStripControlHost (c);
435
436                         Assert.AreEqual (new Size (100, 50), c.Size, "H7");
437                         Assert.AreEqual (new Size (100, 50), tsi.Size, "H8");
438                         
439                         Form f = new Form ();
440                         f.ShowInTaskbar = false;
441                         f.Controls.Add (ts);
442                         ts.Items.Add (tsi);
443                         f.Show ();
444                         Assert.AreEqual (new Size (100, 50), c.Size, "H9");
445                         Assert.AreEqual (new Size (100, 50), tsi.Size, "H10");
446                         Assert.AreEqual (new Size (292, 53), ts.Size, "H11");
447                 }
448
449                 [Test]
450                 public void MethodOnHostedControlResize ()
451                 {
452                         ToolStripControlHostChild control_host = new ToolStripControlHostChild (new Control ());
453                         Control c = control_host.Control;
454
455                         Assert.AreEqual (false, control_host.OnHostedControlResizeFired, "#A1");
456
457                         c.Size = new Size (666, 666);
458                         Assert.AreEqual (true, control_host.OnHostedControlResizeFired, "#A2");
459                 }
460
461                 private class ToolStripControlHostChild : ToolStripControlHost {
462                         bool on_hosted_control_resize_fired;
463
464                         public ToolStripControlHostChild (Control c) : base (c)
465                         {
466                         }
467
468                         protected override void OnHostedControlResize (EventArgs e)
469                         {
470                                 base.OnHostedControlResize (e);
471                                 on_hosted_control_resize_fired = true;
472                         }
473
474                         public bool OnHostedControlResizeFired
475                         {
476                                 get
477                                 {
478                                         return on_hosted_control_resize_fired;
479                                 }
480                         }
481                 }
482
483                 private class EventWatcher
484                 {
485                         private string events = string.Empty;
486                         
487                         public EventWatcher (ToolStripControlHost tsi)
488                         {
489                                 tsi.Enter += new EventHandler (delegate (Object obj, EventArgs e) { events += ("Enter;"); });
490                                 tsi.GotFocus += new EventHandler (delegate (Object obj, EventArgs e) { events += ("GotFocus;"); });
491                                 tsi.KeyDown += new KeyEventHandler (delegate (Object obj, KeyEventArgs e) { events += ("KeyDown;"); });
492                                 tsi.KeyPress += new KeyPressEventHandler (delegate (Object obj, KeyPressEventArgs e) { events += ("KeyPress;"); });
493                                 tsi.KeyUp += new KeyEventHandler (delegate (Object obj, KeyEventArgs e) { events += ("KeyUp;"); });
494                                 tsi.Leave += new EventHandler (delegate (Object obj, EventArgs e) { events += ("Leave;"); });
495                                 tsi.LostFocus += new EventHandler (delegate (Object obj, EventArgs e) { events += ("LostFocus;"); });
496                                 tsi.Validated += new EventHandler (delegate (Object obj, EventArgs e) { events += ("Validated;"); });
497                                 tsi.Validating += new CancelEventHandler (delegate (Object obj, CancelEventArgs e) { events += ("Validating;"); });
498                         }
499
500                         public override string ToString ()
501                         {
502                                 return events.TrimEnd (';');
503                         }
504                         
505                         public void Clear ()
506                         {
507                                 events = string.Empty;
508                         }
509                 }
510                 
511                 private class ExposeProtectedProperties : ToolStripControlHost
512                 {
513                         public ExposeProtectedProperties (Control c) : base (c) {}
514                         public ExposeProtectedProperties () : base (new Control ()) {}
515                         
516                         public new Size DefaultSize { get { return base.DefaultSize; } }
517                 }
518
519                 private class VariableSizeControl : Control 
520                 {
521                         public override Size GetPreferredSize (Size proposedSize)
522                         {
523                                 return new Size (999, 999);
524                         }
525
526                         protected override Size DefaultSize {
527                                 get {
528                                         return new Size (-1, -1);
529                                 }
530                         }
531                 }
532         }
533 }
534 #endif