BaseDataListTest.cs 24 KB

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