ControlTest.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. //
  2. // Tests for System.Web.UI.Control
  3. //
  4. // Author:
  5. // Peter Dennis Bartok ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  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.SystemWeb.Framework;
  39. using MonoTests.stand_alone.WebHarness;
  40. using MonoTests.Common;
  41. #if NET_2_0
  42. using System.Web.UI.Adapters;
  43. #endif
  44. #if NET_4_0
  45. using System.Web.Routing;
  46. #endif
  47. namespace MonoTests.System.Web.UI
  48. {
  49. [TestFixture]
  50. public class ControlTest
  51. {
  52. [Test]
  53. public void DataBindingInterfaceTest ()
  54. {
  55. Control c;
  56. DataBindingCollection db;
  57. c = new Control ();
  58. Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB1");
  59. db = ((IDataBindingsAccessor) c).DataBindings;
  60. Assert.IsNotNull (db, "DB2");
  61. Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB3");
  62. db.Add (new DataBinding ("property", typeof (bool), "expression"));
  63. Assert.AreEqual (true, ((IDataBindingsAccessor) c).HasDataBindings);
  64. }
  65. [Test] // bug #82546
  66. public void FindControl_NamingContainer ()
  67. {
  68. Control topControl = new NamingContainer ();
  69. topControl.ID = "top";
  70. Control controlLevel1A = new Control ();
  71. controlLevel1A.ID = "Level1#A";
  72. topControl.Controls.Add (controlLevel1A);
  73. Control controlLevel1B = new Control ();
  74. controlLevel1B.ID = "Level1#B";
  75. topControl.Controls.Add (controlLevel1B);
  76. Control controlLevel2AA = new Control ();
  77. controlLevel2AA.ID = "Level2#AA";
  78. controlLevel1A.Controls.Add (controlLevel2AA);
  79. Control controlLevel2AB = new Control ();
  80. controlLevel2AB.ID = "Level2#AB";
  81. controlLevel1A.Controls.Add (controlLevel2AB);
  82. Control foundControl = topControl.FindControl ("Level1#A");
  83. Assert.IsNotNull (foundControl, "#A1");
  84. Assert.AreSame (controlLevel1A, foundControl, "#A2");
  85. foundControl = topControl.FindControl ("LEVEL1#B");
  86. Assert.IsNotNull (foundControl, "#B1");
  87. Assert.AreSame (controlLevel1B, foundControl, "#B2");
  88. foundControl = topControl.FindControl ("LeVeL2#AB");
  89. Assert.IsNotNull (foundControl, "#C1");
  90. Assert.AreSame (controlLevel2AB, foundControl, "#C2");
  91. foundControl = topControl.FindControl ("doesnotexist");
  92. Assert.IsNull (foundControl, "#D1");
  93. foundControl = topControl.FindControl ("top");
  94. Assert.IsNull (foundControl, "#D2");
  95. foundControl = controlLevel1A.FindControl ("Level2#AA");
  96. Assert.IsNotNull (foundControl, "#E1");
  97. Assert.AreSame (controlLevel2AA, foundControl, "#E2");
  98. foundControl = controlLevel1A.FindControl ("LEveL2#ab");
  99. Assert.IsNotNull (foundControl, "#F1");
  100. Assert.AreSame (controlLevel2AB, foundControl, "#F2");
  101. }
  102. [Test]
  103. public void UniqueID1 ()
  104. {
  105. // Standalone NC
  106. Control nc = new MyNC ();
  107. Assert.IsNull (nc.UniqueID, "nulltest");
  108. }
  109. [Test]
  110. public void UniqueID1_1 () {
  111. // Standalone NC
  112. Control nc = new MyNC ();
  113. Page p = new Page ();
  114. p.Controls.Add (nc);
  115. Assert.IsNotNull (nc.UniqueID, "notnull");
  116. }
  117. [Test]
  118. public void UniqueID2 ()
  119. {
  120. // NC in NC
  121. Control nc = new MyNC ();
  122. Control nc2 = new MyNC ();
  123. nc2.Controls.Add (nc);
  124. Assert.IsNotNull (nc.UniqueID, "notnull");
  125. Assert.IsTrue (nc.UniqueID.IndexOfAny (new char[] { ':', '$' }) == -1, "separator");
  126. }
  127. [Test]
  128. public void UniqueID2_1 () {
  129. // NC in NC
  130. Control nc = new MyNC ();
  131. Control nc2 = new MyNC ();
  132. nc2.Controls.Add (nc);
  133. Page p = new Page ();
  134. p.Controls.Add (nc2);
  135. Assert.IsNotNull (nc.UniqueID, "notnull");
  136. Assert.IsTrue (nc.UniqueID.IndexOfAny (new char [] { ':', '$' }) != -1, "separator");
  137. }
  138. [Test]
  139. public void UniqueID3 ()
  140. {
  141. // NC in control
  142. Control control = new Control ();
  143. Control nc = new MyNC ();
  144. control.Controls.Add (nc);
  145. Assert.IsNull (nc.UniqueID, "null");
  146. }
  147. [Test]
  148. public void UniqueID4 ()
  149. {
  150. // NC in control
  151. Control control = new Control ();
  152. Control nc = new MyNC ();
  153. nc.Controls.Add (control);
  154. Assert.IsNotNull (control.UniqueID, "notnull");
  155. }
  156. [Test]
  157. public void UniqueID5 ()
  158. {
  159. // NC in control
  160. Control control = new Control ();
  161. Control nc = new MyNC ();
  162. Control nc2 = new MyNC ();
  163. nc2.Controls.Add (nc);
  164. nc.Controls.Add (control);
  165. Assert.IsNotNull (control.UniqueID, "notnull");
  166. Assert.IsNull (nc2.ID, "null-1");
  167. Assert.IsNull (nc.ID, "null-2");
  168. Assert.IsTrue (-1 != control.UniqueID.IndexOfAny (new char[] { ':', '$' }), "separator");
  169. }
  170. [Test]
  171. public void UniqueID6 () {
  172. // NC in NC
  173. Control nc = new MyNC ();
  174. Page p = new Page ();
  175. p.Controls.Add (nc);
  176. Assert.IsNotNull (nc.UniqueID, "notnull");
  177. Control c1 = new Control ();
  178. Control c2 = new Control ();
  179. nc.Controls.Add (c1);
  180. nc.Controls.Add (c2);
  181. string uid1_1 = c1.UniqueID;
  182. string uid2_1 = c2.UniqueID;
  183. nc.Controls.Clear ();
  184. Assert.IsNull (c1.UniqueID);
  185. Assert.IsNull (c2.UniqueID);
  186. // ad in another order
  187. nc.Controls.Add (c2);
  188. nc.Controls.Add (c1);
  189. string uid1_2 = c1.UniqueID;
  190. string uid2_2 = c2.UniqueID;
  191. Assert.IsFalse (uid1_1 == uid1_2);
  192. Assert.IsFalse (uid2_1 == uid2_2);
  193. Assert.AreEqual (uid1_1, uid2_2);
  194. Assert.AreEqual (uid2_1, uid1_2);
  195. nc.Controls.Remove (c1);
  196. nc.Controls.Add (c1);
  197. string uid1_3 = c1.UniqueID;
  198. Assert.IsFalse (uid1_3 == uid1_2, "id was not reset");
  199. #if NET_2_0
  200. EnsureIDControl c3 = new EnsureIDControl ();
  201. nc.Controls.Add (c3);
  202. string uid3_1 = c3.UniqueID;
  203. c3.DoEnsureID ();
  204. Assert.IsNotNull (c3.ID);
  205. nc.Controls.Remove (c3);
  206. nc.Controls.Add (c3);
  207. string uid3_2 = c3.UniqueID;
  208. Assert.IsNull (c3.ID);
  209. Assert.IsFalse (uid3_1 == uid3_2, "id was not reset");
  210. #endif
  211. }
  212. [Test]
  213. public void ClientID ()
  214. {
  215. // NC in control
  216. Control control = new Control ();
  217. Control nc = new MyNC ();
  218. Control nc2 = new MyNC ();
  219. Control nc3 = new MyNC ();
  220. nc3.Controls.Add (nc2);
  221. nc2.Controls.Add (nc);
  222. nc.Controls.Add (control);
  223. #if NET_2_0
  224. string expected = "ctl00_ctl00_ctl00";
  225. #else
  226. string expected = "_ctl0__ctl0__ctl0";
  227. #endif
  228. Assert.AreEqual (expected, control.ClientID, "ClientID");
  229. }
  230. // From bug #76919: Control uses _controls instead of
  231. // Controls when RenderChildren is called.
  232. [Test]
  233. public void Controls1 ()
  234. {
  235. DerivedControl derived = new DerivedControl ();
  236. derived.Controls.Add (new LiteralControl ("hola"));
  237. StringWriter sw = new StringWriter ();
  238. HtmlTextWriter htw = new HtmlTextWriter (sw);
  239. derived.RenderControl (htw);
  240. string result = sw.ToString ();
  241. Assert.AreEqual ("", result, "#01");
  242. }
  243. [Test] // Bug #471305
  244. [Category ("NunitWeb")]
  245. public void NoDoubleOnInitOnRemoveAdd ()
  246. {
  247. WebTest.CopyResource (GetType (), "NoDoubleOnInitOnRemoveAdd.aspx", "NoDoubleOnInitOnRemoveAdd.aspx");
  248. WebTest.CopyResource (GetType (), "NoDoubleOnInitOnRemoveAdd.aspx.cs", "NoDoubleOnInitOnRemoveAdd.aspx.cs");
  249. WebTest t = new WebTest ("NoDoubleOnInitOnRemoveAdd.aspx");
  250. string html = t.Run ();
  251. Assert.AreEqual (-1, html.IndexOf ("<span>label</span><span>label</span>"), "#A1");
  252. }
  253. #if NET_2_0
  254. [Test]
  255. [Category("NunitWeb")]
  256. public void AppRelativeTemplateSourceDirectory ()
  257. {
  258. new WebTest(PageInvoker.CreateOnLoad(AppRelativeTemplateSourceDirectory_Load)).Run();
  259. }
  260. public static void AppRelativeTemplateSourceDirectory_Load(Page p)
  261. {
  262. Control ctrl = new Control();
  263. Assert.AreEqual("~/", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#1");
  264. ctrl.AppRelativeTemplateSourceDirectory = "~/Fake";
  265. Assert.AreEqual("~/Fake", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#2");
  266. }
  267. [Test]
  268. public void ApplyStyleSheetSkin ()
  269. {
  270. Page p = new Page ();
  271. p.StyleSheetTheme = "";
  272. Control c = new Control ();
  273. c.ApplyStyleSheetSkin (p);
  274. }
  275. [Test]
  276. [Category ("NunitWeb")]
  277. public void ApplyStyleSheetSkin_1 ()
  278. {
  279. WebTest.CopyResource (GetType (), "Theme2.skin", "App_Themes/Theme2/Theme2.skin");
  280. WebTest t = new WebTest ();
  281. PageDelegates pd = new PageDelegates ();
  282. pd.PreInit = ApplyStyleSheetSkin_PreInit;
  283. pd.Load = ApplyStyleSheetSkin_Load;
  284. t.Invoker = new PageInvoker (pd);
  285. string str = t.Run ();
  286. }
  287. public static void ApplyStyleSheetSkin_PreInit (Page p)
  288. {
  289. p.Theme = "Theme2";
  290. }
  291. public static void ApplyStyleSheetSkin_Load (Page p)
  292. {
  293. Label lbl = new Label ();
  294. lbl.ID = "StyleLbl";
  295. lbl.SkinID = "red";
  296. lbl.Text = "StyleLabel";
  297. p.Controls.Add (lbl);
  298. lbl.ApplyStyleSheetSkin (p);
  299. Assert.AreEqual (Color.Red, lbl.ForeColor, "ApplyStyleSheetSkin_BackColor");
  300. Assert.AreEqual ("TextFromSkinFile", lbl.Text, "ApplyStyleSheetSkin");
  301. }
  302. [Test]
  303. [Category ("NunitWeb")]
  304. public void ClearChildControlState ()
  305. {
  306. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildControlState_Load));
  307. t.Run ();
  308. FormRequest fr = new FormRequest (t.Response, "form1");
  309. fr.Controls.Add ("__EVENTTARGET");
  310. fr.Controls.Add ("__EVENTARGUMENT");
  311. fr.Controls["__EVENTTARGET"].Value = "";
  312. fr.Controls["__EVENTARGUMENT"].Value = "";
  313. t.Request = fr;
  314. t.Run ();
  315. }
  316. public static void ClearChildControlState_Load (Page p)
  317. {
  318. ControlWithState c1 = new ControlWithState ();
  319. p.Form.Controls.Add (c1);
  320. if (p.IsPostBack) {
  321. c1.ClearChildControlState ();
  322. }
  323. ControlWithState c2 = new ControlWithState ();
  324. c1.Controls.Add (c2);
  325. ControlWithState c3 = new ControlWithState ();
  326. c2.Controls.Add (c3);
  327. if (!p.IsPostBack) {
  328. c1.State = "State";
  329. c2.State = "Cool";
  330. c3.State = "SubCool";
  331. }
  332. else {
  333. Assert.AreEqual ("State", c1.State, "ControlState#1");
  334. Assert.AreEqual (null, c2.State, "ControlState#2");
  335. Assert.AreEqual (null, c3.State, "ControlState#2");
  336. }
  337. }
  338. [Test]
  339. [Category ("NunitWeb")]
  340. public void ClearChildState ()
  341. {
  342. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildState_Load));
  343. t.Run ();
  344. FormRequest fr = new FormRequest (t.Response, "form1");
  345. fr.Controls.Add ("__EVENTTARGET");
  346. fr.Controls.Add ("__EVENTARGUMENT");
  347. fr.Controls["__EVENTTARGET"].Value = "";
  348. fr.Controls["__EVENTARGUMENT"].Value = "";
  349. t.Request = fr;
  350. t.Run ();
  351. }
  352. public static void ClearChildState_Load (Page p)
  353. {
  354. ControlWithState c1 = new ControlWithState ();
  355. p.Form.Controls.Add (c1);
  356. if (p.IsPostBack) {
  357. c1.ClearChildState ();
  358. }
  359. ControlWithState c2 = new ControlWithState ();
  360. c1.Controls.Add (c2);
  361. ControlWithState c3 = new ControlWithState ();
  362. c2.Controls.Add (c3);
  363. if (!p.IsPostBack) {
  364. c1.State = "State";
  365. c2.State = "Cool";
  366. c2.Viewstate = "Very Cool";
  367. c3.State = "SubCool";
  368. c3.Viewstate = "Super Cool";
  369. }
  370. else {
  371. Assert.AreEqual ("State", c1.State, "ClearChildState#1");
  372. Assert.AreEqual (null, c2.State, "ClearChildState#2");
  373. Assert.AreEqual (null, c3.State, "ClearChildState#3");
  374. Assert.AreEqual (null, c2.Viewstate, "ClearChildState#4");
  375. Assert.AreEqual (null, c3.Viewstate, "ClearChildState#5");
  376. }
  377. }
  378. [Test]
  379. public void DataBind ()
  380. {
  381. MyNC ctrl = new MyNC ();
  382. ctrl.DataBinding += new EventHandler (ctrl_DataBinding);
  383. Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
  384. ctrl.DataBind (false);
  385. Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
  386. ctrl.DataBind (true);
  387. Assert.AreEqual (true, _eventDataBinding, "After DataBinding");
  388. }
  389. bool _eventDataBinding;
  390. void ctrl_DataBinding (object sender, EventArgs e)
  391. {
  392. _eventDataBinding = true;
  393. }
  394. [Test]
  395. public void DataBindChildren ()
  396. {
  397. MyNC ctrl1 = new MyNC ();
  398. Control ctrl2 = new Control ();
  399. Control ctrl3 = new Control ();
  400. ctrl2.DataBinding += new EventHandler (ctrl2_DataBinding);
  401. ctrl3.DataBinding += new EventHandler (ctrl3_DataBinding);
  402. ctrl2.Controls.Add (ctrl3);
  403. ctrl1.Controls.Add (ctrl2);
  404. Assert.AreEqual (false, _eventChild1, "Before DataBinding#1");
  405. Assert.AreEqual (false, _eventChild2, "Before DataBinding#2");
  406. ctrl1.DataBindChildren ();
  407. Assert.AreEqual (true, _eventChild1, "After DataBinding#1");
  408. Assert.AreEqual (true, _eventChild2, "After DataBinding#2");
  409. }
  410. bool _eventChild1;
  411. bool _eventChild2;
  412. void ctrl3_DataBinding (object sender, EventArgs e)
  413. {
  414. _eventChild1 = true;
  415. }
  416. void ctrl2_DataBinding (object sender, EventArgs e)
  417. {
  418. _eventChild2 = true;
  419. }
  420. [Test]
  421. public void EnsureID ()
  422. {
  423. MyNC ctrl = new MyNC ();
  424. MyNC ctrl1 = new MyNC ();
  425. ctrl.Controls.Add (ctrl1);
  426. Page p = new Page ();
  427. p.Controls.Add (ctrl);
  428. ctrl.EnsureID ();
  429. if (String.IsNullOrEmpty (ctrl.ID))
  430. Assert.Fail ("EnsureID#1");
  431. ctrl1.EnsureID ();
  432. if (String.IsNullOrEmpty (ctrl1.ID))
  433. Assert.Fail ("EnsureID#2");
  434. }
  435. [Test]
  436. [Category("NotWorking")]
  437. public void Focus ()
  438. {
  439. WebTest t = new WebTest (PageInvoker.CreateOnLoad (Focus_Load));
  440. string html = t.Run ();
  441. Assert.AreEqual (3, contain (html, "TestBox"), "Focus script not created");
  442. }
  443. public static void Focus_Load (Page p)
  444. {
  445. TextBox tbx = new TextBox ();
  446. tbx.ID = "TestBox";
  447. p.Controls.Add (tbx);
  448. tbx.Focus ();
  449. }
  450. int contain (string orig, string compare)
  451. {
  452. if (orig.IndexOf (compare) == -1)
  453. return 0;
  454. return 1 + contain (orig.Substring (orig.IndexOf (compare) + compare.Length), compare);
  455. }
  456. [Test]
  457. public void HasEvent ()
  458. {
  459. MyNC ctrl1 = new MyNC ();
  460. Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#1");
  461. EventHandler ctrl_hdlr = new EventHandler (ctrl1_Init);
  462. ctrl1.Init += new EventHandler (ctrl1_Init);
  463. ctrl1.Init += ctrl_hdlr;
  464. Assert.AreEqual (true, ctrl1.HasEvents (), "HasEvent#2");
  465. // Dosn't work than removed handler
  466. //ctrl1.Init -= ctrl_hdlr;
  467. //Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#3");
  468. }
  469. void ctrl1_Init (object sender, EventArgs e)
  470. {
  471. throw new Exception ("The method or operation is not implemented.");
  472. }
  473. [Test]
  474. public void IsViewStateEnabled ()
  475. {
  476. DerivedControl c = new DerivedControl ();
  477. Assert.IsTrue (c.DoIsViewStateEnabled);
  478. Page p = new Page ();
  479. c.Page = p;
  480. p.Controls.Add (c);
  481. Assert.IsTrue (c.DoIsViewStateEnabled);
  482. p.EnableViewState = false;
  483. Assert.IsFalse (c.DoIsViewStateEnabled);
  484. }
  485. [Test]
  486. [Category ("NunitWeb")]
  487. public void ControlState ()
  488. {
  489. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
  490. t.Run ();
  491. FormRequest fr = new FormRequest (t.Response, "form1");
  492. fr.Controls.Add ("__EVENTTARGET");
  493. fr.Controls.Add ("__EVENTARGUMENT");
  494. fr.Controls["__EVENTTARGET"].Value = "";
  495. fr.Controls["__EVENTARGUMENT"].Value = "";
  496. t.Request = fr;
  497. t.Run ();
  498. }
  499. public static void ControlState_Load (Page p)
  500. {
  501. ControlWithState c1 = new ControlWithState ();
  502. ControlWithState c2 = new ControlWithState ();
  503. c1.Controls.Add (c2);
  504. p.Form.Controls.Add (c1);
  505. if (!p.IsPostBack) {
  506. c1.State = "State";
  507. c2.State = "Cool";
  508. }
  509. else {
  510. ControlWithState c3 = new ControlWithState ();
  511. p.Form.Controls.Add (c3);
  512. Assert.AreEqual ("State", c1.State, "ControlState");
  513. Assert.AreEqual ("Cool", c2.State, "ControlState");
  514. }
  515. }
  516. [Test]
  517. [Category ("NunitWeb")]
  518. public void ControlState2 () {
  519. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState2_Load));
  520. t.Run ();
  521. FormRequest fr = new FormRequest (t.Response, "form1");
  522. fr.Controls.Add ("__EVENTTARGET");
  523. fr.Controls.Add ("__EVENTARGUMENT");
  524. fr.Controls ["__EVENTTARGET"].Value = "";
  525. fr.Controls ["__EVENTARGUMENT"].Value = "";
  526. t.Request = fr;
  527. t.Run ();
  528. fr = new FormRequest (t.Response, "form1");
  529. fr.Controls.Add ("__EVENTTARGET");
  530. fr.Controls.Add ("__EVENTARGUMENT");
  531. fr.Controls ["__EVENTTARGET"].Value = "";
  532. fr.Controls ["__EVENTARGUMENT"].Value = "";
  533. t.Request = fr;
  534. t.Run ();
  535. }
  536. public static void ControlState2_Load (Page p) {
  537. ControlWithState parent = new ControlWithState ();
  538. p.Form.Controls.Add (parent);
  539. if (!p.IsPostBack) {
  540. // emulate DataBind
  541. parent.Controls.Clear ();
  542. parent.ClearChildControlState ();
  543. ControlWithState c1 = new ControlWithState ();
  544. ControlWithState c2 = new ControlWithState ();
  545. parent.Controls.Add (c1);
  546. parent.Controls.Add (c2);
  547. c1.State = "State1_1";
  548. c2.State = "State1_2";
  549. parent.State = "First";
  550. }
  551. else if (parent.State == "First") {
  552. // emulate DataBind
  553. parent.Controls.Clear ();
  554. parent.ClearChildControlState ();
  555. ControlWithState c1 = new ControlWithState ();
  556. ControlWithState c2 = new ControlWithState ();
  557. parent.Controls.Add (c1);
  558. parent.Controls.Add (c2);
  559. c1.State = "State2_1";
  560. c2.State = "State2_2";
  561. parent.State = "Second";
  562. }
  563. else {
  564. // emulate CrerateChildControl only
  565. parent.Controls.Clear ();
  566. ControlWithState c1 = new ControlWithState ();
  567. ControlWithState c2 = new ControlWithState ();
  568. parent.Controls.Add (c1);
  569. parent.Controls.Add (c2);
  570. Assert.AreEqual ("State2_1", c1.State, "ControlState#1");
  571. Assert.AreEqual ("State2_2", c2.State, "ControlState#2");
  572. }
  573. }
  574. [Test]
  575. public void ClientIDSeparator ()
  576. {
  577. DerivedControl ctrl = new DerivedControl ();
  578. Assert.AreEqual (95, (int) ctrl.ClientIDSeparator, "ClientIDSeparator");
  579. }
  580. [Test]
  581. public void IDSeparator ()
  582. {
  583. DerivedControl ctrl = new DerivedControl ();
  584. Assert.AreEqual (36, (int) ctrl.IdSeparator, "IDSeparator");
  585. }
  586. [Test]
  587. [Category ("NunitWeb")]
  588. public void IsChildControlStateCleared ()
  589. {
  590. WebTest t = new WebTest (PageInvoker.CreateOnLoad (IsChildControlStateCleared_Load));
  591. t.Run ();
  592. FormRequest fr = new FormRequest (t.Response, "form1");
  593. fr.Controls.Add ("__EVENTTARGET");
  594. fr.Controls.Add ("__EVENTARGUMENT");
  595. fr.Controls["__EVENTTARGET"].Value = "";
  596. fr.Controls["__EVENTARGUMENT"].Value = "";
  597. t.Request = fr;
  598. t.Run ();
  599. }
  600. public static void IsChildControlStateCleared_Load (Page p)
  601. {
  602. ControlWithState c1 = new ControlWithState ();
  603. p.Form.Controls.Add (c1);
  604. if (p.IsPostBack) {
  605. Assert.IsFalse (c1.IsChildControlStateCleared, "ControlState#1");
  606. c1.ClearChildControlState ();
  607. Assert.IsTrue (c1.IsChildControlStateCleared, "ControlState#1");
  608. }
  609. ControlWithState c2 = new ControlWithState ();
  610. c1.Controls.Add (c2);
  611. ControlWithState c3 = new ControlWithState ();
  612. c2.Controls.Add (c3);
  613. if (p.IsPostBack) {
  614. Assert.IsFalse (c2.IsChildControlStateCleared, "ControlState#1");
  615. Assert.IsFalse (c3.IsChildControlStateCleared, "ControlState#1");
  616. }
  617. if (!p.IsPostBack) {
  618. c1.State = "State";
  619. c2.State = "Cool";
  620. c3.State = "SubCool";
  621. }
  622. }
  623. [Test]
  624. [Category ("NunitWeb")]
  625. public void LoadViewStateByID ()
  626. {
  627. ControlWithState c1 = new ControlWithState ();
  628. ControlWithState c2 = new ControlWithState ();
  629. c1.Controls.Add (c2);
  630. Assert.AreEqual (false, c1.LoadViewStateByID, "LoadViewStateByID#1");
  631. }
  632. [Test]
  633. [Category ("NunitWeb")]
  634. public void OpenFile ()
  635. {
  636. WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFile_Load));
  637. t.Run ();
  638. }
  639. public static void OpenFile_Load (Page p)
  640. {
  641. DerivedControl ctrl = new DerivedControl ();
  642. Stream strem = ctrl.OpenFile ("~/MyPage.aspx");
  643. Assert.IsNotNull (strem, "OpenFile failed");
  644. }
  645. [Test]
  646. [Category ("NunitWeb")]
  647. [ExpectedException (typeof (FileNotFoundException))]
  648. public void OpenFile_Exception ()
  649. {
  650. WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFileException_Load));
  651. t.Run ();
  652. }
  653. public static void OpenFileException_Load (Page p)
  654. {
  655. DerivedControl ctrl = new DerivedControl ();
  656. Stream strem = ctrl.OpenFile ("~/Fake.tmp");
  657. }
  658. [Test]
  659. [Category ("NunitWeb")]
  660. public void ResolveClientUrl ()
  661. {
  662. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveClientUrl_Load));
  663. string html = t.Run ();
  664. }
  665. public static void ResolveClientUrl_Load (Page p)
  666. {
  667. Control ctrl = new Control ();
  668. p.Controls.Add (ctrl);
  669. string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
  670. Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
  671. Assert.AreEqual ("", ctrl.ResolveClientUrl (""), "empty string");
  672. Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~"), "~");
  673. Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~/"), "~/");
  674. Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
  675. Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
  676. Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
  677. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveClientUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
  678. Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveClientUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
  679. Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  680. Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  681. Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveClientUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
  682. Assert.AreEqual ("NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  683. Assert.AreEqual ("NunitWeb/../MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
  684. Assert.AreEqual ("NunitWeb/./MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
  685. Assert.AreEqual ("http://google.com/", ctrl.ResolveClientUrl ("http://google.com/"), "ResolveClientUrl Failed");
  686. Assert.AreEqual ("MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  687. Assert.AreEqual ("../MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  688. Assert.AreEqual ("./MyPage.aspx", ctrl.ResolveClientUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
  689. Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
  690. }
  691. [Test]
  692. [Category ("NunitWeb")]
  693. public void ResolveClientUrl2 ()
  694. {
  695. WebTest t = new WebTest ("ResolveUrl.aspx");
  696. PageDelegates delegates = new PageDelegates ();
  697. delegates.Load = ResolveClientUrl2_Load;
  698. t.Invoker = new PageInvoker (delegates);
  699. string html = t.Run ();
  700. }
  701. public static void ResolveClientUrl2_Load (Page p)
  702. {
  703. Control uc = p.FindControl ("WebUserControl1");
  704. Control ctrl = uc.FindControl ("Label");
  705. string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
  706. Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
  707. Assert.AreEqual ("", ctrl.ResolveClientUrl (""), "empty string");
  708. Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~"), "~");
  709. Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~/"), "~/");
  710. Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
  711. Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
  712. Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
  713. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveClientUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
  714. Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveClientUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
  715. Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  716. Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  717. Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveClientUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
  718. Assert.AreEqual ("Folder/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  719. Assert.AreEqual ("Folder/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
  720. Assert.AreEqual ("Folder/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
  721. Assert.AreEqual ("http://google.com/", ctrl.ResolveClientUrl ("http://google.com/"), "ResolveClientUrl Failed");
  722. Assert.AreEqual ("MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  723. Assert.AreEqual ("../MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  724. Assert.AreEqual ("Folder/MyPage.aspx", ctrl.ResolveClientUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
  725. Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
  726. }
  727. [Test]
  728. [Category ("NunitWeb")]
  729. public void ResolveUrl ()
  730. {
  731. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveUrl_Load));
  732. string html = t.Run ();
  733. }
  734. public static void ResolveUrl_Load (Page p)
  735. {
  736. #if TARGET_JVM
  737. string appPath = "/MainsoftWebApp20";
  738. #else
  739. string appPath = "/NunitWeb";
  740. #endif
  741. Control ctrl = new Control ();
  742. p.Controls.Add (ctrl);
  743. Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~/MyPage.aspx"), "ResolveClientUrl Failed");
  744. Assert.AreEqual ("", ctrl.ResolveUrl (""), "empty string");
  745. Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~"), "~");
  746. Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~/"), "~/");
  747. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
  748. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
  749. Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
  750. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
  751. Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
  752. Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  753. Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  754. Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
  755. Assert.AreEqual (appPath + "/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  756. Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
  757. Assert.AreEqual (appPath + "/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
  758. Assert.AreEqual ("http://google.com/", ctrl.ResolveUrl ("http://google.com/"), "ResolveClientUrl Failed");
  759. Assert.AreEqual (appPath + "/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  760. Assert.AreEqual ("/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  761. Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
  762. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
  763. Assert.AreEqual ("/", ctrl.ResolveUrl (".."), "..");
  764. Assert.AreEqual ("/", ctrl.ResolveUrl ("../"), "../");
  765. }
  766. [Test]
  767. [Category ("NunitWeb")]
  768. public void ResolveUrl2 ()
  769. {
  770. WebTest t = new WebTest ("ResolveUrl.aspx");
  771. PageDelegates delegates = new PageDelegates ();
  772. delegates.Load = ResolveUrl2_Load;
  773. t.Invoker = new PageInvoker (delegates);
  774. string html = t.Run ();
  775. }
  776. public static void ResolveUrl2_Load (Page p)
  777. {
  778. #if TARGET_JVM
  779. string appPath = "/MainsoftWebApp20";
  780. #else
  781. string appPath = "/NunitWeb";
  782. #endif
  783. Control uc = p.FindControl ("WebUserControl1");
  784. Control ctrl = uc.FindControl ("Label");
  785. Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~/MyPage.aspx"), "ResolveClientUrl Failed");
  786. Assert.AreEqual ("", ctrl.ResolveUrl (""), "empty string");
  787. Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~"), "~");
  788. Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("~/"), "~/");
  789. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
  790. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
  791. Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
  792. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
  793. Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
  794. Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  795. Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  796. Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
  797. Assert.AreEqual (appPath + "/Folder/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  798. Assert.AreEqual (appPath + "/Folder/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
  799. Assert.AreEqual (appPath + "/Folder/NunitWeb/MyPage.aspx", ctrl.ResolveUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
  800. Assert.AreEqual ("http://google.com/", ctrl.ResolveUrl ("http://google.com/"), "ResolveClientUrl Failed");
  801. Assert.AreEqual (appPath + "/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  802. Assert.AreEqual ("/MyPage.aspx?param=val&yes=no", ctrl.ResolveUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  803. Assert.AreEqual (appPath + "/Folder/MyPage.aspx", ctrl.ResolveUrl ("./MyPage.aspx"), "ResolveClientUrl Failed");
  804. Assert.AreEqual (appPath + "/MyPage.aspx", ctrl.ResolveUrl ("../MyPage.aspx"), "ResolveClientUrl Failed");
  805. Assert.AreEqual (appPath + "/", ctrl.ResolveUrl (".."), "..");
  806. Assert.AreEqual (appPath + "/", ctrl.ResolveUrl ("../"), "../");
  807. Assert.AreEqual ("/", ctrl.ResolveUrl ("../.."), "../..");
  808. Assert.AreEqual ("/", ctrl.ResolveUrl ("../../"), "../../");
  809. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveUrl ("../../MyPage.aspx"), "../../MyPage.aspx");
  810. }
  811. [Test]
  812. [Category ("NotWorking")] // Not implemented exception
  813. public void ResolveAdapter_2 ()
  814. {
  815. DerivedControl ctrl = new DerivedControl ();
  816. Assert.AreEqual (null, ctrl.ResolveAdapter (), "ResolveAdapter");
  817. }
  818. [Test]
  819. public void EnableTheming ()
  820. {
  821. DerivedControl ctrl = new DerivedControl ();
  822. Assert.AreEqual (true, ctrl.EnableTheming, "EnableTheming#1");
  823. ctrl.EnableTheming = false;
  824. Assert.AreEqual (false, ctrl.EnableTheming, "EnableTheming#2");
  825. }
  826. #endif
  827. [Test]
  828. public void BindingContainer ()
  829. {
  830. ControlWithTemplate c = new ControlWithTemplate ();
  831. c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
  832. // cause CreateChildControls called
  833. c.FindControl ("stam");
  834. }
  835. static void BindingContainer_BuildTemplate (Control control)
  836. {
  837. Control child1 = new Control ();
  838. control.Controls.Add (child1);
  839. Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
  840. Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
  841. NamingContainer nc = new NamingContainer ();
  842. Control child2 = new Control ();
  843. nc.Controls.Add (child2);
  844. control.Controls.Add (nc);
  845. Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
  846. Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
  847. #if NET_2_0
  848. // DetailsViewPagerRow marked to be not BindingContainer
  849. DetailsViewPagerRow row = new DetailsViewPagerRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
  850. TableCell cell = new TableCell ();
  851. Control child3 = new Control ();
  852. cell.Controls.Add (child3);
  853. row.Cells.Add (cell);
  854. control.Controls.Add (row);
  855. Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
  856. Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
  857. #endif
  858. }
  859. #if NET_2_0
  860. [Test]
  861. public void Control_Adapter ()
  862. {
  863. MyNC ctr = new MyNC ();
  864. Assert.AreEqual (null, ctr.Adapter (), "Adapter");
  865. }
  866. #endif
  867. [Test]
  868. public void ChildControlsCreated () {
  869. ChildControlsCreatedControl ctr = new ChildControlsCreatedControl ();
  870. ctr.Controls.Add (new Control ());
  871. //ctr.DoEnsureChildControls ();
  872. Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#1");
  873. ctr.SetChildControlsCreated (false);
  874. Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#2");
  875. }
  876. #if NET_2_0
  877. [Test (Description="Bug #594238")]
  878. public void OverridenControlsPropertyAndPostBack_Bug594238 ()
  879. {
  880. WebTest t = new WebTest ("OverridenControlsPropertyAndPostBack_Bug594238.aspx");
  881. t.Run ();
  882. FormRequest fr = new FormRequest (t.Response, "form1");
  883. fr.Controls.Add ("__EVENTTARGET");
  884. fr.Controls.Add ("__EVENTARGUMENT");
  885. fr.Controls ["__EVENTTARGET"].Value = "container$children$lb";
  886. fr.Controls ["__EVENTARGUMENT"].Value = String.Empty;
  887. t.Request = fr;
  888. string originalHtml = @"<span id=""container""><a href=""javascript:__doPostBack('container$children$lb','')"" id=""container_children_lb"">Woot! I got clicked!</a></span><hr/>";
  889. string pageHtml = t.Run ();
  890. string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
  891. HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
  892. }
  893. [TestFixtureTearDown]
  894. public void Tear_down ()
  895. {
  896. WebTest.Unload ();
  897. }
  898. [TestFixtureSetUp]
  899. public void TestFixtureSetUp ()
  900. {
  901. WebTest.CopyResource (GetType (), "ResolveUrl.aspx", "ResolveUrl.aspx");
  902. WebTest.CopyResource (GetType (), "ResolveUrl.ascx", "Folder/ResolveUrl.ascx");
  903. WebTest.CopyResource (GetType (), "OverridenControlsPropertyAndPostBack_Bug594238.aspx", "OverridenControlsPropertyAndPostBack_Bug594238.aspx");
  904. }
  905. #endif
  906. #if NET_4_0
  907. [Test]
  908. public void GetRouteUrl_Object ()
  909. {
  910. var t = new WebTest (PageInvoker.CreateOnLoad (GetRouteUrl_Object_Load));
  911. t.Run ();
  912. }
  913. public static void GetRouteUrl_Object_Load (Page p)
  914. {
  915. RouteTable.Routes.Clear ();
  916. var ctl = new Control ();
  917. object obj = new { foo = "one", bar = "two" };
  918. string path = ctl.GetRouteUrl (obj);
  919. Assert.IsNull (path, "#A1");
  920. RouteTable.Routes.Add (new Route ("{foo}-{bar}", new PageRouteHandler ("~/default.aspx")));
  921. path = ctl.GetRouteUrl (obj);
  922. Assert.IsNotNull (path, "#A2-1");
  923. Assert.AreEqual ("/NunitWeb/one-two", path, "#A2-2");
  924. path = ctl.GetRouteUrl ((object)null);
  925. Assert.IsNull (path, "#A3");
  926. }
  927. [Test]
  928. public void GetRouteUrl_RouteValueDictionary ()
  929. {
  930. var t = new WebTest (PageInvoker.CreateOnLoad (GetRouteUrl_RouteValueDictionary_Load));
  931. t.Run ();
  932. }
  933. public static void GetRouteUrl_RouteValueDictionary_Load (Page p)
  934. {
  935. RouteTable.Routes.Clear ();
  936. var ctl = new Control ();
  937. var rvd = new RouteValueDictionary {
  938. {"foo", "one"},
  939. {"bar", "two"}
  940. };
  941. string path = ctl.GetRouteUrl (rvd);
  942. Assert.IsNull (path, "#A1");
  943. RouteTable.Routes.Add (new Route ("{foo}-{bar}", new PageRouteHandler ("~/default.aspx")));
  944. path = ctl.GetRouteUrl (rvd);
  945. Assert.IsNotNull (path, "#A2-1");
  946. Assert.AreEqual ("/NunitWeb/one-two", path, "#A2-2");
  947. path = ctl.GetRouteUrl ((RouteValueDictionary) null);
  948. Assert.IsNull (path, "#A3");
  949. }
  950. [Test]
  951. public void GetRouteUrl_String_Object ()
  952. {
  953. var t = new WebTest (PageInvoker.CreateOnLoad (GetRouteUrl_String_Object_Load));
  954. t.Run ();
  955. }
  956. public static void GetRouteUrl_String_Object_Load (Page p)
  957. {
  958. RouteTable.Routes.Clear ();
  959. var ctl = new Control ();
  960. object obj = new { foo = "one", bar = "two" };
  961. string path;
  962. AssertExtensions.Throws<ArgumentException> (() => {
  963. path = ctl.GetRouteUrl ("myroute1", obj);
  964. }, "#A1");
  965. RouteTable.Routes.Add (new Route ("{foo}-{bar}", new PageRouteHandler ("~/default.aspx")));
  966. RouteTable.Routes.Add ("myroute1", new Route ("{bar}-{foo}", new PageRouteHandler ("~/default.aspx")));
  967. path = ctl.GetRouteUrl ("myroute1", obj);
  968. Assert.IsNotNull (path, "#A2-1");
  969. Assert.AreEqual ("/NunitWeb/two-one", path, "#A2-2");
  970. path = ctl.GetRouteUrl ("myroute1", (object) null);
  971. Assert.IsNull (path, "#A3");
  972. }
  973. [Test]
  974. public void GetRouteUrl_String_RouteValueDictionary ()
  975. {
  976. var t = new WebTest (PageInvoker.CreateOnLoad (GetRouteUrl_String_RouteValueDictionary_Load));
  977. t.Run ();
  978. }
  979. public static void GetRouteUrl_String_RouteValueDictionary_Load (Page p)
  980. {
  981. RouteTable.Routes.Clear ();
  982. var ctl = new Control ();
  983. var rvd = new RouteValueDictionary {
  984. {"foo", "one"},
  985. {"bar", "two"}
  986. };
  987. string path;
  988. AssertExtensions.Throws<ArgumentException> (() => {
  989. path = ctl.GetRouteUrl ("myroute", rvd);
  990. }, "#A1");
  991. RouteTable.Routes.Add (new Route ("{foo}-{bar}", new PageRouteHandler ("~/default.aspx")));
  992. RouteTable.Routes.Add ("myroute", new Route ("{bar}-{foo}", new PageRouteHandler ("~/default.aspx")));
  993. path = ctl.GetRouteUrl ("myroute", rvd);
  994. Assert.IsNotNull (path, "#A2-1");
  995. Assert.AreEqual ("/NunitWeb/two-one", path, "#A2-2");
  996. path = ctl.GetRouteUrl ("myroute", (RouteValueDictionary) null);
  997. Assert.IsNull (path, "#A3-1");
  998. path = ctl.GetRouteUrl (null, (RouteValueDictionary) null);
  999. Assert.IsNull (path, "#A3-2");
  1000. path = ctl.GetRouteUrl (String.Empty, (RouteValueDictionary) null);
  1001. Assert.IsNull (path, "#A3-3");
  1002. }
  1003. #endif
  1004. #region helpcalsses
  1005. #if NET_2_0
  1006. class ControlWithState : Control
  1007. {
  1008. string _state;
  1009. public string State
  1010. {
  1011. get { return _state; }
  1012. set { _state = value; }
  1013. }
  1014. public string Viewstate {
  1015. get { return (string) ViewState ["Viewstate"]; }
  1016. set { ViewState ["Viewstate"] = value; }
  1017. }
  1018. protected override void OnInit (EventArgs e)
  1019. {
  1020. base.OnInit (e);
  1021. Page.RegisterRequiresControlState (this);
  1022. }
  1023. protected override object SaveControlState ()
  1024. {
  1025. return State;
  1026. }
  1027. protected override void LoadControlState (object savedState)
  1028. {
  1029. State = (string) savedState;
  1030. }
  1031. public new void ClearChildState ()
  1032. {
  1033. base.ClearChildState ();
  1034. }
  1035. public new void ClearChildControlState ()
  1036. {
  1037. base.ClearChildControlState ();
  1038. }
  1039. public new bool IsChildControlStateCleared
  1040. {
  1041. get { return base.IsChildControlStateCleared; }
  1042. }
  1043. public new bool LoadViewStateByID
  1044. {
  1045. get { return base.LoadViewStateByID; }
  1046. }
  1047. }
  1048. #endif
  1049. class MyNC : Control, INamingContainer
  1050. {
  1051. #if NET_2_0
  1052. public ControlAdapter Adapter ()
  1053. {
  1054. return base.Adapter;
  1055. }
  1056. public new void DataBind (bool opt)
  1057. {
  1058. base.DataBind (opt);
  1059. }
  1060. public new void DataBindChildren ()
  1061. {
  1062. base.DataBindChildren ();
  1063. }
  1064. public new void EnsureID ()
  1065. {
  1066. base.EnsureID ();
  1067. }
  1068. public new bool HasEvents ()
  1069. {
  1070. return base.HasEvents ();
  1071. }
  1072. #endif
  1073. }
  1074. class DerivedControl : Control
  1075. {
  1076. ControlCollection coll;
  1077. public DerivedControl ()
  1078. {
  1079. coll = new ControlCollection (this);
  1080. }
  1081. public override ControlCollection Controls
  1082. {
  1083. get { return coll; }
  1084. }
  1085. #if NET_2_0
  1086. public bool DoIsViewStateEnabled
  1087. {
  1088. get { return IsViewStateEnabled; }
  1089. }
  1090. public new char ClientIDSeparator
  1091. {
  1092. get { return base.ClientIDSeparator; }
  1093. }
  1094. public new char IdSeparator
  1095. {
  1096. get { return base.IdSeparator; }
  1097. }
  1098. public new Stream OpenFile (string path)
  1099. {
  1100. return base.OpenFile (path);
  1101. }
  1102. public new ControlAdapter ResolveAdapter ()
  1103. {
  1104. return base.ResolveAdapter ();
  1105. }
  1106. #endif
  1107. }
  1108. class NamingContainer : Control, INamingContainer
  1109. {
  1110. }
  1111. class ControlWithTemplate : Control
  1112. {
  1113. ITemplate _template;
  1114. [TemplateContainer (typeof (TemplateContainer))]
  1115. public ITemplate Template
  1116. {
  1117. get { return _template; }
  1118. set { _template = value; }
  1119. }
  1120. protected override void CreateChildControls ()
  1121. {
  1122. Controls.Clear ();
  1123. TemplateContainer container = new TemplateContainer ();
  1124. Controls.Add (container);
  1125. if (Template != null)
  1126. Template.InstantiateIn (container);
  1127. }
  1128. }
  1129. class TemplateContainer : Control, INamingContainer
  1130. {
  1131. }
  1132. #endregion
  1133. }
  1134. #if NET_2_0
  1135. public class Customadaptercontrol : Control
  1136. {
  1137. public new ControlAdapter Adapter {
  1138. get { return base.Adapter; }
  1139. }
  1140. public new ControlAdapter ResolveAdapter ()
  1141. {
  1142. return base.ResolveAdapter ();
  1143. }
  1144. }
  1145. public class Customadapter : ControlAdapter
  1146. {
  1147. }
  1148. class EnsureIDControl : Control {
  1149. public void DoEnsureID () {
  1150. EnsureID ();
  1151. }
  1152. }
  1153. #endif
  1154. public class ChildControlsCreatedControl : Control
  1155. {
  1156. protected override void CreateChildControls () {
  1157. Controls.Add (new Control ());
  1158. }
  1159. public void DoEnsureChildControls () {
  1160. EnsureChildControls ();
  1161. }
  1162. public void SetChildControlsCreated (bool value) {
  1163. ChildControlsCreated = value;
  1164. }
  1165. }
  1166. }