DataListTest.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. //
  2. // DataListTest.cs - Unit tests for System.Web.UI.WebControls.DataList
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Data;
  32. using System.Drawing;
  33. using System.IO;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.Web.UI.WebControls;
  37. using NUnit.Framework;
  38. namespace MonoTests.System.Web.UI.WebControls {
  39. public class TestDataList : DataList {
  40. public string Tag {
  41. get { return base.TagName; }
  42. }
  43. public StateBag StateBag {
  44. get { return base.ViewState; }
  45. }
  46. private HtmlTextWriter GetWriter ()
  47. {
  48. StringWriter sw = new StringWriter ();
  49. sw.NewLine = "\n";
  50. return new HtmlTextWriter (sw);
  51. }
  52. public string Render ()
  53. {
  54. HtmlTextWriter writer = GetWriter ();
  55. base.Render (writer);
  56. return writer.InnerWriter.ToString ();
  57. }
  58. public string RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo)
  59. {
  60. HtmlTextWriter writer = GetWriter ();
  61. (this as IRepeatInfoUser).RenderItem (itemType, repeatIndex, repeatInfo, writer);
  62. return writer.InnerWriter.ToString ();
  63. }
  64. public void TrackState ()
  65. {
  66. TrackViewState ();
  67. }
  68. public void LoadState (object state)
  69. {
  70. LoadViewState (state);
  71. }
  72. public object SaveState ()
  73. {
  74. return SaveViewState ();
  75. }
  76. public void DoCancelCommand (DataListCommandEventArgs e)
  77. {
  78. OnCancelCommand (e);
  79. }
  80. public void DoDeleteCommand (DataListCommandEventArgs e)
  81. {
  82. OnDeleteCommand (e);
  83. }
  84. public void DoEditCommand (DataListCommandEventArgs e)
  85. {
  86. OnEditCommand (e);
  87. }
  88. public void DoItemCommand (DataListCommandEventArgs e)
  89. {
  90. OnItemCommand (e);
  91. }
  92. public void DoUpdateCommand (DataListCommandEventArgs e)
  93. {
  94. OnUpdateCommand (e);
  95. }
  96. public void DoItemCreated (DataListItemEventArgs e)
  97. {
  98. OnItemCreated (e);
  99. }
  100. public void DoItemDataBound (DataListItemEventArgs e)
  101. {
  102. OnItemDataBound (e);
  103. }
  104. public void DoSelectedIndexChanged (EventArgs e)
  105. {
  106. OnSelectedIndexChanged (e);
  107. }
  108. public void DoBubbleEvent (object source, EventArgs e)
  109. {
  110. OnBubbleEvent (source, e);
  111. }
  112. }
  113. public class TestTemplate : ITemplate {
  114. public void InstantiateIn (Control container)
  115. {
  116. }
  117. }
  118. [TestFixture]
  119. public class DataListTest {
  120. private HtmlTextWriter GetWriter ()
  121. {
  122. StringWriter sw = new StringWriter ();
  123. sw.NewLine = "\n";
  124. return new HtmlTextWriter (sw);
  125. }
  126. // IList based
  127. private ArrayList GetData (int n)
  128. {
  129. ArrayList al = new ArrayList ();
  130. for (int i = 0; i < n; i++) {
  131. al.Add (i.ToString ());
  132. }
  133. return al;
  134. }
  135. // IListSource based
  136. private TestDataSource GetDataSource (int n)
  137. {
  138. return new TestDataSource (GetData (n));
  139. }
  140. public void CheckIRepeatInfoUser (IRepeatInfoUser riu)
  141. {
  142. Assert.IsFalse (riu.HasFooter, "HasFooter");
  143. Assert.IsFalse (riu.HasHeader, "HasHeader");
  144. Assert.IsFalse (riu.HasSeparators, "HasSeparators");
  145. Assert.AreEqual (0, riu.RepeatedItemCount, "RepeatedItemCount");
  146. }
  147. [Test]
  148. public void ConstantStrings ()
  149. {
  150. Assert.AreEqual ("Cancel", DataList.CancelCommandName, "CancelCommandName");
  151. Assert.AreEqual ("Delete", DataList.DeleteCommandName, "DeleteCommandName");
  152. Assert.AreEqual ("Edit", DataList.EditCommandName, "EditCommandName");
  153. Assert.AreEqual ("Select", DataList.SelectCommandName, "SelectCommandName");
  154. Assert.AreEqual ("Update", DataList.UpdateCommandName, "UpdateCommandName");
  155. }
  156. [Test]
  157. public void DefaultProperties ()
  158. {
  159. TestDataList dl = new TestDataList ();
  160. CheckIRepeatInfoUser (dl);
  161. #if NET_2_0
  162. Assert.AreEqual ("table", dl.Tag, "TagName");
  163. #else
  164. Assert.AreEqual ("span", dl.Tag, "TagName");
  165. #endif
  166. Assert.AreEqual (0, dl.Attributes.Count, "Attributes.Count-1");
  167. Assert.AreEqual (0, dl.StateBag.Count, "ViewState.Count-1");
  168. // Styles
  169. Assert.IsNotNull (dl.AlternatingItemStyle, "AlternatingItemStyle");
  170. Assert.IsNotNull (dl.EditItemStyle, "EditItemStyle");
  171. Assert.IsNotNull (dl.FooterStyle, "FooterStyle");
  172. Assert.IsNotNull (dl.HeaderStyle, "HeaderStyle");
  173. Assert.IsNotNull (dl.ItemStyle, "ItemStyle");
  174. Assert.IsNotNull (dl.SelectedItemStyle, "SelectedItemStyle");
  175. Assert.IsNotNull (dl.SeparatorStyle, "SeparatorStyle");
  176. // Templates
  177. Assert.IsNull (dl.AlternatingItemTemplate, "AlternatingItemTemplate");
  178. Assert.IsNull (dl.EditItemTemplate, "EditItemTemplate");
  179. Assert.IsNull (dl.FooterTemplate, "FooterTemplate");
  180. Assert.IsNull (dl.HeaderTemplate, "HeaderTemplate");
  181. Assert.IsNull (dl.ItemTemplate, "ItemTemplate");
  182. Assert.IsNull (dl.SelectedItemTemplate, "SelectedItemTemplate");
  183. Assert.IsNull (dl.SeparatorTemplate, "SeparatorTemplate");
  184. // Indexes
  185. Assert.AreEqual (-1, dl.EditItemIndex, "EditItemIndex");
  186. Assert.AreEqual (-1, dl.SelectedIndex, "SelectedIndex");
  187. // others (from DataList)
  188. Assert.IsFalse (dl.ExtractTemplateRows, "ExtractTemplateRows");
  189. Assert.AreEqual (0, dl.Items.Count, "Items.Count");
  190. Assert.AreEqual (0, dl.RepeatColumns, "RepeatColumns");
  191. Assert.AreEqual (RepeatDirection.Vertical, dl.RepeatDirection, "RepeatDirection");
  192. Assert.AreEqual (RepeatLayout.Table, dl.RepeatLayout, "RepeatLayout");
  193. Assert.IsNull (dl.SelectedItem, "SelectedItem");
  194. Assert.IsTrue (dl.ShowFooter, "ShowFooter");
  195. Assert.IsTrue (dl.ShowHeader, "ShowHeader");
  196. // the CellPadding, CellSpacing, GridLines and HorizontalAlign are defined
  197. // in BaseDataList but couldn't be totally tested from there
  198. Assert.AreEqual (-1, dl.CellPadding, "CellPadding");
  199. Assert.AreEqual (0, dl.CellSpacing, "CellSpacing");
  200. #if NET_2_0
  201. Assert.AreEqual (GridLines.None, dl.GridLines, "GridLines");
  202. #else
  203. Assert.AreEqual (GridLines.Both, dl.GridLines, "GridLines");
  204. #endif
  205. Assert.AreEqual (HorizontalAlign.NotSet, dl.HorizontalAlign, "HorizontalAlign");
  206. Assert.AreEqual (0, dl.Attributes.Count, "Attributes.Count-2");
  207. Assert.AreEqual (0, dl.StateBag.Count, "ViewState.Count-2");
  208. }
  209. [Test]
  210. public void NullProperties ()
  211. {
  212. TestDataList dl = new TestDataList ();
  213. Assert.AreEqual (0, dl.Attributes.Count, "Attributes.Count-1");
  214. Assert.AreEqual (0, dl.StateBag.Count, "ViewState.Count-1");
  215. // some properties couldn't be set in BaseDataList without causing a
  216. // InvalidCastException, so they get test here first...
  217. dl.CellPadding = -1;
  218. Assert.AreEqual (-1, dl.CellPadding, "CellPadding");
  219. dl.CellSpacing = 0;
  220. Assert.AreEqual (0, dl.CellSpacing, "CellSpacing");
  221. dl.GridLines = GridLines.None;
  222. Assert.AreEqual (GridLines.None, dl.GridLines, "GridLines");
  223. dl.HorizontalAlign = HorizontalAlign.NotSet;
  224. Assert.AreEqual (HorizontalAlign.NotSet, dl.HorizontalAlign, "HorizontalAlign");
  225. #if NET_2_0
  226. int sc = 0;
  227. // so the TableStyle isn't kept directly in the ViewState
  228. #else
  229. int sc = 4;
  230. #endif
  231. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-2");
  232. // now for the DataList properties
  233. // touching all styles
  234. Assert.IsTrue (dl.AlternatingItemStyle.BackColor.IsEmpty, "AlternatingItemStyle");
  235. Assert.IsTrue (dl.EditItemStyle.BackColor.IsEmpty, "EditItemStyle");
  236. Assert.IsTrue (dl.FooterStyle.BackColor.IsEmpty, "FooterStyle");
  237. Assert.IsTrue (dl.HeaderStyle.BackColor.IsEmpty, "HeaderStyle");
  238. Assert.IsTrue (dl.ItemStyle.BackColor.IsEmpty, "ItemStyle");
  239. Assert.IsTrue (dl.SelectedItemStyle.BackColor.IsEmpty, "SelectedItemStyle");
  240. Assert.IsTrue (dl.SeparatorStyle.BackColor.IsEmpty, "SeparatorStyle");
  241. dl.AlternatingItemTemplate = null;
  242. Assert.IsNull (dl.AlternatingItemTemplate, "AlternatingItemTemplate");
  243. dl.EditItemTemplate = null;
  244. Assert.IsNull (dl.EditItemTemplate, "EditItemTemplate");
  245. dl.FooterTemplate = null;
  246. Assert.IsNull (dl.FooterTemplate, "FooterTemplate");
  247. dl.HeaderTemplate = null;
  248. Assert.IsNull (dl.HeaderTemplate, "HeaderTemplate");
  249. dl.ItemTemplate = null;
  250. Assert.IsNull (dl.ItemTemplate, "ItemTemplate");
  251. dl.SelectedItemTemplate = null;
  252. Assert.IsNull (dl.SelectedItemTemplate, "SelectedItemTemplate");
  253. dl.SeparatorTemplate = null;
  254. Assert.IsNull (dl.SeparatorTemplate, "SeparatorTemplate");
  255. dl.EditItemIndex = -1;
  256. Assert.AreEqual (-1, dl.EditItemIndex, "EditItemIndex");
  257. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-2b");
  258. dl.SelectedIndex = -1;
  259. Assert.AreEqual (-1, dl.SelectedIndex, "SelectedIndex");
  260. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-2c");
  261. dl.ExtractTemplateRows = false;
  262. Assert.IsFalse (dl.ExtractTemplateRows, "ExtractTemplateRows");
  263. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-3");
  264. dl.RepeatColumns = 0;
  265. Assert.AreEqual (0, dl.RepeatColumns, "RepeatColumns");
  266. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-4");
  267. dl.RepeatDirection = RepeatDirection.Vertical;
  268. Assert.AreEqual (RepeatDirection.Vertical, dl.RepeatDirection, "RepeatDirection");
  269. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-5");
  270. dl.RepeatLayout = RepeatLayout.Table;
  271. Assert.AreEqual (RepeatLayout.Table, dl.RepeatLayout, "RepeatLayout");
  272. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-6");
  273. dl.ShowFooter = true;
  274. Assert.IsTrue (dl.ShowFooter, "ShowFooter");
  275. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-7");
  276. dl.ShowHeader = true;
  277. Assert.IsTrue (dl.ShowHeader, "ShowHeader");
  278. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-8");
  279. // and all this didn't affect IRepeatInfoUser
  280. CheckIRepeatInfoUser (dl);
  281. Assert.AreEqual (0, dl.Attributes.Count, "Attributes.Count-2");
  282. }
  283. [Test]
  284. public void CleanProperties ()
  285. {
  286. TestDataList dl = new TestDataList ();
  287. IRepeatInfoUser riu = (dl as IRepeatInfoUser);
  288. Assert.AreEqual (0, dl.Attributes.Count, "Attributes.Count-1");
  289. Assert.AreEqual (0, dl.StateBag.Count, "ViewState.Count-1");
  290. dl.CellPadding = 0;
  291. Assert.AreEqual (0, dl.CellPadding, "CellPadding");
  292. dl.CellSpacing = 1;
  293. Assert.AreEqual (1, dl.CellSpacing, "CellSpacing");
  294. dl.GridLines = GridLines.Vertical;
  295. Assert.AreEqual (GridLines.Vertical, dl.GridLines, "GridLines");
  296. dl.HorizontalAlign = HorizontalAlign.Center;
  297. Assert.AreEqual (HorizontalAlign.Center, dl.HorizontalAlign, "HorizontalAlign");
  298. #if NET_2_0
  299. int sc = 0;
  300. // so the TableStyle isn't kept directly in the ViewState
  301. #else
  302. int sc = 4;
  303. #endif
  304. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-2");
  305. // now for the DataList properties
  306. // touching all styles
  307. dl.AlternatingItemStyle.BackColor = Color.AliceBlue;
  308. Assert.IsFalse (dl.AlternatingItemStyle.BackColor.IsEmpty, "AlternatingItemStyle");
  309. dl.EditItemStyle.BackColor = Color.AntiqueWhite;
  310. Assert.IsFalse (dl.EditItemStyle.BackColor.IsEmpty, "EditItemStyle");
  311. dl.FooterStyle.BackColor = Color.Aqua;
  312. Assert.IsFalse (dl.FooterStyle.BackColor.IsEmpty, "FooterStyle");
  313. dl.HeaderStyle.BackColor = Color.Aquamarine;
  314. Assert.IsFalse (dl.HeaderStyle.BackColor.IsEmpty, "HeaderStyle");
  315. dl.ItemStyle.BackColor = Color.Azure;
  316. Assert.IsFalse (dl.ItemStyle.BackColor.IsEmpty, "ItemStyle");
  317. dl.SelectedItemStyle.BackColor = Color.Beige;
  318. Assert.IsFalse (dl.SelectedItemStyle.BackColor.IsEmpty, "SelectedItemStyle");
  319. dl.SeparatorStyle.BackColor = Color.Bisque;
  320. Assert.IsFalse (dl.SeparatorStyle.BackColor.IsEmpty, "SeparatorStyle");
  321. dl.AlternatingItemTemplate = new TestTemplate ();
  322. Assert.IsNotNull (dl.AlternatingItemTemplate, "AlternatingItemTemplate");
  323. dl.EditItemTemplate = new TestTemplate ();
  324. Assert.IsNotNull (dl.EditItemTemplate, "EditItemTemplate");
  325. dl.FooterTemplate = new TestTemplate ();
  326. Assert.IsTrue (riu.HasFooter, "HasFooter");
  327. Assert.IsNotNull (dl.FooterTemplate, "FooterTemplate");
  328. dl.HeaderTemplate = new TestTemplate ();
  329. Assert.IsTrue (riu.HasHeader, "HasHeader");
  330. Assert.IsNotNull (dl.HeaderTemplate, "HeaderTemplate");
  331. dl.ItemTemplate = new TestTemplate ();
  332. Assert.IsNotNull (dl.ItemTemplate, "ItemTemplate");
  333. dl.SelectedItemTemplate = new TestTemplate ();
  334. Assert.IsNotNull (dl.SelectedItemTemplate, "SelectedItemTemplate");
  335. dl.SeparatorTemplate = new TestTemplate ();
  336. Assert.IsTrue (riu.HasSeparators, "HasSeparators");
  337. Assert.IsNotNull (dl.SeparatorTemplate, "SeparatorTemplate");
  338. dl.EditItemIndex = 0;
  339. Assert.AreEqual (0, dl.EditItemIndex, "EditItemIndex");
  340. #if NET_2_0
  341. dl.EditItemIndex = -1;
  342. #endif
  343. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-2b");
  344. dl.SelectedIndex = 0;
  345. Assert.AreEqual (0, dl.SelectedIndex, "SelectedIndex");
  346. #if NET_2_0
  347. dl.SelectedIndex = -1;
  348. #endif
  349. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-2c");
  350. dl.ExtractTemplateRows = true;
  351. Assert.IsTrue (dl.ExtractTemplateRows, "ExtractTemplateRows");
  352. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-3");
  353. dl.RepeatColumns = 1;
  354. Assert.AreEqual (1, dl.RepeatColumns, "RepeatColumns");
  355. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-4");
  356. dl.RepeatDirection = RepeatDirection.Horizontal;
  357. Assert.AreEqual (RepeatDirection.Horizontal, dl.RepeatDirection, "RepeatDirection");
  358. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-5");
  359. dl.RepeatLayout = RepeatLayout.Flow;
  360. Assert.AreEqual (RepeatLayout.Flow, dl.RepeatLayout, "RepeatLayout");
  361. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-6");
  362. dl.ShowFooter = false;
  363. Assert.IsFalse (riu.HasFooter, "HasFooter(lost)");
  364. Assert.IsFalse (dl.ShowFooter, "ShowFooter");
  365. Assert.AreEqual (sc++, dl.StateBag.Count, "ViewState.Count-7");
  366. dl.ShowHeader = false;
  367. Assert.IsFalse (riu.HasHeader, "HasHeader(lost)");
  368. Assert.IsFalse (dl.ShowHeader, "ShowHeader");
  369. Assert.AreEqual (sc, dl.StateBag.Count, "ViewState.Count-8");
  370. // reverting back changes to default...
  371. dl.CellPadding = -1;
  372. Assert.AreEqual (-1, dl.CellPadding, "-CellPadding");
  373. dl.CellSpacing = 0;
  374. Assert.AreEqual (0, dl.CellSpacing, "-CellSpacing");
  375. dl.GridLines = GridLines.None;
  376. Assert.AreEqual (GridLines.None, dl.GridLines, "-GridLines");
  377. dl.HorizontalAlign = HorizontalAlign.NotSet;
  378. Assert.AreEqual (HorizontalAlign.NotSet, dl.HorizontalAlign, "-HorizontalAlign");
  379. Assert.AreEqual (sc, dl.StateBag.Count, "ViewState.Count-9");
  380. dl.AlternatingItemStyle.Reset ();
  381. Assert.IsTrue (dl.AlternatingItemStyle.BackColor.IsEmpty, "-AlternatingItemStyle");
  382. dl.EditItemStyle.Reset ();
  383. Assert.IsTrue (dl.EditItemStyle.BackColor.IsEmpty, "-EditItemStyle");
  384. dl.FooterStyle.Reset ();
  385. Assert.IsTrue (dl.FooterStyle.BackColor.IsEmpty, "-FooterStyle");
  386. dl.HeaderStyle.Reset ();
  387. Assert.IsTrue (dl.HeaderStyle.BackColor.IsEmpty, "-HeaderStyle");
  388. dl.ItemStyle.Reset ();
  389. Assert.IsTrue (dl.ItemStyle.BackColor.IsEmpty, "-ItemStyle");
  390. dl.SelectedItemStyle.Reset ();
  391. Assert.IsTrue (dl.SelectedItemStyle.BackColor.IsEmpty, "-SelectedItemStyle");
  392. dl.SeparatorStyle.Reset ();
  393. Assert.IsTrue (dl.SeparatorStyle.BackColor.IsEmpty, "-SeparatorStyle");
  394. dl.AlternatingItemTemplate = null;
  395. Assert.IsNull (dl.AlternatingItemTemplate, "-AlternatingItemTemplate");
  396. dl.EditItemTemplate = null;
  397. Assert.IsNull (dl.EditItemTemplate, "-EditItemTemplate");
  398. dl.FooterTemplate = null;
  399. Assert.IsNull (dl.FooterTemplate, "-FooterTemplate");
  400. dl.HeaderTemplate = null;
  401. Assert.IsNull (dl.HeaderTemplate, "-HeaderTemplate");
  402. dl.ItemTemplate = null;
  403. Assert.IsNull (dl.ItemTemplate, "-ItemTemplate");
  404. dl.SelectedItemTemplate = null;
  405. Assert.IsNull (dl.SelectedItemTemplate, "-SelectedItemTemplate");
  406. dl.SeparatorTemplate = null;
  407. Assert.IsNull (dl.SeparatorTemplate, "-SeparatorTemplate");
  408. dl.EditItemIndex = -1;
  409. Assert.AreEqual (-1, dl.EditItemIndex, "-EditItemIndex");
  410. dl.SelectedIndex = -1;
  411. Assert.AreEqual (-1, dl.SelectedIndex, "-SelectedIndex");
  412. dl.ExtractTemplateRows = false;
  413. Assert.IsFalse (dl.ExtractTemplateRows, "-ExtractTemplateRows");
  414. dl.RepeatColumns = 0;
  415. Assert.AreEqual (0, dl.RepeatColumns, "-RepeatColumns");
  416. dl.RepeatDirection = RepeatDirection.Vertical;
  417. Assert.AreEqual (RepeatDirection.Vertical, dl.RepeatDirection, "-RepeatDirection");
  418. dl.RepeatLayout = RepeatLayout.Table;
  419. Assert.AreEqual (RepeatLayout.Table, dl.RepeatLayout, "-RepeatLayout");
  420. dl.ShowFooter = true;
  421. Assert.IsTrue (dl.ShowFooter, "-ShowFooter");
  422. dl.ShowHeader = true;
  423. Assert.IsTrue (dl.ShowHeader, "-ShowHeader");
  424. Assert.AreEqual (sc, dl.StateBag.Count, "ViewState.Count-10");
  425. // and all this didn't affect IRepeatInfoUser
  426. CheckIRepeatInfoUser (dl);
  427. Assert.AreEqual (0, dl.Attributes.Count, "Attributes.Count-2");
  428. }
  429. [Test]
  430. public void RepeatedItemCount ()
  431. {
  432. TestDataList dl = new TestDataList ();
  433. IRepeatInfoUser riu = (dl as IRepeatInfoUser);
  434. dl.DataSource = GetData (10);
  435. Assert.AreEqual (0, riu.RepeatedItemCount, "before Bind");
  436. dl.DataBind ();
  437. Assert.AreEqual (10, riu.RepeatedItemCount, "after Bind");
  438. }
  439. [Test]
  440. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  441. public void GetItemStyle_Header_Empty ()
  442. {
  443. TestDataList dl = new TestDataList ();
  444. IRepeatInfoUser riu = (dl as IRepeatInfoUser);
  445. // empty list/controls
  446. riu.GetItemStyle (ListItemType.Header, 0);
  447. }
  448. [Test]
  449. public void GetItemStyle_Header ()
  450. {
  451. TestDataList dl = new TestDataList ();
  452. dl.DataSource = GetData (6);
  453. dl.DataBind ();
  454. IRepeatInfoUser riu = (dl as IRepeatInfoUser);
  455. Assert.IsNull (riu.GetItemStyle (ListItemType.Header, 0));
  456. }
  457. [Test]
  458. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  459. public void GetItemStyle_Separator_Empty ()
  460. {
  461. TestDataList dl = new TestDataList ();
  462. IRepeatInfoUser riu = (dl as IRepeatInfoUser);
  463. // empty list/controls
  464. riu.GetItemStyle (ListItemType.Separator, 0);
  465. }
  466. [Test]
  467. public void GetItemStyle_Separator ()
  468. {
  469. TestDataList dl = new TestDataList ();
  470. dl.DataSource = GetData (6);
  471. dl.DataBind ();
  472. IRepeatInfoUser riu = (dl as IRepeatInfoUser);
  473. Assert.IsNull (riu.GetItemStyle (ListItemType.Separator, 0));
  474. }
  475. [Test]
  476. public void GetItemStyle_Pager_Empty ()
  477. {
  478. TestDataList dl = new TestDataList ();
  479. IRepeatInfoUser riu = (dl as IRepeatInfoUser);
  480. // Pager isn't supported in DataList
  481. Assert.IsNull (riu.GetItemStyle (ListItemType.Pager, 0), "Pager-0");
  482. }
  483. [Test]
  484. public void Controls ()
  485. {
  486. TestDataList dl = new TestDataList ();
  487. Assert.AreEqual (0, dl.Controls.Count, "Controls-1");
  488. Assert.AreEqual (0, dl.Items.Count, "Items-1");
  489. dl.DataSource = GetDataSource (3);
  490. Assert.AreEqual (0, dl.Controls.Count, "Controls-2");
  491. Assert.AreEqual (0, dl.Items.Count, "Items-2");
  492. dl.DataBind ();
  493. Assert.AreEqual (0, dl.Controls.Count, "Controls-3");
  494. Assert.AreEqual (0, dl.Items.Count, "Items-3");
  495. }
  496. [Test]
  497. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  498. public void EditItemIndexTooLow ()
  499. {
  500. TestDataList dl = new TestDataList ();
  501. dl.EditItemIndex = -2;
  502. }
  503. [Test]
  504. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  505. public void SelectedIndexTooLow ()
  506. {
  507. TestDataList dl = new TestDataList ();
  508. dl.SelectedIndex = -2;
  509. }
  510. [Test]
  511. public void SelectIndexOutOfRange ()
  512. {
  513. TestDataList dl = new TestDataList ();
  514. // No exception is thrown
  515. dl.SelectedIndex = 25;
  516. }
  517. [Test]
  518. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  519. public void SelectItemOutOfRange ()
  520. {
  521. TestDataList dl = new TestDataList ();
  522. dl.SelectedIndex = 25;
  523. DataListItem dli = dl.SelectedItem;
  524. }
  525. [Test]
  526. public void SaveViewState ()
  527. {
  528. TestDataList dl = new TestDataList ();
  529. dl.TrackState ();
  530. object[] vs = (object[]) dl.SaveState ();
  531. #if NET_2_0
  532. Assert.AreEqual (9, vs.Length, "Size");
  533. #else
  534. Assert.AreEqual (8, vs.Length, "Size");
  535. #endif
  536. // By default the viewstate is all null
  537. int i = 0;
  538. for (; i < vs.Length; i++)
  539. Assert.IsNull (vs [i], "Empty-" + i);
  540. i = 0;
  541. #if NET_2_0
  542. i++;
  543. #else
  544. dl.GridLines = GridLines.Vertical;
  545. vs = (object []) dl.SaveState ();
  546. Assert.IsNotNull (vs[i++], "GridLines");
  547. #endif
  548. dl.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
  549. vs = (object []) dl.SaveState ();
  550. Assert.IsNotNull (vs[i++], "ItemStyle");
  551. dl.SelectedItemStyle.HorizontalAlign = HorizontalAlign.Center;
  552. vs = (object []) dl.SaveState ();
  553. Assert.IsNotNull (vs[i++], "SelectedItemStyle");
  554. dl.AlternatingItemStyle.HorizontalAlign = HorizontalAlign.Center;
  555. vs = (object []) dl.SaveState ();
  556. Assert.IsNotNull (vs[i++], "AlternatingItemStyle");
  557. dl.EditItemStyle.HorizontalAlign = HorizontalAlign.Center;
  558. vs = (object []) dl.SaveState ();
  559. Assert.IsNotNull (vs[i++], "EditItemStyle");
  560. dl.SeparatorStyle.HorizontalAlign = HorizontalAlign.Center;
  561. vs = (object []) dl.SaveState ();
  562. Assert.IsNotNull (vs[i++], "SeparatorStyle");
  563. dl.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
  564. vs = (object []) dl.SaveState ();
  565. Assert.IsNotNull (vs[i++], "HeaderStyle");
  566. dl.FooterStyle.HorizontalAlign = HorizontalAlign.Center;
  567. vs = (object []) dl.SaveState ();
  568. Assert.IsNotNull (vs[i++], "FooterStyle");
  569. #if NET_2_0
  570. // GridLines was moved last
  571. dl.GridLines = GridLines.Vertical;
  572. vs = (object []) dl.SaveState ();
  573. Assert.IsNotNull (vs[i++], "GridLines");
  574. #endif
  575. }
  576. #if NET_2_0
  577. [Test]
  578. [ExpectedException (typeof (InvalidOperationException))]
  579. public void SelectedValue_WithoutDataKeyField ()
  580. {
  581. TestDataList dl = new TestDataList ();
  582. Assert.IsNull (dl.SelectedValue, "SelectedValue");
  583. }
  584. [Test]
  585. public void SelectedValue_WithUnexistingDataKeyField ()
  586. {
  587. TestDataList dl = new TestDataList ();
  588. dl.DataKeyField = "mono";
  589. Assert.IsNull (dl.SelectedValue, "SelectedValue");
  590. }
  591. #endif
  592. private bool cancelCommandEvent;
  593. private bool deleteCommandEvent;
  594. private bool editCommandEvent;
  595. private bool itemCommandEvent;
  596. private bool itemCreatedEvent;
  597. private bool itemDataBoundEvent;
  598. private bool updateCommandEvent;
  599. private bool selectedIndexChangedEvent;
  600. private void ResetEvents ()
  601. {
  602. cancelCommandEvent = false;
  603. deleteCommandEvent = false;
  604. editCommandEvent = false;
  605. itemCommandEvent = false;
  606. itemCreatedEvent = false;
  607. itemDataBoundEvent = false;
  608. updateCommandEvent = false;
  609. selectedIndexChangedEvent = false;
  610. }
  611. private void CancelCommandHandler (object sender, DataListCommandEventArgs e)
  612. {
  613. cancelCommandEvent = true;
  614. }
  615. private void DeleteCommandHandler (object sender, DataListCommandEventArgs e)
  616. {
  617. deleteCommandEvent = true;
  618. }
  619. private void EditCommandHandler (object sender, DataListCommandEventArgs e)
  620. {
  621. editCommandEvent = true;
  622. }
  623. private void ItemCommandHandler (object sender, DataListCommandEventArgs e)
  624. {
  625. itemCommandEvent = true;
  626. }
  627. private void ItemCreatedHandler (object sender, DataListItemEventArgs e)
  628. {
  629. itemCreatedEvent = true;
  630. }
  631. private void ItemDataBoundHandler (object sender, DataListItemEventArgs e)
  632. {
  633. itemDataBoundEvent = true;
  634. }
  635. private void SelectedIndexChangedHandler (object sender, EventArgs e)
  636. {
  637. selectedIndexChangedEvent = true;
  638. }
  639. private void UpdateCommandHandler (object sender, DataListCommandEventArgs e)
  640. {
  641. updateCommandEvent = true;
  642. }
  643. [Test]
  644. public void Events ()
  645. {
  646. TestDataList dl = new TestDataList ();
  647. DataListCommandEventArgs command_args = new DataListCommandEventArgs (null,
  648. null, new CommandEventArgs (String.Empty, String.Empty));
  649. DataListItemEventArgs item_args = new DataListItemEventArgs (null);
  650. ResetEvents ();
  651. dl.CancelCommand += new DataListCommandEventHandler (CancelCommandHandler);
  652. dl.DoCancelCommand (command_args);
  653. Assert.IsTrue (cancelCommandEvent, "cancelCommandEvent");
  654. ResetEvents ();
  655. dl.DeleteCommand += new DataListCommandEventHandler (DeleteCommandHandler);
  656. dl.DoDeleteCommand (command_args);
  657. Assert.IsTrue (deleteCommandEvent, "deleteCommandEvent");
  658. ResetEvents ();
  659. dl.EditCommand += new DataListCommandEventHandler (EditCommandHandler);
  660. dl.DoEditCommand (command_args);
  661. Assert.IsTrue (editCommandEvent, "editCommandEvent");
  662. ResetEvents ();
  663. dl.ItemCommand += new DataListCommandEventHandler (ItemCommandHandler);
  664. dl.DoItemCommand (command_args);
  665. Assert.IsTrue (itemCommandEvent, "itemCommandEvent");
  666. ResetEvents ();
  667. dl.ItemCreated += new DataListItemEventHandler (ItemCreatedHandler);
  668. dl.DoItemCreated (item_args);
  669. Assert.IsTrue (itemCreatedEvent, "itemCreatedEvent");
  670. ResetEvents ();
  671. dl.ItemDataBound += new DataListItemEventHandler (ItemDataBoundHandler);
  672. dl.DoItemDataBound (item_args);
  673. Assert.IsTrue (itemDataBoundEvent, "itemDataBoundEvent");
  674. ResetEvents ();
  675. dl.UpdateCommand += new DataListCommandEventHandler (UpdateCommandHandler);
  676. dl.DoUpdateCommand (command_args);
  677. Assert.IsTrue (updateCommandEvent, "updateCommandEvent");
  678. ResetEvents ();
  679. dl.SelectedIndexChanged += new EventHandler (SelectedIndexChangedHandler);
  680. dl.DoSelectedIndexChanged (new EventArgs ());
  681. Assert.IsTrue (selectedIndexChangedEvent, "selectedIndexChangedEvent");
  682. }
  683. [Test]
  684. public void BubbleEvent ()
  685. {
  686. TestDataList dl = new TestDataList ();
  687. DataListCommandEventArgs command_args;
  688. //
  689. // Cancel
  690. //
  691. ResetEvents ();
  692. command_args = new DataListCommandEventArgs (null,
  693. null, new CommandEventArgs ("Cancel", String.Empty));
  694. dl.CancelCommand += new DataListCommandEventHandler (CancelCommandHandler);
  695. dl.DoBubbleEvent (this, command_args);
  696. Assert.IsTrue (cancelCommandEvent, "cancelCommandEvent-1");
  697. ResetEvents ();
  698. command_args = new DataListCommandEventArgs (null,
  699. null, new CommandEventArgs ("cancel", String.Empty));
  700. dl.CancelCommand += new DataListCommandEventHandler (CancelCommandHandler);
  701. dl.DoBubbleEvent (this, command_args);
  702. Assert.IsTrue (cancelCommandEvent, "cancelCommandEvent-2");
  703. ResetEvents ();
  704. command_args = new DataListCommandEventArgs (null,
  705. null, new CommandEventArgs ("CANCEL", String.Empty));
  706. dl.CancelCommand += new DataListCommandEventHandler (CancelCommandHandler);
  707. dl.DoBubbleEvent (this, command_args);
  708. Assert.IsTrue (cancelCommandEvent, "cancelCommandEvent-3");
  709. //
  710. // Delete
  711. //
  712. ResetEvents ();
  713. command_args = new DataListCommandEventArgs (null,
  714. null, new CommandEventArgs ("Delete", String.Empty));
  715. dl.DeleteCommand += new DataListCommandEventHandler (DeleteCommandHandler);
  716. dl.DoBubbleEvent (this, command_args);
  717. Assert.IsTrue (deleteCommandEvent, "deleteCommandEvent-1");
  718. ResetEvents ();
  719. command_args = new DataListCommandEventArgs (null,
  720. null, new CommandEventArgs ("delete", String.Empty));
  721. dl.DeleteCommand += new DataListCommandEventHandler (DeleteCommandHandler);
  722. dl.DoBubbleEvent (this, command_args);
  723. Assert.IsTrue (deleteCommandEvent, "deleteCommandEvent-2");
  724. ResetEvents ();
  725. command_args = new DataListCommandEventArgs (null,
  726. null, new CommandEventArgs ("DELETE", String.Empty));
  727. dl.DeleteCommand += new DataListCommandEventHandler (DeleteCommandHandler);
  728. dl.DoBubbleEvent (this, command_args);
  729. Assert.IsTrue (deleteCommandEvent, "deleteCommandEvent-3");
  730. //
  731. // Edit
  732. //
  733. ResetEvents ();
  734. command_args = new DataListCommandEventArgs (null,
  735. null, new CommandEventArgs ("Edit", String.Empty));
  736. dl.EditCommand += new DataListCommandEventHandler (EditCommandHandler);
  737. dl.DoBubbleEvent (this, command_args);
  738. Assert.IsTrue (editCommandEvent, "editCommandEvent-1");
  739. ResetEvents ();
  740. command_args = new DataListCommandEventArgs (null,
  741. null, new CommandEventArgs ("edit", String.Empty));
  742. dl.EditCommand += new DataListCommandEventHandler (EditCommandHandler);
  743. dl.DoBubbleEvent (this, command_args);
  744. Assert.IsTrue (editCommandEvent, "editCommandEvent-2");
  745. ResetEvents ();
  746. command_args = new DataListCommandEventArgs (null,
  747. null, new CommandEventArgs ("EDIT", String.Empty));
  748. dl.EditCommand += new DataListCommandEventHandler (EditCommandHandler);
  749. dl.DoBubbleEvent (this, command_args);
  750. Assert.IsTrue (editCommandEvent, "editCommandEvent-3");
  751. //
  752. // Item
  753. //
  754. ResetEvents ();
  755. command_args = new DataListCommandEventArgs (null,
  756. null, new CommandEventArgs ("Item", String.Empty));
  757. dl.ItemCommand += new DataListCommandEventHandler (ItemCommandHandler);
  758. dl.DoBubbleEvent (this, command_args);
  759. Assert.IsTrue (itemCommandEvent, "itemCommandEvent-1");
  760. ResetEvents ();
  761. command_args = new DataListCommandEventArgs (null,
  762. null, new CommandEventArgs ("item", String.Empty));
  763. dl.ItemCommand += new DataListCommandEventHandler (ItemCommandHandler);
  764. dl.DoBubbleEvent (this, command_args);
  765. Assert.IsTrue (itemCommandEvent, "itemCommandEvent-2");
  766. ResetEvents ();
  767. command_args = new DataListCommandEventArgs (null,
  768. null, new CommandEventArgs ("ITEM", String.Empty));
  769. dl.ItemCommand += new DataListCommandEventHandler (ItemCommandHandler);
  770. dl.DoBubbleEvent (this, command_args);
  771. Assert.IsTrue (itemCommandEvent, "itemCommandEvent-3");
  772. //
  773. // Update
  774. //
  775. ResetEvents ();
  776. command_args = new DataListCommandEventArgs (null,
  777. null, new CommandEventArgs ("Update", String.Empty));
  778. dl.UpdateCommand += new DataListCommandEventHandler (UpdateCommandHandler);
  779. dl.DoBubbleEvent (this, command_args);
  780. Assert.IsTrue (updateCommandEvent, "updateCommandEvent-1");
  781. ResetEvents ();
  782. command_args = new DataListCommandEventArgs (null,
  783. null, new CommandEventArgs ("update", String.Empty));
  784. dl.UpdateCommand += new DataListCommandEventHandler (UpdateCommandHandler);
  785. dl.DoBubbleEvent (this, command_args);
  786. Assert.IsTrue (updateCommandEvent, "updateCommandEvent-2");
  787. ResetEvents ();
  788. command_args = new DataListCommandEventArgs (null,
  789. null, new CommandEventArgs ("UPDATE", String.Empty));
  790. dl.UpdateCommand += new DataListCommandEventHandler (UpdateCommandHandler);
  791. dl.DoBubbleEvent (this, command_args);
  792. Assert.IsTrue (updateCommandEvent, "updateCommandEvent-3");
  793. //
  794. // Select
  795. //
  796. DataListItem item = new DataListItem (0, ListItemType.Item);
  797. ResetEvents ();
  798. command_args = new DataListCommandEventArgs (item, null,
  799. new CommandEventArgs ("Select", String.Empty));
  800. dl.SelectedIndexChanged += new EventHandler (SelectedIndexChangedHandler);
  801. dl.DoBubbleEvent (this, command_args);
  802. Assert.IsTrue (selectedIndexChangedEvent, "selectedIndexChangedEvent-1");
  803. ResetEvents ();
  804. command_args = new DataListCommandEventArgs (item, null,
  805. new CommandEventArgs ("select", String.Empty));
  806. dl.SelectedIndexChanged += new EventHandler (SelectedIndexChangedHandler);
  807. dl.DoBubbleEvent (this, command_args);
  808. Assert.IsTrue (selectedIndexChangedEvent, "selectedIndexChangedEvent-2");
  809. ResetEvents ();
  810. command_args = new DataListCommandEventArgs (item, null,
  811. new CommandEventArgs ("SELECT", String.Empty));
  812. dl.SelectedIndexChanged += new EventHandler (SelectedIndexChangedHandler);
  813. dl.DoBubbleEvent (this, command_args);
  814. Assert.IsTrue (selectedIndexChangedEvent, "selectedIndexChangedEvent-3");
  815. }
  816. }
  817. }