BaseDataListTest.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. //
  2. // BaseDataListTest.cs
  3. // - Unit tests for System.Web.UI.WebControls.BaseDataList
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[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 System;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.ComponentModel;
  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 TestBaseDataList : BaseDataList {
  40. private bool dataBindingCalled;
  41. public TestBaseDataList ()
  42. : base ()
  43. {
  44. }
  45. public string Tag {
  46. get { return base.TagName; }
  47. }
  48. public StateBag StateBag {
  49. get { return base.ViewState; }
  50. }
  51. #if NET_2_0
  52. public bool IsDataBoundByDataSourceId {
  53. get { return base.IsBoundUsingDataSourceID; }
  54. }
  55. public bool IsInitialized {
  56. get { return base.Initialized; }
  57. }
  58. public bool RequiresDataBind {
  59. get { return base.RequiresDataBinding; }
  60. }
  61. public DataSourceSelectArguments Arguments {
  62. get { return base.SelectArguments; }
  63. }
  64. #endif
  65. public void Add (object o)
  66. {
  67. base.AddParsedSubObject (o);
  68. }
  69. public string Render ()
  70. {
  71. StringWriter sw = new StringWriter ();
  72. sw.NewLine = "\n";
  73. HtmlTextWriter writer = new HtmlTextWriter (sw);
  74. base.Render (writer);
  75. return writer.InnerWriter.ToString ();
  76. }
  77. protected override void CreateControlHierarchy (bool useDataSource)
  78. {
  79. }
  80. protected override void PrepareControlHierarchy ()
  81. {
  82. }
  83. public void DoSelectedIndexChanged (EventArgs e)
  84. {
  85. OnSelectedIndexChanged (e);
  86. }
  87. #if NET_2_0
  88. public DataSourceSelectArguments CreateArguments ()
  89. {
  90. return base.CreateDataSourceSelectArguments ();
  91. }
  92. public IEnumerable Data ()
  93. {
  94. return base.GetData ();
  95. }
  96. public void DataBindBool (bool raise)
  97. {
  98. DataBind (raise);
  99. }
  100. public void Ensure ()
  101. {
  102. base.EnsureDataBound ();
  103. }
  104. #endif
  105. public bool DataBindingCalled {
  106. get { return dataBindingCalled; }
  107. set { dataBindingCalled = value; }
  108. }
  109. protected override void OnDataBinding (EventArgs e)
  110. {
  111. dataBindingCalled = true;
  112. base.OnDataBinding (e);
  113. }
  114. #if NET_2_0
  115. private bool dataPropertyChangedCalled;
  116. private bool dataSourceViewChangedCalled;
  117. private bool initCalled;
  118. private bool loadCalled;
  119. private bool preRenderCalled;
  120. public bool DataPropertyChangedCalled {
  121. get { return dataPropertyChangedCalled; }
  122. set { dataPropertyChangedCalled = value; }
  123. }
  124. protected override void OnDataPropertyChanged ()
  125. {
  126. dataPropertyChangedCalled = true;
  127. base.OnDataPropertyChanged ();
  128. }
  129. public bool DataSourceViewChangedCalled {
  130. get { return dataSourceViewChangedCalled; }
  131. set { dataSourceViewChangedCalled = value; }
  132. }
  133. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  134. {
  135. dataSourceViewChangedCalled = true;
  136. base.OnDataSourceViewChanged (sender, e);
  137. }
  138. public void BaseOnDataSourceViewChanged (object sender, EventArgs e)
  139. {
  140. base.OnDataSourceViewChanged (sender, e);
  141. }
  142. public bool InitCalled {
  143. get { return initCalled; }
  144. set { initCalled = value; }
  145. }
  146. protected override void OnInit (EventArgs e)
  147. {
  148. initCalled = true;
  149. base.OnInit (e);
  150. }
  151. public void BaseOnInit (EventArgs e)
  152. {
  153. base.OnInit (e);
  154. }
  155. public bool LoadCalled {
  156. get { return loadCalled; }
  157. set { loadCalled = value; }
  158. }
  159. protected override void OnLoad (EventArgs e)
  160. {
  161. loadCalled = true;
  162. base.OnLoad (e);
  163. }
  164. public void BaseOnLoad (EventArgs e)
  165. {
  166. base.OnLoad (e);
  167. }
  168. public bool PreRenderCalled {
  169. get { return preRenderCalled; }
  170. set { preRenderCalled = value; }
  171. }
  172. protected override void OnPreRender (EventArgs e)
  173. {
  174. preRenderCalled = true;
  175. base.OnPreRender (e);
  176. }
  177. public void BaseOnPreRender (EventArgs e)
  178. {
  179. base.OnPreRender (e);
  180. }
  181. #endif
  182. }
  183. public class TestDataSource : IListSource {
  184. private ArrayList list;
  185. public TestDataSource (ArrayList al)
  186. {
  187. list = al;
  188. }
  189. public bool ContainsListCollection {
  190. get { return true; }
  191. }
  192. public IList GetList ()
  193. {
  194. return list;
  195. }
  196. }
  197. #if NET_2_0
  198. public class Test2DataSource : WebControl, IDataSource {
  199. public DataSourceView GetView (string viewName)
  200. {
  201. return new Test2DataSourceView (this, String.Empty);
  202. }
  203. public ICollection GetViewNames ()
  204. {
  205. return null;
  206. }
  207. public event EventHandler DataSourceChanged;
  208. }
  209. public class Test2DataSourceView : DataSourceView {
  210. public Test2DataSourceView (IDataSource owner, string viewName)
  211. : base (owner, viewName)
  212. {
  213. }
  214. protected override IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
  215. {
  216. ArrayList al = new ArrayList (3);
  217. for (int i=0; i < 3; i++)
  218. al.Add (i+1);
  219. return al;
  220. }
  221. }
  222. #endif
  223. [TestFixture]
  224. public class BaseDataListTest {
  225. private HtmlTextWriter GetWriter ()
  226. {
  227. StringWriter sw = new StringWriter ();
  228. sw.NewLine = "\n";
  229. return new HtmlTextWriter (sw);
  230. }
  231. // IEnumerable (and IList) based
  232. private IEnumerable GetData (int n)
  233. {
  234. ArrayList al = new ArrayList ();
  235. for (int i = 0; i < n; i++) {
  236. al.Add (i.ToString ());
  237. }
  238. return (IEnumerable) al;
  239. }
  240. // IListSource based
  241. private TestDataSource GetDataSource (int n)
  242. {
  243. return new TestDataSource ((ArrayList)GetData (n));
  244. }
  245. [Test]
  246. public void DefaultProperties ()
  247. {
  248. TestBaseDataList bdl = new TestBaseDataList ();
  249. Assert.AreEqual ("span", bdl.Tag, "TagName");
  250. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-1");
  251. Assert.AreEqual (0, bdl.StateBag.Count, "ViewState.Count-1");
  252. Assert.AreEqual (String.Empty, bdl.Caption, "Caption");
  253. Assert.AreEqual (TableCaptionAlign.NotSet, bdl.CaptionAlign, "CaptionAlign");
  254. Assert.AreEqual (-1, bdl.CellPadding, "CellPadding");
  255. Assert.AreEqual (0, bdl.CellSpacing, "CellSpacing");
  256. Assert.AreEqual (0, bdl.Controls.Count, "Controls.Count");
  257. Assert.AreEqual (String.Empty, bdl.DataKeyField, "DataKeyField");
  258. Assert.AreEqual (String.Empty, bdl.DataMember, "DataMember");
  259. Assert.IsNull (bdl.DataSource, "DataSource");
  260. Assert.AreEqual (GridLines.Both, bdl.GridLines, "GridLines");
  261. Assert.AreEqual (HorizontalAlign.NotSet, bdl.HorizontalAlign, "HorizontalAlign");
  262. Assert.IsFalse (bdl.UseAccessibleHeader, "UseAccessibleHeader");
  263. #if NET_2_0
  264. Assert.AreEqual (String.Empty, bdl.DataSourceID, "DataSourceID");
  265. #endif
  266. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-2");
  267. Assert.AreEqual (0, bdl.StateBag.Count, "ViewState.Count-2");
  268. Assert.AreEqual (0, bdl.DataKeys.Count, "DataKeys.Count");
  269. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-3");
  270. // triggered by DataKeys, which makes DataKeysArray store its value.
  271. Assert.AreEqual (1, bdl.StateBag.Count, "ViewState.Count-3");
  272. Assert.AreEqual (typeof (ArrayList), bdl.StateBag ["DataKeys"].GetType (), "ViewState.Value-1");
  273. }
  274. [Test]
  275. public void NullProperties ()
  276. {
  277. TestBaseDataList bdl = new TestBaseDataList ();
  278. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-1");
  279. Assert.AreEqual (0, bdl.StateBag.Count, "ViewState.Count-1");
  280. bdl.Caption = null;
  281. Assert.AreEqual (String.Empty, bdl.Caption, "Caption");
  282. bdl.CaptionAlign = TableCaptionAlign.NotSet;
  283. Assert.AreEqual (TableCaptionAlign.NotSet, bdl.CaptionAlign, "CaptionAlign");
  284. Assert.AreEqual (1, bdl.StateBag.Count, "ViewState.Count-2");
  285. // many properties can't be set without causing a InvalidCastException
  286. bdl.DataKeyField = null;
  287. Assert.AreEqual (String.Empty, bdl.DataKeyField, "DataKeyField");
  288. bdl.DataMember = null;
  289. Assert.AreEqual (String.Empty, bdl.DataMember, "DataMember");
  290. bdl.DataSource = null;
  291. Assert.IsNull (bdl.DataSource, "DataSource");
  292. bdl.UseAccessibleHeader = false;
  293. Assert.IsFalse (bdl.UseAccessibleHeader, "UseAccessibleHeader");
  294. #if NET_2_0
  295. bdl.DataSourceID = String.Empty;
  296. Assert.AreEqual (String.Empty, bdl.DataSourceID, "DataSourceID");
  297. Assert.AreEqual (3, bdl.StateBag.Count, "ViewState.Count-3");
  298. #else
  299. Assert.AreEqual (2, bdl.StateBag.Count, "ViewState.Count-3");
  300. #endif
  301. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-2");
  302. }
  303. [Test]
  304. public void CleanProperties ()
  305. {
  306. TestBaseDataList bdl = new TestBaseDataList ();
  307. bdl.Caption = "Mono";
  308. Assert.AreEqual ("Mono", bdl.Caption, "Caption");
  309. bdl.CaptionAlign = TableCaptionAlign.Top;
  310. Assert.AreEqual (TableCaptionAlign.Top, bdl.CaptionAlign, "CaptionAlign");
  311. // many properties can't be set without causing a InvalidCastException
  312. bdl.DataKeyField = "key";
  313. Assert.AreEqual ("key", bdl.DataKeyField, "DataKeyField");
  314. bdl.DataMember = "member";
  315. Assert.AreEqual ("member", bdl.DataMember, "DataMember");
  316. bdl.DataSource = GetData (2);
  317. Assert.IsNotNull (bdl.DataSource, "DataSource");
  318. bdl.UseAccessibleHeader = true;
  319. Assert.IsTrue (bdl.UseAccessibleHeader, "UseAccessibleHeader");
  320. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-1");
  321. Assert.AreEqual (5, bdl.StateBag.Count, "ViewState.Count-1");
  322. bdl.Caption = null;
  323. Assert.AreEqual (String.Empty, bdl.Caption, "-Caption");
  324. bdl.CaptionAlign = TableCaptionAlign.NotSet;
  325. Assert.AreEqual (TableCaptionAlign.NotSet, bdl.CaptionAlign, "-CaptionAlign");
  326. // many properties can't be set without causing a InvalidCastException
  327. bdl.DataKeyField = null;
  328. Assert.AreEqual (String.Empty, bdl.DataKeyField, "-DataKeyField");
  329. bdl.DataMember = null;
  330. Assert.AreEqual (String.Empty, bdl.DataMember, "-DataMember");
  331. bdl.DataSource = null;
  332. Assert.IsNull (bdl.DataSource, "-DataSource");
  333. bdl.UseAccessibleHeader = false;
  334. Assert.IsFalse (bdl.UseAccessibleHeader, "-UseAccessibleHeader");
  335. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-2");
  336. // CaptionAlign and UseAccessibleHeader aren't removed
  337. Assert.AreEqual (2, bdl.StateBag.Count, "ViewState.Count-2");
  338. }
  339. [Test]
  340. public void TableCaption ()
  341. {
  342. TestBaseDataList bdl = new TestBaseDataList ();
  343. foreach (TableCaptionAlign tca in Enum.GetValues (typeof (TableCaptionAlign))) {
  344. bdl.CaptionAlign = tca;
  345. }
  346. }
  347. [Test]
  348. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  349. public void TableCaption_Int32MaxValue ()
  350. {
  351. TestBaseDataList bdl = new TestBaseDataList ();
  352. bdl.CaptionAlign = (TableCaptionAlign)Int32.MaxValue;
  353. }
  354. [Test]
  355. public void DataSource_IEnumerable ()
  356. {
  357. TestBaseDataList bdl = new TestBaseDataList ();
  358. bdl.DataSource = GetData (2);
  359. Assert.IsNotNull (bdl.DataSource, "DataSource");
  360. }
  361. [Test]
  362. public void DataSource_IListSource ()
  363. {
  364. TestBaseDataList bdl = new TestBaseDataList ();
  365. bdl.DataSource = GetDataSource (3);
  366. Assert.IsNotNull (bdl.DataSource, "DataSource");
  367. }
  368. [Test]
  369. [ExpectedException (typeof (ArgumentException))]
  370. public void DataSource_Other ()
  371. {
  372. TestBaseDataList bdl = new TestBaseDataList ();
  373. bdl.DataSource = new object ();
  374. }
  375. // CreateControlStyle isn't overriden so we can't assign it's properties
  376. [Test]
  377. [ExpectedException (typeof (InvalidCastException))]
  378. public void CellPadding_InvalidCastException ()
  379. {
  380. TestBaseDataList bdl = new TestBaseDataList ();
  381. Assert.AreEqual (-1, bdl.CellPadding, "CellPadding");
  382. bdl.CellPadding = -1;
  383. }
  384. [Test]
  385. [ExpectedException (typeof (InvalidCastException))]
  386. public void CellSpacing_InvalidCastException ()
  387. {
  388. TestBaseDataList bdl = new TestBaseDataList ();
  389. Assert.AreEqual (0, bdl.CellSpacing, "CellSpacing");
  390. bdl.CellSpacing = 0;
  391. }
  392. [Test]
  393. [ExpectedException (typeof (InvalidCastException))]
  394. public void GridLines_InvalidCastException ()
  395. {
  396. TestBaseDataList bdl = new TestBaseDataList ();
  397. Assert.AreEqual (GridLines.Both, bdl.GridLines, "GridLines");
  398. bdl.GridLines = GridLines.Both;
  399. }
  400. [Test]
  401. [ExpectedException (typeof (InvalidCastException))]
  402. public void HorizontalAlign_InvalidCastException ()
  403. {
  404. TestBaseDataList bdl = new TestBaseDataList ();
  405. Assert.AreEqual (HorizontalAlign.NotSet, bdl.HorizontalAlign, "HorizontalAlign");
  406. bdl.HorizontalAlign = HorizontalAlign.NotSet;
  407. }
  408. [Test]
  409. [ExpectedException (typeof (NullReferenceException))]
  410. public void IsBindableType_Null ()
  411. {
  412. BaseDataList.IsBindableType (null);
  413. }
  414. [Test]
  415. public void AddParsedSubObject ()
  416. {
  417. TestBaseDataList bdl = new TestBaseDataList ();
  418. bdl.Add (null);
  419. bdl.Add (new LiteralControl ("mono"));
  420. bdl.Add (new DataListItem (0, ListItemType.Item));
  421. bdl.Add (String.Empty);
  422. bdl.Add (new Control ());
  423. Assert.AreEqual (0, bdl.Controls.Count, "Controls");
  424. Assert.AreEqual (0, bdl.StateBag.Count, "StateBag");
  425. }
  426. [Test]
  427. public void Render_Empty ()
  428. {
  429. TestBaseDataList bdl = new TestBaseDataList ();
  430. Assert.AreEqual (String.Empty, bdl.Render ());
  431. }
  432. [Test]
  433. public void Render ()
  434. {
  435. TestBaseDataList bdl = new TestBaseDataList ();
  436. bdl.DataSource = GetDataSource (3);
  437. bdl.DataBind ();
  438. Assert.AreEqual (String.Empty, bdl.Render ());
  439. }
  440. private bool selectedIndexChangedEvent;
  441. private void SelectedIndexChangedHandler (object sender, EventArgs e)
  442. {
  443. selectedIndexChangedEvent = true;
  444. }
  445. [Test]
  446. public void Events ()
  447. {
  448. TestBaseDataList bdl = new TestBaseDataList ();
  449. selectedIndexChangedEvent = false;
  450. bdl.SelectedIndexChanged += new EventHandler (SelectedIndexChangedHandler);
  451. bdl.DoSelectedIndexChanged (new EventArgs ());
  452. Assert.IsTrue (selectedIndexChangedEvent, "selectedIndexChangedEvent");
  453. }
  454. [Test]
  455. public void OnDataBinding ()
  456. {
  457. // does DataBind calls base.OnDataBinding (like most sample do)
  458. // or does it call the overriden OnDataBinding (which seems logical)
  459. TestBaseDataList bdl = new TestBaseDataList ();
  460. Assert.IsFalse (bdl.DataBindingCalled, "Before DataBind");
  461. bdl.DataSource = GetDataSource (3);
  462. bdl.DataBind ();
  463. Assert.IsTrue (bdl.DataBindingCalled, "After DataBind");
  464. }
  465. #if NET_2_0
  466. [Test]
  467. public void DataSourceID ()
  468. {
  469. TestBaseDataList bdl = new TestBaseDataList ();
  470. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID-1");
  471. bdl.DataSourceID = "mono";
  472. Assert.IsTrue (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID-2");
  473. bdl.DataBind ();
  474. Assert.IsTrue (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID-3");
  475. }
  476. [Test]
  477. // LAMESPEC ??? [ExpectedException (typeof (HttpException))]
  478. [ExpectedException (typeof (InvalidOperationException))]
  479. public void DataSourceID_WithDataSource ()
  480. {
  481. Page p = new Page ();
  482. TestBaseDataList bdl = new TestBaseDataList ();
  483. bdl.Page = p;
  484. bdl.DataSource = GetDataSource (1);
  485. Test2DataSource ds = new Test2DataSource ();
  486. ds.ID = "mono";
  487. ds.Page = p;
  488. p.Controls.Add (ds);
  489. p.Controls.Add (bdl);
  490. bdl.DataSourceID = "mono";
  491. Assert.IsNotNull (bdl.Data (), "GetData");
  492. }
  493. [Test]
  494. [ExpectedException (typeof (HttpException))]
  495. [Ignore ("LAMESPEC -and/or- bad test")]
  496. public void DataSource_WithDataSourceID ()
  497. {
  498. Test2DataSource ds = new Test2DataSource ();
  499. ds.ID = "mono";
  500. TestBaseDataList bdl = new TestBaseDataList ();
  501. Page p = new Page ();
  502. bdl.Page = p;
  503. ds.Page = p;
  504. p.Controls.Add (ds);
  505. p.Controls.Add (bdl);
  506. bdl.DataSourceID = "mono";
  507. Assert.IsNotNull (bdl.Data (), "GetData");
  508. bdl.DataSource = GetDataSource (1);
  509. bdl.DataBind ();
  510. }
  511. [Test]
  512. public void EnsureDataBound_WithoutDataSourceID ()
  513. {
  514. TestBaseDataList bdl = new TestBaseDataList ();
  515. Assert.IsFalse (bdl.DataBindingCalled, "Before EnsureDataBound");
  516. bdl.Ensure ();
  517. Assert.IsFalse (bdl.DataBindingCalled, "After EnsureDataBound");
  518. }
  519. [Test]
  520. public void EnsureDataBound_WithDataSourceID ()
  521. {
  522. XmlDataSource ds = new XmlDataSource ();
  523. ds.Data = "";
  524. ds.ID = "mono";
  525. TestBaseDataList bdl = new TestBaseDataList ();
  526. Page p = new Page ();
  527. bdl.Page = p;
  528. p.Controls.Add (ds);
  529. p.Controls.Add (bdl);
  530. bdl.DataSourceID = "mono";
  531. Assert.IsFalse (bdl.DataBindingCalled, "Before EnsureDataBound");
  532. bdl.Ensure ();
  533. Assert.IsFalse (bdl.DataBindingCalled, "After EnsureDataBound");
  534. bdl.BaseOnLoad (EventArgs.Empty);
  535. bdl.Ensure ();
  536. Assert.IsTrue (bdl.DataBindingCalled, "After BaseOnLoad|RequiresDataBinding");
  537. }
  538. [Test]
  539. public void GetData ()
  540. {
  541. Test2DataSource ds = new Test2DataSource ();
  542. ds.ID = "mono";
  543. TestBaseDataList bdl = new TestBaseDataList ();
  544. Page p = new Page ();
  545. bdl.Page = p;
  546. ds.Page = p;
  547. p.Controls.Add (ds);
  548. p.Controls.Add (bdl);
  549. bdl.DataSourceID = "mono";
  550. Assert.IsNotNull (bdl.Data (), "GetData");
  551. }
  552. [Test]
  553. public void GetData_WithoutDataSourceID ()
  554. {
  555. TestBaseDataList bdl = new TestBaseDataList ();
  556. Assert.IsNull (bdl.Data (), "GetData");
  557. }
  558. [Test]
  559. [ExpectedException (typeof (HttpException))]
  560. public void GetData_WithUnexistingDataSourceID ()
  561. {
  562. TestBaseDataList bdl = new TestBaseDataList ();
  563. bdl.Page = new Page ();
  564. bdl.DataSourceID = "mono";
  565. bdl.Data ();
  566. }
  567. [Test]
  568. public void OnDataBinding_True ()
  569. {
  570. TestBaseDataList bdl = new TestBaseDataList ();
  571. Assert.IsFalse (bdl.DataBindingCalled, "Before DataBind");
  572. bdl.DataSource = GetDataSource (3);
  573. bdl.DataBindBool (true);
  574. Assert.IsTrue (bdl.DataBindingCalled, "After DataBind");
  575. }
  576. [Test]
  577. public void OnDataBinding_False ()
  578. {
  579. TestBaseDataList bdl = new TestBaseDataList ();
  580. Assert.IsFalse (bdl.DataBindingCalled, "Before DataBind");
  581. bdl.DataSource = GetDataSource (3);
  582. bdl.DataBindBool (false);
  583. Assert.IsFalse (bdl.DataBindingCalled, "After DataBind");
  584. }
  585. [Test]
  586. public void OnDataPropertyChanged ()
  587. {
  588. TestBaseDataList bdl = new TestBaseDataList ();
  589. bdl.DataPropertyChangedCalled = false;
  590. bdl.DataMember = String.Empty;
  591. Assert.IsTrue (bdl.DataPropertyChangedCalled, "OnDataPropertyChanged-DataMember");
  592. Assert.IsFalse (bdl.IsInitialized, "Initialized-DataMember");
  593. bdl.DataPropertyChangedCalled = false;
  594. bdl.DataSource = null;
  595. Assert.IsTrue (bdl.DataPropertyChangedCalled, "OnDataPropertyChanged-DataSource");
  596. Assert.IsFalse (bdl.IsInitialized, "Initialized-DataSource");
  597. bdl.DataPropertyChangedCalled = false;
  598. bdl.DataSourceID = String.Empty;
  599. Assert.IsTrue (bdl.DataPropertyChangedCalled, "OnDataPropertyChanged-DataSourceID");
  600. Assert.IsFalse (bdl.IsInitialized, "Initialized-DataSourceID");
  601. }
  602. [Test]
  603. public void OnInit ()
  604. {
  605. TestBaseDataList bdl = new TestBaseDataList ();
  606. Assert.IsFalse (bdl.IsInitialized, "Initialized-1");
  607. bdl.BaseOnInit (EventArgs.Empty);
  608. Assert.IsFalse (bdl.IsInitialized, "Initialized-2");
  609. // OnInit doesn't set Initialized to true
  610. }
  611. [Test]
  612. public void OnDataSourceViewChanged ()
  613. {
  614. TestBaseDataList bdl = new TestBaseDataList ();
  615. Assert.IsFalse (bdl.RequiresDataBind, "RequiresDataBind-1");
  616. bdl.BaseOnDataSourceViewChanged (this, EventArgs.Empty);
  617. Assert.IsTrue (bdl.RequiresDataBind, "RequiresDataBind-2");
  618. }
  619. [Test]
  620. public void OnLoad_WithoutPage ()
  621. {
  622. TestBaseDataList bdl = new TestBaseDataList ();
  623. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID");
  624. Assert.IsTrue (bdl.EnableViewState, "EnabledViewState");
  625. Assert.IsNull (bdl.Page, "Page");
  626. bdl.BaseOnLoad (EventArgs.Empty);
  627. Assert.IsTrue (bdl.IsInitialized, "IsInitialized");
  628. Assert.IsFalse (bdl.RequiresDataBind, "RequiresDataBind");
  629. }
  630. [Test]
  631. public void OnLoad_WithoutPageWithoutViewState ()
  632. {
  633. TestBaseDataList bdl = new TestBaseDataList ();
  634. bdl.EnableViewState = false;
  635. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID");
  636. Assert.IsFalse (bdl.EnableViewState, "EnabledViewState");
  637. Assert.IsNull (bdl.Page, "Page");
  638. bdl.BaseOnLoad (EventArgs.Empty);
  639. Assert.IsTrue (bdl.IsInitialized, "IsInitialized");
  640. Assert.IsFalse (bdl.RequiresDataBind, "RequiresDataBind");
  641. }
  642. [Test]
  643. public void OnLoad_WithPage ()
  644. {
  645. TestBaseDataList bdl = new TestBaseDataList ();
  646. Page p = new Page ();
  647. bdl.Page = p;
  648. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID-2");
  649. Assert.IsTrue (bdl.EnableViewState, "EnabledViewState-2");
  650. Assert.IsFalse (bdl.Page.IsPostBack, "IsPostBack-2");
  651. bdl.BaseOnLoad (EventArgs.Empty);
  652. Assert.IsTrue (bdl.IsInitialized, "IsInitialized-2");
  653. Assert.IsTrue (bdl.RequiresDataBind, "RequiresDataBind-2");
  654. }
  655. [Test]
  656. public void OnLoad_WithPageWithoutViewState ()
  657. {
  658. TestBaseDataList bdl = new TestBaseDataList ();
  659. Page p = new Page ();
  660. bdl.Page = p;
  661. bdl.EnableViewState = false;
  662. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID");
  663. Assert.IsFalse (bdl.EnableViewState, "EnabledViewState");
  664. Assert.IsFalse (bdl.Page.IsPostBack, "IsPostBack");
  665. bdl.BaseOnLoad (EventArgs.Empty);
  666. Assert.IsTrue (bdl.IsInitialized, "IsInitialized");
  667. Assert.IsTrue (bdl.RequiresDataBind, "RequiresDataBind");
  668. }
  669. [Test]
  670. public void OnLoad_WithDataSource ()
  671. {
  672. XmlDataSource ds = new XmlDataSource ();
  673. ds.ID = "mono";
  674. TestBaseDataList bdl = new TestBaseDataList ();
  675. Page p = new Page ();
  676. bdl.Page = p;
  677. p.Controls.Add (ds);
  678. p.Controls.Add (bdl);
  679. bdl.DataSourceID = "mono";
  680. Assert.IsTrue (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID");
  681. Assert.IsTrue (bdl.EnableViewState, "EnabledViewState");
  682. Assert.IsFalse (bdl.Page.IsPostBack, "IsPostBack");
  683. bdl.BaseOnLoad (EventArgs.Empty);
  684. Assert.IsTrue (bdl.IsInitialized, "IsInitialized");
  685. Assert.IsTrue (bdl.RequiresDataBind, "RequiresDataBind");
  686. }
  687. #endif
  688. [Test]
  689. public void IsBindableType ()
  690. {
  691. // documented
  692. Assert.IsTrue (BaseDataList.IsBindableType (typeof (bool)), "bool");
  693. Assert.IsTrue (BaseDataList.IsBindableType (typeof (byte)), "byte");
  694. Assert.IsTrue (BaseDataList.IsBindableType (typeof (sbyte)), "sbyte");
  695. Assert.IsTrue (BaseDataList.IsBindableType (typeof (short)), "short");
  696. Assert.IsTrue (BaseDataList.IsBindableType (typeof (ushort)), "ushort");
  697. Assert.IsTrue (BaseDataList.IsBindableType (typeof (int)), "int");
  698. Assert.IsTrue (BaseDataList.IsBindableType (typeof (uint)), "uint");
  699. Assert.IsTrue (BaseDataList.IsBindableType (typeof (long)), "long");
  700. Assert.IsTrue (BaseDataList.IsBindableType (typeof (ulong)), "ulong");
  701. Assert.IsTrue (BaseDataList.IsBindableType (typeof (char)), "char");
  702. Assert.IsTrue (BaseDataList.IsBindableType (typeof (double)), "double");
  703. Assert.IsTrue (BaseDataList.IsBindableType (typeof (float)), "float");
  704. Assert.IsTrue (BaseDataList.IsBindableType (typeof (DateTime)), "DateTime");
  705. Assert.IsTrue (BaseDataList.IsBindableType (typeof (decimal)), "decimal");
  706. Assert.IsTrue (BaseDataList.IsBindableType (typeof (string)), "string");
  707. // and others (from TypeCode)
  708. Assert.IsFalse (BaseDataList.IsBindableType (typeof (object)), "object");
  709. Assert.IsFalse (BaseDataList.IsBindableType (typeof (DBNull)), "DBNull");
  710. // and junk
  711. Assert.IsFalse (BaseDataList.IsBindableType (this.GetType ()), "this");
  712. }
  713. }
  714. }