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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.HashMap;
  24. import java.util.List;
  25. import javax.xml.transform.stream.StreamResult;
  26. import org.junit.Assert;
  27. import org.junit.Test;
  28. import org.xml.sax.Attributes;
  29. import org.xml.sax.SAXException;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.when;
  32. import org.apache.xmlgraphics.util.QName;
  33. import org.apache.fop.accessibility.StructureTreeElement;
  34. import org.apache.fop.apps.FOUserAgent;
  35. import org.apache.fop.apps.FopFactory;
  36. import org.apache.fop.fonts.FontInfo;
  37. import org.apache.fop.render.intermediate.IFContext;
  38. import org.apache.fop.render.intermediate.IFException;
  39. import org.apache.fop.render.intermediate.extensions.AbstractAction;
  40. import org.apache.fop.render.intermediate.extensions.DocumentNavigationExtensionConstants;
  41. import org.apache.fop.render.intermediate.extensions.DocumentNavigationHandler;
  42. import org.apache.fop.render.intermediate.extensions.GoToXYAction;
  43. import org.apache.fop.render.pdf.PDFDocumentHandler;
  44. import org.apache.fop.render.pdf.PDFDocumentNavigationHandler;
  45. public class DocumentNavigationHandlerTestCase {
  46. @Test
  47. public void testGotoXY() throws SAXException, IFException {
  48. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  49. PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua));
  50. documentHandler.setResult(new StreamResult(new ByteArrayOutputStream()));
  51. documentHandler.setFontInfo(new FontInfo());
  52. documentHandler.startDocument();
  53. documentHandler.startPage(0, "", "", new Dimension());
  54. documentHandler.endPage();
  55. int currentPage = 1;
  56. documentHandler.startPage(currentPage, "", "", new Dimension());
  57. final List<GoToXYAction> goToXYActions = new ArrayList<GoToXYAction>();
  58. PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) {
  59. public void addResolvedAction(AbstractAction action) throws IFException {
  60. super.addResolvedAction(action);
  61. goToXYActions.add((GoToXYAction) action);
  62. }
  63. };
  64. DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
  65. new HashMap<String, StructureTreeElement>());
  66. QName xy = DocumentNavigationExtensionConstants.GOTO_XY;
  67. Attributes attributes = mock(Attributes.class);
  68. when(attributes.getValue("page-index")).thenReturn("0");
  69. when(attributes.getValue("x")).thenReturn("0");
  70. when(attributes.getValue("y")).thenReturn("0");
  71. navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes);
  72. navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null);
  73. documentHandler.endPage();
  74. //Since user may merge IF files we want to use current page
  75. Assert.assertEquals(goToXYActions.get(0).getPageIndex(), currentPage);
  76. }
  77. }