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.

PDFPainterTestCase.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.pdf;
  19. import java.awt.Color;
  20. import java.awt.Rectangle;
  21. import org.junit.Test;
  22. import static org.junit.Assert.fail;
  23. import static org.mockito.Matchers.endsWith;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.times;
  26. import static org.mockito.Mockito.verify;
  27. import static org.mockito.Mockito.when;
  28. import org.apache.fop.apps.FOUserAgent;
  29. import org.apache.fop.fo.Constants;
  30. import org.apache.fop.render.intermediate.IFContext;
  31. import org.apache.fop.traits.BorderProps;
  32. public class PDFPainterTestCase {
  33. @Test
  34. public void testDrawBorderRect() {
  35. // the goal of this test is to check that the drawing of rounded corners in PDF calls
  36. // PDFGraphicsPaiter.cubicBezierTo(); the check is done by verifying that a " c " command is written
  37. // to the PDFContentGenerator
  38. // mock
  39. PDFContentGenerator pdfContentGenerator = mock(PDFContentGenerator.class);
  40. // the next line is commented out because the format() method is static and not possible to mock
  41. // when(PDFContentGenerator.format(anyFloat())).thenReturn("20.0");
  42. // mock
  43. FOUserAgent foUserAgent = mock(FOUserAgent.class);
  44. when(foUserAgent.isAccessibilityEnabled()).thenReturn(false);
  45. // mock
  46. IFContext ifContext = mock(IFContext.class);
  47. when(ifContext.getUserAgent()).thenReturn(foUserAgent);
  48. // mock
  49. PDFDocumentHandler pdfDocumentHandler = mock(PDFDocumentHandler.class);
  50. when(pdfDocumentHandler.getGenerator()).thenReturn(pdfContentGenerator);
  51. when(pdfDocumentHandler.getContext()).thenReturn(ifContext);
  52. // mock
  53. PDFLogicalStructureHandler pdfLogicalStructureHandler = mock(PDFLogicalStructureHandler.class);
  54. // real, not mock
  55. PDFPainter pdfPainter = new PDFPainter(pdfDocumentHandler, pdfLogicalStructureHandler);
  56. // build rectangle 200 x 50 (points, which are converted to milipoints)
  57. Rectangle rectangle = new Rectangle(0, 0, 200000, 50000);
  58. // build border properties: width 4pt, radius 30pt
  59. int style = Constants.EN_SOLID;
  60. BorderProps.Mode mode = BorderProps.Mode.SEPARATE;
  61. Color color = Color.BLACK;
  62. int borderWidth = 4000;
  63. int radiusStart = 30000;
  64. int radiusEnd = 30000;
  65. BorderProps border1 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  66. BorderProps border2 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  67. BorderProps border3 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  68. BorderProps border4 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  69. try {
  70. pdfPainter.drawBorderRect(rectangle, border1, border2, border3, border4, Color.WHITE);
  71. // since we cannot mock the PDFContentGenerator.format() static method we have to restrict the
  72. // verification to commands that end with " c ".
  73. verify(pdfContentGenerator, times(16)).add(endsWith(" c "));
  74. } catch (Exception e) {
  75. fail("something broke...");
  76. }
  77. }
  78. }