Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[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 PropertyEnabled ()
181                 {
182                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
183                         EventWatcher ew = new EventWatcher (tsi);
184
185                         tsi.Enabled = false;
186                         Assert.AreEqual (false, tsi.Enabled, "B1");
187                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
188
189                         ew.Clear ();
190                         tsi.Enabled = false;
191                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
192                 }
193
194                 [Test]
195                 public void PropertyFont ()
196                 {
197                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
198                         EventWatcher ew = new EventWatcher (tsi);
199
200                         Font f = new Font ("Arial", 12);
201
202                         tsi.Font = f;
203                         Assert.AreSame (f, tsi.Font, "B1");
204                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
205
206                         ew.Clear ();
207                         tsi.Font = f;
208                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
209                 }
210
211                 [Test]
212                 public void PropertyForeColor ()
213                 {
214                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
215                         EventWatcher ew = new EventWatcher (tsi);
216
217                         tsi.ForeColor = Color.BurlyWood;
218                         Assert.AreEqual (Color.BurlyWood, tsi.ForeColor, "B1");
219                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
220
221                         ew.Clear ();
222                         tsi.ForeColor = Color.BurlyWood;
223                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
224
225                         // CausesValidation initially comes from hosted control
226                         tsi = new ToolStripControlHost (new Button ());
227                         Assert.AreEqual (SystemColors.ControlText, tsi.ForeColor, "B4");
228
229                         tsi = new ToolStripControlHost (new TextBox ());
230                         Assert.AreEqual (SystemColors.WindowText, tsi.ForeColor, "B5");
231                 }
232
233                 [Test]
234                 public void PropertyRightToLeft ()
235                 {
236                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
237                         EventWatcher ew = new EventWatcher (tsi);
238
239                         tsi.RightToLeft = RightToLeft.Yes;
240                         Assert.AreEqual (RightToLeft.Yes, tsi.RightToLeft, "B1");
241                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
242
243                         ew.Clear ();
244                         tsi.RightToLeft = RightToLeft.Yes;
245                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
246                 }
247
248                 [Test]
249                 public void PropertySite ()
250                 {
251                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
252                         EventWatcher ew = new EventWatcher (tsi);
253
254                         ISite i = new Form ().Site;
255                         tsi.Site = i;
256                         Assert.AreSame (i, tsi.Site, "B1");
257                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
258
259                         ew.Clear ();
260                         tsi.Site = i;
261                         Assert.AreSame (string.Empty, ew.ToString (), "B3");
262                 }
263
264                 [Test]
265                 public void PropertySize ()
266                 {
267                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
268                         EventWatcher ew = new EventWatcher (tsi);
269
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");
273
274                         ew.Clear ();
275                         tsi.Size = new Size (42, 42);
276                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
277                 }
278
279                 [Test]
280                 public void PropertyText ()
281                 {
282                         ToolStripControlHost tsi = new ToolStripControlHost (new Control ());
283                         EventWatcher ew = new EventWatcher (tsi);
284
285                         tsi.Text = "Text";
286                         Assert.AreEqual ("Text", tsi.Text, "B1");
287                         Assert.AreEqual (string.Empty, ew.ToString (), "B2");
288
289                         ew.Clear ();
290                         tsi.Text = "Text";
291                         Assert.AreEqual (string.Empty, ew.ToString (), "B3");
292                 }
293
294
295                 //[Test]
296                 //public void PropertyAnchorAndDocking ()
297                 //{
298                 //        ToolStripItem ts = new NullToolStripItem ();
299
300                 //        ts.Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
301
302                 //        Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Bottom, ts.Anchor, "A1");
303                 //        Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
304
305                 //        ts.Anchor = AnchorStyles.Left | AnchorStyles.Right;
306
307                 //        Assert.AreEqual (AnchorStyles.Left | AnchorStyles.Right, ts.Anchor, "A1");
308                 //        Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
309
310                 //        ts.Dock = DockStyle.Left;
311
312                 //        Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
313                 //        Assert.AreEqual (DockStyle.Left, ts.Dock, "A2");
314
315                 //        ts.Dock = DockStyle.None;
316
317                 //        Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
318                 //        Assert.AreEqual (DockStyle.None, ts.Dock, "A2");
319
320                 //        ts.Dock = DockStyle.Top;
321
322                 //        Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, ts.Anchor, "A1");
323                 //        Assert.AreEqual (DockStyle.Top, ts.Dock, "A2");
324                 //}
325                 
326                 [Test]
327                 [Ignore ("Accessibility still needs some work")]
328                 public void Accessibility ()
329                 {
330                         ToolStripControlHost tsi = new ToolStripControlHost (new Button ());
331                         AccessibleObject ao = tsi.AccessibilityObject;
332
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");
341
342                         tsi.Name = "Label1";
343                         tsi.Text = "Test Label";
344                         tsi.AccessibleDescription = "Label Desc";
345
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");
354
355                         tsi.AccessibleName = "Access Label";
356                         Assert.AreEqual ("Access Label", ao.Name, "L21");
357
358                         tsi.Text = "Test Label";
359                         Assert.AreEqual ("Access Label", ao.Name, "L22");
360
361                         tsi.AccessibleDefaultActionDescription = "AAA";
362                         Assert.AreEqual ("AAA", tsi.AccessibleDefaultActionDescription, "L23");
363                 }
364
365                 [Test]
366                 public void BehaviorBackColor ()
367                 {
368                         ToolStrip ts = new ToolStrip ();
369                         ToolStripItem tsi = new ToolStripControlHost (new Control ());
370
371                         ts.Items.Add (tsi);
372
373                         Assert.AreEqual (SystemColors.Control, ts.BackColor, "C1");
374                         Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C2");
375
376                         ts.BackColor = Color.BlueViolet;
377
378                         Assert.AreEqual (Color.BlueViolet, ts.BackColor, "C3");
379                         Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C4");
380
381                         tsi.BackColor = Color.Snow;
382
383                         Assert.AreEqual (Color.BlueViolet, ts.BackColor, "C5");
384                         Assert.AreEqual (Color.Snow, tsi.BackColor, "C6");
385
386                         tsi.ResetBackColor ();
387
388                         Assert.AreEqual (SystemColors.Control, tsi.BackColor, "C7");
389                 }
390
391                 [Test]
392                 [Ignore ("Need some AutoSize work done in ToolStripControlHost")]
393                 public void BehaviorSize ()
394                 {
395                         Control c = new Control ();
396                         ToolStripControlHost tsi = new ToolStripControlHost (c);
397                         ToolStrip ts = new ToolStrip ();
398                         
399                         Assert.AreEqual (new Size (0, 0), c.Size, "H1");
400                         Assert.AreEqual (new Size (0, 0), tsi.Size, "H2");
401
402                         c = new TextBox ();
403                         tsi = new ToolStripControlHost (c);
404
405                         Assert.AreEqual (new Size (100, 20), c.Size, "H3");
406                         Assert.AreEqual (new Size (100, 20), tsi.Size, "H4");
407
408                         c = new ComboBox ();
409                         tsi = new ToolStripControlHost (c);
410
411                         Assert.AreEqual (new Size (121, 21), c.Size, "H5");
412                         Assert.AreEqual (new Size (121, 21), tsi.Size, "H6");
413
414                         c = new ProgressBar ();
415                         tsi = new ToolStripControlHost (c);
416
417                         Assert.AreEqual (new Size (100, 23), c.Size, "H7");
418                         Assert.AreEqual (new Size (100, 23), tsi.Size, "H8");
419                         
420                         c = new PictureBox ();
421                         tsi = new ToolStripControlHost (c);
422
423                         Assert.AreEqual (new Size (100, 50), c.Size, "H7");
424                         Assert.AreEqual (new Size (100, 50), tsi.Size, "H8");
425                         
426                         Form f = new Form ();
427                         f.ShowInTaskbar = false;
428                         f.Controls.Add (ts);
429                         ts.Items.Add (tsi);
430                         f.Show ();
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");
434                 }
435                 
436                 private class EventWatcher
437                 {
438                         private string events = string.Empty;
439                         
440                         public EventWatcher (ToolStripControlHost tsi)
441                         {
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;"); });
451                         }
452
453                         public override string ToString ()
454                         {
455                                 return events.TrimEnd (';');
456                         }
457                         
458                         public void Clear ()
459                         {
460                                 events = string.Empty;
461                         }
462                 }
463                 
464                 private class ExposeProtectedProperties : ToolStripControlHost
465                 {
466                         public ExposeProtectedProperties () : base (new Control ()) {}
467                         
468                         public new Size DefaultSize { get { return base.DefaultSize; } }
469                 }
470         }
471 }
472 #endif