WebControlTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. //
  2. // Tests for System.Web.UI.WebControls.WebControl.cs
  3. //
  4. // Author:
  5. // Peter Dennis Bartok ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Drawing;
  33. using System.IO;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.UI.WebControls;
  38. namespace MonoTests.System.Web.UI.WebControls
  39. {
  40. [TestFixture]
  41. public class WebControlTest {
  42. private static HtmlTextWriter GetWriter () {
  43. StringWriter sw = new StringWriter ();
  44. sw.NewLine = "\n";
  45. return new HtmlTextWriter (sw);
  46. }
  47. private bool IsEqual(object[] a1, object[] a2, string assertion) {
  48. int matches;
  49. bool[] notfound;
  50. if (a1.Length != a2.Length) {
  51. if (assertion != null) {
  52. Assert.Fail(assertion + "( different length )");
  53. }
  54. return false;
  55. }
  56. matches = 0;
  57. notfound = new bool[a1.Length];
  58. for (int i = 0; i < a1.Length; i++) {
  59. for (int j = 0; j < a2.Length; j++) {
  60. if (a1[i].Equals(a2[j])) {
  61. matches++;
  62. break;
  63. }
  64. }
  65. if ((assertion != null) && (matches != i+1)) {
  66. Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
  67. }
  68. }
  69. return matches == a1.Length;
  70. }
  71. public class NamingContainer : WebControl, INamingContainer {
  72. }
  73. public class WebControlTestClass : WebControl {
  74. public WebControlTestClass() : base() {
  75. }
  76. public WebControlTestClass(string tag) : base(tag) {
  77. }
  78. public WebControlTestClass(HtmlTextWriterTag tag) : base(tag) {
  79. }
  80. public new HtmlTextWriterTag TagKey {
  81. get {
  82. return base.TagKey;
  83. }
  84. }
  85. public new string TagName {
  86. get {
  87. return base.TagName;
  88. }
  89. }
  90. public StateBag Bag {
  91. get {
  92. return base.ViewState;
  93. }
  94. }
  95. public override bool EnableViewState {
  96. get {
  97. return base.EnableViewState;
  98. }
  99. }
  100. public string Render () {
  101. HtmlTextWriter writer;
  102. writer = WebControlTest.GetWriter();
  103. base.Render (writer);
  104. return writer.InnerWriter.ToString ();
  105. }
  106. public bool IsTrackingVS ()
  107. {
  108. return IsTrackingViewState;
  109. }
  110. public void SetTrackingVS ()
  111. {
  112. TrackViewState ();
  113. }
  114. public object Save() {
  115. return base.SaveViewState();
  116. }
  117. public void Load(object o) {
  118. base.LoadViewState(o);
  119. }
  120. public string[] KeyValuePairs() {
  121. IEnumerator e;
  122. string[] result;
  123. int item;
  124. e = ViewState.GetEnumerator();
  125. result = new string[ViewState.Keys.Count];
  126. item = 0;
  127. while (e.MoveNext()) {
  128. DictionaryEntry d;
  129. StateItem si;
  130. d = (DictionaryEntry)e.Current;
  131. si = (StateItem)d.Value;
  132. if (si.Value is String[]) {
  133. string[] values;
  134. values = (string[]) si.Value;
  135. result[item] = d.Key.ToString() + "=";
  136. if (values.Length > 0) {
  137. result[item] += values[0];
  138. for (int i = 1; i < values.Length; i++) {
  139. result[item] += ", " + values[i];
  140. }
  141. }
  142. } else {
  143. result[item] = d.Key.ToString() + "=" + si.Value;
  144. }
  145. item++;
  146. }
  147. return result;
  148. }
  149. }
  150. [Test]
  151. public void Constructors ()
  152. {
  153. WebControlTestClass w;
  154. w = new WebControlTestClass();
  155. Assert.AreEqual(Color.Empty, w.BackColor, "C1");
  156. Assert.AreEqual(HtmlTextWriterTag.Span, w.TagKey, "C2");
  157. Assert.AreEqual("span", w.TagName, "C3");
  158. w = new WebControlTestClass("Small");
  159. Assert.AreEqual(HtmlTextWriterTag.Unknown, w.TagKey, "C4");
  160. Assert.AreEqual("Small", w.TagName, "C5");
  161. w = new WebControlTestClass(HtmlTextWriterTag.Small);
  162. Assert.AreEqual(HtmlTextWriterTag.Small, w.TagKey, "C5");
  163. Assert.AreEqual("small", w.TagName, "C6");
  164. }
  165. [Test]
  166. public void StyleCreation () {
  167. WebControlTestClass w;
  168. w = new WebControlTestClass(HtmlTextWriterTag.Small);
  169. Assert.AreEqual(HtmlTextWriterTag.Small, w.TagKey, "C5");
  170. Assert.AreEqual("small", w.TagName, "C6");
  171. Assert.AreEqual(false, w.ControlStyleCreated, "C7"); // No style
  172. Assert.AreEqual(Color.Empty, w.BackColor, "C8"); // Force style creation?
  173. Assert.AreEqual(false, w.ControlStyleCreated, "C9"); // Nope, 'get' access didn't create it
  174. w.BackColor = Color.Red; // Forces style creation!
  175. Assert.AreEqual(Color.Red, w.BackColor, "C10");
  176. Assert.AreEqual(true, w.ControlStyleCreated, "C11"); // Now we have a style
  177. w = new WebControlTestClass(HtmlTextWriterTag.Script);
  178. Assert.AreEqual(HtmlTextWriterTag.Script, w.TagKey, "C12");
  179. Assert.AreEqual("script", w.TagName, "C13");
  180. Assert.AreEqual(false, w.ControlStyleCreated, "C14"); // Double-check
  181. Assert.IsNotNull(w.ControlStyle, "C15"); // Grab style, forcing creation
  182. Assert.AreEqual(true, w.ControlStyleCreated, "C16"); // Double-check
  183. }
  184. [Test]
  185. public void Defaults () {
  186. WebControlTestClass w;
  187. w = new WebControlTestClass(HtmlTextWriterTag.Small);
  188. Assert.AreEqual ("", w.AccessKey, "D1");
  189. Assert.AreEqual (0, w.Attributes.Count, "D2");
  190. Assert.AreEqual (Color.Empty, w.BackColor, "D3");
  191. Assert.AreEqual (Color.Empty, w.BorderColor, "D4");
  192. Assert.AreEqual (BorderStyle.NotSet, w.BorderStyle, "D5");
  193. Assert.AreEqual (Unit.Empty, w.BorderWidth, "D6");
  194. Assert.AreEqual (string.Empty, w.CssClass, "D7");
  195. Assert.AreEqual (true, w.Enabled, "D8");
  196. Assert.AreEqual (Color.Empty, w.ForeColor, "D9");
  197. Assert.AreEqual (Unit.Empty, w.Height, "D10");
  198. Assert.AreEqual (0, w.Style.Count, "D11");
  199. Assert.AreEqual (0, w.TabIndex, "D12");
  200. Assert.AreEqual ("", w.ToolTip, "D13");
  201. Assert.AreEqual (Unit.Empty, w.Width, "D14");
  202. }
  203. [Test]
  204. public void Assignment () {
  205. WebControlTestClass w;
  206. w = new WebControlTestClass(HtmlTextWriterTag.Small);
  207. w.BackColor = Color.Red;
  208. Assert.AreEqual (Color.Red, w.BackColor, "A1");
  209. w.Attributes["test"] = "testme";
  210. Assert.AreEqual (1, w.Attributes.Count, "A2");
  211. Assert.AreEqual ("testme", w.Attributes["test"], "A3");
  212. w.BorderColor = Color.Green;
  213. Assert.AreEqual (Color.Green, w.BorderColor, "A4");
  214. w.BorderStyle = BorderStyle.Dotted;
  215. Assert.AreEqual (BorderStyle.Dotted, w.BorderStyle, "A5");
  216. w.BorderWidth = new Unit("12px");
  217. Assert.AreEqual (12, w.BorderWidth.Value, "A6");
  218. Assert.AreEqual (string.Empty, w.CssClass, "A7");
  219. w.Enabled = false;
  220. Assert.AreEqual (false, w.Enabled, "A8");
  221. w.ForeColor = Color.BlueViolet;
  222. Assert.AreEqual (Color.BlueViolet, w.ForeColor, "A9");
  223. w.Height = new Unit(6.5);
  224. Assert.AreEqual (6, w.Height.Value, "A10");
  225. Assert.AreEqual (0, w.Style.Count, "A11");
  226. w.TabIndex = 10;
  227. Assert.AreEqual (10, w.TabIndex, "A12");
  228. w.ToolTip = "I am a tip";
  229. Assert.AreEqual ("I am a tip", w.ToolTip, "A13");
  230. w.Width = new Unit(6.5, UnitType.Cm);
  231. Assert.AreEqual (6.5, w.Width.Value, "A14");
  232. Assert.AreEqual(false, w.IsTrackingVS (), "A15");
  233. w.SetTrackingVS ();
  234. Assert.AreEqual(true, w.IsTrackingVS (), "A16");
  235. w.Enabled = true;
  236. Assert.AreEqual(true, w.Enabled, "A17");
  237. w.Save();
  238. w.Attributes["PrivateTag"] = "blah";
  239. Assert.AreEqual(2, w.Attributes.Count, "A18");
  240. w.Attributes.Clear();
  241. w.Attributes["Style"] = "background-color: #ff00ff";
  242. Assert.AreEqual(1, w.Attributes.Count, "A19");
  243. Assert.AreEqual(1, w.Style.Count, "A20");
  244. w.Attributes.Clear();
  245. w.Attributes.Add("Style", "foreground-color=#ff00ff");
  246. Assert.AreEqual(1, w.Attributes.Count, "A21");
  247. Assert.AreEqual(0, w.Style.Count, "A22");
  248. w.Attributes.Clear();
  249. w.Attributes.Add("Style", "background: black; text-align: left;");
  250. Assert.AreEqual(1, w.Attributes.Count, "A23");
  251. Assert.AreEqual(2, w.Style.Count, "A24");
  252. w.Attributes["Style"] = "background: black; text-align: left; foreground: white;";
  253. Assert.AreEqual(1, w.Attributes.Count, "A25");
  254. Assert.AreEqual(3, w.Style.Count, "A26");
  255. w.Style["background-color"] = Color.Purple.ToString();
  256. Assert.AreEqual(4, w.Style.Count, "A27");
  257. w.AccessKey = "I";
  258. Assert.AreEqual("I", w.AccessKey, "A28");
  259. // Check the bag
  260. string[] expect = {
  261. "BorderStyle=Dotted",
  262. "Width=6.5cm",
  263. "Height=6px",
  264. "BorderWidth=12px",
  265. "ForeColor=Color [BlueViolet]",
  266. "BorderColor=Color [Green]",
  267. "ToolTip=I am a tip",
  268. "BackColor=Color [Red]",
  269. "TabIndex=10",
  270. "AccessKey=I",
  271. "Enabled=True" };
  272. IsEqual(expect, w.KeyValuePairs(), "A29");
  273. }
  274. [Test]
  275. public void Methods() {
  276. WebControlTestClass w;
  277. WebControlTestClass w_copy;
  278. w = new WebControlTestClass(HtmlTextWriterTag.Xml);
  279. w_copy = new WebControlTestClass(HtmlTextWriterTag.Xml);
  280. w.Enabled = true;
  281. w.AccessKey = "I";
  282. w.Attributes["Argl"] = "Arglbla";
  283. w.Attributes.Add("Style", "background: black; text-align: left;");
  284. Assert.AreEqual(2, w.Attributes.Count, "M1");
  285. Assert.AreEqual(2, w.Style.Count, "M2");
  286. w_copy.TabIndex = 10;
  287. w_copy.Attributes["Blah"] = "blahblah";
  288. Assert.AreEqual(0, w.TabIndex, "M3");
  289. Assert.AreEqual(10, w_copy.TabIndex, "M4");
  290. w_copy.CopyBaseAttributes(w);
  291. Assert.AreEqual(10, w_copy.TabIndex, "M5");
  292. Assert.AreEqual("blahblah", w_copy.Attributes["Blah"], "M6");
  293. Assert.AreEqual("Arglbla", w_copy.Attributes["Argl"], "M7");
  294. // Styles should make it over, too
  295. Assert.AreEqual("black", w_copy.Style["background"], "M8");
  296. // Check the bag
  297. string[] expect = {
  298. "TabIndex=10",
  299. "AccessKey=I" };
  300. IsEqual(expect, w_copy.KeyValuePairs(), "M9");
  301. Assert.AreEqual("<xml accesskey=\"I\" Argl=\"Arglbla\" style=\"background: black; text-align: left;\">\n\n</xml>", w.Render(), "M10");
  302. Assert.AreEqual("<xml accesskey=\"I\" tabindex=\"10\" Blah=\"blahblah\" Argl=\"Arglbla\" style=\"background: black; text-align: left;\">\n\n</xml>", w_copy.Render(), "M11");
  303. }
  304. [Test]
  305. public void CopyEnabled ()
  306. {
  307. Label l = new Label (), ll = new Label ();
  308. l.Enabled = false;
  309. ll.CopyBaseAttributes (l);
  310. Assert.IsFalse (ll.Enabled, "enabled should be copied");
  311. }
  312. [Test]
  313. public void RenderClientId ()
  314. {
  315. NamingContainer container = new NamingContainer ();
  316. WebControlTestClass child = new WebControlTestClass ();
  317. container.Controls.Add (child);
  318. container.ID = "naming";
  319. child.ID = "fooid";
  320. Assert.AreEqual ("<span id=\"naming_fooid\"></span>", child.Render (), "A1");
  321. }
  322. [Test]
  323. public void ViewState() {
  324. WebControlTestClass w;
  325. WebControlTestClass w_copy;
  326. object state;
  327. w = new WebControlTestClass(HtmlTextWriterTag.Xml);
  328. w_copy = new WebControlTestClass(HtmlTextWriterTag.Xml);
  329. w.SetTrackingVS ();
  330. w.BackColor = Color.Red;
  331. w.Attributes["test"] = "testme";
  332. w.BorderColor = Color.Green;
  333. w.BorderStyle = BorderStyle.Dotted;
  334. w.BorderWidth = new Unit("12px");
  335. w.Enabled = false;
  336. w.ForeColor = Color.BlueViolet;
  337. w.Height = new Unit(6.5);
  338. w.TabIndex = 10;
  339. w.ToolTip = "I am a tip";
  340. w.Width = new Unit(6.5, UnitType.Cm);
  341. w.Enabled = true;
  342. w.Attributes["PrivateTag"] = "blah";
  343. w.Attributes["Style"] = "background-color: #ff00ff";
  344. state = w.Save();
  345. w_copy.Load(state);
  346. w_copy.SetTrackingVS();
  347. /* MS: <xml tabindex="10" title="I am a tip" test="testme" PrivateTag="blah"
  348. style="color:BlueViolet;background-color:Red;border-color:Green;border-width:12px;border-style:Dotted;height:6px;width:6.5cm;background-color: #ff00ff">
  349. </xml>
  350. */
  351. Assert.AreEqual(w.Render(), w_copy.Render(), "VS1");
  352. }
  353. [Test]
  354. public void RenderBeginTag_TagOnly ()
  355. {
  356. HtmlTextWriter writer = WebControlTest.GetWriter ();
  357. WebControl wc = new WebControl (HtmlTextWriterTag.Table);
  358. wc.RenderBeginTag (writer);
  359. string s = writer.InnerWriter.ToString ();
  360. Assert.AreEqual ("<table>\n", s, "table");
  361. }
  362. [Test]
  363. public void RenderBeginTag_Attributes ()
  364. {
  365. HtmlTextWriter writer = WebControlTest.GetWriter ();
  366. WebControl wc = new WebControl (HtmlTextWriterTag.Table);
  367. wc.ID = "test1";
  368. wc.RenderBeginTag (writer);
  369. string s = writer.InnerWriter.ToString ();
  370. Assert.AreEqual ("<table id=\"test1\">\n", s, "ID");
  371. writer = WebControlTest.GetWriter ();
  372. wc.ID = null;
  373. Assert.IsNull (wc.ID, "ID");
  374. wc.RenderBeginTag (writer);
  375. s = writer.InnerWriter.ToString ();
  376. Assert.AreEqual ("<table>\n", s, "-ID");
  377. }
  378. [Test]
  379. public void RenderBeginTag_Style ()
  380. {
  381. HtmlTextWriter writer = WebControlTest.GetWriter ();
  382. WebControl wc = new WebControl (HtmlTextWriterTag.Table);
  383. wc.BackColor = Color.Aqua;
  384. wc.RenderBeginTag (writer);
  385. string s = writer.InnerWriter.ToString ();
  386. Assert.AreEqual ("<table style=\"background-color:Aqua;\">\n", s, "BackColor");
  387. writer = WebControlTest.GetWriter ();
  388. wc.BackColor = new Color ();
  389. Assert.IsTrue (wc.BackColor.IsEmpty, "IsEmpty");
  390. wc.RenderBeginTag (writer);
  391. s = writer.InnerWriter.ToString ();
  392. Assert.AreEqual ("<table>\n", s, "-BackColor");
  393. }
  394. [Test]
  395. public void EmptyStringTag ()
  396. {
  397. WebControlTestClass wc = new WebControlTestClass (String.Empty);
  398. Assert.AreEqual ("<>\n\n</>", wc.Render ());
  399. }
  400. [Test]
  401. public void NullStringTag ()
  402. {
  403. WebControlTestClass wc = new WebControlTestClass (null);
  404. Assert.AreEqual ("<>\n\n</>", wc.Render ());
  405. }
  406. [Test]
  407. public void UnknownTag ()
  408. {
  409. WebControlTestClass wc = new WebControlTestClass (HtmlTextWriterTag.Unknown);
  410. Assert.AreEqual ("<>\n\n</>", wc.Render ());
  411. }
  412. [Test]
  413. public void EnabledViewState ()
  414. {
  415. WebControlTestClass c = new WebControlTestClass ();
  416. c.SetTrackingVS ();
  417. c.Enabled = false;
  418. object o = c.Save ();
  419. c = new WebControlTestClass ();
  420. c.Load (o);
  421. Assert.IsFalse (c.Enabled, "not enabled");
  422. }
  423. }
  424. }