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