WebControlTest.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. using MonoTests.stand_alone.WebHarness;
  39. #if NET_2_0
  40. using System.Web.UI.Adapters;
  41. using System.Web.UI.WebControls.Adapters;
  42. #endif
  43. namespace MonoTests.System.Web.UI.WebControls
  44. {
  45. [TestFixture]
  46. public class WebControlTest {
  47. private static HtmlTextWriter GetWriter () {
  48. StringWriter sw = new StringWriter ();
  49. sw.NewLine = "\n";
  50. return new HtmlTextWriter (sw);
  51. }
  52. private bool IsEqual(object[] a1, object[] a2, string assertion) {
  53. int matches;
  54. bool[] notfound;
  55. if (a1.Length != a2.Length) {
  56. if (assertion != null) {
  57. Assert.Fail(assertion + "( different length )");
  58. }
  59. return false;
  60. }
  61. matches = 0;
  62. notfound = new bool[a1.Length];
  63. for (int i = 0; i < a1.Length; i++) {
  64. for (int j = 0; j < a2.Length; j++) {
  65. if (a1[i].Equals(a2[j])) {
  66. matches++;
  67. break;
  68. }
  69. }
  70. if ((assertion != null) && (matches != i+1)) {
  71. Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
  72. }
  73. }
  74. return matches == a1.Length;
  75. }
  76. public class CustomControl : WebControl
  77. {
  78. public virtual string CustomProperty {
  79. get {
  80. return (string) ViewState ["CustomProperty"];
  81. }
  82. set {
  83. ViewState ["CustomProperty"] = value;
  84. }
  85. }
  86. protected override Style CreateControlStyle () {
  87. return new Style ();
  88. }
  89. public void DoTrackViewState () {
  90. TrackViewState ();
  91. }
  92. public object DoSaveViewState () {
  93. return SaveViewState ();
  94. }
  95. public void DoLoadViewState (object state) {
  96. LoadViewState (state);
  97. }
  98. }
  99. public class CustomControl2 : CustomControl
  100. {
  101. protected override Style CreateControlStyle () {
  102. Style style = new Style (ViewState);
  103. style.BackColor = Color.Blue;
  104. return style;
  105. }
  106. }
  107. public class NamingContainer : WebControl, INamingContainer {
  108. }
  109. public class WebControlTestClass : WebControl {
  110. public WebControlTestClass() : base() {
  111. }
  112. public WebControlTestClass(string tag) : base(tag) {
  113. }
  114. public WebControlTestClass(HtmlTextWriterTag tag) : base(tag) {
  115. }
  116. public new HtmlTextWriterTag TagKey {
  117. get {
  118. return base.TagKey;
  119. }
  120. }
  121. public new string TagName {
  122. get {
  123. return base.TagName;
  124. }
  125. }
  126. public StateBag Bag {
  127. get {
  128. return base.ViewState;
  129. }
  130. }
  131. public override bool EnableViewState {
  132. get {
  133. return base.EnableViewState;
  134. }
  135. }
  136. public string Render () {
  137. HtmlTextWriter writer;
  138. writer = WebControlTest.GetWriter();
  139. base.Render (writer);
  140. return writer.InnerWriter.ToString ();
  141. }
  142. public bool IsTrackingVS ()
  143. {
  144. return IsTrackingViewState;
  145. }
  146. public void SetTrackingVS ()
  147. {
  148. TrackViewState ();
  149. }
  150. public object Save() {
  151. return base.SaveViewState();
  152. }
  153. public void Load(object o) {
  154. base.LoadViewState(o);
  155. }
  156. public string[] KeyValuePairs() {
  157. IEnumerator e;
  158. string[] result;
  159. int item;
  160. e = ViewState.GetEnumerator();
  161. result = new string[ViewState.Keys.Count];
  162. item = 0;
  163. while (e.MoveNext()) {
  164. DictionaryEntry d;
  165. StateItem si;
  166. d = (DictionaryEntry)e.Current;
  167. si = (StateItem)d.Value;
  168. if (si.Value is String[]) {
  169. string[] values;
  170. values = (string[]) si.Value;
  171. result[item] = d.Key.ToString() + "=";
  172. if (values.Length > 0) {
  173. result[item] += values[0];
  174. for (int i = 1; i < values.Length; i++) {
  175. result[item] += ", " + values[i];
  176. }
  177. }
  178. } else {
  179. result[item] = d.Key.ToString() + "=" + si.Value;
  180. }
  181. item++;
  182. }
  183. return result;
  184. }
  185. public Style DoCreateControlStyle () {
  186. return base.CreateControlStyle ();
  187. }
  188. }
  189. [Test]
  190. public void CreateControlStyle () {
  191. WebControlTestClass w = new WebControlTestClass ();
  192. Assert.AreEqual (false, w.ControlStyleCreated, "CreateControlStyle#1");
  193. Style s = w.DoCreateControlStyle ();
  194. Assert.AreEqual (false, w.ControlStyleCreated, "CreateControlStyle#2");
  195. s = w.ControlStyle;
  196. Assert.AreEqual (true, w.ControlStyleCreated, "CreateControlStyle#3");
  197. }
  198. [Test]
  199. public void Constructors ()
  200. {
  201. WebControlTestClass w;
  202. w = new WebControlTestClass();
  203. Assert.AreEqual(Color.Empty, w.BackColor, "C1");
  204. Assert.AreEqual(HtmlTextWriterTag.Span, w.TagKey, "C2");
  205. Assert.AreEqual("span", w.TagName, "C3");
  206. w = new WebControlTestClass("Small");
  207. Assert.AreEqual(HtmlTextWriterTag.Unknown, w.TagKey, "C4");
  208. Assert.AreEqual("Small", w.TagName, "C5");
  209. w = new WebControlTestClass(HtmlTextWriterTag.Small);
  210. Assert.AreEqual(HtmlTextWriterTag.Small, w.TagKey, "C5");
  211. Assert.AreEqual("small", w.TagName, "C6");
  212. }
  213. [Test]
  214. public void StyleCreation () {
  215. WebControlTestClass w;
  216. w = new WebControlTestClass(HtmlTextWriterTag.Small);
  217. Assert.AreEqual(HtmlTextWriterTag.Small, w.TagKey, "C5");
  218. Assert.AreEqual("small", w.TagName, "C6");
  219. Assert.AreEqual(false, w.ControlStyleCreated, "C7"); // No style
  220. Assert.AreEqual(Color.Empty, w.BackColor, "C8"); // Force style creation?
  221. Assert.AreEqual(false, w.ControlStyleCreated, "C9"); // Nope, 'get' access didn't create it
  222. w.BackColor = Color.Red; // Forces style creation!
  223. Assert.AreEqual(Color.Red, w.BackColor, "C10");
  224. Assert.AreEqual(true, w.ControlStyleCreated, "C11"); // Now we have a style
  225. w = new WebControlTestClass(HtmlTextWriterTag.Script);
  226. Assert.AreEqual(HtmlTextWriterTag.Script, w.TagKey, "C12");
  227. Assert.AreEqual("script", w.TagName, "C13");
  228. Assert.AreEqual(false, w.ControlStyleCreated, "C14"); // Double-check
  229. Assert.IsNotNull(w.ControlStyle, "C15"); // Grab style, forcing creation
  230. Assert.AreEqual(true, w.ControlStyleCreated, "C16"); // Double-check
  231. }
  232. [Test]
  233. public void Defaults () {
  234. WebControlTestClass w;
  235. w = new WebControlTestClass(HtmlTextWriterTag.Small);
  236. Assert.AreEqual ("", w.AccessKey, "D1");
  237. Assert.AreEqual (0, w.Attributes.Count, "D2");
  238. Assert.AreEqual (Color.Empty, w.BackColor, "D3");
  239. Assert.AreEqual (Color.Empty, w.BorderColor, "D4");
  240. Assert.AreEqual (BorderStyle.NotSet, w.BorderStyle, "D5");
  241. Assert.AreEqual (Unit.Empty, w.BorderWidth, "D6");
  242. Assert.AreEqual (string.Empty, w.CssClass, "D7");
  243. Assert.AreEqual (true, w.Enabled, "D8");
  244. Assert.AreEqual (Color.Empty, w.ForeColor, "D9");
  245. Assert.AreEqual (Unit.Empty, w.Height, "D10");
  246. Assert.AreEqual (0, w.Style.Count, "D11");
  247. Assert.AreEqual (0, w.TabIndex, "D12");
  248. Assert.AreEqual ("", w.ToolTip, "D13");
  249. Assert.AreEqual (Unit.Empty, w.Width, "D14");
  250. }
  251. [Test]
  252. public void Assignment () {
  253. WebControlTestClass w;
  254. w = new WebControlTestClass(HtmlTextWriterTag.Small);
  255. w.BackColor = Color.Red;
  256. Assert.AreEqual (Color.Red, w.BackColor, "A1");
  257. w.Attributes["test"] = "testme";
  258. Assert.AreEqual (1, w.Attributes.Count, "A2");
  259. Assert.AreEqual ("testme", w.Attributes["test"], "A3");
  260. w.BorderColor = Color.Green;
  261. Assert.AreEqual (Color.Green, w.BorderColor, "A4");
  262. w.BorderStyle = BorderStyle.Dotted;
  263. Assert.AreEqual (BorderStyle.Dotted, w.BorderStyle, "A5");
  264. w.BorderWidth = new Unit("12px");
  265. Assert.AreEqual (12, w.BorderWidth.Value, "A6");
  266. Assert.AreEqual (string.Empty, w.CssClass, "A7");
  267. w.Enabled = false;
  268. Assert.AreEqual (false, w.Enabled, "A8");
  269. w.ForeColor = Color.BlueViolet;
  270. Assert.AreEqual (Color.BlueViolet, w.ForeColor, "A9");
  271. w.Height = new Unit(6.5);
  272. Assert.AreEqual (6, w.Height.Value, "A10");
  273. Assert.AreEqual (0, w.Style.Count, "A11");
  274. w.TabIndex = 10;
  275. Assert.AreEqual (10, w.TabIndex, "A12");
  276. w.ToolTip = "I am a tip";
  277. Assert.AreEqual ("I am a tip", w.ToolTip, "A13");
  278. w.Width = new Unit(6.5, UnitType.Cm);
  279. Assert.AreEqual (6.5, w.Width.Value, "A14");
  280. Assert.AreEqual(false, w.IsTrackingVS (), "A15");
  281. w.SetTrackingVS ();
  282. Assert.AreEqual(true, w.IsTrackingVS (), "A16");
  283. w.Enabled = true;
  284. Assert.AreEqual(true, w.Enabled, "A17");
  285. w.Save();
  286. w.Attributes["PrivateTag"] = "blah";
  287. Assert.AreEqual(2, w.Attributes.Count, "A18");
  288. w.Attributes.Clear();
  289. w.Attributes["Style"] = "background-color: #ff00ff";
  290. Assert.AreEqual(1, w.Attributes.Count, "A19");
  291. Assert.AreEqual(1, w.Style.Count, "A20");
  292. w.Attributes.Clear();
  293. w.Attributes.Add("Style", "foreground-color=#ff00ff");
  294. Assert.AreEqual(1, w.Attributes.Count, "A21");
  295. Assert.AreEqual(0, w.Style.Count, "A22");
  296. w.Attributes.Clear();
  297. w.Attributes.Add("Style", "background: black; text-align: left;");
  298. Assert.AreEqual(1, w.Attributes.Count, "A23");
  299. Assert.AreEqual(2, w.Style.Count, "A24");
  300. w.Attributes["Style"] = "background: black; text-align: left; foreground: white;";
  301. Assert.AreEqual(1, w.Attributes.Count, "A25");
  302. Assert.AreEqual(3, w.Style.Count, "A26");
  303. w.Style["background-color"] = Color.Purple.ToString();
  304. Assert.AreEqual(4, w.Style.Count, "A27");
  305. w.AccessKey = "I";
  306. Assert.AreEqual("I", w.AccessKey, "A28");
  307. // Check the bag
  308. string[] expect = {
  309. "BorderStyle=Dotted",
  310. "Width=6.5cm",
  311. "Height=6px",
  312. "BorderWidth=12px",
  313. "ForeColor=Color [BlueViolet]",
  314. "BorderColor=Color [Green]",
  315. "ToolTip=I am a tip",
  316. "BackColor=Color [Red]",
  317. "TabIndex=10",
  318. "AccessKey=I",
  319. "Enabled=True" };
  320. IsEqual(expect, w.KeyValuePairs(), "A29");
  321. }
  322. [Test]
  323. public void Methods() {
  324. WebControlTestClass w;
  325. WebControlTestClass w_copy;
  326. w = new WebControlTestClass(HtmlTextWriterTag.Xml);
  327. w_copy = new WebControlTestClass(HtmlTextWriterTag.Xml);
  328. w.Enabled = true;
  329. w.AccessKey = "I";
  330. w.Attributes["Argl"] = "Arglbla";
  331. w.Attributes.Add("Style", "background: black; text-align: left;");
  332. Assert.AreEqual(2, w.Attributes.Count, "M1");
  333. Assert.AreEqual(2, w.Style.Count, "M2");
  334. w_copy.TabIndex = 10;
  335. w_copy.Attributes["Blah"] = "blahblah";
  336. Assert.AreEqual(0, w.TabIndex, "M3");
  337. Assert.AreEqual(10, w_copy.TabIndex, "M4");
  338. w_copy.CopyBaseAttributes(w);
  339. Assert.AreEqual(10, w_copy.TabIndex, "M5");
  340. Assert.AreEqual("blahblah", w_copy.Attributes["Blah"], "M6");
  341. Assert.AreEqual("Arglbla", w_copy.Attributes["Argl"], "M7");
  342. // Styles should make it over, too
  343. Assert.AreEqual("black", w_copy.Style["background"], "M8");
  344. // Check the bag
  345. string[] expect = {
  346. "TabIndex=10",
  347. "AccessKey=I" };
  348. IsEqual(expect, w_copy.KeyValuePairs(), "M9");
  349. Assert.AreEqual("<xml accesskey=\"I\" Argl=\"Arglbla\" style=\"background: black; text-align: left;\">\n\n</xml>", w.Render(), "M10");
  350. Assert.AreEqual("<xml accesskey=\"I\" tabindex=\"10\" Blah=\"blahblah\" Argl=\"Arglbla\" style=\"background: black; text-align: left;\">\n\n</xml>", w_copy.Render(), "M11");
  351. }
  352. [Test]
  353. public void CopyEnabled ()
  354. {
  355. Label l = new Label (), ll = new Label ();
  356. l.Enabled = false;
  357. ll.CopyBaseAttributes (l);
  358. Assert.IsFalse (ll.Enabled, "enabled should be copied");
  359. WebControlTestClass c = new WebControlTestClass ();
  360. c.SetTrackingVS ();
  361. c.CopyBaseAttributes (l);
  362. object o = c.Save ();
  363. c = new WebControlTestClass ();
  364. c.Load (o);
  365. Assert.IsFalse (c.Enabled, "enabled should be copied#2");
  366. }
  367. [Test]
  368. public void RenderClientId ()
  369. {
  370. NamingContainer container = new NamingContainer ();
  371. WebControlTestClass child = new WebControlTestClass ();
  372. container.Controls.Add (child);
  373. container.ID = "naming";
  374. child.ID = "fooid";
  375. Assert.AreEqual ("<span id=\"naming_fooid\"></span>", child.Render (), "A1");
  376. }
  377. [Test]
  378. public void ViewState() {
  379. WebControlTestClass w;
  380. WebControlTestClass w_copy;
  381. object state;
  382. w = new WebControlTestClass(HtmlTextWriterTag.Xml);
  383. w_copy = new WebControlTestClass(HtmlTextWriterTag.Xml);
  384. w.SetTrackingVS ();
  385. w.BackColor = Color.Red;
  386. w.Attributes["test"] = "testme";
  387. w.BorderColor = Color.Green;
  388. w.BorderStyle = BorderStyle.Dotted;
  389. w.BorderWidth = new Unit("12px");
  390. w.Enabled = false;
  391. w.ForeColor = Color.BlueViolet;
  392. w.Height = new Unit(6.5);
  393. w.TabIndex = 10;
  394. w.ToolTip = "I am a tip";
  395. w.Width = new Unit(6.5, UnitType.Cm);
  396. w.Enabled = true;
  397. w.Attributes["PrivateTag"] = "blah";
  398. w.Attributes["Style"] = "background-color: #ff00ff";
  399. state = w.Save();
  400. w_copy.Load(state);
  401. w_copy.SetTrackingVS();
  402. /* MS: <xml tabindex="10" title="I am a tip" test="testme" PrivateTag="blah"
  403. style="color:BlueViolet;background-color:Red;border-color:Green;border-width:12px;border-style:Dotted;height:6px;width:6.5cm;background-color: #ff00ff">
  404. </xml>
  405. */
  406. HtmlDiff.AssertAreEqual (w.Render(), w_copy.Render(), "VS1");
  407. }
  408. [Test]
  409. public void ViewState2 () {
  410. CustomControl c = new CustomControl ();
  411. CustomControl copy = new CustomControl ();
  412. object state;
  413. c.DoTrackViewState ();
  414. c.CustomProperty = "CustomProperty";
  415. c.ControlStyle.BackColor = Color.Red;
  416. c.ControlStyle.BorderColor = Color.Green;
  417. c.ControlStyle.BorderStyle = BorderStyle.Dotted;
  418. state = c.DoSaveViewState ();
  419. copy.DoLoadViewState (state);
  420. Assert.IsFalse (copy.ControlStyleCreated, "copy.ControlStyleCreated");
  421. }
  422. [Test]
  423. public void ViewState3 () {
  424. CustomControl2 c = new CustomControl2 ();
  425. CustomControl2 copy = new CustomControl2 ();
  426. object state;
  427. c.DoTrackViewState ();
  428. c.ControlStyle.BackColor = Color.Red;
  429. state = c.DoSaveViewState ();
  430. copy.DoLoadViewState (state);
  431. Assert.AreEqual (Color.Blue, copy.ControlStyle.BackColor, "copy.BackColor");
  432. }
  433. [Test]
  434. public void RenderBeginTag_TagOnly ()
  435. {
  436. HtmlTextWriter writer = WebControlTest.GetWriter ();
  437. WebControl wc = new WebControl (HtmlTextWriterTag.Table);
  438. wc.RenderBeginTag (writer);
  439. string s = writer.InnerWriter.ToString ();
  440. Assert.AreEqual ("<table>\n", s, "table");
  441. }
  442. [Test]
  443. public void RenderBeginTag_Attributes ()
  444. {
  445. HtmlTextWriter writer = WebControlTest.GetWriter ();
  446. WebControl wc = new WebControl (HtmlTextWriterTag.Table);
  447. wc.ID = "test1";
  448. wc.RenderBeginTag (writer);
  449. string s = writer.InnerWriter.ToString ();
  450. Assert.AreEqual ("<table id=\"test1\">\n", s, "ID");
  451. writer = WebControlTest.GetWriter ();
  452. wc.ID = null;
  453. Assert.IsNull (wc.ID, "ID");
  454. wc.RenderBeginTag (writer);
  455. s = writer.InnerWriter.ToString ();
  456. Assert.AreEqual ("<table>\n", s, "-ID");
  457. }
  458. [Test]
  459. public void RenderBeginTag_Style ()
  460. {
  461. HtmlTextWriter writer = WebControlTest.GetWriter ();
  462. WebControl wc = new WebControl (HtmlTextWriterTag.Table);
  463. wc.BackColor = Color.Aqua;
  464. wc.RenderBeginTag (writer);
  465. string s = writer.InnerWriter.ToString ();
  466. Assert.AreEqual ("<table style=\"background-color:Aqua;\">\n", s, "BackColor");
  467. writer = WebControlTest.GetWriter ();
  468. wc.BackColor = new Color ();
  469. Assert.IsTrue (wc.BackColor.IsEmpty, "IsEmpty");
  470. wc.RenderBeginTag (writer);
  471. s = writer.InnerWriter.ToString ();
  472. Assert.AreEqual ("<table>\n", s, "-BackColor");
  473. }
  474. [Test]
  475. public void RenderBeginTag_BorderWidth_span ()
  476. {
  477. HtmlTextWriter writer = WebControlTest.GetWriter ();
  478. WebControl wc = new WebControl (HtmlTextWriterTag.Span);
  479. wc.BorderWidth = Unit.Pixel (1);
  480. wc.RenderBeginTag (writer);
  481. string s = writer.InnerWriter.ToString ();
  482. #if NET_2_0
  483. Assert.AreEqual ("<span style=\"display:inline-block;border-width:1px;border-style:solid;\">", s, "BorderWidth");
  484. #else
  485. Assert.AreEqual ("<span style=\"border-width:1px;border-style:solid;\">", s, "BorderWidth");
  486. #endif
  487. }
  488. [Test]
  489. public void RenderBeginTag_BorderWidth_table ()
  490. {
  491. HtmlTextWriter writer = WebControlTest.GetWriter ();
  492. WebControl wc = new WebControl (HtmlTextWriterTag.Table);
  493. wc.BorderWidth = Unit.Pixel (1);
  494. wc.RenderBeginTag (writer);
  495. string s = writer.InnerWriter.ToString ();
  496. Assert.AreEqual ("<table style=\"border-width:1px;border-style:solid;\">\n", s, "BorderWidth");
  497. }
  498. [Test]
  499. public void EmptyStringTag ()
  500. {
  501. WebControlTestClass wc = new WebControlTestClass (String.Empty);
  502. Assert.AreEqual ("<>\n\n</>", wc.Render ());
  503. }
  504. [Test]
  505. public void NullStringTag ()
  506. {
  507. WebControlTestClass wc = new WebControlTestClass (null);
  508. Assert.AreEqual ("<>\n\n</>", wc.Render ());
  509. }
  510. [Test]
  511. public void UnknownTag ()
  512. {
  513. WebControlTestClass wc = new WebControlTestClass (HtmlTextWriterTag.Unknown);
  514. Assert.AreEqual ("<>\n\n</>", wc.Render ());
  515. }
  516. [Test]
  517. public void EnabledViewState ()
  518. {
  519. WebControlTestClass c = new WebControlTestClass ();
  520. c.SetTrackingVS ();
  521. c.Enabled = false;
  522. object o = c.Save ();
  523. c = new WebControlTestClass ();
  524. c.Load (o);
  525. Assert.IsFalse (c.Enabled, "not enabled");
  526. }
  527. [Test]
  528. public void EnabledViewState2 () {
  529. WebControlTestClass c = new WebControlTestClass ();
  530. c.Enabled = false;
  531. c.SetTrackingVS ();
  532. c.Width = 100; // to cause saveviewstate return not null
  533. object o = c.Save ();
  534. c = new WebControlTestClass ();
  535. c.Enabled = false;
  536. c.Load (o);
  537. Assert.IsFalse (c.Enabled, "not enabled");
  538. }
  539. [Test]
  540. public void AttributeIsCaseInsensitive ()
  541. {
  542. WebControlTestClass c = new WebControlTestClass ();
  543. c.Attributes ["hola"] = "hello";
  544. c.Attributes ["HOla"] = "hi";
  545. Assert.AreEqual ("hi", c.Attributes ["hoLA"], "#01");
  546. }
  547. #if NET_2_0
  548. class MyControlAdapter : ControlAdapter
  549. {
  550. protected override void Render (HtmlTextWriter w)
  551. {
  552. w.WriteLine("MyControlAdapter.Render");
  553. }
  554. }
  555. class MyWebControlAdapter : WebControlAdapter
  556. {
  557. protected override void RenderBeginTag (HtmlTextWriter w)
  558. {
  559. w.WriteLine("RenderBeginTag");
  560. }
  561. protected override void RenderContents (HtmlTextWriter w)
  562. {
  563. w.WriteLine("RenderContents");
  564. }
  565. protected override void RenderEndTag (HtmlTextWriter w)
  566. {
  567. w.WriteLine("RenderEndTag");
  568. }
  569. }
  570. class MyWebControl : WebControl
  571. {
  572. public ControlAdapter my_control_adapter = new MyWebControlAdapter();
  573. protected override global::System.Web.UI.Adapters.ControlAdapter ResolveAdapter ()
  574. {
  575. return my_control_adapter;
  576. }
  577. public void DoRender(HtmlTextWriter writer)
  578. {
  579. Render(writer);
  580. }
  581. public bool GetIsEnabled
  582. {
  583. get
  584. {
  585. return IsEnabled;
  586. }
  587. }
  588. }
  589. [Test]
  590. public void RenderWithWebControlAdapter ()
  591. {
  592. MyWebControl c = new MyWebControl ();
  593. StringWriter sw = new StringWriter ();
  594. HtmlTextWriter w = new HtmlTextWriter (sw);
  595. c.DoRender (w);
  596. Assert.AreEqual ("RenderBeginTag\nRenderContents\nRenderEndTag\n", sw.ToString (), "RenderWithWebControlAdapter #1");
  597. }
  598. [Test]
  599. public void RenderWithControlAdapter ()
  600. {
  601. MyWebControl c = new MyWebControl ();
  602. c.my_control_adapter = new MyControlAdapter ();
  603. StringWriter sw = new StringWriter ();
  604. HtmlTextWriter w = new HtmlTextWriter (sw);
  605. c.DoRender (w);
  606. Assert.AreEqual ("MyControlAdapter.Render\n", sw.ToString (), "RenderWithControlAdapter #1");
  607. }
  608. [Test]
  609. public void IsEnabled ()
  610. {
  611. MyWebControl parent = new MyWebControl ();
  612. MyWebControl child = new MyWebControl ();
  613. parent.Controls.Add (child);
  614. Assert.IsTrue (child.GetIsEnabled, "IsEnabled #1");
  615. parent.Enabled = false;
  616. Assert.IsFalse (child.GetIsEnabled, "IsEnabled #2");
  617. parent.Enabled = true;
  618. child.Enabled = false;
  619. Assert.IsFalse (child.GetIsEnabled, "IsEnabled #3");
  620. }
  621. #endif
  622. }
  623. }