2 // Tests for System.Web.UI.WebControls.WebControl.cs
5 // Peter Dennis Bartok (pbartok@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit.Framework;
33 using System.Collections;
36 using System.Globalization;
39 using System.Web.UI.WebControls;
40 using MonoTests.stand_alone.WebHarness;
42 namespace MonoTests.System.Web.UI.WebControls
45 public class WebControlTest {
46 private static HtmlTextWriter GetWriter () {
47 StringWriter sw = new StringWriter ();
49 return new HtmlTextWriter (sw);
52 private bool IsEqual(object[] a1, object[] a2, string assertion) {
56 if (a1.Length != a2.Length) {
57 if (assertion != null) {
58 Assert.Fail(assertion + "( different length )");
64 notfound = new bool[a1.Length];
66 for (int i = 0; i < a1.Length; i++) {
67 for (int j = 0; j < a2.Length; j++) {
68 if (a1[i].Equals(a2[j])) {
73 if ((assertion != null) && (matches != i+1)) {
74 Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
78 return matches == a1.Length;
81 public class CustomControl : WebControl
83 public virtual string CustomProperty {
85 return (string) ViewState ["CustomProperty"];
88 ViewState ["CustomProperty"] = value;
92 protected override Style CreateControlStyle () {
96 public void DoTrackViewState () {
100 public object DoSaveViewState () {
101 return SaveViewState ();
104 public void DoLoadViewState (object state) {
105 LoadViewState (state);
109 public class CustomControl2 : CustomControl
111 protected override Style CreateControlStyle () {
112 Style style = new Style (ViewState);
113 style.BackColor = Color.Blue;
118 public class NamingContainer : WebControl, INamingContainer {
122 public class WebControlTestClass : WebControl {
123 public WebControlTestClass() : base() {
126 public WebControlTestClass(string tag) : base(tag) {
129 public WebControlTestClass(HtmlTextWriterTag tag) : base(tag) {
132 public new HtmlTextWriterTag TagKey {
138 public new string TagName {
144 public StateBag Bag {
146 return base.ViewState;
150 public override bool EnableViewState {
152 return base.EnableViewState;
156 public string Render () {
157 HtmlTextWriter writer;
159 writer = WebControlTest.GetWriter();
160 base.Render (writer);
161 return writer.InnerWriter.ToString ();
164 public bool IsTrackingVS ()
166 return IsTrackingViewState;
169 public void SetTrackingVS ()
174 public object Save() {
175 return base.SaveViewState();
178 public void Load(object o) {
179 base.LoadViewState(o);
182 public string[] KeyValuePairs() {
187 e = ViewState.GetEnumerator();
188 result = new string[ViewState.Keys.Count];
191 while (e.MoveNext()) {
195 d = (DictionaryEntry)e.Current;
196 si = (StateItem)d.Value;
198 if (si.Value is String[]) {
201 values = (string[]) si.Value;
202 result[item] = d.Key.ToString() + "=";
203 if (values.Length > 0) {
204 result[item] += values[0];
206 for (int i = 1; i < values.Length; i++) {
207 result[item] += ", " + values[i];
211 result[item] = d.Key.ToString() + "=" + si.Value;
219 public Style DoCreateControlStyle () {
220 return base.CreateControlStyle ();
225 public void CreateControlStyle () {
226 WebControlTestClass w = new WebControlTestClass ();
227 Assert.AreEqual (false, w.ControlStyleCreated, "CreateControlStyle#1");
228 Style s = w.DoCreateControlStyle ();
229 Assert.AreEqual (false, w.ControlStyleCreated, "CreateControlStyle#2");
231 Assert.AreEqual (true, w.ControlStyleCreated, "CreateControlStyle#3");
235 public void Constructors ()
237 WebControlTestClass w;
239 w = new WebControlTestClass();
240 Assert.AreEqual(Color.Empty, w.BackColor, "C1");
241 Assert.AreEqual(HtmlTextWriterTag.Span, w.TagKey, "C2");
242 Assert.AreEqual("span", w.TagName, "C3");
244 w = new WebControlTestClass("Small");
245 Assert.AreEqual(HtmlTextWriterTag.Unknown, w.TagKey, "C4");
246 Assert.AreEqual("Small", w.TagName, "C5");
248 w = new WebControlTestClass(HtmlTextWriterTag.Small);
249 Assert.AreEqual(HtmlTextWriterTag.Small, w.TagKey, "C5");
250 Assert.AreEqual("small", w.TagName, "C6");
254 public void StyleCreation () {
255 WebControlTestClass w;
257 w = new WebControlTestClass(HtmlTextWriterTag.Small);
258 Assert.AreEqual(HtmlTextWriterTag.Small, w.TagKey, "C5");
259 Assert.AreEqual("small", w.TagName, "C6");
261 Assert.AreEqual(false, w.ControlStyleCreated, "C7"); // No style
262 Assert.AreEqual(Color.Empty, w.BackColor, "C8"); // Force style creation?
263 Assert.AreEqual(false, w.ControlStyleCreated, "C9"); // Nope, 'get' access didn't create it
265 w.BackColor = Color.Red; // Forces style creation!
266 Assert.AreEqual(Color.Red, w.BackColor, "C10");
267 Assert.AreEqual(true, w.ControlStyleCreated, "C11"); // Now we have a style
269 w = new WebControlTestClass(HtmlTextWriterTag.Script);
270 Assert.AreEqual(HtmlTextWriterTag.Script, w.TagKey, "C12");
271 Assert.AreEqual("script", w.TagName, "C13");
272 Assert.AreEqual(false, w.ControlStyleCreated, "C14"); // Double-check
273 Assert.IsNotNull(w.ControlStyle, "C15"); // Grab style, forcing creation
274 Assert.AreEqual(true, w.ControlStyleCreated, "C16"); // Double-check
278 public void Defaults () {
279 WebControlTestClass w;
281 w = new WebControlTestClass(HtmlTextWriterTag.Small);
283 Assert.AreEqual ("", w.AccessKey, "D1");
284 Assert.AreEqual (0, w.Attributes.Count, "D2");
285 Assert.AreEqual (Color.Empty, w.BackColor, "D3");
286 Assert.AreEqual (Color.Empty, w.BorderColor, "D4");
287 Assert.AreEqual (BorderStyle.NotSet, w.BorderStyle, "D5");
288 Assert.AreEqual (Unit.Empty, w.BorderWidth, "D6");
289 Assert.AreEqual (string.Empty, w.CssClass, "D7");
290 Assert.AreEqual (true, w.Enabled, "D8");
291 Assert.AreEqual (Color.Empty, w.ForeColor, "D9");
292 Assert.AreEqual (Unit.Empty, w.Height, "D10");
293 Assert.AreEqual (0, w.Style.Count, "D11");
294 Assert.AreEqual (0, w.TabIndex, "D12");
295 Assert.AreEqual ("", w.ToolTip, "D13");
296 Assert.AreEqual (Unit.Empty, w.Width, "D14");
300 public void Assignment () {
301 WebControlTestClass w;
303 w = new WebControlTestClass(HtmlTextWriterTag.Small);
305 w.BackColor = Color.Red;
306 Assert.AreEqual (Color.Red, w.BackColor, "A1");
308 w.Attributes["test"] = "testme";
309 Assert.AreEqual (1, w.Attributes.Count, "A2");
310 Assert.AreEqual ("testme", w.Attributes["test"], "A3");
312 w.BorderColor = Color.Green;
313 Assert.AreEqual (Color.Green, w.BorderColor, "A4");
315 w.BorderStyle = BorderStyle.Dotted;
316 Assert.AreEqual (BorderStyle.Dotted, w.BorderStyle, "A5");
318 w.BorderWidth = new Unit("12px");
319 Assert.AreEqual (12, w.BorderWidth.Value, "A6");
321 Assert.AreEqual (string.Empty, w.CssClass, "A7");
324 Assert.AreEqual (false, w.Enabled, "A8");
326 w.ForeColor = Color.BlueViolet;
327 Assert.AreEqual (Color.BlueViolet, w.ForeColor, "A9");
329 w.Height = new Unit(6.5);
330 Assert.AreEqual (6, w.Height.Value, "A10");
332 Assert.AreEqual (0, w.Style.Count, "A11");
335 Assert.AreEqual (10, w.TabIndex, "A12");
337 w.ToolTip = "I am a tip";
338 Assert.AreEqual ("I am a tip", w.ToolTip, "A13");
340 w.Width = new Unit(6.5, UnitType.Cm);
341 Assert.AreEqual (6.5, w.Width.Value, "A14");
343 Assert.AreEqual(false, w.IsTrackingVS (), "A15");
345 Assert.AreEqual(true, w.IsTrackingVS (), "A16");
348 Assert.AreEqual(true, w.Enabled, "A17");
351 w.Attributes["PrivateTag"] = "blah";
352 Assert.AreEqual(2, w.Attributes.Count, "A18");
354 w.Attributes.Clear();
355 w.Attributes["Style"] = "background-color: #ff00ff";
356 Assert.AreEqual(1, w.Attributes.Count, "A19");
357 Assert.AreEqual(1, w.Style.Count, "A20");
359 w.Attributes.Clear();
360 w.Attributes.Add("Style", "foreground-color=#ff00ff");
361 Assert.AreEqual(1, w.Attributes.Count, "A21");
362 Assert.AreEqual(0, w.Style.Count, "A22");
364 w.Attributes.Clear();
365 w.Attributes.Add("Style", "background: black; text-align: left;");
366 Assert.AreEqual(1, w.Attributes.Count, "A23");
367 Assert.AreEqual(2, w.Style.Count, "A24");
369 w.Attributes["Style"] = "background: black; text-align: left; foreground: white;";
370 Assert.AreEqual(1, w.Attributes.Count, "A25");
371 Assert.AreEqual(3, w.Style.Count, "A26");
373 w.Style["background-color"] = Color.Purple.ToString();
374 Assert.AreEqual(4, w.Style.Count, "A27");
377 Assert.AreEqual("I", w.AccessKey, "A28");
381 "BorderStyle=Dotted",
385 "ForeColor=Color [BlueViolet]",
386 "BorderColor=Color [Green]",
387 "ToolTip=I am a tip",
388 "BackColor=Color [Red]",
393 IsEqual(expect, w.KeyValuePairs(), "A29");
397 public void Methods() {
398 WebControlTestClass w;
399 WebControlTestClass w_copy;
401 w = new WebControlTestClass(HtmlTextWriterTag.Xml);
402 w_copy = new WebControlTestClass(HtmlTextWriterTag.Xml);
406 w.Attributes["Argl"] = "Arglbla";
407 w.Attributes.Add("Style", "background: black; text-align: left;");
408 Assert.AreEqual(2, w.Attributes.Count, "M1");
409 Assert.AreEqual(2, w.Style.Count, "M2");
410 w_copy.TabIndex = 10;
411 w_copy.Attributes["Blah"] = "blahblah";
413 Assert.AreEqual(0, w.TabIndex, "M3");
414 Assert.AreEqual(10, w_copy.TabIndex, "M4");
415 w_copy.CopyBaseAttributes(w);
417 Assert.AreEqual(10, w_copy.TabIndex, "M5");
418 Assert.AreEqual("blahblah", w_copy.Attributes["Blah"], "M6");
419 Assert.AreEqual("Arglbla", w_copy.Attributes["Argl"], "M7");
421 // Styles should make it over, too
422 Assert.AreEqual("black", w_copy.Style["background"], "M8");
429 IsEqual(expect, w_copy.KeyValuePairs(), "M9");
431 Assert.AreEqual("<xml accesskey=\"I\" Argl=\"Arglbla\" style=\"background: black; text-align: left;\">\n\n</xml>", w.Render(), "M10");
432 Assert.AreEqual("<xml accesskey=\"I\" tabindex=\"10\" Blah=\"blahblah\" Argl=\"Arglbla\" style=\"background: black; text-align: left;\">\n\n</xml>", w_copy.Render(), "M11");
436 public void CopyEnabled ()
438 Label l = new Label (), ll = new Label ();
440 ll.CopyBaseAttributes (l);
441 Assert.IsFalse (ll.Enabled, "enabled should be copied");
446 public void RenderClientId ()
448 NamingContainer container = new NamingContainer ();
449 WebControlTestClass child = new WebControlTestClass ();
451 container.Controls.Add (child);
452 container.ID = "naming";
455 Assert.AreEqual ("<span id=\"naming_fooid\"></span>", child.Render (), "A1");
460 public void ViewState() {
461 WebControlTestClass w;
462 WebControlTestClass w_copy;
465 w = new WebControlTestClass(HtmlTextWriterTag.Xml);
466 w_copy = new WebControlTestClass(HtmlTextWriterTag.Xml);
469 w.BackColor = Color.Red;
470 w.Attributes["test"] = "testme";
471 w.BorderColor = Color.Green;
472 w.BorderStyle = BorderStyle.Dotted;
473 w.BorderWidth = new Unit("12px");
475 w.ForeColor = Color.BlueViolet;
476 w.Height = new Unit(6.5);
478 w.ToolTip = "I am a tip";
479 w.Width = new Unit(6.5, UnitType.Cm);
481 w.Attributes["PrivateTag"] = "blah";
482 w.Attributes["Style"] = "background-color: #ff00ff";
487 w_copy.SetTrackingVS();
488 /* MS: <xml tabindex="10" title="I am a tip" test="testme" PrivateTag="blah"
489 style="color:BlueViolet;background-color:Red;border-color:Green;border-width:12px;border-style:Dotted;height:6px;width:6.5cm;background-color: #ff00ff">
492 HtmlDiff.AssertAreEqual (w.Render(), w_copy.Render(), "VS1");
496 public void ViewState2 () {
497 CustomControl c = new CustomControl ();
498 CustomControl copy = new CustomControl ();
501 c.DoTrackViewState ();
502 c.CustomProperty = "CustomProperty";
503 c.ControlStyle.BackColor = Color.Red;
504 c.ControlStyle.BorderColor = Color.Green;
505 c.ControlStyle.BorderStyle = BorderStyle.Dotted;
507 state = c.DoSaveViewState ();
509 copy.DoLoadViewState (state);
511 Assert.IsFalse (copy.ControlStyleCreated, "copy.ControlStyleCreated");
515 public void ViewState3 () {
516 CustomControl2 c = new CustomControl2 ();
517 CustomControl2 copy = new CustomControl2 ();
520 c.DoTrackViewState ();
521 c.ControlStyle.BackColor = Color.Red;
523 state = c.DoSaveViewState ();
525 copy.DoLoadViewState (state);
527 Assert.AreEqual (Color.Blue, copy.ControlStyle.BackColor, "copy.BackColor");
532 public void RenderBeginTag_TagOnly ()
534 HtmlTextWriter writer = WebControlTest.GetWriter ();
535 WebControl wc = new WebControl (HtmlTextWriterTag.Table);
536 wc.RenderBeginTag (writer);
537 string s = writer.InnerWriter.ToString ();
538 Assert.AreEqual ("<table>\n", s, "table");
542 public void RenderBeginTag_Attributes ()
544 HtmlTextWriter writer = WebControlTest.GetWriter ();
545 WebControl wc = new WebControl (HtmlTextWriterTag.Table);
547 wc.RenderBeginTag (writer);
548 string s = writer.InnerWriter.ToString ();
549 Assert.AreEqual ("<table id=\"test1\">\n", s, "ID");
551 writer = WebControlTest.GetWriter ();
553 Assert.IsNull (wc.ID, "ID");
554 wc.RenderBeginTag (writer);
555 s = writer.InnerWriter.ToString ();
556 Assert.AreEqual ("<table>\n", s, "-ID");
560 public void RenderBeginTag_Style ()
562 HtmlTextWriter writer = WebControlTest.GetWriter ();
563 WebControl wc = new WebControl (HtmlTextWriterTag.Table);
564 wc.BackColor = Color.Aqua;
565 wc.RenderBeginTag (writer);
566 string s = writer.InnerWriter.ToString ();
567 Assert.AreEqual ("<table style=\"background-color:Aqua;\">\n", s, "BackColor");
569 writer = WebControlTest.GetWriter ();
570 wc.BackColor = new Color ();
571 Assert.IsTrue (wc.BackColor.IsEmpty, "IsEmpty");
572 wc.RenderBeginTag (writer);
573 s = writer.InnerWriter.ToString ();
574 Assert.AreEqual ("<table>\n", s, "-BackColor");
578 public void RenderBeginTag_BorderWidth_span ()
580 HtmlTextWriter writer = WebControlTest.GetWriter ();
581 WebControl wc = new WebControl (HtmlTextWriterTag.Span);
582 wc.BorderWidth = Unit.Pixel (1);
583 wc.RenderBeginTag (writer);
584 string s = writer.InnerWriter.ToString ();
586 Assert.AreEqual ("<span style=\"display:inline-block;border-width:1px;border-style:solid;\">", s, "BorderWidth");
588 Assert.AreEqual ("<span style=\"border-width:1px;border-style:solid;\">", s, "BorderWidth");
593 public void RenderBeginTag_BorderWidth_table ()
595 HtmlTextWriter writer = WebControlTest.GetWriter ();
596 WebControl wc = new WebControl (HtmlTextWriterTag.Table);
597 wc.BorderWidth = Unit.Pixel (1);
598 wc.RenderBeginTag (writer);
599 string s = writer.InnerWriter.ToString ();
600 Assert.AreEqual ("<table style=\"border-width:1px;border-style:solid;\">\n", s, "BorderWidth");
604 public void EmptyStringTag ()
606 WebControlTestClass wc = new WebControlTestClass (String.Empty);
607 Assert.AreEqual ("<>\n\n</>", wc.Render ());
611 public void NullStringTag ()
613 WebControlTestClass wc = new WebControlTestClass (null);
614 Assert.AreEqual ("<>\n\n</>", wc.Render ());
618 public void UnknownTag ()
620 WebControlTestClass wc = new WebControlTestClass (HtmlTextWriterTag.Unknown);
621 Assert.AreEqual ("<>\n\n</>", wc.Render ());
625 public void EnabledViewState ()
627 WebControlTestClass c = new WebControlTestClass ();
630 object o = c.Save ();
631 c = new WebControlTestClass ();
633 Assert.IsFalse (c.Enabled, "not enabled");
637 public void AttributeIsCaseInsensitive ()
639 WebControlTestClass c = new WebControlTestClass ();
640 c.Attributes ["hola"] = "hello";
641 c.Attributes ["HOla"] = "hi";
642 Assert.AreEqual ("hi", c.Attributes ["hoLA"], "#01");