PageRenderTime 189ms CodeModel.GetById 37ms RepoModel.GetById 5ms app.codeStats 1ms

/mcs/class/System.Web/Test/System.Web.UI.WebControls/DataGridTest.cs

https://bitbucket.org/danipen/mono
C# | 2320 lines | 1814 code | 374 blank | 132 comment | 21 complexity | 1ad6083b23bd500497e5bb4e903e012b MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. //
  2. // Tests for System.Web.UI.WebControls.DataGrid.cs
  3. //
  4. // Author:
  5. // Jackson Harper (jackson@ximian.com)
  6. // Gonzalo Paniagua Javier (gonzalo@ximian.com)
  7. //
  8. //
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using NUnit.Framework;
  31. using AttributeCollection = System.ComponentModel.AttributeCollection;
  32. using System;
  33. using System.Drawing;
  34. using System.IO;
  35. using System.Globalization;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. using System.Collections;
  40. using System.Data;
  41. using System.ComponentModel;
  42. using System.Diagnostics;
  43. #if NET_2_0
  44. using System.Collections.Generic;
  45. using MonoTests.SystemWeb.Framework;
  46. using MonoTests.stand_alone.WebHarness;
  47. #endif
  48. namespace MonoTests.System.Web.UI.WebControls {
  49. public class DataGridPoker : DataGrid {
  50. public DataGridPoker ()
  51. {
  52. TrackViewState ();
  53. }
  54. public string GetTagName ()
  55. {
  56. return TagName;
  57. }
  58. public void PrepareCH ()
  59. {
  60. PrepareControlHierarchy ();
  61. }
  62. public string Render ()
  63. {
  64. StringWriter sw = new StringWriter ();
  65. HtmlTextWriter tw = new HtmlTextWriter (sw);
  66. Render (tw);
  67. return sw.ToString ();
  68. }
  69. public StateBag GetViewState ()
  70. {
  71. return ViewState;
  72. }
  73. public new Style ControlStyle ()
  74. {
  75. return CreateControlStyle ();
  76. }
  77. public void DoCancelCommand (DataGridCommandEventArgs e)
  78. {
  79. OnCancelCommand (e);
  80. }
  81. public void DoDeleteCommand (DataGridCommandEventArgs e)
  82. {
  83. OnDeleteCommand (e);
  84. }
  85. public void DoEditCommand (DataGridCommandEventArgs e)
  86. {
  87. OnEditCommand (e);
  88. }
  89. public void DoItemCommand (DataGridCommandEventArgs e)
  90. {
  91. OnItemCommand (e);
  92. }
  93. public void DoUpdateCommand (DataGridCommandEventArgs e)
  94. {
  95. OnUpdateCommand (e);
  96. }
  97. public void DoItemCreated (DataGridItemEventArgs e)
  98. {
  99. OnItemCreated (e);
  100. }
  101. public void DoItemDataBound (DataGridItemEventArgs e)
  102. {
  103. OnItemDataBound (e);
  104. }
  105. public void DoPageIndexChanged (DataGridPageChangedEventArgs e)
  106. {
  107. OnPageIndexChanged (e);
  108. }
  109. public void DoSortCommand (DataGridSortCommandEventArgs e)
  110. {
  111. OnSortCommand (e);
  112. }
  113. public void DoBubbleEvent (object source, EventArgs e)
  114. {
  115. OnBubbleEvent (source, e);
  116. }
  117. public void TrackState ()
  118. {
  119. TrackViewState ();
  120. }
  121. public object SaveState ()
  122. {
  123. return SaveViewState ();
  124. }
  125. public ArrayList CreateColumns (PagedDataSource data_source, bool use_data_source)
  126. {
  127. return CreateColumnSet (data_source, use_data_source);
  128. }
  129. public void CreateControls (bool use_data_source)
  130. {
  131. CreateControlHierarchy (use_data_source);
  132. }
  133. public void InitPager (DataGridItem item, int columnSpan,
  134. PagedDataSource pagedDataSource)
  135. {
  136. InitializePager (item, columnSpan, pagedDataSource);
  137. }
  138. }
  139. public class AmazingEnumerable : IEnumerable {
  140. private IList list;
  141. public int CallCount;
  142. public AmazingEnumerable (IList list)
  143. {
  144. this.list = list;
  145. }
  146. public IEnumerator GetEnumerator ()
  147. {
  148. CallCount++;
  149. return list.GetEnumerator ();
  150. }
  151. }
  152. [TestFixture]
  153. public class DataGridTest {
  154. #if NET_2_0
  155. [TestFixtureSetUp()]
  156. public void FixtureSetup ()
  157. {
  158. WebTest.CopyResource (GetType (), "DataGrid.aspx", "DataGrid.aspx");
  159. }
  160. [TestFixtureTearDown()]
  161. public void FixtureTearDown ()
  162. {
  163. WebTest.Unload ();
  164. }
  165. #endif
  166. [Test]
  167. public void Defaults ()
  168. {
  169. DataGridPoker p = new DataGridPoker ();
  170. Assert.AreEqual (DataGrid.CancelCommandName, "Cancel", "A1");
  171. Assert.AreEqual (DataGrid.DeleteCommandName, "Delete", "A2");
  172. Assert.AreEqual (DataGrid.EditCommandName, "Edit", "A3");
  173. Assert.AreEqual (DataGrid.NextPageCommandArgument, "Next", "A4");
  174. Assert.AreEqual (DataGrid.PageCommandName, "Page", "A5");
  175. Assert.AreEqual (DataGrid.PrevPageCommandArgument, "Prev", "A6");
  176. Assert.AreEqual (DataGrid.SelectCommandName, "Select", "A7");
  177. Assert.AreEqual (DataGrid.SortCommandName, "Sort", "A8");
  178. Assert.AreEqual (DataGrid.UpdateCommandName, "Update", "A9");
  179. Assert.AreEqual (p.AllowCustomPaging, false, "A10");
  180. Assert.AreEqual (p.AllowPaging, false, "A11");
  181. Assert.AreEqual (p.AllowSorting, false, "A12");
  182. Assert.AreEqual (p.AutoGenerateColumns, true, "A13");
  183. Assert.AreEqual (p.BackImageUrl, String.Empty, "A14");
  184. Assert.AreEqual (p.CurrentPageIndex, 0, "A15");
  185. Assert.AreEqual (p.EditItemIndex, -1, "A16");
  186. Assert.AreEqual (p.PageCount, 0, "A17");
  187. Assert.AreEqual (p.PageSize, 10, "A18");
  188. Assert.AreEqual (p.SelectedIndex, -1, "A19");
  189. Assert.AreEqual (p.SelectedItem, null, "A20");
  190. Assert.AreEqual (p.ShowFooter, false, "A21");
  191. Assert.AreEqual (p.ShowHeader, true, "A22");
  192. Assert.AreEqual (p.VirtualItemCount, 0, "A23");
  193. }
  194. [Test]
  195. public void TagName ()
  196. {
  197. DataGridPoker p = new DataGridPoker ();
  198. #if NET_2_0
  199. Assert.AreEqual (p.GetTagName (), "table", "A1");
  200. #else
  201. Assert.AreEqual (p.GetTagName (), "span", "A1");
  202. #endif
  203. }
  204. [Test]
  205. [ExpectedException (typeof (ArgumentNullException))]
  206. public void NullBackImage ()
  207. {
  208. DataGridPoker p = new DataGridPoker ();
  209. p.BackImageUrl = null;
  210. Assert.AreEqual (p.BackImageUrl, String.Empty, "A1");
  211. }
  212. [Test]
  213. public void CleanProperties ()
  214. {
  215. DataGridPoker p = new DataGridPoker ();
  216. p.AllowCustomPaging = true;
  217. Assert.IsTrue (p.AllowCustomPaging, "A1");
  218. p.AllowCustomPaging = false;
  219. Assert.IsFalse (p.AllowCustomPaging, "A2");
  220. p.AllowPaging = true;
  221. Assert.IsTrue (p.AllowPaging, "A3");
  222. p.AllowPaging = false;
  223. Assert.IsFalse (p.AllowPaging, "A4");
  224. p.AllowSorting = true;
  225. Assert.IsTrue (p.AllowSorting, "A5");
  226. p.AllowSorting = false;
  227. Assert.IsFalse (p.AllowSorting, "A6");
  228. p.AutoGenerateColumns = true;
  229. Assert.IsTrue (p.AutoGenerateColumns, "A7");
  230. p.AutoGenerateColumns = false;
  231. Assert.IsFalse (p.AutoGenerateColumns, "A8");
  232. p.BackImageUrl = "foobar";
  233. Assert.AreEqual (p.BackImageUrl, "foobar", "A9");
  234. p.CurrentPageIndex = 0;
  235. Assert.AreEqual (p.CurrentPageIndex, 0, "A10");
  236. p.CurrentPageIndex = Int32.MaxValue;
  237. Assert.AreEqual (p.CurrentPageIndex, Int32.MaxValue, "A11");
  238. p.EditItemIndex = 0;
  239. Assert.AreEqual (p.EditItemIndex, 0, "A12");
  240. p.EditItemIndex = -1;
  241. Assert.AreEqual (p.EditItemIndex, -1, "A13");
  242. p.EditItemIndex = Int32.MaxValue;
  243. Assert.AreEqual (p.EditItemIndex, Int32.MaxValue, "A14");
  244. p.PageSize = 1;
  245. Assert.AreEqual (p.PageSize, 1, "A15");
  246. p.PageSize = Int32.MaxValue;
  247. p.SelectedIndex = 0;
  248. Assert.AreEqual (p.SelectedIndex, 0, "A16");
  249. p.SelectedIndex = -1;
  250. Assert.AreEqual (p.SelectedIndex, -1, "A17");
  251. p.SelectedIndex = Int32.MaxValue;
  252. Assert.AreEqual (p.SelectedIndex, Int32.MaxValue, "A18");
  253. p.ShowFooter = true;
  254. Assert.IsTrue (p.ShowFooter, "A19");
  255. p.ShowFooter = false;
  256. Assert.IsFalse (p.ShowFooter, "A20");
  257. p.ShowHeader = true;
  258. Assert.IsTrue (p.ShowHeader, "A21");
  259. p.ShowHeader = false;
  260. Assert.IsFalse (p.ShowHeader, "A22");
  261. p.VirtualItemCount = 0;
  262. Assert.AreEqual (p.VirtualItemCount, 0, "A23");
  263. p.VirtualItemCount = Int32.MaxValue;
  264. Assert.AreEqual (p.VirtualItemCount, Int32.MaxValue, "A24");
  265. }
  266. [Test]
  267. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  268. public void CurrentPageIndexTooLow ()
  269. {
  270. DataGridPoker p = new DataGridPoker ();
  271. p.CurrentPageIndex = -1;
  272. }
  273. [Test]
  274. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  275. public void EditItemIndexTooLow ()
  276. {
  277. DataGridPoker p = new DataGridPoker ();
  278. p.EditItemIndex = -2;
  279. }
  280. [Test]
  281. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  282. public void PageSizeTooLow ()
  283. {
  284. DataGridPoker p = new DataGridPoker ();
  285. p.PageSize = 0;
  286. }
  287. [Test]
  288. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  289. public void SelectedIndexTooLow ()
  290. {
  291. DataGridPoker p = new DataGridPoker ();
  292. p.SelectedIndex = -2;
  293. }
  294. [Test]
  295. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  296. public void VirtualItemCountTooLow ()
  297. {
  298. DataGridPoker p = new DataGridPoker ();
  299. p.VirtualItemCount = -1;
  300. }
  301. [Test]
  302. public void ViewState ()
  303. {
  304. DataGridPoker p = new DataGridPoker ();
  305. StateBag vs = p.GetViewState ();
  306. Assert.AreEqual (vs.Count, 0, "A1");
  307. p.AllowCustomPaging = true;
  308. Assert.AreEqual (vs.Count, 1, "A2");
  309. Assert.AreEqual (vs ["AllowCustomPaging"], true, "A3");
  310. p.AllowCustomPaging = false;
  311. Assert.AreEqual (vs.Count, 1, "A4");
  312. Assert.AreEqual (vs ["AllowCustomPaging"], false, "A5");
  313. p.AllowPaging = true;
  314. Assert.AreEqual (vs.Count, 2, "A6");
  315. Assert.AreEqual (vs ["AllowPaging"], true, "A7");
  316. p.AllowPaging = false;
  317. Assert.AreEqual (vs.Count, 2, "A8");
  318. Assert.AreEqual (vs ["AllowPaging"], false, "A9");
  319. p.AllowSorting = true;
  320. Assert.AreEqual (vs.Count, 3, "A10");
  321. Assert.AreEqual (vs ["AllowSorting"], true, "A11");
  322. p.AllowSorting = false;
  323. Assert.AreEqual (vs.Count, 3, "A12");
  324. Assert.AreEqual (vs ["AllowSorting"], false, "A13");
  325. p.AutoGenerateColumns = true;
  326. Assert.AreEqual (vs.Count, 4, "A14");
  327. Assert.AreEqual (vs ["AutoGenerateColumns"], true, "A15");
  328. p.AutoGenerateColumns = false;
  329. Assert.AreEqual (vs.Count, 4, "A16");
  330. Assert.AreEqual (vs ["AutoGenerateColumns"], false, "A17");
  331. p.CurrentPageIndex = 1;
  332. Assert.AreEqual (vs.Count, 5, "A18");
  333. Assert.AreEqual (vs ["CurrentPageIndex"], 1, "A19");
  334. p.EditItemIndex = 1;
  335. Assert.AreEqual (vs.Count, 6, "A20");
  336. Assert.AreEqual (vs ["EditItemIndex"], 1, "A20");
  337. p.PageSize = 25;
  338. Assert.AreEqual (vs.Count, 7, "A21");
  339. Assert.AreEqual (vs ["PageSize"], 25, "A22");
  340. p.SelectedIndex = 25;
  341. Assert.AreEqual (vs.Count, 8, "A23");
  342. Assert.AreEqual (vs ["SelectedIndex"], 25, "A24");
  343. p.ShowFooter = false;
  344. Assert.AreEqual (vs.Count, 9, "A25");
  345. Assert.AreEqual (vs ["ShowFooter"], false, "A26");
  346. p.ShowFooter = true;
  347. Assert.AreEqual (vs ["ShowFooter"], true, "A27");
  348. p.ShowHeader = false;
  349. Assert.AreEqual (vs.Count, 10, "A28");
  350. Assert.AreEqual (vs ["ShowHeader"], false, "A29");
  351. p.ShowHeader = true;
  352. Assert.AreEqual (vs ["ShowHeader"], true, "A30");
  353. p.VirtualItemCount = 100;
  354. Assert.AreEqual (vs.Count, 11, "A31");
  355. Assert.AreEqual (vs ["VirtualItemCount"], 100, "A32");
  356. }
  357. [Test]
  358. public void SelectIndexOutOfRange ()
  359. {
  360. DataGridPoker p = new DataGridPoker ();
  361. // No exception is thrown
  362. p.SelectedIndex = 25;
  363. }
  364. [Test]
  365. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  366. public void SelectItemOutOfRange ()
  367. {
  368. DataGridPoker p = new DataGridPoker ();
  369. DataGridItem d;
  370. p.SelectedIndex = 25;
  371. d = p.SelectedItem;
  372. }
  373. [Test]
  374. public void ControlStyle ()
  375. {
  376. DataGridPoker p = new DataGridPoker ();
  377. Assert.AreEqual (p.ControlStyle ().GetType (),
  378. typeof (TableStyle), "A1");
  379. TableStyle t = (TableStyle) p.ControlStyle ();
  380. Assert.AreEqual (t.GridLines, GridLines.Both, "A2");
  381. Assert.AreEqual (t.CellSpacing, 0, "A3");
  382. }
  383. [Test]
  384. public void Styles ()
  385. {
  386. DataGridPoker p = new DataGridPoker ();
  387. StateBag vs = p.GetViewState ();
  388. p.BackImageUrl = "foobar url";
  389. // The styles get stored in the view state
  390. #if NET_2_0
  391. Assert.AreEqual (vs.Count, 0, "A1");
  392. Assert.IsNull (vs ["BackImageUrl"], "A2");
  393. Assert.IsNull (vs ["GridLines"], "A3");
  394. Assert.IsNull (vs ["CellSpacing"], "A4");
  395. #else
  396. Assert.AreEqual (vs.Count, 3, "A1");
  397. Assert.AreEqual (vs ["BackImageUrl"], "foobar url", "A2");
  398. Assert.AreEqual (vs ["GridLines"], GridLines.Both, "A3");
  399. Assert.AreEqual (vs ["CellSpacing"], 0, "A4");
  400. #endif
  401. }
  402. private bool cancel_command;
  403. private bool delete_command;
  404. private bool edit_command;
  405. private bool item_command;
  406. private bool update_command;
  407. private bool item_created;
  408. private bool item_data_bound;
  409. private bool page_index_changed;
  410. private bool sort_command;
  411. private bool selected_changed;
  412. private int new_page_index;
  413. private void ResetEvents ()
  414. {
  415. cancel_command =
  416. delete_command =
  417. edit_command =
  418. item_command =
  419. update_command =
  420. item_created =
  421. item_data_bound =
  422. page_index_changed =
  423. sort_command =
  424. selected_changed = false;
  425. new_page_index = Int32.MinValue;
  426. }
  427. private void CancelCommandHandler (object sender, DataGridCommandEventArgs e)
  428. {
  429. cancel_command = true;
  430. }
  431. private void DeleteCommandHandler (object sender, DataGridCommandEventArgs e)
  432. {
  433. delete_command = true;
  434. }
  435. private void EditCommandHandler (object sender, DataGridCommandEventArgs e)
  436. {
  437. edit_command = true;
  438. }
  439. private void ItemCommandHandler (object sender, DataGridCommandEventArgs e)
  440. {
  441. item_command = true;
  442. }
  443. private void UpdateCommandHandler (object sender, DataGridCommandEventArgs e)
  444. {
  445. update_command = true;
  446. }
  447. private void ItemCreatedHandler (object sender, DataGridItemEventArgs e)
  448. {
  449. item_created = true;
  450. }
  451. private void ItemDataBoundHandler (object sender, DataGridItemEventArgs e)
  452. {
  453. item_data_bound = true;
  454. }
  455. private void PageIndexChangedHandler (object sender, DataGridPageChangedEventArgs e)
  456. {
  457. page_index_changed = true;
  458. new_page_index = e.NewPageIndex;
  459. }
  460. private void SortCommandHandler (object sender, DataGridSortCommandEventArgs e)
  461. {
  462. sort_command = true;
  463. }
  464. private void SelectedIndexChangedHandler (object sender, EventArgs e)
  465. {
  466. selected_changed = true;
  467. }
  468. [Test]
  469. public void Events ()
  470. {
  471. DataGridPoker p = new DataGridPoker ();
  472. DataGridCommandEventArgs command_args = new DataGridCommandEventArgs (null,
  473. null, new CommandEventArgs (String.Empty, String.Empty));
  474. DataGridItemEventArgs item_args = new DataGridItemEventArgs (null);
  475. DataGridPageChangedEventArgs page_args = new DataGridPageChangedEventArgs (null, 0);
  476. DataGridSortCommandEventArgs sort_args = new DataGridSortCommandEventArgs (null,
  477. command_args);
  478. ResetEvents ();
  479. p.CancelCommand += new DataGridCommandEventHandler (CancelCommandHandler);
  480. p.DoCancelCommand (command_args);
  481. Assert.IsTrue (cancel_command, "A1");
  482. ResetEvents ();
  483. p.DeleteCommand += new DataGridCommandEventHandler (DeleteCommandHandler);
  484. p.DoDeleteCommand (command_args);
  485. Assert.IsTrue (delete_command, "A2");
  486. ResetEvents ();
  487. p.EditCommand += new DataGridCommandEventHandler (EditCommandHandler);
  488. p.DoEditCommand (command_args);
  489. Assert.IsTrue (edit_command, "A3");
  490. ResetEvents ();
  491. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  492. p.DoItemCommand (command_args);
  493. Assert.IsTrue (item_command, "A4");
  494. ResetEvents ();
  495. p.UpdateCommand += new DataGridCommandEventHandler (UpdateCommandHandler);
  496. p.DoUpdateCommand (command_args);
  497. Assert.IsTrue (update_command, "A5");
  498. ResetEvents ();
  499. p.ItemCreated += new DataGridItemEventHandler (ItemCreatedHandler);
  500. p.DoItemCreated (item_args);
  501. Assert.IsTrue (item_created, "A6");
  502. ResetEvents ();
  503. p.ItemDataBound += new DataGridItemEventHandler (ItemDataBoundHandler);
  504. p.DoItemDataBound (item_args);
  505. Assert.IsTrue (item_data_bound, "A7");
  506. ResetEvents ();
  507. p.PageIndexChanged += new DataGridPageChangedEventHandler (PageIndexChangedHandler);
  508. p.DoPageIndexChanged (page_args);
  509. Assert.IsTrue (page_index_changed, "A8");
  510. ResetEvents ();
  511. p.SortCommand += new DataGridSortCommandEventHandler (SortCommandHandler);
  512. p.DoSortCommand (sort_args);
  513. Assert.IsTrue (sort_command, "A9");
  514. }
  515. [Test]
  516. public void BubbleEvent ()
  517. {
  518. DataGridPoker p = new DataGridPoker ();
  519. DataGridCommandEventArgs command_args;
  520. //
  521. // Cancel
  522. //
  523. ResetEvents ();
  524. command_args = new DataGridCommandEventArgs (null,
  525. null, new CommandEventArgs ("Cancel", String.Empty));
  526. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  527. p.CancelCommand += new DataGridCommandEventHandler (CancelCommandHandler);
  528. p.DoBubbleEvent (this, command_args);
  529. Assert.IsTrue (cancel_command, "A1");
  530. Assert.IsTrue (item_command, "#01");
  531. ResetEvents ();
  532. command_args = new DataGridCommandEventArgs (null,
  533. null, new CommandEventArgs ("cancel", String.Empty));
  534. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  535. p.CancelCommand += new DataGridCommandEventHandler (CancelCommandHandler);
  536. p.DoBubbleEvent (this, command_args);
  537. Assert.IsTrue (cancel_command, "A2");
  538. Assert.IsTrue (item_command, "#02");
  539. ResetEvents ();
  540. command_args = new DataGridCommandEventArgs (null,
  541. null, new CommandEventArgs ("CANCEL", String.Empty));
  542. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  543. p.CancelCommand += new DataGridCommandEventHandler (CancelCommandHandler);
  544. p.DoBubbleEvent (this, command_args);
  545. Assert.IsTrue (cancel_command, "A3");
  546. Assert.IsTrue (item_command, "#03");
  547. //
  548. // Delete
  549. //
  550. ResetEvents ();
  551. command_args = new DataGridCommandEventArgs (null,
  552. null, new CommandEventArgs ("Delete", String.Empty));
  553. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  554. p.DeleteCommand += new DataGridCommandEventHandler (DeleteCommandHandler);
  555. p.DoBubbleEvent (this, command_args);
  556. Assert.IsTrue (delete_command, "A4");
  557. Assert.IsTrue (item_command, "#04");
  558. ResetEvents ();
  559. command_args = new DataGridCommandEventArgs (null,
  560. null, new CommandEventArgs ("delete", String.Empty));
  561. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  562. p.DeleteCommand += new DataGridCommandEventHandler (DeleteCommandHandler);
  563. p.DoBubbleEvent (this, command_args);
  564. Assert.IsTrue (delete_command, "A5");
  565. Assert.IsTrue (item_command, "#05");
  566. ResetEvents ();
  567. command_args = new DataGridCommandEventArgs (null,
  568. null, new CommandEventArgs ("DELETE", String.Empty));
  569. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  570. p.DeleteCommand += new DataGridCommandEventHandler (DeleteCommandHandler);
  571. p.DoBubbleEvent (this, command_args);
  572. Assert.IsTrue (delete_command, "A6");
  573. Assert.IsTrue (item_command, "#06");
  574. //
  575. // Edit
  576. //
  577. ResetEvents ();
  578. command_args = new DataGridCommandEventArgs (null,
  579. null, new CommandEventArgs ("Edit", String.Empty));
  580. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  581. p.EditCommand += new DataGridCommandEventHandler (EditCommandHandler);
  582. p.DoBubbleEvent (this, command_args);
  583. Assert.IsTrue (edit_command, "A7");
  584. Assert.IsTrue (item_command, "#07");
  585. ResetEvents ();
  586. command_args = new DataGridCommandEventArgs (null,
  587. null, new CommandEventArgs ("edit", String.Empty));
  588. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  589. p.EditCommand += new DataGridCommandEventHandler (EditCommandHandler);
  590. p.DoBubbleEvent (this, command_args);
  591. Assert.IsTrue (edit_command, "A8");
  592. Assert.IsTrue (item_command, "#08");
  593. ResetEvents ();
  594. command_args = new DataGridCommandEventArgs (null,
  595. null, new CommandEventArgs ("EDIT", String.Empty));
  596. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  597. p.EditCommand += new DataGridCommandEventHandler (EditCommandHandler);
  598. p.DoBubbleEvent (this, command_args);
  599. Assert.IsTrue (edit_command, "A9");
  600. Assert.IsTrue (item_command, "#09");
  601. //
  602. // Item
  603. //
  604. ResetEvents ();
  605. command_args = new DataGridCommandEventArgs (null,
  606. null, new CommandEventArgs ("Item", String.Empty));
  607. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  608. p.DoBubbleEvent (this, command_args);
  609. Assert.IsTrue (item_command, "A10");
  610. ResetEvents ();
  611. command_args = new DataGridCommandEventArgs (null,
  612. null, new CommandEventArgs ("item", String.Empty));
  613. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  614. p.DoBubbleEvent (this, command_args);
  615. Assert.IsTrue (item_command, "A11");
  616. ResetEvents ();
  617. command_args = new DataGridCommandEventArgs (null,
  618. null, new CommandEventArgs ("ITEM", String.Empty));
  619. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  620. p.DoBubbleEvent (this, command_args);
  621. Assert.IsTrue (item_command, "A12");
  622. //
  623. // Sort
  624. //
  625. ResetEvents ();
  626. command_args = new DataGridCommandEventArgs (null,
  627. null, new CommandEventArgs ("Sort", String.Empty));
  628. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  629. p.SortCommand += new DataGridSortCommandEventHandler (SortCommandHandler);
  630. p.DoBubbleEvent (this, command_args);
  631. Assert.IsTrue (sort_command, "A13");
  632. Assert.IsTrue (item_command, "#10");
  633. ResetEvents ();
  634. command_args = new DataGridCommandEventArgs (null,
  635. null, new CommandEventArgs ("sort", String.Empty));
  636. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  637. p.SortCommand += new DataGridSortCommandEventHandler (SortCommandHandler);
  638. p.DoBubbleEvent (this, command_args);
  639. Assert.IsTrue (sort_command, "A14");
  640. Assert.IsTrue (item_command, "#11");
  641. ResetEvents ();
  642. command_args = new DataGridCommandEventArgs (null,
  643. null, new CommandEventArgs ("SORT", String.Empty));
  644. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  645. p.SortCommand += new DataGridSortCommandEventHandler (SortCommandHandler);
  646. p.DoBubbleEvent (this, command_args);
  647. Assert.IsTrue (sort_command, "A15");
  648. Assert.IsTrue (item_command, "#12");
  649. //
  650. // Update
  651. //
  652. ResetEvents ();
  653. command_args = new DataGridCommandEventArgs (null,
  654. null, new CommandEventArgs ("Update", String.Empty));
  655. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  656. p.UpdateCommand += new DataGridCommandEventHandler (UpdateCommandHandler);
  657. p.DoBubbleEvent (this, command_args);
  658. Assert.IsTrue (update_command, "A16");
  659. Assert.IsTrue (item_command, "#13");
  660. ResetEvents ();
  661. command_args = new DataGridCommandEventArgs (null,
  662. null, new CommandEventArgs ("update", String.Empty));
  663. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  664. p.UpdateCommand += new DataGridCommandEventHandler (UpdateCommandHandler);
  665. p.DoBubbleEvent (this, command_args);
  666. Assert.IsTrue (update_command, "A17");
  667. Assert.IsTrue (item_command, "#14");
  668. ResetEvents ();
  669. command_args = new DataGridCommandEventArgs (null,
  670. null, new CommandEventArgs ("UPDATE", String.Empty));
  671. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  672. p.UpdateCommand += new DataGridCommandEventHandler (UpdateCommandHandler);
  673. p.DoBubbleEvent (this, command_args);
  674. Assert.IsTrue (update_command, "A18");
  675. Assert.IsTrue (item_command, "#15");
  676. //
  677. // Select
  678. //
  679. DataGridItem item = new DataGridItem (0, 0, ListItemType.Item);
  680. ResetEvents ();
  681. command_args = new DataGridCommandEventArgs (item, null,
  682. new CommandEventArgs ("Select", String.Empty));
  683. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  684. p.SelectedIndexChanged += new EventHandler (SelectedIndexChangedHandler);
  685. p.DoBubbleEvent (this, command_args);
  686. Assert.IsTrue (selected_changed, "A19");
  687. Assert.IsTrue (item_command, "#16");
  688. ResetEvents ();
  689. command_args = new DataGridCommandEventArgs (item, null,
  690. new CommandEventArgs ("select", String.Empty));
  691. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  692. p.SelectedIndexChanged += new EventHandler (SelectedIndexChangedHandler);
  693. p.DoBubbleEvent (this, command_args);
  694. Assert.IsTrue (selected_changed, "A20");
  695. Assert.IsTrue (item_command, "#17");
  696. ResetEvents ();
  697. command_args = new DataGridCommandEventArgs (item, null,
  698. new CommandEventArgs ("SELECT", String.Empty));
  699. p.ItemCommand += new DataGridCommandEventHandler (ItemCommandHandler);
  700. p.SelectedIndexChanged += new EventHandler (SelectedIndexChangedHandler);
  701. p.DoBubbleEvent (this, command_args);
  702. Assert.IsTrue (selected_changed, "A21");
  703. Assert.IsTrue (item_command, "#18");
  704. }
  705. [Test]
  706. public void BubblePageCommand ()
  707. {
  708. DataGridPoker p = new DataGridPoker ();
  709. DataGridItem item = new DataGridItem (0, 0, ListItemType.Item);
  710. DataGridCommandEventArgs command_args;
  711. //
  712. // Prev
  713. //
  714. ResetEvents ();
  715. command_args = new DataGridCommandEventArgs (item, null,
  716. new CommandEventArgs ("Page", "Prev"));
  717. p.CurrentPageIndex = 10;
  718. p.PageIndexChanged += new DataGridPageChangedEventHandler (PageIndexChangedHandler);
  719. p.DoBubbleEvent (this, command_args);
  720. Assert.IsTrue (page_index_changed, "A1");
  721. Assert.AreEqual (new_page_index, 9, "A2");
  722. ResetEvents ();
  723. command_args = new DataGridCommandEventArgs (item, null,
  724. new CommandEventArgs ("page", "prev"));
  725. p.CurrentPageIndex = 10;
  726. p.PageIndexChanged += new DataGridPageChangedEventHandler (PageIndexChangedHandler);
  727. p.DoBubbleEvent (this, command_args);
  728. Assert.IsTrue (page_index_changed, "A3");
  729. Assert.AreEqual (new_page_index, 9, "A4");
  730. ResetEvents ();
  731. command_args = new DataGridCommandEventArgs (item, null,
  732. new CommandEventArgs ("PAGE", "PREV"));
  733. p.CurrentPageIndex = 10;
  734. p.PageIndexChanged += new DataGridPageChangedEventHandler (PageIndexChangedHandler);
  735. p.DoBubbleEvent (this, command_args);
  736. Assert.IsTrue (page_index_changed, "A5");
  737. Assert.AreEqual (new_page_index, 9, "A6");
  738. //
  739. // Next
  740. //
  741. ResetEvents ();
  742. command_args = new DataGridCommandEventArgs (item, null,
  743. new CommandEventArgs ("Page", "Next"));
  744. p.CurrentPageIndex = 10;
  745. p.PageIndexChanged += new DataGridPageChangedEventHandler (PageIndexChangedHandler);
  746. p.DoBubbleEvent (this, command_args);
  747. Assert.IsTrue (page_index_changed, "A5");
  748. Assert.AreEqual (new_page_index, 11, "A6");
  749. ResetEvents ();
  750. command_args = new DataGridCommandEventArgs (item, null,
  751. new CommandEventArgs ("page", "next"));
  752. p.CurrentPageIndex = 10;
  753. p.PageIndexChanged += new DataGridPageChangedEventHandler (PageIndexChangedHandler);
  754. p.DoBubbleEvent (this, command_args);
  755. Assert.IsTrue (page_index_changed, "A7");
  756. Assert.AreEqual (new_page_index, 11, "A8");
  757. ResetEvents ();
  758. command_args = new DataGridCommandEventArgs (item, null,
  759. new CommandEventArgs ("PAGE", "NEXT"));
  760. p.CurrentPageIndex = 10;
  761. p.PageIndexChanged += new DataGridPageChangedEventHandler (PageIndexChangedHandler);
  762. p.DoBubbleEvent (this, command_args);
  763. Assert.IsTrue (page_index_changed, "A9");
  764. Assert.AreEqual (new_page_index, 11, "A10");
  765. //
  766. // Specific
  767. //
  768. ResetEvents ();
  769. command_args = new DataGridCommandEventArgs (item, null,
  770. new CommandEventArgs ("Page", "25"));
  771. p.CurrentPageIndex = 10;
  772. p.PageIndexChanged += new DataGridPageChangedEventHandler (PageIndexChangedHandler);
  773. p.DoBubbleEvent (this, command_args);
  774. Assert.IsTrue (page_index_changed, "A11");
  775. Assert.AreEqual (new_page_index, 24, "A12");
  776. ResetEvents ();
  777. command_args = new DataGridCommandEventArgs (item, null,
  778. new CommandEventArgs ("Page", "0"));
  779. p.CurrentPageIndex = 10;
  780. p.PageIndexChanged += new DataGridPageChangedEventHandler (PageIndexChangedHandler);
  781. p.DoBubbleEvent (this, command_args);
  782. Assert.IsTrue (page_index_changed, "A11");
  783. Assert.AreEqual (new_page_index, -1, "A12");
  784. }
  785. [Test]
  786. [ExpectedException (typeof (FormatException))]
  787. public void BadBubblePageArg ()
  788. {
  789. DataGridPoker p = new DataGridPoker ();
  790. DataGridItem item = new DataGridItem (0, 0, ListItemType.Item);
  791. DataGridCommandEventArgs command_args;
  792. ResetEvents ();
  793. command_args = new DataGridCommandEventArgs (item, null,
  794. new CommandEventArgs ("Page", "i am bad"));
  795. p.DoBubbleEvent (this, command_args);
  796. }
  797. [Test]
  798. [ExpectedException (typeof (InvalidCastException))]
  799. public void BadBubblePageArg2 ()
  800. {
  801. DataGridPoker p = new DataGridPoker ();
  802. DataGridItem item = new DataGridItem (0, 0, ListItemType.Item);
  803. DataGridCommandEventArgs command_args;
  804. ResetEvents ();
  805. command_args = new DataGridCommandEventArgs (item, null,
  806. new CommandEventArgs ("Page", new object ()));
  807. p.DoBubbleEvent (this, command_args);
  808. }
  809. [Test]
  810. public void SaveViewState ()
  811. {
  812. DataGridPoker p = new DataGridPoker ();
  813. p.TrackState ();
  814. object [] vs = (object []) p.SaveState ();
  815. #if NET_2_0
  816. Assert.AreEqual (vs.Length, 11, "A1");
  817. #else
  818. Assert.AreEqual (vs.Length, 10, "A1");
  819. #endif
  820. // By default the viewstate is all null
  821. for (int i = 0; i < vs.Length; i++)
  822. Assert.IsNull (vs [i], "A2-" + i);
  823. //
  824. // TODO: What goes in the [1] and [9] slots?
  825. //
  826. p.AllowPaging = true;
  827. vs = (object []) p.SaveState ();
  828. Assert.IsNotNull (vs [0], "A3");
  829. /*
  830. This test doesn't work right now. It must be an issue
  831. in the DataGridPagerStyle
  832. p.PagerStyle.Visible = true;
  833. vs = (object []) p.SaveState ();
  834. Assert.IsNotNull (vs [2], "A5");
  835. */
  836. p.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
  837. vs = (object []) p.SaveState ();
  838. Assert.IsNotNull (vs [3], "A6");
  839. p.FooterStyle.HorizontalAlign = HorizontalAlign.Center;
  840. vs = (object []) p.SaveState ();
  841. Assert.IsNotNull (vs [4], "A7");
  842. p.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
  843. vs = (object []) p.SaveState ();
  844. Assert.IsNotNull (vs [5], "A8");
  845. p.AlternatingItemStyle.HorizontalAlign = HorizontalAlign.Center;
  846. vs = (object []) p.SaveState ();
  847. Assert.IsNotNull (vs [6], "A9");
  848. p.SelectedItemStyle.HorizontalAlign = HorizontalAlign.Center;
  849. vs = (object []) p.SaveState ();
  850. Assert.IsNotNull (vs [7], "A10");
  851. p.EditItemStyle.HorizontalAlign = HorizontalAlign.Center;
  852. vs = (object []) p.SaveState ();
  853. Assert.IsNotNull (vs [8], "A11");
  854. PagedDataSource source = new PagedDataSource ();
  855. DataTable table = new DataTable ();
  856. ArrayList columns;
  857. table.Columns.Add (new DataColumn ("one", typeof (string)));
  858. table.Columns.Add (new DataColumn ("two", typeof (string)));
  859. table.Columns.Add (new DataColumn ("three", typeof (string)));
  860. source.DataSource = new DataView (table);
  861. columns = p.CreateColumns (source, true);
  862. vs = (object []) p.SaveState ();
  863. #if NET_2_0
  864. Assert.IsNull (vs [9], "A12");
  865. p.BackImageUrl = "foobar url";
  866. vs = (object []) p.SaveState ();
  867. Assert.IsNotNull (vs [9], "A12");
  868. Assert.IsNotNull (vs [10], "A12");
  869. Assert.AreEqual (vs [10].GetType (), typeof (object []), "A12");
  870. object [] cols = (object []) vs [10];
  871. Assert.AreEqual (cols.Length, 3, "A13");
  872. #else
  873. Assert.IsNotNull (vs [9], "A12");
  874. Assert.AreEqual (vs [9].GetType (), typeof (object []), "A12");
  875. object [] cols = (object []) vs [9];
  876. Assert.AreEqual (cols.Length, 3, "A13");
  877. #endif
  878. }
  879. [Test]
  880. public void CreateColumnSet ()
  881. {
  882. DataGridPoker p = new DataGridPoker ();
  883. PagedDataSource source = new PagedDataSource ();
  884. DataTable table = new DataTable ();
  885. ArrayList columns;
  886. table.Columns.Add (new DataColumn ("one", typeof (string)));
  887. table.Columns.Add (new DataColumn ("two", typeof (string)));
  888. table.Columns.Add (new DataColumn ("three", typeof (string)));
  889. source.DataSource = new DataView (table);
  890. columns = p.CreateColumns (source, true);
  891. Assert.AreEqual (columns.Count, 3, "A1");
  892. Assert.AreEqual (((DataGridColumn) columns [0]).HeaderText, "one", "A2");
  893. Assert.AreEqual (((DataGridColumn) columns [1]).HeaderText, "two", "A3");
  894. Assert.AreEqual (((DataGridColumn) columns [2]).HeaderText, "three", "A4");
  895. // AutoGenerated columns are not added to the ColumnsCollection
  896. Assert.AreEqual (p.Columns.Count, 0, "A5");
  897. // Without allowing data dinding,
  898. columns = p.CreateColumns (source, false);
  899. Assert.AreEqual (columns.Count, 3, "A6");
  900. Assert.AreEqual (((DataGridColumn) columns [0]).HeaderText, "one", "A7");
  901. Assert.AreEqual (((DataGridColumn) columns [1]).HeaderText, "two", "A8");
  902. Assert.AreEqual (((DataGridColumn) columns [2]).HeaderText, "three", "A9");
  903. // Mixing with already added columns
  904. p = new DataGridPoker ();
  905. DataGridColumn a = new ButtonColumn ();
  906. DataGridColumn b = new ButtonColumn ();
  907. a.HeaderText = "A";
  908. b.HeaderText = "B";
  909. p.Columns.Add (a);
  910. p.Columns.Add (b);
  911. columns = p.CreateColumns (source, true);
  912. Assert.AreEqual (columns.Count, 5, "A6");
  913. Assert.AreEqual (((DataGridColumn) columns [0]).HeaderText, "A", "A10");
  914. Assert.AreEqual (((DataGridColumn) columns [1]).HeaderText, "B", "A11");
  915. Assert.AreEqual (((DataGridColumn) columns [2]).HeaderText, "one", "A12");
  916. Assert.AreEqual (((DataGridColumn) columns [3]).HeaderText, "two", "A13");
  917. Assert.AreEqual (((DataGridColumn) columns [4]).HeaderText, "three", "A14");
  918. // Assigned properties of the newly created columns
  919. BoundColumn one = (BoundColumn) columns [2];
  920. Assert.AreEqual (one.HeaderText, "one", "A15");
  921. Assert.AreEqual (one.DataField, "one", "A16");
  922. Assert.AreEqual (one.DataFormatString, String.Empty, "A17");
  923. Assert.AreEqual (one.SortExpression, "one", "A18");
  924. Assert.AreEqual (one.HeaderImageUrl, String.Empty, "A19");
  925. Assert.AreEqual (one.FooterText, String.Empty, "A20");
  926. }
  927. [Test]
  928. public void CreateColumnsBinding ()
  929. {
  930. DataGridPoker p = new DataGridPoker ();
  931. PagedDataSource source = new PagedDataSource ();
  932. DataTable table = new DataTable ();
  933. ArrayList columns;
  934. table.Columns.Add (new DataColumn ("one", typeof (string)));
  935. table.Columns.Add (new DataColumn ("two", typeof (string)));
  936. table.Columns.Add (new DataColumn ("three", typeof (string)));
  937. source.DataSource = new DataView (table);
  938. columns = p.CreateColumns (source, true);
  939. Assert.AreEqual (columns.Count, 3, "A1");
  940. Assert.AreEqual (((DataGridColumn) columns [0]).HeaderText, "one", "A2");
  941. Assert.AreEqual (((DataGridColumn) columns [1]).HeaderText, "two", "A3");
  942. Assert.AreEqual (((DataGridColumn) columns [2]).HeaderText, "three", "A4");
  943. table.Columns.Add (new DataColumn ("four", typeof (string)));
  944. table.Columns.Add (new DataColumn ("five", typeof (string)));
  945. table.Columns.Add (new DataColumn ("six", typeof (string)));
  946. // Just gets the old columns
  947. columns = p.CreateColumns (source, false);
  948. Assert.AreEqual (columns.Count, 3, "A5");
  949. Assert.AreEqual (((DataGridColumn) columns [0]).HeaderText, "one", "A6");
  950. Assert.AreEqual (((DataGridColumn) columns [1]).HeaderText, "two", "A7");
  951. Assert.AreEqual (((DataGridColumn) columns [2]).HeaderText, "three", "A8");
  952. columns = p.CreateColumns (source, true);
  953. Assert.AreEqual (columns.Count, 6, "A9");
  954. Assert.AreEqual (((DataGridColumn) columns [0]).HeaderText, "one", "A10");
  955. Assert.AreEqual (((DataGridColumn) columns [1]).HeaderText, "two", "A11");
  956. Assert.AreEqual (((DataGridColumn) columns [2]).HeaderText, "three", "A12");
  957. Assert.AreEqual (((DataGridColumn) columns [3]).HeaderText, "four", "A13");
  958. Assert.AreEqual (((DataGridColumn) columns [4]).HeaderText, "five", "A14");
  959. Assert.AreEqual (((DataGridColumn) columns [5]).HeaderText, "six", "A15");
  960. // Assigned properties of the newly created columns
  961. BoundColumn one = (BoundColumn) columns [0];
  962. Assert.AreEqual (one.HeaderText, "one", "A16");
  963. Assert.AreEqual (one.DataField, "one", "A17");
  964. Assert.AreEqual (one.DataFormatString, String.Empty, "A18");
  965. Assert.AreEqual (one.SortExpression, "one", "A19");
  966. Assert.AreEqual (one.HeaderImageUrl, String.Empty, "A20");
  967. Assert.AreEqual (one.FooterText, String.Empty, "A21");
  968. }
  969. [Test]
  970. public void CreateSimpleColumns ()
  971. {
  972. DataGridPoker p = new DataGridPoker ();
  973. PagedDataSource source = new PagedDataSource ();
  974. ArrayList list = new ArrayList ();
  975. ArrayList columns;
  976. list.Add ("One");
  977. list.Add ("Two");
  978. list.Add ("Three");
  979. source.DataSource = list;
  980. columns = p.CreateColumns (source, true);
  981. Assert.AreEqual (1, columns.Count, "A1");
  982. Assert.AreEqual ("Item", ((DataGridColumn) columns [0]).HeaderText, "A2");
  983. AmazingEnumerable amazing = new AmazingEnumerable (list);
  984. source.DataSource = amazing;
  985. columns = p.CreateColumns (source, true);
  986. Assert.AreEqual (1, columns.Count, "A3");
  987. BoundColumn one = (BoundColumn) columns [0];
  988. Assert.AreEqual ("Item", one.HeaderText, "A4");
  989. // I guess this makes it bind to itself ?
  990. Assert.AreEqual (BoundColumn.thisExpr, one.DataField, "A5");
  991. Assert.AreEqual (String.Empty, one.DataFormatString, "A6");
  992. Assert.AreEqual ("Item", one.SortExpression, "A7");
  993. Assert.AreEqual (String.Empty, one.HeaderImageUrl, "A8");
  994. Assert.AreEqual (String.Empty, one.FooterText, "A9");
  995. Assert.AreEqual ("Item", one.HeaderText, "A10");
  996. source.DataSource = new ArrayList ();
  997. columns = p.CreateColumns (source, true);
  998. Assert.AreEqual (0, columns.Count, "A11");
  999. }
  1000. [Test]
  1001. public void DataBindingEnumerator ()
  1002. {
  1003. DataGridPoker p = new DataGridPoker ();
  1004. PagedDataSource source = new PagedDataSource ();
  1005. ArrayList list = new ArrayList ();
  1006. ArrayList columns;
  1007. list.Add ("One");
  1008. list.Add ("Two");
  1009. list.Add ("Three");
  1010. AmazingEnumerable amazing = new AmazingEnumerable (list);
  1011. source.DataSource = amazing;
  1012. columns = p.CreateColumns (source, true);
  1013. Assert.AreEqual (1, columns.Count, "A1");
  1014. Assert.AreEqual ("Item", ((DataGridColumn) columns [0]).HeaderText, "A2");
  1015. Assert.AreEqual (1, amazing.CallCount, "A3");
  1016. Assert.AreEqual (0, p.DataKeys.Count, "A4");
  1017. }
  1018. class Custom : ICustomTypeDescriptor {
  1019. public AttributeCollection GetAttributes ()
  1020. {
  1021. throw new Exception ();
  1022. }
  1023. public string GetClassName()
  1024. {
  1025. throw new Exception ();
  1026. }
  1027. public string GetComponentName()
  1028. {
  1029. throw new Exception ();
  1030. }
  1031. public TypeConverter GetConverter()
  1032. {
  1033. throw new Exception ();
  1034. }
  1035. public EventDescriptor GetDefaultEvent()
  1036. {
  1037. throw new Exception ();
  1038. }
  1039. public PropertyDescriptor GetDefaultProperty()
  1040. {
  1041. throw new Exception ();
  1042. }
  1043. public object GetEditor (Type editorBaseType)
  1044. {
  1045. throw new Exception ();
  1046. }
  1047. public EventDescriptorCollection GetEvents ()
  1048. {
  1049. throw new Exception ();
  1050. }
  1051. public EventDescriptorCollection GetEvents (Attribute[] arr)
  1052. {
  1053. throw new Exception ();
  1054. }
  1055. public int CallCount;
  1056. public PropertyDescriptorCollection GetProperties()
  1057. {
  1058. // MS calls this one
  1059. if (CallCount++ > 0)
  1060. throw new Exception ("This should not happen");
  1061. PropertyDescriptorCollection coll = new PropertyDescriptorCollection (null);
  1062. coll.Add (new MyPropertyDescriptor ());
  1063. return coll;
  1064. }
  1065. public PropertyDescriptorCollection GetProperties (Attribute[] arr)
  1066. {
  1067. // We call this one
  1068. return GetProperties ();
  1069. }
  1070. public object GetPropertyOwner (PropertyDescriptor pd)
  1071. {
  1072. throw new Exception ();
  1073. }
  1074. }
  1075. class MyPropertyDescriptor : PropertyDescriptor {
  1076. int val;
  1077. public MyPropertyDescriptor () : base ("CustomName", null)
  1078. {
  1079. }
  1080. public override Type ComponentType {
  1081. get { return typeof (MyPropertyDescriptor); }
  1082. }
  1083. public override bool IsReadOnly {
  1084. get { return true; }
  1085. }
  1086. public override Type PropertyType {
  1087. get { return typeof (int); }
  1088. }
  1089. public override object GetValue (object component)
  1090. {
  1091. return val++;
  1092. }
  1093. public override void SetValue (object component, object value)
  1094. {
  1095. }
  1096. public override void ResetValue (object component)
  1097. {
  1098. }
  1099. public override bool CanResetValue (object component)
  1100. {
  1101. return false;
  1102. }
  1103. public override bool ShouldSerializeValue (object component)
  1104. {
  1105. return false;
  1106. }
  1107. }
  1108. class MyEnumerable : IEnumerable {
  1109. public object Item;
  1110. public IEnumerator GetEnumerator ()
  1111. {
  1112. ArrayList list = new ArrayList ();
  1113. list.Add (Item);
  1114. return list.GetEnumerator ();
  1115. }
  1116. }
  1117. [Test]
  1118. public void DataBindingCustomElement ()
  1119. {
  1120. DataGridPoker p = new DataGridPoker ();
  1121. p.DataKeyField = "CustomName";
  1122. PagedDataSource source = new PagedDataSource ();
  1123. MyEnumerable myenum = new MyEnumerable ();
  1124. myenum.Item = new Custom ();
  1125. source.DataSource = myenum;
  1126. ArrayList columns = p.CreateColumns (source, true);
  1127. Assert.AreEqual (1, columns.Count, "A1");
  1128. Assert.AreEqual ("CustomName", ((DataGridColumn) columns [0]).HeaderText, "A2");
  1129. Assert.AreEqual (0, p.DataKeys.Count, "A3");
  1130. }
  1131. #if NET_2_0
  1132. public class data
  1133. {
  1134. private static ArrayList _data = new ArrayList ();
  1135. static data () {
  1136. _data.Add (new DataItem (1, "heh1"));
  1137. _data.Add (new DataItem (2, "heh2"));
  1138. _data.Add (new DataItem (3, "heh3"));
  1139. _data.Add (new DataItem (4, "heh4"));
  1140. _data.Add (new DataItem (5, "heh5"));
  1141. _data.Add (new DataItem (6, "heh6"));
  1142. _data.Add (new DataItem (7, "heh7"));
  1143. _data.Add (new DataItem (8, "heh8"));
  1144. _data.Add (new DataItem (9, "heh9"));
  1145. _data.Add (new DataItem (10, "heh10"));
  1146. }
  1147. public data () {
  1148. }
  1149. public ArrayList GetAllItems () {
  1150. return _data;
  1151. }
  1152. public ArrayList GetPagedItems (int startIndex, int maxRows)
  1153. {
  1154. ArrayList list = new ArrayList ();
  1155. if (startIndex < _data.Count - 1) {
  1156. int countToReturn = Math.Min (maxRows, _data.Count - startIndex);
  1157. for (int i = startIndex; i < startIndex + countToReturn; i++) {
  1158. list.Add (_data [i]);
  1159. }
  1160. }
  1161. return list;
  1162. }
  1163. public int GetCount ()
  1164. {
  1165. return _data.Count;
  1166. }
  1167. public void UpdateItem (int id, string name) {
  1168. foreach (DataItem i in _data) {
  1169. if (i.ID == id) {
  1170. i.Name = name;
  1171. return;
  1172. }
  1173. }
  1174. }
  1175. }
  1176. public class DataItem
  1177. {
  1178. int _id = 0;
  1179. string _name = "";
  1180. public DataItem (int id, string name) {
  1181. _id = id;
  1182. _name = name;
  1183. }
  1184. public int ID {
  1185. get { return _id; }
  1186. }
  1187. public string Name {
  1188. get { return _name; }
  1189. set { _name = value; }
  1190. }
  1191. }
  1192. public class DataSourceObject
  1193. {
  1194. public static List<string> GetList (string sortExpression, int startRowIndex, int maximumRows) {
  1195. return GetList ();
  1196. }
  1197. public static List<string> GetList (int startRowIndex, int maximumRows) {
  1198. return GetList ();
  1199. }
  1200. public static List<string> GetList (string sortExpression) {
  1201. return GetList ();
  1202. }
  1203. public static List<string> GetList () {
  1204. List<string> list = new List<string> ();
  1205. list.Add ("Norway");
  1206. list.Add ("Sweden");
  1207. list.Add ("France");
  1208. list.Add ("Italy");
  1209. list.Add ("Israel");
  1210. list.Add ("Russia");
  1211. return list;
  1212. }
  1213. public static int GetCount () {
  1214. return GetList ().Count;
  1215. }
  1216. }
  1217. [Test]
  1218. [ExpectedException(typeof(InvalidOperationException))]
  1219. public void DataSourceAndDataSourceID ()
  1220. {
  1221. Page page = new Page ();
  1222. DataGridPoker dg = new DataGridPoker ();
  1223. page.Controls.Add (dg);
  1224. DataTable dt = new DataTable ();
  1225. dt.Columns.Add (new DataColumn ("something", typeof (Int32)));
  1226. DataRow dr = dt.NewRow ();
  1227. dt.Rows.Add (new object [] { 1 });
  1228. DataView dv = new DataView (dt);
  1229. dg.DataSource = dv;
  1230. ObjectDataSource ds = new ObjectDataSource ();
  1231. ds.TypeName = typeof (DataSourceObject).AssemblyQualifiedName;
  1232. ds.SelectMethod = "GetList";
  1233. ds.SortParameterName = "sortExpression";
  1234. ds.ID = "Data";
  1235. page.Controls.Add (ds);
  1236. dg.DataSourceID = "Data";
  1237. dg.DataBind ();
  1238. }
  1239. [Test]
  1240. public void DataBindingDataSourceID ()
  1241. {
  1242. Page page = new Page ();
  1243. DataGridPoker dg = new DataGridPoker ();
  1244. page.Controls.Add (dg);
  1245. ObjectDataSource ds = new ObjectDataSource ();
  1246. ds.TypeName = typeof (DataSourceObject).AssemblyQualifiedName;
  1247. ds.SelectMethod = "GetList";
  1248. ds.SortParameterName = "sortExpression";
  1249. ds.ID = "Data";
  1250. page.Controls.Add (ds);
  1251. dg.DataSourceID = "Data";
  1252. dg.DataBind ();
  1253. Assert.AreEqual (6, dg.Items.Count, "DataBindingDataSourceID");
  1254. }
  1255. [Test]
  1256. [NUnit.Framework.Category ("NunitWeb")]
  1257. public void DataBindingDataSourceIDAutomatic ()
  1258. {
  1259. WebTest t = new WebTest ();
  1260. PageDelegates pd = new PageDelegates();
  1261. pd.Load = DataSourceIDAutomatic_Load;
  1262. pd.PreRender = DataSourceIDAutomatic_PreRender;
  1263. t.Invoker = new PageInvoker (pd);
  1264. t.Run ();
  1265. }
  1266. public static void DataSourceIDAutomatic_Load (Page page)
  1267. {
  1268. DataGridPoker dg = new DataGridPoker ();
  1269. dg.ID = "DataGrid";
  1270. page.Controls.Add (dg);
  1271. ObjectDataSource ds = new ObjectDataSource ();
  1272. ds.TypeName = typeof (DataSourceObject).AssemblyQualifiedName;
  1273. ds.SelectMethod = "GetList";
  1274. ds.SortParameterName = "sortExpression";
  1275. ds.ID = "Data";
  1276. page.Controls.Add (ds);
  1277. dg.DataSourceID = "Data";
  1278. }
  1279. public static void DataSourceIDAutomatic_PreRender (Page page)
  1280. {
  1281. DataGrid dg = (DataGrid)page.FindControl ("DataGrid");
  1282. Assert.AreEqual (6, dg.Items.Count, "DataBindingDataSourceID");
  1283. }
  1284. [Test]
  1285. public void DataSourceIDBindingNoColumns ()
  1286. {
  1287. Page page = new Page ();
  1288. DataGridPoker dg = new DataGridPoker ();
  1289. dg.ID = "DataGrid";
  1290. dg.AutoGenerateColumns = false;
  1291. page.Controls.Add (dg);
  1292. ObjectDataSource ds = new ObjectDataSource ();
  1293. ds.TypeName = typeof (DataSourceObject).AssemblyQualifiedName;
  1294. ds.SelectMethod = "GetList";
  1295. ds.SortParameterName = "sortExpression";
  1296. ds.ID = "Data";
  1297. page.Controls.Add (ds);
  1298. dg.DataSourceID = "Data";
  1299. dg.DataBind ();
  1300. Assert.AreEqual (0, dg.Columns.Count, "Columns Count");
  1301. Assert.AreEqual (0, dg.Items.Count, "Items Count");
  1302. }
  1303. [Test]
  1304. public void DataSourceIDBindingManualColumns ()
  1305. {
  1306. Page page = new Page ();
  1307. DataGridPoker dg = new DataGridPoker ();
  1308. dg.ID = "DataGrid";
  1309. dg.AutoGenerateColumns = false;
  1310. BoundColumn col = new BoundColumn();
  1311. col.DataField = "something";
  1312. dg.Columns.Add (col);
  1313. page.Controls.Add (dg);
  1314. DataTable dt = new DataTable ();
  1315. dt.Columns.Add (new DataColumn ("something", typeof (Int32)));
  1316. DataRow dr = dt.NewRow ();
  1317. dt.Rows.Add (new object [] { 1 });
  1318. dt.Rows.Add (new object [] { 2 });
  1319. dt.Rows.Add (new object [] { 3 });
  1320. dt.Rows.Add (new object [] { 4 });
  1321. dt.Rows.Add (new object [] { 5 });
  1322. dt.Rows.Add (new object [] { 6 });
  1323. DataView dv = new DataView (dt);
  1324. dg.DataSource = dv;
  1325. dg.DataBind ();
  1326. Assert.AreEqual (1, dg.Columns.Count, "Columns Count");
  1327. Assert.AreEqual (6, dg.Items.Count, "Items Count");
  1328. Assert.AreEqual ("1", dg.Items[0].Cells[0].Text, "Cell content");
  1329. }
  1330. [Test]
  1331. [NUnit.Framework.Category ("NunitWeb")]
  1332. public void Paging ()
  1333. {
  1334. WebTest t = new WebTest ("DataGrid.aspx");
  1335. t.Invoker = PageInvoker.CreateOnInit (DataGrid_OnInit);
  1336. string html = t.Run ();
  1337. string gridHtml = HtmlDiff.GetControlFromPageHtml (html);
  1338. #if NET_4_0
  1339. string expectedFirstPage = "\n <table cellspacing=\"0\" cellpadding=\"4\" id=\"DataGrid1\" style=\"color:#333333;border-collapse:collapse;\">\r\n\t<tr style=\"color:White;background-color:#990000;font-weight:bold;\">\r\n\t\t<td>ID</td><td>Name</td><td>&nbsp;</td><td>&nbsp;</td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl02$ctl00&#39;,&#39;&#39;)\" style=\"color:White;\">ID</a></td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl02$ctl01&#39;,&#39;&#39;)\" style=\"color:White;\">Name</a></td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl02$ctl02&#39;,&#39;&#39;)\" style=\"color:White;\">Comment</a></td>\r\n\t</tr><tr style=\"color:#333333;background-color:#FFFBD6;\">\r\n\t\t<td>1</td><td>heh1</td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl03$ctl00&#39;,&#39;&#39;)\" style=\"color:#333333;\">Edit</a></td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl03$ctl01&#39;,&#39;&#39;)\" style=\"color:#333333;\">Delete</a></td><td>1</td><td>heh1</td><td>Comment 1</td>\r\n\t</tr><tr style=\"color:#333333;background-color:White;\">\r\n\t\t<td>2</td><td>heh2</td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl04$ctl00&#39;,&#39;&#39;)\" style=\"color:#333333;\">Edit</a></td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl04$ctl01&#39;,&#39;&#39;)\" style=\"color:#333333;\">Delete</a></td><td>2</td><td>heh2</td><td>Comment 2</td>\r\n\t</tr><tr style=\"color:#333333;background-color:#FFFBD6;\">\r\n\t\t<td>3</td><td>heh3</td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl05$ctl00&#39;,&#39;&#39;)\" style=\"color:#333333;\">Edit</a></td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl05$ctl01&#39;,&#39;&#39;)\" style=\"color:#333333;\">Delete</a></td><td>3</td><td>heh3</td><td>Comment 3</td>\r\n\t</tr><tr style=\"color:#333333;background-color:White;\">\r\n\t\t<td>4</td><td>heh4</td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl06$ctl00&#39;,&#39;&#39;)\" style=\"color:#333333;\">Edit</a></td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl06$ctl01&#39;,&#39;&#39;)\" style=\"color:#333333;\">Delete</a></td><td>4</td><td>heh4</td><td>Comment 4</td>\r\n\t</tr><tr style=\"color:#333333;background-color:#FFFBD6;\">\r\n\t\t<td>5</td><td>heh5</td><td><a href=\"javascript:__doPostBack(&#39;DataGrid1$ctl07$ctl00&#39;,&#39;&#39;)\" style=\"color:#333333;\">Edit</a></td><td><a href=\"javascript:_…

Large files files are truncated, but you can click here to view the full file