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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import javax.xml.transform.stream.StreamResult;
  28. import org.junit.Assert;
  29. import org.junit.Test;
  30. import org.xml.sax.Attributes;
  31. import org.xml.sax.SAXException;
  32. import static org.mockito.Mockito.mock;
  33. import static org.mockito.Mockito.when;
  34. import org.apache.xmlgraphics.util.QName;
  35. import org.apache.fop.accessibility.StructureTreeElement;
  36. import org.apache.fop.apps.FOUserAgent;
  37. import org.apache.fop.apps.FopFactory;
  38. import org.apache.fop.fonts.FontInfo;
  39. import org.apache.fop.render.intermediate.IFContext;
  40. import org.apache.fop.render.intermediate.IFException;
  41. import org.apache.fop.render.intermediate.extensions.AbstractAction;
  42. import org.apache.fop.render.intermediate.extensions.DocumentNavigationExtensionConstants;
  43. import org.apache.fop.render.intermediate.extensions.DocumentNavigationHandler;
  44. import org.apache.fop.render.intermediate.extensions.GoToXYAction;
  45. import org.apache.fop.render.pdf.PDFDocumentHandler;
  46. import org.apache.fop.render.pdf.PDFDocumentNavigationHandler;
  47. public class DocumentNavigationHandlerTestCase {
  48. @Test
  49. public void testGotoXY() throws SAXException, IFException {
  50. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  51. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  52. documentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  53. documentHandler.setFontInfo(new FontInfo());
  54. documentHandler.startDocument();
  55. documentHandler.startPage(0, "", "", new Dimension());
  56. documentHandler.endPage();
  57. int currentPage = 1;
  58. documentHandler.startPage(currentPage, "", "", new Dimension());
  59. final List<GoToXYAction> goToXYActions = new ArrayList<GoToXYAction>();
  60. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) {
  61. public void addResolvedAction(AbstractAction action) throws IFException {
  62. super.addResolvedAction(action);
  63. goToXYActions.add((GoToXYAction) action);
  64. }
  65. };
  66. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  67. new HashMap<String, StructureTreeElement>());
  68. QName xy = DocumentNavigationExtensionConstants.GOTO_XY;
  69. Attributes attributes = mock(Attributes.class);
  70. when(attributes.getValue("page-index")).thenReturn("0");
  71. when(attributes.getValue("x")).thenReturn("0");
  72. when(attributes.getValue("y")).thenReturn("0");
  73. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  74. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  75. documentHandler.endPage();
  76. //Since user may merge IF files we want to use current page
  77. Assert.assertEquals(goToXYActions.get(0).getPageIndex(), currentPage);
  78. }
  79. @Test
  80. public void testGotoXYUniqueLinks() throws IFException, SAXException {
  81. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  82. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  83. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  84. documentHandler.setResult(new StreamResult(bos));
  85. documentHandler.setFontInfo(new FontInfo());
  86. documentHandler.startDocument();
  87. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler);
  88. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  89. new HashMap<String, StructureTreeElement>());
  90. QName xy = DocumentNavigationExtensionConstants.GOTO_XY;
  91. Attributes attributes = mock(Attributes.class);
  92. when(attributes.getValue("page-index")).thenReturn("0");
  93. when(attributes.getValue("x")).thenReturn("0");
  94. when(attributes.getValue("y")).thenReturn("0");
  95. documentHandler.startPage(0, "", "", new Dimension());
  96. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  97. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  98. documentHandler.endPage();
  99. documentHandler.startPage(1, "", "", new Dimension());
  100. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  101. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  102. documentHandler.endPage();
  103. Iterator<String> i = Arrays.asList(bos.toString().split("\n")).iterator();
  104. List<String> pageLink = new ArrayList<String>();
  105. while (i.hasNext()) {
  106. if (i.next().equals("/S /GoTo")) {
  107. pageLink.add(i.next());
  108. }
  109. }
  110. Assert.assertEquals(pageLink.size(), 2);
  111. Assert.assertFalse(pageLink.get(0).equals(pageLink.get(1)));
  112. }
  113. }