PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Branches/0.9.5-DeltaShell/test/Common_/DelftTools.Tests/Controls/Swf/Table/TableViewPasteControllerTest.cs

#
C# | 283 lines | 190 code | 54 blank | 39 comment | 0 complexity | 0413f1c3041c722d4df2317dee005033 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.ComponentModel;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. using DelftTools.Controls;
  7. using DelftTools.Controls.Swf.Table;
  8. using DelftTools.Tests.Controls.Swf.Table.TestClasses;
  9. using DelftTools.TestUtils;
  10. using NUnit.Framework;
  11. using SortOrder = DelftTools.Controls.SortOrder;
  12. namespace DelftTools.Tests.Controls.Swf.Table
  13. {
  14. [TestFixture]
  15. public class TableViewPasteControllerTest
  16. {
  17. [Test]
  18. public void PasteFailsOnSortedColumn()
  19. {
  20. TableView tableView = GetTableViewWithTwoStringColumnsAndOneRow();
  21. //hook up a copy paste controller
  22. var tableViewCopyPasteController = new TableViewPasteController(tableView);
  23. //sort on the second column
  24. tableView.Columns[1].SortOrder = SortOrder.Ascending;
  25. //setup eventhandler for failed paste checking error message
  26. int callCount = 0;
  27. tableViewCopyPasteController.PasteFailed += (s, e) =>
  28. {
  29. Assert.AreEqual("Cannot paste into sorted column",
  30. e.Value);
  31. callCount++;
  32. };
  33. //action! paste in the first column..second column is hit so should fail
  34. tableView.SelectCells(0, 0, 0, 0);
  35. tableViewCopyPasteController.PasteLines(new[] { "kaas\tmelk" });
  36. //we should have failed
  37. Assert.AreEqual(1,callCount);
  38. }
  39. [Test]
  40. public void PasteFailsInFilterGrid()
  41. {
  42. TableView tableView = GetTableViewWithTwoStringColumnsAndOneRow();
  43. //hook up a copy paste controller
  44. var tableViewCopyPasteController = new TableViewPasteController(tableView);
  45. //filter first column
  46. tableView.Columns[0].FilterString = "[Name] Like 'Jaap%'";
  47. //setup eventhandler for failed paste checking error message
  48. int callCount = 0;
  49. tableViewCopyPasteController.PasteFailed += (s, e) =>
  50. {
  51. Assert.AreEqual("Cannot paste into filtered tableview.",
  52. e.Value);
  53. callCount++;
  54. };
  55. //action! paste in the first column..
  56. tableView.SelectCells(0, 0, 0, 0);
  57. tableViewCopyPasteController.PasteLines(new[] { "kaas" });
  58. //we should have failed
  59. Assert.AreEqual(1, callCount);
  60. }
  61. [Test]
  62. public void PasteDoesNotAddNewRowsToSortedTableView()
  63. {
  64. TableView tableView = GetTableViewWithTwoStringColumnsAndOneRow();
  65. //hook up a copy paste controller
  66. var tableViewCopyPasteController = new TableViewPasteController(tableView);
  67. //select the first cell of the second column
  68. tableView.SelectCells(0, 1, 0, 1);
  69. //sort fist column
  70. tableView.Columns[0].SortOrder = SortOrder.Ascending;
  71. //action paste a possible 3 lines
  72. tableViewCopyPasteController.PasteLines(new[] {"kaas\tmelk","noot\tmies","appel\tflap"});
  73. //assert rowcount is still one..no new rows are added on a sorted grid
  74. Assert.AreEqual(1,tableView.RowCount);
  75. //make sure we did something
  76. Assert.AreEqual("kaas",tableView.GetRowCellValue(0,1));
  77. }
  78. [Test]
  79. public void PasteFillsSelection()
  80. {
  81. var list = new BindingList<Person>()
  82. {
  83. new Person {Name = "jan", Age = 25},
  84. new Person {Name = "fon", Age = 33},
  85. new Person {Name = "peer", Age = 25},
  86. new Person {Name = "fo", Age = 33}
  87. };
  88. var tableView = new TableView {Data = list};
  89. //select all cells in first column
  90. tableView.SelectCells(0, 0, 3, 0);
  91. //hook up a copy paste controller
  92. var tableViewCopyPasteController = new TableViewPasteController(tableView);
  93. //this should set the first column to kees,anton,kees,anton
  94. tableViewCopyPasteController.PasteLines(new[] {"kees", "anton"});
  95. //asert the names are now like this
  96. Assert.AreEqual(new[] {"kees", "anton", "kees", "anton"}, list.Select(p => p.Name).ToArray());
  97. }
  98. [Test]
  99. public void PasteFailsIntoNonSquareSelection()
  100. {
  101. var list = new BindingList<Person>()
  102. {
  103. new Person {Name = "jan", Age = 25},
  104. new Person {Name = "fon", Age = 33},
  105. new Person {Name = "peer", Age = 25},
  106. new Person {Name = "fo", Age = 33}
  107. };
  108. var tableView = new TableView { Data = list };
  109. //make a diagonal selection
  110. tableView.SelectedCells.Add(new TableViewCell(0,0));
  111. tableView.SelectedCells.Add(new TableViewCell(1, 1));
  112. var tableViewCopyPasteController = new TableViewPasteController(tableView);
  113. int callCount = 0;
  114. tableViewCopyPasteController.PasteFailed += (s, e) => {
  115. callCount++;
  116. Assert.AreEqual(
  117. "Cannot paste into non rectangular selection",
  118. e.Value);
  119. };
  120. //this should a paste failed event
  121. tableViewCopyPasteController.PasteLines(new[] { "kees", "anton" });
  122. Assert.AreEqual(1,callCount);
  123. }
  124. [Test]
  125. public void CopyPasteEnumInBindingList()
  126. {
  127. var list = new BindingList<ClassWithEnum>
  128. {
  129. new ClassWithEnum{Type = FruitType.Banaan},
  130. new ClassWithEnum{Type = FruitType.Peer}
  131. };
  132. var tableView = new TableView { Data = list };
  133. tableView.SetEnumComboboxEditor(typeof(FruitType),0);
  134. var tableViewCopyPasteController = new TableViewPasteController(tableView);
  135. //select 2nd row
  136. tableView.SelectCells(1,0,1,0);
  137. //paste twee bananen en een peer ;)
  138. tableViewCopyPasteController.PasteLines(new[]{"Banaan","Banaan","Peer" });
  139. //we should have 3 bananas and a pear
  140. Assert.AreEqual("Banaan","Banaan","Banaan","Peer",list.Select(c=>c.Type).ToArray());
  141. }
  142. [Test]
  143. public void PasteTakesInvisibleColumnsIntoAccount()
  144. {
  145. //if a possible paste is too big it is important to not exceed the visiblecolcount - 1
  146. var list = new BindingList<Person>()
  147. {
  148. new Person {Name = "jan", Age = 25},
  149. new Person {Name = "fon", Age = 33},
  150. new Person {Name = "peer", Age = 25},
  151. new Person {Name = "fo", Age = 33}
  152. };
  153. var tableView = new TableView {Data = list};
  154. //hide the age column
  155. tableView.Columns[1].Visible = false;
  156. //select top left
  157. tableView.SelectCells(0, 0, 0, 0);
  158. var tableViewCopyPasteController = new TableViewPasteController(tableView);
  159. //paste some non sense
  160. tableViewCopyPasteController.PasteLines(new[] {"kees\tkan\twel"});
  161. //first person should be kees now
  162. Assert.AreEqual("kees", list[0].Name);
  163. }
  164. [Test]
  165. public void PasteIntoGridBoundToDataTable()
  166. {
  167. var tableWithTwoStrings = new DataTable();
  168. tableWithTwoStrings.Columns.Add("A", typeof(string));
  169. tableWithTwoStrings.Columns.Add("B", typeof(string));
  170. tableWithTwoStrings.Rows.Add("a", "b");
  171. var tableView = new TableView { Data = tableWithTwoStrings };
  172. Clipboard.SetText(string.Format("{0}\t{1}" + Environment.NewLine + "{0}\t{1}", "oe", "oe1"));
  173. tableView.PasteClipboardContents();
  174. //WindowsFormsTestHelper.ShowModal(tableView);
  175. //should overwrite existing row and add another one
  176. Assert.AreEqual(2, tableWithTwoStrings.Rows.Count);
  177. Assert.AreEqual("oe", tableWithTwoStrings.Rows[0][0]);
  178. Assert.AreEqual("oe1", tableWithTwoStrings.Rows[0][1]);
  179. Assert.AreEqual("oe", tableWithTwoStrings.Rows[1][0]);
  180. Assert.AreEqual("oe1", tableWithTwoStrings.Rows[1][1]);
  181. }
  182. [Test]
  183. public void PastingIntoReadOnlyCellsDoesNotChangeTheValues()
  184. {
  185. var tableWithTwoStrings = new DataTable();
  186. tableWithTwoStrings.Columns.Add("A", typeof(string));
  187. tableWithTwoStrings.Columns.Add("B", typeof(string));
  188. tableWithTwoStrings.Rows.Add("a", "b");
  189. var tableView = new TableView { Data = tableWithTwoStrings };
  190. //values in the first column are read only
  191. tableView.ReadOnlyCellFilter = delegate (TableViewCell cell) { return cell.ColumnIndex== 0; };
  192. //select top row
  193. tableView.SelectCells(0, 0, 0, 1);
  194. var tableViewCopyPasteController = new TableViewPasteController(tableView);
  195. //paste some non sense.
  196. tableViewCopyPasteController.PasteLines(new[] { "c\td" });
  197. //only column b should have changed
  198. Assert.AreEqual("a", tableWithTwoStrings.Rows[0][0]);
  199. Assert.AreEqual("d", tableWithTwoStrings.Rows[0][1]);
  200. }
  201. [Test]
  202. public void PasteIntoARowSelection()
  203. {
  204. var tableWithTwoStrings = new DataTable();
  205. tableWithTwoStrings.Columns.Add("A", typeof(string));
  206. tableWithTwoStrings.Columns.Add("B", typeof(string));
  207. tableWithTwoStrings.Rows.Add("a", "b");
  208. var tableView = new TableView { Data = tableWithTwoStrings };
  209. var tableViewCopyPasteController = new TableViewPasteController(tableView);
  210. //select top row
  211. tableView.SelectCells(0, 0, 0, 1);
  212. //paste some non sense.
  213. tableViewCopyPasteController.PasteLines(new[] { "c" });
  214. //first line should now be [c c]
  215. Assert.AreEqual("c", tableWithTwoStrings.Rows[0][0]);
  216. Assert.AreEqual("c", tableWithTwoStrings.Rows[0][1]);
  217. }
  218. private static TableView GetTableViewWithTwoStringColumnsAndOneRow()
  219. {
  220. var tableView = new TableView();
  221. var tableWithTwoStrings = new DataTable();
  222. tableWithTwoStrings.Columns.Add("Name", typeof(string));
  223. tableWithTwoStrings.Columns.Add("B", typeof(string));
  224. tableWithTwoStrings.Rows.Add("a", "b");
  225. tableView.Data = tableWithTwoStrings;
  226. return tableView;
  227. }
  228. }
  229. }