BaseDataListTest.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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
  271. Assert.AreEqual (1, bdl.StateBag.Count, "ViewState.Count-3");
  272. }
  273. [Test]
  274. public void NullProperties ()
  275. {
  276. TestBaseDataList bdl = new TestBaseDataList ();
  277. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-1");
  278. Assert.AreEqual (0, bdl.StateBag.Count, "ViewState.Count-1");
  279. bdl.Caption = null;
  280. Assert.AreEqual (String.Empty, bdl.Caption, "Caption");
  281. bdl.CaptionAlign = TableCaptionAlign.NotSet;
  282. Assert.AreEqual (TableCaptionAlign.NotSet, bdl.CaptionAlign, "CaptionAlign");
  283. Assert.AreEqual (1, bdl.StateBag.Count, "ViewState.Count-2");
  284. // many properties can't be set without causing a InvalidCastException
  285. bdl.DataKeyField = null;
  286. Assert.AreEqual (String.Empty, bdl.DataKeyField, "DataKeyField");
  287. bdl.DataMember = null;
  288. Assert.AreEqual (String.Empty, bdl.DataMember, "DataMember");
  289. bdl.DataSource = null;
  290. Assert.IsNull (bdl.DataSource, "DataSource");
  291. bdl.UseAccessibleHeader = false;
  292. Assert.IsFalse (bdl.UseAccessibleHeader, "UseAccessibleHeader");
  293. #if NET_2_0
  294. bdl.DataSourceID = String.Empty;
  295. Assert.AreEqual (String.Empty, bdl.DataSourceID, "DataSourceID");
  296. Assert.AreEqual (3, bdl.StateBag.Count, "ViewState.Count-3");
  297. #else
  298. Assert.AreEqual (2, bdl.StateBag.Count, "ViewState.Count-3");
  299. #endif
  300. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-2");
  301. }
  302. [Test]
  303. public void CleanProperties ()
  304. {
  305. TestBaseDataList bdl = new TestBaseDataList ();
  306. bdl.Caption = "Mono";
  307. Assert.AreEqual ("Mono", bdl.Caption, "Caption");
  308. bdl.CaptionAlign = TableCaptionAlign.Top;
  309. Assert.AreEqual (TableCaptionAlign.Top, bdl.CaptionAlign, "CaptionAlign");
  310. // many properties can't be set without causing a InvalidCastException
  311. bdl.DataKeyField = "key";
  312. Assert.AreEqual ("key", bdl.DataKeyField, "DataKeyField");
  313. bdl.DataMember = "member";
  314. Assert.AreEqual ("member", bdl.DataMember, "DataMember");
  315. bdl.DataSource = GetData (2);
  316. Assert.IsNotNull (bdl.DataSource, "DataSource");
  317. bdl.UseAccessibleHeader = true;
  318. Assert.IsTrue (bdl.UseAccessibleHeader, "UseAccessibleHeader");
  319. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-1");
  320. Assert.AreEqual (5, bdl.StateBag.Count, "ViewState.Count-1");
  321. bdl.Caption = null;
  322. Assert.AreEqual (String.Empty, bdl.Caption, "-Caption");
  323. bdl.CaptionAlign = TableCaptionAlign.NotSet;
  324. Assert.AreEqual (TableCaptionAlign.NotSet, bdl.CaptionAlign, "-CaptionAlign");
  325. // many properties can't be set without causing a InvalidCastException
  326. bdl.DataKeyField = null;
  327. Assert.AreEqual (String.Empty, bdl.DataKeyField, "-DataKeyField");
  328. bdl.DataMember = null;
  329. Assert.AreEqual (String.Empty, bdl.DataMember, "-DataMember");
  330. bdl.DataSource = null;
  331. Assert.IsNull (bdl.DataSource, "-DataSource");
  332. bdl.UseAccessibleHeader = false;
  333. Assert.IsFalse (bdl.UseAccessibleHeader, "-UseAccessibleHeader");
  334. Assert.AreEqual (0, bdl.Attributes.Count, "Attributes.Count-2");
  335. // CaptionAlign and UseAccessibleHeader aren't removed
  336. Assert.AreEqual (2, bdl.StateBag.Count, "ViewState.Count-2");
  337. }
  338. [Test]
  339. public void TableCaption ()
  340. {
  341. TestBaseDataList bdl = new TestBaseDataList ();
  342. foreach (TableCaptionAlign tca in Enum.GetValues (typeof (TableCaptionAlign))) {
  343. bdl.CaptionAlign = tca;
  344. }
  345. }
  346. [Test]
  347. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  348. public void TableCaption_Int32MaxValue ()
  349. {
  350. TestBaseDataList bdl = new TestBaseDataList ();
  351. bdl.CaptionAlign = (TableCaptionAlign)Int32.MaxValue;
  352. }
  353. [Test]
  354. public void DataSource_IEnumerable ()
  355. {
  356. TestBaseDataList bdl = new TestBaseDataList ();
  357. bdl.DataSource = GetData (2);
  358. Assert.IsNotNull (bdl.DataSource, "DataSource");
  359. }
  360. [Test]
  361. public void DataSource_IListSource ()
  362. {
  363. TestBaseDataList bdl = new TestBaseDataList ();
  364. bdl.DataSource = GetDataSource (3);
  365. Assert.IsNotNull (bdl.DataSource, "DataSource");
  366. }
  367. [Test]
  368. [ExpectedException (typeof (ArgumentException))]
  369. public void DataSource_Other ()
  370. {
  371. TestBaseDataList bdl = new TestBaseDataList ();
  372. bdl.DataSource = new object ();
  373. }
  374. // CreateControlStyle isn't overriden so we can't assign it's properties
  375. [Test]
  376. [ExpectedException (typeof (InvalidCastException))]
  377. public void CellPadding_InvalidCastException ()
  378. {
  379. TestBaseDataList bdl = new TestBaseDataList ();
  380. Assert.AreEqual (-1, bdl.CellPadding, "CellPadding");
  381. bdl.CellPadding = -1;
  382. }
  383. [Test]
  384. [ExpectedException (typeof (InvalidCastException))]
  385. public void CellSpacing_InvalidCastException ()
  386. {
  387. TestBaseDataList bdl = new TestBaseDataList ();
  388. Assert.AreEqual (0, bdl.CellSpacing, "CellSpacing");
  389. bdl.CellSpacing = 0;
  390. }
  391. [Test]
  392. [ExpectedException (typeof (InvalidCastException))]
  393. public void GridLines_InvalidCastException ()
  394. {
  395. TestBaseDataList bdl = new TestBaseDataList ();
  396. Assert.AreEqual (GridLines.Both, bdl.GridLines, "GridLines");
  397. bdl.GridLines = GridLines.Both;
  398. }
  399. [Test]
  400. [ExpectedException (typeof (InvalidCastException))]
  401. public void HorizontalAlign_InvalidCastException ()
  402. {
  403. TestBaseDataList bdl = new TestBaseDataList ();
  404. Assert.AreEqual (HorizontalAlign.NotSet, bdl.HorizontalAlign, "HorizontalAlign");
  405. bdl.HorizontalAlign = HorizontalAlign.NotSet;
  406. }
  407. [Test]
  408. [ExpectedException (typeof (NullReferenceException))]
  409. public void IsBindableType_Null ()
  410. {
  411. BaseDataList.IsBindableType (null);
  412. }
  413. [Test]
  414. public void AddParsedSubObject ()
  415. {
  416. TestBaseDataList bdl = new TestBaseDataList ();
  417. bdl.Add (null);
  418. bdl.Add (new LiteralControl ("mono"));
  419. bdl.Add (new DataListItem (0, ListItemType.Item));
  420. bdl.Add (String.Empty);
  421. bdl.Add (new Control ());
  422. Assert.AreEqual (0, bdl.Controls.Count, "Controls");
  423. Assert.AreEqual (0, bdl.StateBag.Count, "StateBag");
  424. }
  425. [Test]
  426. public void Render_Empty ()
  427. {
  428. TestBaseDataList bdl = new TestBaseDataList ();
  429. Assert.AreEqual (String.Empty, bdl.Render ());
  430. }
  431. [Test]
  432. public void Render ()
  433. {
  434. TestBaseDataList bdl = new TestBaseDataList ();
  435. bdl.DataSource = GetDataSource (3);
  436. bdl.DataBind ();
  437. Assert.AreEqual (String.Empty, bdl.Render ());
  438. }
  439. private bool selectedIndexChangedEvent;
  440. private void SelectedIndexChangedHandler (object sender, EventArgs e)
  441. {
  442. selectedIndexChangedEvent = true;
  443. }
  444. [Test]
  445. public void Events ()
  446. {
  447. TestBaseDataList bdl = new TestBaseDataList ();
  448. selectedIndexChangedEvent = false;
  449. bdl.SelectedIndexChanged += new EventHandler (SelectedIndexChangedHandler);
  450. bdl.DoSelectedIndexChanged (new EventArgs ());
  451. Assert.IsTrue (selectedIndexChangedEvent, "selectedIndexChangedEvent");
  452. }
  453. [Test]
  454. public void OnDataBinding ()
  455. {
  456. // does DataBind calls base.OnDataBinding (like most sample do)
  457. // or does it call the overriden OnDataBinding (which seems logical)
  458. TestBaseDataList bdl = new TestBaseDataList ();
  459. Assert.IsFalse (bdl.DataBindingCalled, "Before DataBind");
  460. bdl.DataSource = GetDataSource (3);
  461. bdl.DataBind ();
  462. Assert.IsTrue (bdl.DataBindingCalled, "After DataBind");
  463. }
  464. #if NET_2_0
  465. [Test]
  466. public void DataSourceID ()
  467. {
  468. TestBaseDataList bdl = new TestBaseDataList ();
  469. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID-1");
  470. bdl.DataSourceID = "mono";
  471. Assert.IsTrue (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID-2");
  472. bdl.DataBind ();
  473. Assert.IsTrue (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID-3");
  474. }
  475. [Test]
  476. // LAMESPEC ??? [ExpectedException (typeof (HttpException))]
  477. [ExpectedException (typeof (InvalidOperationException))]
  478. public void DataSourceID_WithDataSource ()
  479. {
  480. Page p = new Page ();
  481. TestBaseDataList bdl = new TestBaseDataList ();
  482. bdl.Page = p;
  483. bdl.DataSource = GetDataSource (1);
  484. Test2DataSource ds = new Test2DataSource ();
  485. ds.ID = "mono";
  486. ds.Page = p;
  487. p.Controls.Add (ds);
  488. p.Controls.Add (bdl);
  489. bdl.DataSourceID = "mono";
  490. Assert.IsNotNull (bdl.Data (), "GetData");
  491. }
  492. [Test]
  493. [ExpectedException (typeof (HttpException))]
  494. [Ignore ("LAMESPEC -and/or- bad test")]
  495. public void DataSource_WithDataSourceID ()
  496. {
  497. Test2DataSource ds = new Test2DataSource ();
  498. ds.ID = "mono";
  499. TestBaseDataList bdl = new TestBaseDataList ();
  500. Page p = new Page ();
  501. bdl.Page = p;
  502. ds.Page = p;
  503. p.Controls.Add (ds);
  504. p.Controls.Add (bdl);
  505. bdl.DataSourceID = "mono";
  506. Assert.IsNotNull (bdl.Data (), "GetData");
  507. bdl.DataSource = GetDataSource (1);
  508. bdl.DataBind ();
  509. }
  510. [Test]
  511. public void EnsureDataBound_WithoutDataSourceID ()
  512. {
  513. TestBaseDataList bdl = new TestBaseDataList ();
  514. Assert.IsFalse (bdl.DataBindingCalled, "Before EnsureDataBound");
  515. bdl.Ensure ();
  516. Assert.IsFalse (bdl.DataBindingCalled, "After EnsureDataBound");
  517. }
  518. [Test]
  519. public void EnsureDataBound_WithDataSourceID ()
  520. {
  521. XmlDataSource ds = new XmlDataSource ();
  522. ds.Data = "";
  523. ds.ID = "mono";
  524. TestBaseDataList bdl = new TestBaseDataList ();
  525. Page p = new Page ();
  526. bdl.Page = p;
  527. p.Controls.Add (ds);
  528. p.Controls.Add (bdl);
  529. bdl.DataSourceID = "mono";
  530. Assert.IsFalse (bdl.DataBindingCalled, "Before EnsureDataBound");
  531. bdl.Ensure ();
  532. Assert.IsFalse (bdl.DataBindingCalled, "After EnsureDataBound");
  533. bdl.BaseOnLoad (EventArgs.Empty);
  534. bdl.Ensure ();
  535. Assert.IsTrue (bdl.DataBindingCalled, "After BaseOnLoad|RequiresDataBinding");
  536. }
  537. [Test]
  538. public void GetData ()
  539. {
  540. Test2DataSource ds = new Test2DataSource ();
  541. ds.ID = "mono";
  542. TestBaseDataList bdl = new TestBaseDataList ();
  543. Page p = new Page ();
  544. bdl.Page = p;
  545. ds.Page = p;
  546. p.Controls.Add (ds);
  547. p.Controls.Add (bdl);
  548. bdl.DataSourceID = "mono";
  549. Assert.IsNotNull (bdl.Data (), "GetData");
  550. }
  551. [Test]
  552. public void GetData_WithoutDataSourceID ()
  553. {
  554. TestBaseDataList bdl = new TestBaseDataList ();
  555. Assert.IsNull (bdl.Data (), "GetData");
  556. }
  557. [Test]
  558. [ExpectedException (typeof (HttpException))]
  559. public void GetData_WithUnexistingDataSourceID ()
  560. {
  561. TestBaseDataList bdl = new TestBaseDataList ();
  562. bdl.Page = new Page ();
  563. bdl.DataSourceID = "mono";
  564. bdl.Data ();
  565. }
  566. [Test]
  567. public void OnDataBinding_True ()
  568. {
  569. TestBaseDataList bdl = new TestBaseDataList ();
  570. Assert.IsFalse (bdl.DataBindingCalled, "Before DataBind");
  571. bdl.DataSource = GetDataSource (3);
  572. bdl.DataBindBool (true);
  573. Assert.IsTrue (bdl.DataBindingCalled, "After DataBind");
  574. }
  575. [Test]
  576. public void OnDataBinding_False ()
  577. {
  578. TestBaseDataList bdl = new TestBaseDataList ();
  579. Assert.IsFalse (bdl.DataBindingCalled, "Before DataBind");
  580. bdl.DataSource = GetDataSource (3);
  581. bdl.DataBindBool (false);
  582. Assert.IsFalse (bdl.DataBindingCalled, "After DataBind");
  583. }
  584. [Test]
  585. public void OnDataPropertyChanged ()
  586. {
  587. TestBaseDataList bdl = new TestBaseDataList ();
  588. bdl.DataPropertyChangedCalled = false;
  589. bdl.DataMember = String.Empty;
  590. Assert.IsTrue (bdl.DataPropertyChangedCalled, "OnDataPropertyChanged-DataMember");
  591. Assert.IsFalse (bdl.IsInitialized, "Initialized-DataMember");
  592. bdl.DataPropertyChangedCalled = false;
  593. bdl.DataSource = null;
  594. Assert.IsTrue (bdl.DataPropertyChangedCalled, "OnDataPropertyChanged-DataSource");
  595. Assert.IsFalse (bdl.IsInitialized, "Initialized-DataSource");
  596. bdl.DataPropertyChangedCalled = false;
  597. bdl.DataSourceID = String.Empty;
  598. Assert.IsTrue (bdl.DataPropertyChangedCalled, "OnDataPropertyChanged-DataSourceID");
  599. Assert.IsFalse (bdl.IsInitialized, "Initialized-DataSourceID");
  600. }
  601. [Test]
  602. public void OnInit ()
  603. {
  604. TestBaseDataList bdl = new TestBaseDataList ();
  605. Assert.IsFalse (bdl.IsInitialized, "Initialized-1");
  606. bdl.BaseOnInit (EventArgs.Empty);
  607. Assert.IsFalse (bdl.IsInitialized, "Initialized-2");
  608. // OnInit doesn't set Initialized to true
  609. }
  610. [Test]
  611. public void OnDataSourceViewChanged ()
  612. {
  613. TestBaseDataList bdl = new TestBaseDataList ();
  614. Assert.IsFalse (bdl.RequiresDataBind, "RequiresDataBind-1");
  615. bdl.BaseOnDataSourceViewChanged (this, EventArgs.Empty);
  616. Assert.IsTrue (bdl.RequiresDataBind, "RequiresDataBind-2");
  617. }
  618. [Test]
  619. public void OnLoad_WithoutPage ()
  620. {
  621. TestBaseDataList bdl = new TestBaseDataList ();
  622. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID");
  623. Assert.IsTrue (bdl.EnableViewState, "EnabledViewState");
  624. Assert.IsNull (bdl.Page, "Page");
  625. bdl.BaseOnLoad (EventArgs.Empty);
  626. Assert.IsTrue (bdl.IsInitialized, "IsInitialized");
  627. Assert.IsFalse (bdl.RequiresDataBind, "RequiresDataBind");
  628. }
  629. [Test]
  630. public void OnLoad_WithoutPageWithoutViewState ()
  631. {
  632. TestBaseDataList bdl = new TestBaseDataList ();
  633. bdl.EnableViewState = false;
  634. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID");
  635. Assert.IsFalse (bdl.EnableViewState, "EnabledViewState");
  636. Assert.IsNull (bdl.Page, "Page");
  637. bdl.BaseOnLoad (EventArgs.Empty);
  638. Assert.IsTrue (bdl.IsInitialized, "IsInitialized");
  639. Assert.IsFalse (bdl.RequiresDataBind, "RequiresDataBind");
  640. }
  641. [Test]
  642. public void OnLoad_WithPage ()
  643. {
  644. TestBaseDataList bdl = new TestBaseDataList ();
  645. Page p = new Page ();
  646. bdl.Page = p;
  647. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID-2");
  648. Assert.IsTrue (bdl.EnableViewState, "EnabledViewState-2");
  649. Assert.IsFalse (bdl.Page.IsPostBack, "IsPostBack-2");
  650. bdl.BaseOnLoad (EventArgs.Empty);
  651. Assert.IsTrue (bdl.IsInitialized, "IsInitialized-2");
  652. Assert.IsTrue (bdl.RequiresDataBind, "RequiresDataBind-2");
  653. }
  654. [Test]
  655. public void OnLoad_WithPageWithoutViewState ()
  656. {
  657. TestBaseDataList bdl = new TestBaseDataList ();
  658. Page p = new Page ();
  659. bdl.Page = p;
  660. bdl.EnableViewState = false;
  661. Assert.IsFalse (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID");
  662. Assert.IsFalse (bdl.EnableViewState, "EnabledViewState");
  663. Assert.IsFalse (bdl.Page.IsPostBack, "IsPostBack");
  664. bdl.BaseOnLoad (EventArgs.Empty);
  665. Assert.IsTrue (bdl.IsInitialized, "IsInitialized");
  666. Assert.IsTrue (bdl.RequiresDataBind, "RequiresDataBind");
  667. }
  668. [Test]
  669. public void OnLoad_WithDataSource ()
  670. {
  671. XmlDataSource ds = new XmlDataSource ();
  672. ds.ID = "mono";
  673. TestBaseDataList bdl = new TestBaseDataList ();
  674. Page p = new Page ();
  675. bdl.Page = p;
  676. p.Controls.Add (ds);
  677. p.Controls.Add (bdl);
  678. bdl.DataSourceID = "mono";
  679. Assert.IsTrue (bdl.IsDataBoundByDataSourceId, "IsBoundUsingDataSourceID");
  680. Assert.IsTrue (bdl.EnableViewState, "EnabledViewState");
  681. Assert.IsFalse (bdl.Page.IsPostBack, "IsPostBack");
  682. bdl.BaseOnLoad (EventArgs.Empty);
  683. Assert.IsTrue (bdl.IsInitialized, "IsInitialized");
  684. Assert.IsTrue (bdl.RequiresDataBind, "RequiresDataBind");
  685. }
  686. #endif
  687. [Test]
  688. public void IsBindableType ()
  689. {
  690. // documented
  691. Assert.IsTrue (BaseDataList.IsBindableType (typeof (bool)), "bool");
  692. Assert.IsTrue (BaseDataList.IsBindableType (typeof (byte)), "byte");
  693. Assert.IsTrue (BaseDataList.IsBindableType (typeof (sbyte)), "sbyte");
  694. Assert.IsTrue (BaseDataList.IsBindableType (typeof (short)), "short");
  695. Assert.IsTrue (BaseDataList.IsBindableType (typeof (ushort)), "ushort");
  696. Assert.IsTrue (BaseDataList.IsBindableType (typeof (int)), "int");
  697. Assert.IsTrue (BaseDataList.IsBindableType (typeof (uint)), "uint");
  698. Assert.IsTrue (BaseDataList.IsBindableType (typeof (long)), "long");
  699. Assert.IsTrue (BaseDataList.IsBindableType (typeof (ulong)), "ulong");
  700. Assert.IsTrue (BaseDataList.IsBindableType (typeof (char)), "char");
  701. Assert.IsTrue (BaseDataList.IsBindableType (typeof (double)), "double");
  702. Assert.IsTrue (BaseDataList.IsBindableType (typeof (float)), "float");
  703. Assert.IsTrue (BaseDataList.IsBindableType (typeof (DateTime)), "DateTime");
  704. Assert.IsTrue (BaseDataList.IsBindableType (typeof (decimal)), "decimal");
  705. Assert.IsTrue (BaseDataList.IsBindableType (typeof (string)), "string");
  706. // and others (from TypeCode)
  707. Assert.IsFalse (BaseDataList.IsBindableType (typeof (object)), "object");
  708. Assert.IsFalse (BaseDataList.IsBindableType (typeof (DBNull)), "DBNull");
  709. // and junk
  710. Assert.IsFalse (BaseDataList.IsBindableType (this.GetType ()), "this");
  711. }
  712. }
  713. }