You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocumentNavigationHandlerTestCase.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.extensions;
  19. import java.awt.Dimension;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.Collection;
  27. import java.util.HashMap;
  28. import java.util.Iterator;
  29. import java.util.List;
  30. import javax.xml.transform.stream.StreamResult;
  31. import org.junit.Assert;
  32. import org.junit.Test;
  33. import org.xml.sax.Attributes;
  34. import org.xml.sax.SAXException;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.when;
  37. import org.apache.xmlgraphics.util.QName;
  38. import org.apache.fop.accessibility.StructureTreeElement;
  39. import org.apache.fop.apps.FOUserAgent;
  40. import org.apache.fop.apps.FopFactory;
  41. import org.apache.fop.fonts.FontInfo;
  42. import org.apache.fop.pdf.PDFLinearizationTestCase;
  43. import org.apache.fop.pdf.PDFVTTestCase;
  44. import org.apache.fop.render.intermediate.IFContext;
  45. import org.apache.fop.render.intermediate.IFException;
  46. import org.apache.fop.render.intermediate.extensions.AbstractAction;
  47. import org.apache.fop.render.intermediate.extensions.Bookmark;
  48. import org.apache.fop.render.intermediate.extensions.BookmarkTree;
  49. import org.apache.fop.render.intermediate.extensions.DocumentNavigationExtensionConstants;
  50. import org.apache.fop.render.intermediate.extensions.DocumentNavigationHandler;
  51. import org.apache.fop.render.intermediate.extensions.GoToXYAction;
  52. import org.apache.fop.render.pdf.PDFDocumentHandler;
  53. import org.apache.fop.render.pdf.PDFDocumentNavigationHandler;
  54. public class DocumentNavigationHandlerTestCase {
  55. @Test
  56. public void testGotoXY() throws SAXException, IFException {
  57. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  58. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  59. documentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  60. documentHandler.setFontInfo(new FontInfo());
  61. documentHandler.startDocument();
  62. documentHandler.startPage(0, "", "", new Dimension());
  63. documentHandler.endPage();
  64. int currentPage = 1;
  65. documentHandler.startPage(currentPage, "", "", new Dimension());
  66. final List<GoToXYAction> goToXYActions = new ArrayList<GoToXYAction>();
  67. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) {
  68. public void addResolvedAction(AbstractAction action) throws IFException {
  69. super.addResolvedAction(action);
  70. goToXYActions.add((GoToXYAction) action);
  71. }
  72. };
  73. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  74. new HashMap<String, StructureTreeElement>());
  75. QName xy = DocumentNavigationExtensionConstants.GOTO_XY;
  76. Attributes attributes = mock(Attributes.class);
  77. when(attributes.getValue("page-index")).thenReturn("0");
  78. when(attributes.getValue("x")).thenReturn("0");
  79. when(attributes.getValue("y")).thenReturn("0");
  80. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  81. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  82. documentHandler.endPage();
  83. //Since user may merge IF files we want to use current page
  84. Assert.assertEquals(goToXYActions.get(0).getPageIndex(), currentPage);
  85. }
  86. @Test
  87. public void testGotoXYPrevousPage() throws SAXException, IFException, IOException {
  88. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  89. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  90. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  91. documentHandler.setResult(new StreamResult(bos));
  92. documentHandler.setFontInfo(new FontInfo());
  93. documentHandler.startDocument();
  94. documentHandler.startPage(0, "", "", new Dimension());
  95. documentHandler.endPage();
  96. documentHandler.startPage(1, "", "", new Dimension());
  97. final List<GoToXYAction> goToXYActions = new ArrayList<GoToXYAction>();
  98. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) {
  99. public void addResolvedAction(AbstractAction action) throws IFException {
  100. super.addResolvedAction(action);
  101. goToXYActions.add((GoToXYAction) action);
  102. }
  103. };
  104. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  105. new HashMap<String, StructureTreeElement>());
  106. QName xy = DocumentNavigationExtensionConstants.GOTO_XY;
  107. Attributes attributes = mock(Attributes.class);
  108. when(attributes.getValue("page-index")).thenReturn("0");
  109. when(attributes.getValue("page-index-relative")).thenReturn("-1");
  110. when(attributes.getValue("x")).thenReturn("0");
  111. when(attributes.getValue("y")).thenReturn("0");
  112. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  113. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  114. documentHandler.endPage();
  115. documentHandler.endDocument();
  116. Assert.assertEquals(goToXYActions.get(0).getPageIndex(), 0);
  117. Collection<StringBuilder> objs = PDFLinearizationTestCase.readObjs(
  118. new ByteArrayInputStream(bos.toByteArray())).values();
  119. String pages = PDFVTTestCase.getObj(objs, "/Type /Pages");
  120. String action = PDFVTTestCase.getObj(objs, "/Type /Action");
  121. String pageRef = action.split("\\[")[1].split(" /XYZ")[0];
  122. Assert.assertTrue(pageRef.endsWith(" 0 R"));
  123. Assert.assertTrue(pages.contains("/Kids [" + pageRef));
  124. }
  125. @Test
  126. public void testGotoXYUniqueLinks() throws IFException, SAXException {
  127. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  128. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  129. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  130. documentHandler.setResult(new StreamResult(bos));
  131. documentHandler.setFontInfo(new FontInfo());
  132. documentHandler.startDocument();
  133. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler);
  134. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  135. new HashMap<String, StructureTreeElement>());
  136. QName xy = DocumentNavigationExtensionConstants.GOTO_XY;
  137. Attributes attributes = mock(Attributes.class);
  138. when(attributes.getValue("page-index")).thenReturn("0");
  139. when(attributes.getValue("x")).thenReturn("0");
  140. when(attributes.getValue("y")).thenReturn("0");
  141. documentHandler.startPage(0, "", "", new Dimension());
  142. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  143. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  144. documentHandler.endPage();
  145. documentHandler.startPage(1, "", "", new Dimension());
  146. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  147. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  148. documentHandler.endPage();
  149. Iterator<String> i = Arrays.asList(bos.toString().split("\n")).iterator();
  150. List<String> pageLink = new ArrayList<String>();
  151. while (i.hasNext()) {
  152. if (i.next().equals("/S /GoTo")) {
  153. pageLink.add(i.next());
  154. }
  155. }
  156. Assert.assertEquals(pageLink.size(), 2);
  157. Assert.assertFalse(pageLink.get(0).equals(pageLink.get(1)));
  158. }
  159. @Test
  160. public void testBookmarkGotoXY() throws SAXException, IFException {
  161. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  162. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  163. documentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  164. documentHandler.setFontInfo(new FontInfo());
  165. documentHandler.startDocument();
  166. documentHandler.startPage(0, "", "", new Dimension());
  167. documentHandler.endPage();
  168. int currentPage = 1;
  169. documentHandler.startPage(currentPage, "", "", new Dimension());
  170. final List<BookmarkTree> trees = new ArrayList<BookmarkTree>();
  171. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) {
  172. public void renderBookmarkTree(BookmarkTree tree) throws IFException {
  173. trees.add(tree);
  174. }
  175. };
  176. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  177. new HashMap<String, StructureTreeElement>());
  178. Attributes attributes = mock(Attributes.class);
  179. when(attributes.getValue("page-index")).thenReturn("0");
  180. when(attributes.getValue("x")).thenReturn("0");
  181. when(attributes.getValue("y")).thenReturn("0");
  182. for (QName q : Arrays.asList(DocumentNavigationExtensionConstants.BOOKMARK_TREE,
  183. DocumentNavigationExtensionConstants.BOOKMARK,
  184. DocumentNavigationExtensionConstants.GOTO_XY)) {
  185. navigationHandler.startElement(q.getNamespaceURI(), q.getLocalName(), null, attributes);
  186. }
  187. for (QName q : Arrays.asList(DocumentNavigationExtensionConstants.GOTO_XY,
  188. DocumentNavigationExtensionConstants.BOOKMARK,
  189. DocumentNavigationExtensionConstants.BOOKMARK_TREE)) {
  190. navigationHandler.endElement(q.getNamespaceURI(), q.getLocalName(), null);
  191. }
  192. documentHandler.endPage();
  193. Bookmark b = (Bookmark) trees.get(0).getBookmarks().get(0);
  194. GoToXYAction a = (GoToXYAction) b.getAction();
  195. Assert.assertEquals(a.getPageIndex(), 0);
  196. }
  197. }