2
0

ControlTest.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. #if NET_2_0
  40. using System.Web.UI.Adapters;
  41. #endif
  42. namespace MonoTests.System.Web.UI
  43. {
  44. [TestFixture]
  45. public class ControlTest
  46. {
  47. [Test]
  48. public void DataBindingInterfaceTest ()
  49. {
  50. Control c;
  51. DataBindingCollection db;
  52. c = new Control ();
  53. Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB1");
  54. db = ((IDataBindingsAccessor) c).DataBindings;
  55. Assert.IsNotNull (db, "DB2");
  56. Assert.AreEqual (false, ((IDataBindingsAccessor) c).HasDataBindings, "DB3");
  57. db.Add (new DataBinding ("property", typeof (bool), "expression"));
  58. Assert.AreEqual (true, ((IDataBindingsAccessor) c).HasDataBindings);
  59. }
  60. [Test]
  61. public void UniqueID1 ()
  62. {
  63. // Standalone NC
  64. Control nc = new MyNC ();
  65. Assert.IsNull (nc.UniqueID, "nulltest");
  66. }
  67. [Test]
  68. public void UniqueID2 ()
  69. {
  70. // NC in NC
  71. Control nc = new MyNC ();
  72. Control nc2 = new MyNC ();
  73. nc2.Controls.Add (nc);
  74. Assert.IsNotNull (nc.UniqueID, "notnull");
  75. Assert.IsTrue (nc.UniqueID.IndexOfAny (new char[] { ':', '$' }) == -1, "separator");
  76. }
  77. [Test]
  78. public void UniqueID3 ()
  79. {
  80. // NC in control
  81. Control control = new Control ();
  82. Control nc = new MyNC ();
  83. control.Controls.Add (nc);
  84. Assert.IsNull (nc.UniqueID, "null");
  85. }
  86. [Test]
  87. public void UniqueID4 ()
  88. {
  89. // NC in control
  90. Control control = new Control ();
  91. Control nc = new MyNC ();
  92. nc.Controls.Add (control);
  93. Assert.IsNotNull (control.UniqueID, "notnull");
  94. }
  95. [Test]
  96. public void UniqueID5 ()
  97. {
  98. // NC in control
  99. Control control = new Control ();
  100. Control nc = new MyNC ();
  101. Control nc2 = new MyNC ();
  102. nc2.Controls.Add (nc);
  103. nc.Controls.Add (control);
  104. Assert.IsNotNull (control.UniqueID, "notnull");
  105. Assert.IsNull (nc2.ID, "null-1");
  106. Assert.IsNull (nc.ID, "null-2");
  107. Assert.IsTrue (-1 != control.UniqueID.IndexOfAny (new char[] { ':', '$' }), "separator");
  108. }
  109. [Test]
  110. public void ClientID ()
  111. {
  112. // NC in control
  113. Control control = new Control ();
  114. Control nc = new MyNC ();
  115. Control nc2 = new MyNC ();
  116. Control nc3 = new MyNC ();
  117. nc3.Controls.Add (nc2);
  118. nc2.Controls.Add (nc);
  119. nc.Controls.Add (control);
  120. #if NET_2_0
  121. string expected = "ctl00_ctl00_ctl00";
  122. #else
  123. string expected = "_ctl0__ctl0__ctl0";
  124. #endif
  125. Assert.AreEqual (expected, control.ClientID, "ClientID");
  126. }
  127. // From bug #76919: Control uses _controls instead of
  128. // Controls when RenderChildren is called.
  129. [Test]
  130. public void Controls1 ()
  131. {
  132. DerivedControl derived = new DerivedControl ();
  133. derived.Controls.Add (new LiteralControl ("hola"));
  134. StringWriter sw = new StringWriter ();
  135. HtmlTextWriter htw = new HtmlTextWriter (sw);
  136. derived.RenderControl (htw);
  137. string result = sw.ToString ();
  138. Assert.AreEqual ("", result, "#01");
  139. }
  140. #if NET_2_0
  141. [Test]
  142. [Category("NunitWeb")]
  143. public void AppRelativeTemplateSourceDirectory ()
  144. {
  145. new WebTest(PageInvoker.CreateOnLoad(AppRelativeTemplateSourceDirectory_Load)).Run();
  146. }
  147. public static void AppRelativeTemplateSourceDirectory_Load(Page p)
  148. {
  149. Control ctrl = new Control();
  150. Assert.AreEqual("~/", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#1");
  151. ctrl.AppRelativeTemplateSourceDirectory = "~/Fake";
  152. Assert.AreEqual("~/Fake", ctrl.AppRelativeTemplateSourceDirectory, "AppRelativeTemplateSourceDirectory#2");
  153. }
  154. [Test]
  155. public void ApplyStyleSheetSkin ()
  156. {
  157. Page p = new Page ();
  158. p.StyleSheetTheme = "";
  159. Control c = new Control ();
  160. c.ApplyStyleSheetSkin (p);
  161. }
  162. [Test]
  163. [Category ("NunitWeb")]
  164. public void ApplyStyleSheetSkin_1 ()
  165. {
  166. #if VISUAL_STUDIO
  167. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.Theme2.skin", "App_Themes/Theme2/Theme2.skin");
  168. #else
  169. WebTest.CopyResource (GetType (), "Theme2.skin", "App_Themes/Theme2/Theme2.skin");
  170. #endif
  171. WebTest t = new WebTest ();
  172. PageDelegates pd = new PageDelegates ();
  173. pd.PreInit = ApplyStyleSheetSkin_PreInit;
  174. pd.Load = ApplyStyleSheetSkin_Load;
  175. t.Invoker = new PageInvoker (pd);
  176. string str = t.Run ();
  177. }
  178. public static void ApplyStyleSheetSkin_PreInit (Page p)
  179. {
  180. p.Theme = "Theme2";
  181. }
  182. public static void ApplyStyleSheetSkin_Load (Page p)
  183. {
  184. Label lbl = new Label ();
  185. lbl.ID = "StyleLbl";
  186. lbl.SkinID = "red";
  187. lbl.Text = "StyleLabel";
  188. p.Controls.Add (lbl);
  189. lbl.ApplyStyleSheetSkin (p);
  190. Assert.AreEqual (Color.Red, lbl.ForeColor, "ApplyStyleSheetSkin_BackColor");
  191. Assert.AreEqual ("TextFromSkinFile", lbl.Text, "ApplyStyleSheetSkin");
  192. }
  193. [Test]
  194. [Category ("NunitWeb")]
  195. public void ClearChildControlState ()
  196. {
  197. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildControlState_Load));
  198. t.Run ();
  199. FormRequest fr = new FormRequest (t.Response, "form1");
  200. fr.Controls.Add ("__EVENTTARGET");
  201. fr.Controls.Add ("__EVENTARGUMENT");
  202. fr.Controls["__EVENTTARGET"].Value = "";
  203. fr.Controls["__EVENTARGUMENT"].Value = "";
  204. t.Request = fr;
  205. t.Run ();
  206. }
  207. public static void ClearChildControlState_Load (Page p)
  208. {
  209. ControlWithState c1 = new ControlWithState ();
  210. p.Form.Controls.Add (c1);
  211. if (p.IsPostBack) {
  212. c1.ClearChildControlState ();
  213. }
  214. ControlWithState c2 = new ControlWithState ();
  215. c1.Controls.Add (c2);
  216. ControlWithState c3 = new ControlWithState ();
  217. c2.Controls.Add (c3);
  218. if (!p.IsPostBack) {
  219. c1.State = "State";
  220. c2.State = "Cool";
  221. c3.State = "SubCool";
  222. }
  223. else {
  224. Assert.AreEqual ("State", c1.State, "ControlState#1");
  225. Assert.AreEqual (null, c2.State, "ControlState#2");
  226. Assert.AreEqual (null, c3.State, "ControlState#2");
  227. }
  228. }
  229. [Test]
  230. [Category ("NunitWeb")]
  231. public void ClearChildState ()
  232. {
  233. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ClearChildState_Load));
  234. t.Run ();
  235. FormRequest fr = new FormRequest (t.Response, "form1");
  236. fr.Controls.Add ("__EVENTTARGET");
  237. fr.Controls.Add ("__EVENTARGUMENT");
  238. fr.Controls["__EVENTTARGET"].Value = "";
  239. fr.Controls["__EVENTARGUMENT"].Value = "";
  240. t.Request = fr;
  241. t.Run ();
  242. }
  243. public static void ClearChildState_Load (Page p)
  244. {
  245. ControlWithState c1 = new ControlWithState ();
  246. p.Form.Controls.Add (c1);
  247. if (p.IsPostBack) {
  248. c1.ClearChildState ();
  249. }
  250. ControlWithState c2 = new ControlWithState ();
  251. c1.Controls.Add (c2);
  252. ControlWithState c3 = new ControlWithState ();
  253. c2.Controls.Add (c3);
  254. if (!p.IsPostBack) {
  255. c1.State = "State";
  256. c2.State = "Cool";
  257. c2.Viewstate = "Very Cool";
  258. c3.State = "SubCool";
  259. c3.Viewstate = "Super Cool";
  260. }
  261. else {
  262. Assert.AreEqual ("State", c1.State, "ClearChildState#1");
  263. Assert.AreEqual (null, c2.State, "ClearChildState#2");
  264. Assert.AreEqual (null, c3.State, "ClearChildState#3");
  265. Assert.AreEqual (null, c2.Viewstate, "ClearChildState#4");
  266. Assert.AreEqual (null, c3.Viewstate, "ClearChildState#5");
  267. }
  268. }
  269. [Test]
  270. public void DataBind ()
  271. {
  272. MyNC ctrl = new MyNC ();
  273. ctrl.DataBinding += new EventHandler (ctrl_DataBinding);
  274. Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
  275. ctrl.DataBind (false);
  276. Assert.AreEqual (false, _eventDataBinding, "Before DataBinding");
  277. ctrl.DataBind (true);
  278. Assert.AreEqual (true, _eventDataBinding, "After DataBinding");
  279. }
  280. bool _eventDataBinding;
  281. void ctrl_DataBinding (object sender, EventArgs e)
  282. {
  283. _eventDataBinding = true;
  284. }
  285. [Test]
  286. public void DataBindChildren ()
  287. {
  288. MyNC ctrl1 = new MyNC ();
  289. Control ctrl2 = new Control ();
  290. Control ctrl3 = new Control ();
  291. ctrl2.DataBinding += new EventHandler (ctrl2_DataBinding);
  292. ctrl3.DataBinding += new EventHandler (ctrl3_DataBinding);
  293. ctrl2.Controls.Add (ctrl3);
  294. ctrl1.Controls.Add (ctrl2);
  295. Assert.AreEqual (false, _eventChild1, "Before DataBinding#1");
  296. Assert.AreEqual (false, _eventChild2, "Before DataBinding#2");
  297. ctrl1.DataBindChildren ();
  298. Assert.AreEqual (true, _eventChild1, "After DataBinding#1");
  299. Assert.AreEqual (true, _eventChild2, "After DataBinding#2");
  300. }
  301. bool _eventChild1;
  302. bool _eventChild2;
  303. void ctrl3_DataBinding (object sender, EventArgs e)
  304. {
  305. _eventChild1 = true;
  306. }
  307. void ctrl2_DataBinding (object sender, EventArgs e)
  308. {
  309. _eventChild2 = true;
  310. }
  311. [Test]
  312. [Category("NotWorking")] //EnsureID is not implemented.
  313. public void EnsureID ()
  314. {
  315. MyNC ctrl = new MyNC ();
  316. MyNC ctrl1 = new MyNC ();
  317. ctrl.Controls.Add (ctrl1);
  318. Page p = new Page ();
  319. p.Controls.Add (ctrl);
  320. ctrl.EnsureID ();
  321. if (String.IsNullOrEmpty (ctrl.ID))
  322. Assert.Fail ("EnsureID#1");
  323. ctrl1.EnsureID ();
  324. if (String.IsNullOrEmpty (ctrl1.ID))
  325. Assert.Fail ("EnsureID#2");
  326. }
  327. [Test]
  328. [Category("NotWorking")]
  329. public void Focus ()
  330. {
  331. WebTest t = new WebTest (PageInvoker.CreateOnLoad (Focus_Load));
  332. string html = t.Run ();
  333. Assert.AreEqual (3, contain (html, "TestBox"), "Focus script not created");
  334. }
  335. public static void Focus_Load (Page p)
  336. {
  337. TextBox tbx = new TextBox ();
  338. tbx.ID = "TestBox";
  339. p.Controls.Add (tbx);
  340. tbx.Focus ();
  341. }
  342. int contain (string orig, string compare)
  343. {
  344. if (orig.IndexOf (compare) == -1)
  345. return 0;
  346. return 1 + contain (orig.Substring (orig.IndexOf (compare) + compare.Length), compare);
  347. }
  348. [Test]
  349. public void HasEvent ()
  350. {
  351. MyNC ctrl1 = new MyNC ();
  352. Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#1");
  353. EventHandler ctrl_hdlr = new EventHandler (ctrl1_Init);
  354. ctrl1.Init += new EventHandler (ctrl1_Init);
  355. ctrl1.Init += ctrl_hdlr;
  356. Assert.AreEqual (true, ctrl1.HasEvents (), "HasEvent#2");
  357. // Dosn't work than removed handler
  358. //ctrl1.Init -= ctrl_hdlr;
  359. //Assert.AreEqual (false, ctrl1.HasEvents (), "HasEvent#3");
  360. }
  361. void ctrl1_Init (object sender, EventArgs e)
  362. {
  363. throw new Exception ("The method or operation is not implemented.");
  364. }
  365. [Test]
  366. public void IsViewStateEnabled ()
  367. {
  368. DerivedControl c = new DerivedControl ();
  369. Assert.IsTrue (c.DoIsViewStateEnabled);
  370. Page p = new Page ();
  371. c.Page = p;
  372. p.Controls.Add (c);
  373. Assert.IsTrue (c.DoIsViewStateEnabled);
  374. p.EnableViewState = false;
  375. Assert.IsFalse (c.DoIsViewStateEnabled);
  376. }
  377. [Test]
  378. [Category ("NunitWeb")]
  379. public void ControlState ()
  380. {
  381. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
  382. t.Run ();
  383. FormRequest fr = new FormRequest (t.Response, "form1");
  384. fr.Controls.Add ("__EVENTTARGET");
  385. fr.Controls.Add ("__EVENTARGUMENT");
  386. fr.Controls["__EVENTTARGET"].Value = "";
  387. fr.Controls["__EVENTARGUMENT"].Value = "";
  388. t.Request = fr;
  389. t.Run ();
  390. }
  391. public static void ControlState_Load (Page p)
  392. {
  393. ControlWithState c1 = new ControlWithState ();
  394. ControlWithState c2 = new ControlWithState ();
  395. c1.Controls.Add (c2);
  396. p.Form.Controls.Add (c1);
  397. if (!p.IsPostBack) {
  398. c1.State = "State";
  399. c2.State = "Cool";
  400. }
  401. else {
  402. ControlWithState c3 = new ControlWithState ();
  403. p.Form.Controls.Add (c3);
  404. Assert.AreEqual ("State", c1.State, "ControlState");
  405. Assert.AreEqual ("Cool", c2.State, "ControlState");
  406. }
  407. }
  408. [Test]
  409. [Category ("NunitWeb")]
  410. public void ControlState2 () {
  411. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState2_Load));
  412. t.Run ();
  413. FormRequest fr = new FormRequest (t.Response, "form1");
  414. fr.Controls.Add ("__EVENTTARGET");
  415. fr.Controls.Add ("__EVENTARGUMENT");
  416. fr.Controls ["__EVENTTARGET"].Value = "";
  417. fr.Controls ["__EVENTARGUMENT"].Value = "";
  418. t.Request = fr;
  419. t.Run ();
  420. fr = new FormRequest (t.Response, "form1");
  421. fr.Controls.Add ("__EVENTTARGET");
  422. fr.Controls.Add ("__EVENTARGUMENT");
  423. fr.Controls ["__EVENTTARGET"].Value = "";
  424. fr.Controls ["__EVENTARGUMENT"].Value = "";
  425. t.Request = fr;
  426. t.Run ();
  427. }
  428. public static void ControlState2_Load (Page p) {
  429. ControlWithState parent = new ControlWithState ();
  430. p.Form.Controls.Add (parent);
  431. if (!p.IsPostBack) {
  432. // emulate DataBind
  433. parent.Controls.Clear ();
  434. parent.ClearChildControlState ();
  435. ControlWithState c1 = new ControlWithState ();
  436. ControlWithState c2 = new ControlWithState ();
  437. parent.Controls.Add (c1);
  438. parent.Controls.Add (c2);
  439. c1.State = "State1_1";
  440. c2.State = "State1_2";
  441. parent.State = "First";
  442. }
  443. else if (parent.State == "First") {
  444. // emulate DataBind
  445. parent.Controls.Clear ();
  446. parent.ClearChildControlState ();
  447. ControlWithState c1 = new ControlWithState ();
  448. ControlWithState c2 = new ControlWithState ();
  449. parent.Controls.Add (c1);
  450. parent.Controls.Add (c2);
  451. c1.State = "State2_1";
  452. c2.State = "State2_2";
  453. parent.State = "Second";
  454. }
  455. else {
  456. // emulate CrerateChildControl only
  457. parent.Controls.Clear ();
  458. ControlWithState c1 = new ControlWithState ();
  459. ControlWithState c2 = new ControlWithState ();
  460. parent.Controls.Add (c1);
  461. parent.Controls.Add (c2);
  462. Assert.AreEqual ("State2_1", c1.State, "ControlState#1");
  463. Assert.AreEqual ("State2_2", c2.State, "ControlState#2");
  464. }
  465. }
  466. [Test]
  467. public void ClientIDSeparator ()
  468. {
  469. DerivedControl ctrl = new DerivedControl ();
  470. Assert.AreEqual (95, (int) ctrl.ClientIDSeparator, "ClientIDSeparator");
  471. }
  472. [Test]
  473. public void IDSeparator ()
  474. {
  475. DerivedControl ctrl = new DerivedControl ();
  476. Assert.AreEqual (36, (int) ctrl.IdSeparator, "IDSeparator");
  477. }
  478. [Test]
  479. [Category ("NunitWeb")]
  480. public void IsChildControlStateCleared ()
  481. {
  482. WebTest t = new WebTest (PageInvoker.CreateOnLoad (IsChildControlStateCleared_Load));
  483. t.Run ();
  484. FormRequest fr = new FormRequest (t.Response, "form1");
  485. fr.Controls.Add ("__EVENTTARGET");
  486. fr.Controls.Add ("__EVENTARGUMENT");
  487. fr.Controls["__EVENTTARGET"].Value = "";
  488. fr.Controls["__EVENTARGUMENT"].Value = "";
  489. t.Request = fr;
  490. t.Run ();
  491. }
  492. public static void IsChildControlStateCleared_Load (Page p)
  493. {
  494. ControlWithState c1 = new ControlWithState ();
  495. p.Form.Controls.Add (c1);
  496. if (p.IsPostBack) {
  497. Assert.IsFalse (c1.IsChildControlStateCleared, "ControlState#1");
  498. c1.ClearChildControlState ();
  499. Assert.IsTrue (c1.IsChildControlStateCleared, "ControlState#1");
  500. }
  501. ControlWithState c2 = new ControlWithState ();
  502. c1.Controls.Add (c2);
  503. ControlWithState c3 = new ControlWithState ();
  504. c2.Controls.Add (c3);
  505. if (p.IsPostBack) {
  506. Assert.IsFalse (c2.IsChildControlStateCleared, "ControlState#1");
  507. Assert.IsFalse (c3.IsChildControlStateCleared, "ControlState#1");
  508. }
  509. if (!p.IsPostBack) {
  510. c1.State = "State";
  511. c2.State = "Cool";
  512. c3.State = "SubCool";
  513. }
  514. }
  515. [Test]
  516. [Category ("NunitWeb")]
  517. public void LoadViewStateByID ()
  518. {
  519. ControlWithState c1 = new ControlWithState ();
  520. ControlWithState c2 = new ControlWithState ();
  521. c1.Controls.Add (c2);
  522. Assert.AreEqual (false, c1.LoadViewStateByID, "LoadViewStateByID#1");
  523. }
  524. [Test]
  525. [Category ("NunitWeb")]
  526. public void OpenFile ()
  527. {
  528. WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFile_Load));
  529. t.Run ();
  530. }
  531. public static void OpenFile_Load (Page p)
  532. {
  533. DerivedControl ctrl = new DerivedControl ();
  534. Stream strem = ctrl.OpenFile ("~/MyPage.aspx");
  535. Assert.IsNotNull (strem, "OpenFile failed");
  536. }
  537. [Test]
  538. [Category ("NunitWeb")]
  539. [ExpectedException (typeof (FileNotFoundException))]
  540. public void OpenFile_Exception ()
  541. {
  542. WebTest t = new WebTest (PageInvoker.CreateOnLoad (OpenFileException_Load));
  543. t.Run ();
  544. }
  545. public static void OpenFileException_Load (Page p)
  546. {
  547. DerivedControl ctrl = new DerivedControl ();
  548. Stream strem = ctrl.OpenFile ("~/Fake.tmp");
  549. }
  550. //// MonoTests.SystemWeb.Framework limitation for Add_browsers - directory include in project
  551. //[Test]
  552. //[Category ("NunitWeb")]
  553. //public void ResolveAdapter_1 ()
  554. //{
  555. // WebTest.CopyResource (GetType (), "adapters.browser", "App_Browsers/adapters.browser");
  556. // WebTest t = new WebTest (PageInvoker.CreateOnInit (ResolveAdapter_Init));
  557. // string html = t.Run ();
  558. //}
  559. //public static void ResolveAdapter_Init (Page p)
  560. //{
  561. // Customadaptercontrol ctrl = new Customadaptercontrol ();
  562. // p.Controls.Add (ctrl);
  563. // ctrl.Load += new EventHandler (ctrl_Load);
  564. //}
  565. //static void ctrl_Load (object sender, EventArgs e)
  566. //{
  567. // Assert.IsNotNull (((Customadaptercontrol) sender).ResolveAdapter (), "ResolveAdapter Failed#1");
  568. // Assert.AreEqual ("Customadapter", ((Customadaptercontrol) sender).ResolveAdapter ().ToString (), "ResolveAdapter Failed#2");
  569. //}
  570. [Test]
  571. [Category ("NunitWeb")]
  572. public void ResolveClientUrl ()
  573. {
  574. WebTest t = new WebTest (PageInvoker.CreateOnLoad (ResolveClientUrl_Load));
  575. string html = t.Run ();
  576. }
  577. public static void ResolveClientUrl_Load (Page p)
  578. {
  579. Control ctrl = new Control ();
  580. p.Controls.Add (ctrl);
  581. string url = ctrl.ResolveClientUrl ("~/MyPage.aspx");
  582. Assert.AreEqual ("MyPage.aspx", url, "ResolveClientUrl Failed");
  583. Assert.AreEqual ("", ctrl.ResolveClientUrl (""), "empty string");
  584. Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~"), "~");
  585. Assert.AreEqual ("./", ctrl.ResolveClientUrl ("~/"), "~/");
  586. Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~/../MyPage.aspx"), "~/../MyPage.aspx");
  587. Assert.AreEqual ("../MyPage.aspx", ctrl.ResolveClientUrl ("~\\..\\MyPage.aspx"), "~\\..\\MyPage.aspx");
  588. Assert.AreEqual ("MyPage.aspx", ctrl.ResolveClientUrl ("~////MyPage.aspx"), "ResolveClientUrl Failed");
  589. Assert.AreEqual ("/MyPage.aspx", ctrl.ResolveClientUrl ("/MyPage.aspx"), "ResolveClientUrl Failed");
  590. Assert.AreEqual ("/folder/MyPage.aspx", ctrl.ResolveClientUrl ("/folder/MyPage.aspx"), "ResolveClientUrl Failed");
  591. Assert.AreEqual ("/NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("/NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  592. Assert.AreEqual ("\\NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("\\NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  593. Assert.AreEqual ("///NunitWeb\\..\\MyPage.aspx", ctrl.ResolveClientUrl ("///NunitWeb\\..\\MyPage.aspx"), "ResolveClientUrl Failed");
  594. Assert.AreEqual ("NunitWeb/MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/MyPage.aspx"), "ResolveClientUrl Failed");
  595. Assert.AreEqual ("NunitWeb/../MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/../MyPage.aspx"), "ResolveClientUrl Failed");
  596. Assert.AreEqual ("NunitWeb/./MyPage.aspx", ctrl.ResolveClientUrl ("NunitWeb/./MyPage.aspx"), "ResolveClientUrl Failed");
  597. Assert.AreEqual ("http://google.com/", ctrl.ResolveClientUrl ("http://google.com/"), "ResolveClientUrl Failed");
  598. Assert.AreEqual ("MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  599. Assert.AreEqual ("../MyPage.aspx?param=val&yes=no", ctrl.ResolveClientUrl ("~/../MyPage.aspx?param=val&yes=no"), "~/../MyPage.aspx");
  600. }
  601. [Test]
  602. [Category ("NotWorking")] // Not implemented exception
  603. public void ResolveAdapter_2 ()
  604. {
  605. DerivedControl ctrl = new DerivedControl ();
  606. Assert.AreEqual (null, ctrl.ResolveAdapter (), "ResolveAdapter");
  607. }
  608. [Test]
  609. public void EnableTheming ()
  610. {
  611. DerivedControl ctrl = new DerivedControl ();
  612. Assert.AreEqual (true, ctrl.EnableTheming, "EnableTheming#1");
  613. ctrl.EnableTheming = false;
  614. Assert.AreEqual (false, ctrl.EnableTheming, "EnableTheming#2");
  615. }
  616. #endif
  617. [Test]
  618. public void BindingContainer ()
  619. {
  620. ControlWithTemplate c = new ControlWithTemplate ();
  621. c.Template = new CompiledTemplateBuilder (new BuildTemplateMethod (BindingContainer_BuildTemplate));
  622. // cause CreateChildControls called
  623. c.FindControl ("stam");
  624. }
  625. static void BindingContainer_BuildTemplate (Control control)
  626. {
  627. Control child1 = new Control ();
  628. control.Controls.Add (child1);
  629. Assert.IsTrue (Object.ReferenceEquals (child1.NamingContainer, control), "NamingContainer #1");
  630. Assert.IsTrue (Object.ReferenceEquals (child1.BindingContainer, control), "BindingContainer #1");
  631. NamingContainer nc = new NamingContainer ();
  632. Control child2 = new Control ();
  633. nc.Controls.Add (child2);
  634. control.Controls.Add (nc);
  635. Assert.IsTrue (Object.ReferenceEquals (child2.NamingContainer, nc), "NamingContainer #2");
  636. Assert.IsTrue (Object.ReferenceEquals (child2.BindingContainer, nc), "BindingContainer #2");
  637. #if NET_2_0
  638. // DetailsViewPagerRow marked to be not BindingContainer
  639. DetailsViewPagerRow row = new DetailsViewPagerRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
  640. TableCell cell = new TableCell ();
  641. Control child3 = new Control ();
  642. cell.Controls.Add (child3);
  643. row.Cells.Add (cell);
  644. control.Controls.Add (row);
  645. Assert.IsTrue (Object.ReferenceEquals (child3.NamingContainer, row), "NamingContainer #3");
  646. Assert.IsTrue (Object.ReferenceEquals (child3.BindingContainer, control), "BindingContainer #3");
  647. #endif
  648. }
  649. #if NET_2_0
  650. [Test]
  651. public void Contorl_Adapter ()
  652. {
  653. MyNC ctr = new MyNC ();
  654. Assert.AreEqual (null, ctr.Adapter (), "Adapter");
  655. }
  656. #endif
  657. [Test]
  658. public void ChildControlsCreated () {
  659. ChildControlsCreatedControl ctr = new ChildControlsCreatedControl ();
  660. ctr.Controls.Add (new Control ());
  661. //ctr.DoEnsureChildControls ();
  662. Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#1");
  663. ctr.SetChildControlsCreated (false);
  664. Assert.AreEqual (1, ctr.Controls.Count, "ChildControlsCreated#2");
  665. }
  666. #if NET_2_0
  667. [TestFixtureTearDown]
  668. public void Tear_down ()
  669. {
  670. WebTest.Unload ();
  671. }
  672. #endif
  673. #region helpcalsses
  674. #if NET_2_0
  675. class ControlWithState : Control
  676. {
  677. string _state;
  678. public string State
  679. {
  680. get { return _state; }
  681. set { _state = value; }
  682. }
  683. public string Viewstate {
  684. get { return (string) ViewState ["Viewstate"]; }
  685. set { ViewState ["Viewstate"] = value; }
  686. }
  687. protected override void OnInit (EventArgs e)
  688. {
  689. base.OnInit (e);
  690. Page.RegisterRequiresControlState (this);
  691. }
  692. protected override object SaveControlState ()
  693. {
  694. return State;
  695. }
  696. protected override void LoadControlState (object savedState)
  697. {
  698. State = (string) savedState;
  699. }
  700. public new void ClearChildState ()
  701. {
  702. base.ClearChildState ();
  703. }
  704. public new void ClearChildControlState ()
  705. {
  706. base.ClearChildControlState ();
  707. }
  708. public new bool IsChildControlStateCleared
  709. {
  710. get { return base.IsChildControlStateCleared; }
  711. }
  712. public new bool LoadViewStateByID
  713. {
  714. get { return base.LoadViewStateByID; }
  715. }
  716. }
  717. #endif
  718. class MyNC : Control, INamingContainer
  719. {
  720. #if NET_2_0
  721. public ControlAdapter Adapter ()
  722. {
  723. return base.Adapter;
  724. }
  725. public new void DataBind (bool opt)
  726. {
  727. base.DataBind (opt);
  728. }
  729. public new void DataBindChildren ()
  730. {
  731. base.DataBindChildren ();
  732. }
  733. public new void EnsureID ()
  734. {
  735. base.EnsureID ();
  736. }
  737. public new bool HasEvents ()
  738. {
  739. return base.HasEvents ();
  740. }
  741. #endif
  742. }
  743. class DerivedControl : Control
  744. {
  745. ControlCollection coll;
  746. public DerivedControl ()
  747. {
  748. coll = new ControlCollection (this);
  749. }
  750. public override ControlCollection Controls
  751. {
  752. get { return coll; }
  753. }
  754. #if NET_2_0
  755. public bool DoIsViewStateEnabled
  756. {
  757. get { return IsViewStateEnabled; }
  758. }
  759. public new char ClientIDSeparator
  760. {
  761. get { return base.ClientIDSeparator; }
  762. }
  763. public new char IdSeparator
  764. {
  765. get { return base.IdSeparator; }
  766. }
  767. public new Stream OpenFile (string path)
  768. {
  769. return base.OpenFile (path);
  770. }
  771. public new ControlAdapter ResolveAdapter ()
  772. {
  773. return base.ResolveAdapter ();
  774. }
  775. #endif
  776. }
  777. class NamingContainer : Control, INamingContainer
  778. {
  779. }
  780. class ControlWithTemplate : Control
  781. {
  782. ITemplate _template;
  783. [TemplateContainer (typeof (TemplateContainer))]
  784. public ITemplate Template
  785. {
  786. get { return _template; }
  787. set { _template = value; }
  788. }
  789. protected override void CreateChildControls ()
  790. {
  791. Controls.Clear ();
  792. TemplateContainer container = new TemplateContainer ();
  793. Controls.Add (container);
  794. if (Template != null)
  795. Template.InstantiateIn (container);
  796. }
  797. }
  798. class TemplateContainer : Control, INamingContainer
  799. {
  800. }
  801. #endregion
  802. }
  803. #if NET_2_0
  804. public class Customadaptercontrol : Control
  805. {
  806. public new ControlAdapter ResolveAdapter ()
  807. {
  808. return base.ResolveAdapter ();
  809. }
  810. }
  811. public class Customadapter : ControlAdapter
  812. {
  813. }
  814. #endif
  815. public class ChildControlsCreatedControl : Control
  816. {
  817. protected override void CreateChildControls () {
  818. Controls.Add (new Control ());
  819. }
  820. public void DoEnsureChildControls () {
  821. EnsureChildControls ();
  822. }
  823. public void SetChildControlsCreated (bool value) {
  824. ChildControlsCreated = value;
  825. }
  826. }
  827. }