PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/AODLTest/TextDocumentBaseTests.cs

https://bitbucket.org/chrisc/aodl
C# | 387 lines | 241 code | 20 blank | 126 comment | 3 complexity | f2cc4f985964d2ce9e7c2381a55bbb51 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 AODL.Document.Export.OpenDocument;
  23. using AODL.IO;
  24. using NUnit.Framework;
  25. using AODL.Document.TextDocuments;
  26. using AODL.Document.Styles;
  27. using AODL.Document.Content.Text;
  28. namespace AODLTest
  29. {
  30. [TestFixture]
  31. public class TextDocumentBaseTests
  32. {
  33. [Test]
  34. public void EmptyDocument()
  35. {
  36. //Create a new text document
  37. TextDocument document = new TextDocument();
  38. document.New();
  39. //Save empty
  40. using (IPackageWriter writer = new OnDiskPackageWriter())
  41. {
  42. document.Save(AARunMeFirstAndOnce.outPutFolder + "empty.odt", new OpenDocumentTextExporter(writer));
  43. }
  44. }
  45. [Test]
  46. public void ParagraphSimpleText()
  47. {
  48. //Create a new text document
  49. TextDocument document = new TextDocument();
  50. document.New();
  51. //Create a standard paragraph using the ParagraphBuilder
  52. Paragraph paragraph = ParagraphBuilder.CreateStandardTextParagraph(document);
  53. //Add some simple text
  54. paragraph.TextContent.Add(new SimpleText(document, "Some simple text!"));
  55. //Add the paragraph to the document
  56. document.Content.Add(paragraph);
  57. //Save empty
  58. using (IPackageWriter writer = new OnDiskPackageWriter())
  59. {
  60. document.Save(AARunMeFirstAndOnce.outPutFolder + "simple.odt", new OpenDocumentTextExporter(writer));
  61. }
  62. }
  63. [Test]
  64. public void ParagraphFormatedText()
  65. {
  66. //Create a new text document
  67. TextDocument document = new TextDocument();
  68. document.New();
  69. //Create a standard paragraph using the ParagraphBuilder
  70. Paragraph paragraph = ParagraphBuilder.CreateStandardTextParagraph(document);
  71. //Add some formated text
  72. FormatedText formText = new FormatedText(document, "T1", "Some formated text!");
  73. formText.TextStyle.TextProperties.Bold = "bold";
  74. paragraph.TextContent.Add(formText);
  75. //Add the paragraph to the document
  76. document.Content.Add(paragraph);
  77. //Save empty
  78. using (IPackageWriter writer = new OnDiskPackageWriter())
  79. {
  80. document.Save(AARunMeFirstAndOnce.outPutFolder + "formated.odt", new OpenDocumentTextExporter(writer));
  81. }
  82. }
  83. [Test]
  84. public void NumberedListTest()
  85. {
  86. //Create a new text document
  87. TextDocument document = new TextDocument();
  88. document.New();
  89. //Create a numbered list
  90. List li = new List(document, "L1", ListStyles.Number, "L1P1");
  91. Assert.IsNotNull(li.Node, "Node object must exist!");
  92. Assert.IsNotNull(li.Style, "Style object must exist!");
  93. Assert.IsNotNull(li.ListStyle.ListlevelStyles, "ListLevelStyleCollection must exist!");
  94. Assert.IsTrue(li.ListStyle.ListlevelStyles.Count == 10, "Must exist exactly 10 ListLevelStyle objects!");
  95. Assert.IsNotNull(li.ListStyle.ListlevelStyles[1].ListLevelProperties, "ListLevelProperties object must exist!");
  96. }
  97. [Test]
  98. public void BulletListTest()
  99. {
  100. //Create a new text document
  101. TextDocument document = new TextDocument();
  102. document.New();
  103. //Create a bullet list
  104. List li = new List(document, "L1", ListStyles.Bullet, "L1P1");
  105. Assert.IsNotNull(li.Node, "Node object must exist!");
  106. Assert.IsNotNull(li.Style, "Style object must exist!");
  107. Assert.IsNotNull(li.ListStyle.ListlevelStyles, "ListLevelStyleCollection must exist!");
  108. Assert.IsTrue(li.ListStyle.ListlevelStyles.Count == 10, "Must exist exactly 10 ListLevelStyle objects!");
  109. Assert.IsNotNull(li.ListStyle.ListlevelStyles[1].ListLevelProperties, "ListLevelProperties object must exist!");
  110. }
  111. [Test]
  112. public void ListItemTest()
  113. {
  114. //Create a new text document
  115. TextDocument document = new TextDocument();
  116. document.New();
  117. //Create a numbered list
  118. List li = new List(document, "L1", ListStyles.Bullet, "L1P1");
  119. //Create a new list item
  120. ListItem lit = new ListItem(li);
  121. Assert.IsNotNull(lit.Content, "Content object must exist!");
  122. //Create a paragraph
  123. Paragraph paragraph = ParagraphBuilder.CreateStandardTextParagraph(document);
  124. //Add some text
  125. paragraph.TextContent.Add(new SimpleText(document, "First item"));
  126. //Add paragraph to the list item
  127. lit.Content.Add(paragraph);
  128. //Add the list item
  129. li.Content.Add(lit);
  130. //Add the list
  131. document.Content.Add(li);
  132. //Save document
  133. using (IPackageWriter writer = new OnDiskPackageWriter())
  134. {
  135. document.Save(AARunMeFirstAndOnce.outPutFolder + "list.odt", new OpenDocumentTextExporter(writer));
  136. }
  137. }
  138. [Test]
  139. public void HeadingsTest()
  140. {
  141. //Create a new text document
  142. TextDocument document = new TextDocument();
  143. document.New();
  144. //Create a new Heading
  145. Header header = new Header(document, Headings.Heading);
  146. //Add some header text
  147. header.TextContent.Add(new SimpleText(document, "I'm the first headline"));
  148. //Add header
  149. document.Content.Add(header);
  150. using (IPackageWriter writer = new OnDiskPackageWriter())
  151. {
  152. document.Save(AARunMeFirstAndOnce.outPutFolder + "Heading.odt", new OpenDocumentTextExporter(writer));
  153. }
  154. }
  155. [Test]
  156. public void HeadingFilledWithTextBuilder()
  157. {
  158. string headingText = "Some Heading with\n styles\t and more";
  159. //Create a new text document
  160. TextDocument document = new TextDocument();
  161. document.New();
  162. //Create a new Heading
  163. Header header = new Header(document, Headings.Heading);
  164. //Create a TextCollection from headingText using the TextBuilder
  165. ITextCollection textCol = TextBuilder.BuildTextCollection(document, headingText);
  166. //Add text collection
  167. header.TextContent = textCol;
  168. //Add header
  169. document.Content.Add(header);
  170. using (IPackageWriter writer = new OnDiskPackageWriter())
  171. {
  172. document.Save(AARunMeFirstAndOnce.outPutFolder + "HeadingWithControlCharacter.odt", new OpenDocumentTextExporter(writer));
  173. }
  174. }
  175. [Test]
  176. public void XLinkTest()
  177. {
  178. //Create new TextDocument
  179. TextDocument document = new TextDocument();
  180. document.New();
  181. //Create a new Paragraph
  182. Paragraph para = new Paragraph(document, "P1");
  183. //Create some simple text
  184. SimpleText stext = new SimpleText(document, "Some simple text ");
  185. //Create a XLink
  186. XLink xlink = new XLink(document, "http://www.sourceforge.net", "Sourceforge");
  187. //Add the textcontent
  188. para.TextContent.Add(stext);
  189. para.TextContent.Add(xlink);
  190. //Add paragraph to the document content
  191. document.Content.Add(para);
  192. //Save
  193. using (IPackageWriter writer = new OnDiskPackageWriter())
  194. {
  195. document.Save(AARunMeFirstAndOnce.outPutFolder + "XLink.odt", new OpenDocumentTextExporter(writer));
  196. }
  197. }
  198. [Test]
  199. public void FootnoteText()
  200. {
  201. //Create new TextDocument
  202. TextDocument document = new TextDocument();
  203. document.New();
  204. //Create a new Paragph
  205. Paragraph para = ParagraphBuilder.CreateStandardTextParagraph(document);
  206. //Create some simple Text
  207. para.TextContent.Add(new SimpleText(document, "Some simple text. And I have a footnode"));
  208. //Create a Footnote
  209. para.TextContent.Add(new Footnote(document, "Footer Text", "1", FootnoteType.Footnode));
  210. //Add the paragraph
  211. document.Content.Add(para);
  212. //Save
  213. using (IPackageWriter writer = new OnDiskPackageWriter())
  214. {
  215. document.Save(AARunMeFirstAndOnce.outPutFolder + "footnote.odt", new OpenDocumentTextExporter(writer));
  216. }
  217. }
  218. [Test]
  219. public void IParagraphCollectionBuilderTest()
  220. {
  221. //some text e.g read from a TextBox
  222. string someText = "Max Mustermann\nMustermann Str. 300\n22222 Hamburg\n\n\n\n"
  223. +"Heinz Willi\nDorfstr. 1\n22225 Hamburg\n\n\n\n"
  224. +"Offer for 200 Intel Pentium 4 CPU's\n\n\n\n"
  225. +"Dear Mr. Willi,\n\n\n\n"
  226. +"thank you for your request. \tWe can offer you the 200 Intel Pentium IV 3 Ghz CPU's for a price of 79,80 € per unit."
  227. +"This special offer is valid to 31.10.2005. If you accept, we can deliver within 24 hours.\n\n\n\n"
  228. +"Best regards \nMax Mustermann";
  229. //Create new TextDocument
  230. TextDocument document = new TextDocument();
  231. document.New();
  232. //Use the ParagraphBuilder to split the string into ParagraphCollection
  233. ParagraphCollection pCollection = ParagraphBuilder.CreateParagraphCollection(
  234. document,
  235. someText,
  236. true,
  237. ParagraphBuilder.ParagraphSeperator);
  238. //Add the paragraph collection
  239. foreach(Paragraph paragraph in pCollection)
  240. document.Content.Add(paragraph);
  241. //save
  242. using (IPackageWriter writer = new OnDiskPackageWriter())
  243. {
  244. document.Save(AARunMeFirstAndOnce.outPutFolder + "Letter.odt", new OpenDocumentTextExporter(writer));
  245. }
  246. }
  247. /// <summary>
  248. /// Manipulate a common style.
  249. /// </summary>
  250. [Test]
  251. public void ManipulateACommonStyle()
  252. {
  253. TextDocument document = new TextDocument();
  254. document.New();
  255. Assert.IsTrue(document.CommonStyles.Count > 0, "Common style resp. style templates must be loaded");
  256. //Find a Header template
  257. IStyle style = document.CommonStyles.GetStyleByName("Heading_20_1");
  258. Assert.IsNotNull(style, "Style with name Heading_20_1 must exist");
  259. Assert.IsTrue(style is ParagraphStyle, "style must be a ParagraphStyle");
  260. ((ParagraphStyle)style).TextProperties.FontName = FontFamilies.BroadwayBT;
  261. //Create a header that use the standard style Heading_20_1
  262. Header header = new Header(document, Headings.Heading_20_1);
  263. //Add some text
  264. header.TextContent.Add(new SimpleText(document, "I am the header text and my style template is modified :)"));
  265. //Add header to the document
  266. document.Content.Add(header);
  267. //save the document
  268. using (IPackageWriter writer = new OnDiskPackageWriter())
  269. {
  270. document.Save(AARunMeFirstAndOnce.outPutFolder + "modifiedCommonStyle.odt", new OpenDocumentTextExporter(writer));
  271. }
  272. }
  273. [Test]
  274. public void InsertAndRemoveTest()
  275. {
  276. //Create a new document
  277. TextDocument document = new TextDocument();
  278. document.New();
  279. //Create a standard paragraph
  280. Paragraph paragraph = ParagraphBuilder.CreateStandardTextParagraph(document);
  281. //Create some simple text
  282. SimpleText simpleText = new SimpleText(document, "Some simple text");
  283. //Add the text
  284. paragraph.TextContent.Add(simpleText);
  285. Assert.IsNotNull(paragraph.TextContent, "Must be contain one element.");
  286. //Remove the simple text
  287. paragraph.TextContent.Remove(simpleText);
  288. Assert.IsNotNull(paragraph.TextContent, "Must be empty");
  289. Assert.IsTrue(paragraph.Node.Value.Length == 0, "Node from simple text must be removed.");
  290. }
  291. [Test]
  292. public void TestParagraphCloning()
  293. {
  294. TextDocument document = new TextDocument();
  295. document.New();
  296. Paragraph paragraph = ParagraphBuilder.CreateStandardTextParagraph(document);
  297. paragraph.TextContent.Add(new SimpleText(document, "Some text"));
  298. Paragraph paragraphClone = (Paragraph)paragraph.Clone();
  299. Assert.AreNotEqual(paragraph.Node, paragraphClone.Node, "Should be cloned and not equal.");
  300. Assert.AreEqual(paragraph.TextContent[0].GetType(), paragraphClone.TextContent[0].GetType(), "Should be cloned and equal.");
  301. }
  302. [Test]
  303. public void TestCloning()
  304. {
  305. TextDocument document = new TextDocument();
  306. document.New();
  307. Paragraph paragraph = ParagraphBuilder.CreateStandardTextParagraph(document);
  308. paragraph.TextContent.Add(new SimpleText(document, "Some text"));
  309. Paragraph paragraphClone = (Paragraph)paragraph.Clone();
  310. //Assert.AreEqual(paragraph.Node, paragraphClone.Node, "Should be cloned and not equal.");
  311. //Assert.AreEqual(paragraph.TextContent[0].GetType(), paragraphClone.TextContent[0].GetType(), "Should be cloned and equal.");
  312. ParagraphStyle paragraphStyle = new ParagraphStyle(document, "P1");
  313. paragraphStyle.TextProperties.Bold = "bold";
  314. //Add paragraph style to the document,
  315. //only automaticaly created styles will be added also automaticaly
  316. document.Styles.Add(paragraphStyle);
  317. paragraphClone.ParagraphStyle = paragraphStyle;
  318. //Clone the clone
  319. Paragraph paragraphClone2 = (Paragraph)paragraphClone.Clone();
  320. Assert.AreNotEqual(paragraphClone2.Node, paragraphClone.Node, "Should be cloned and not equal.");
  321. Assert.AreEqual(paragraphClone2.TextContent[0].GetType(), paragraphClone.TextContent[0].GetType(), "Should be cloned and equal.");
  322. //Cloning of styles isn't supported!
  323. Assert.AreSame(paragraphClone2.ParagraphStyle, paragraphClone.ParagraphStyle, "Must be same style object. Styles have to be cloned explicitly.");
  324. document.Content.Add(paragraph);
  325. document.Content.Add(paragraphClone);
  326. document.Content.Add(paragraphClone2);
  327. using (IPackageWriter writer = new OnDiskPackageWriter())
  328. {
  329. document.Save(AARunMeFirstAndOnce.outPutFolder + "clonedParagraphs.odt", new OpenDocumentTextExporter(writer));
  330. }
  331. }
  332. }
  333. }
  334. /*
  335. * $Log: TextDocumentBaseTests.cs,v $
  336. * Revision 1.5 2008/04/29 15:39:59 mt
  337. * new copyright header
  338. *
  339. * Revision 1.4 2008/02/08 07:12:18 larsbehr
  340. * - added initial chart support
  341. * - several bug fixes
  342. *
  343. * Revision 1.3 2007/04/08 16:51:09 larsbehr
  344. * - finished master pages and styles for text documents
  345. * - several bug fixes
  346. *
  347. * Revision 1.2 2007/03/15 15:29:03 larsbehr
  348. * - added IContent inserting/removing for page header/footer
  349. * - test for new masterpage styles
  350. *
  351. * Revision 1.1 2007/02/25 09:01:28 larsbehr
  352. * initial checkin, import from Sourceforge.net to OpenOffice.org
  353. *
  354. * Revision 1.3 2006/02/02 21:55:59 larsbm
  355. * - Added Clone object support for many AODL object types
  356. * - New Importer implementation PlainTextImporter and CsvImporter
  357. * - New tests
  358. *
  359. * Revision 1.2 2006/01/29 18:52:14 larsbm
  360. * - Added support for common styles (style templates in OpenOffice)
  361. * - Draw TextBox import and export
  362. * - DrawTextBox html export
  363. *
  364. * Revision 1.1 2006/01/29 11:26:02 larsbm
  365. * - Changes for the new version. 1.2. see next changelog for details
  366. *
  367. */