PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/AODLTest/TableTest.cs

https://bitbucket.org/chrisc/aodl
C# | 268 lines | 157 code | 66 blank | 45 comment | 6 complexity | 969f2345d5e2f1c2902ad14c97712570 MD5 | raw file
  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
  4. *
  5. * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * Use is subject to license terms.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  10. * use this file except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
  12. * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. *
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. ************************************************************************/
  22. using System;
  23. using System.Xml;
  24. using System.Drawing;
  25. using AODL.Collections;
  26. using AODL.TextDocument;
  27. using AODL.TextDocument.Content;
  28. using AODL.TextDocument.Style;
  29. using AODL.TextDocument.Style.Properties;
  30. using NUnit.Framework;
  31. namespace AODLTest
  32. {
  33. [TestFixture]
  34. public class TableTest
  35. {
  36. [Test]
  37. public void TableTest1()
  38. {
  39. TextDocument td = new TextDocument();
  40. td.New();
  41. Table t = new Table(td, "table1");
  42. Assert.IsNotNull(t.Node, "Node must exist");
  43. Assert.IsNotNull(t.Style, "Style must exist");
  44. Assert.AreEqual("table1", t.Style.Name, "Name must be table1");
  45. Assert.IsNotNull(((TableStyle)t.Style).Properties, "Must exist!");
  46. Assert.AreEqual("16.99cm", ((TableStyle)t.Style).Properties.Width, "Must be the default 16.99cm");
  47. t.Init(2, 2, 16.99);
  48. Assert.IsNotNull(t.Columns, "Columncollection must exist!");
  49. Assert.IsTrue(t.Columns.Count == 2, "Must be 2 columns!");
  50. Assert.AreEqual("table1.A", t.Columns[0].Style.Name, "Must be table1.A!");
  51. Assert.AreEqual("table1.B", t.Columns[1].Style.Name, "Must be table1.B!");
  52. Assert.IsNotNull(t.Rows, "RowCollection must exist!");
  53. Assert.IsTrue(t.Rows.Count == 2, "Must be 2 rows");
  54. Assert.AreEqual("table1.1", t.Rows[0].Stylename, "Must be table1.1");
  55. Assert.AreEqual("table1.2", t.Rows[1].Stylename, "Must be table1.2");
  56. Assert.IsTrue(t.Rows[0].Cells.Count == 2, "Must be 2 Cells wihtin this row!");
  57. Assert.AreEqual("table1.A11", t.Rows[0].Cells[0].Stylename, "Must be table1.A1");
  58. foreach(Row r in t.Rows)
  59. foreach(Cell c in r.Cells)
  60. c.InsertText("Hallo");
  61. foreach(Row r in t.Rows)
  62. foreach(Cell c in r.Cells)
  63. Assert.IsTrue(c.Content.Count == 1, "Must be all 1");
  64. td.Content.Add(t);
  65. td.SaveTo("table1.odt");
  66. }
  67. [Test]
  68. public void CellParagraphTest()
  69. {
  70. TextDocument doc = new TextDocument();
  71. doc.New();
  72. Table table = new Table(doc, "table1");
  73. table.Init(5, 3, 16.99);
  74. foreach(Row r in table.Rows)
  75. foreach(Cell c in r.Cells)
  76. c.InsertText("Hello");
  77. Paragraph p = new Paragraph(doc, "P1");
  78. FormatedText ft = new FormatedText(p, "T1", "Hello World");
  79. ((TextStyle)ft.Style).Properties.Italic = "italic";
  80. p.TextContent.Add(ft);
  81. table.Rows[0].Cells[0].Content.Add(p);
  82. doc.Content.Add(table);
  83. doc.SaveTo("tablewithstyles.odt");
  84. }
  85. [Test]
  86. public void LongTableTest()
  87. {
  88. TextDocument doc = new TextDocument();
  89. doc.New();
  90. Table table = new Table(doc, "table1");
  91. table.Init(150, 5, 16.99);
  92. foreach(Row r in table.Rows)
  93. foreach(Cell c in r.Cells)
  94. c.InsertText("Hello");
  95. doc.Content.Add(table);
  96. doc.SaveTo("tablelong.odt");
  97. }
  98. [Test]
  99. public void CellWithListTest()
  100. {
  101. TextDocument doc = new TextDocument();
  102. doc.New();
  103. Table table = new Table(doc, "table1");
  104. table.Init(5, 3, 16.99);
  105. ((CellStyle)table.Rows[0].Cells[0].Style).CellProperties.BackgroundColor = Colors.GetColor(Color.Tomato);
  106. List li = new List(doc, "L1", ListStyles.Bullet, "L1P1");
  107. ListItem lit = new ListItem(li);
  108. lit.Paragraph.TextContent.Add(new SimpleText(lit, "Hello"));
  109. li.Content.Add(lit);
  110. //The ListItem will become a inner list !!
  111. lit = new ListItem(li);
  112. lit.Paragraph.TextContent.Add(new SimpleText(lit, "Hello Again"));
  113. //Inner List - see the constrctor usage !
  114. List liinner = new List(doc, li);
  115. Assert.IsNull(liinner.Style, "Style must be null! The inner list inherited his style from the outer list!");
  116. ListItem litinner = new ListItem(liinner);
  117. litinner.Paragraph.TextContent.Add(new SimpleText(lit, "Hello i'm in the inner list"));
  118. liinner.Content.Add(litinner);
  119. //Add the inner list to ListItem lit
  120. lit.Content.Add(liinner);
  121. //Add the ListItem with inner list inside
  122. li.Content.Add(lit);
  123. table.Rows[0].Cells[0].Content.Add(li);
  124. doc.Content.Add(table);
  125. doc.SaveTo("tablewithList.odt");
  126. }
  127. [Test]
  128. public void RowHeaderTest()
  129. {
  130. TextDocument doc = new TextDocument();
  131. doc.New();
  132. Table table = new Table(doc, "table1");
  133. table.Init(5, 2, 16.99, true);
  134. //Set the row header
  135. if(table.RowHeader != null)
  136. {
  137. //Headline
  138. table.RowHeader.RowCollection[0].Cells[0].InsertText("Application");
  139. table.RowHeader.RowCollection[0].Cells[1].InsertText("Short cut");
  140. }
  141. foreach(Row r in table.Rows)
  142. foreach(Cell c in r.Cells)
  143. c.InsertText("Hello");
  144. doc.Content.Add(table);
  145. doc.SaveTo("tableheader.odt");
  146. }
  147. [Test]
  148. public void CellSpanTest()
  149. {
  150. TextDocument doc = new TextDocument();
  151. doc.New();
  152. Table table = new Table(doc, "table1");
  153. table.Init(5, 2, 16.99);
  154. //Create a new row within this table and
  155. //set the cellspan to 2
  156. Row row = new Row(table, "");
  157. //Create a real cell
  158. Cell cell = new Cell(row, "table1.ZZ1");
  159. //Set cell span
  160. cell.ColumnRepeating = "2";
  161. //Set the border
  162. ((CellStyle)cell.Style).CellProperties.Border = Border.NormalSolid;
  163. //add some content to this cell
  164. cell.Content.Add(new Paragraph(doc,
  165. ParentStyles.Standard,
  166. "Hello I'm merged over two cells!"));
  167. //add cell to the row
  168. row.Cells.Add(cell);
  169. //we have to add one CellSpan object, because the
  170. //table has original 2 columns
  171. row.CellSpans.Add(new CellSpan(row));
  172. //at least at this row the table
  173. table.Rows.Add(row);
  174. //add the table to the document
  175. doc.Content.Add(table);
  176. //save it to the disk
  177. doc.SaveTo("tablecellspan.odt");
  178. }
  179. [Test]
  180. public void MergeCellsTest()
  181. {
  182. TextDocument doc = new TextDocument();
  183. doc.New();
  184. Table table = new Table(doc, "table1");
  185. table.Init(4, 5, 16.99);
  186. foreach(Row r in table.Rows)
  187. foreach(Cell c in r.Cells)
  188. c.InsertText("Hello");
  189. //Merge the first cell of the first row and set mergeContent, so
  190. //all content from the merged cells will move
  191. //to the first unmerged cell
  192. table.Rows[0].MergeCells(0, 3, true);
  193. //Merge the first cell of the third row
  194. //set mergeContent and merge all cells
  195. //The result will be that row 3 only have one cell!
  196. table.Rows[2].MergeCells(0, 5, true);
  197. doc.Content.Add(table);
  198. doc.SaveTo("tablemergedcell.odt");
  199. }
  200. }
  201. }
  202.