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.

PSPainterTestCase.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. package org.apache.fop.render.ps;
  18. import java.awt.Color;
  19. import java.awt.Rectangle;
  20. import java.io.IOException;
  21. import java.util.Collections;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.mockito.verification.VerificationMode;
  25. import org.apache.xmlgraphics.ps.PSGenerator;
  26. import org.apache.fop.apps.FOUserAgent;
  27. import org.apache.fop.fo.Constants;
  28. import org.apache.fop.render.intermediate.IFContext;
  29. import org.apache.fop.render.intermediate.IFState;
  30. import org.apache.fop.traits.BorderProps;
  31. import static org.junit.Assert.fail;
  32. import static org.mockito.Matchers.anyFloat;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.never;
  35. import static org.mockito.Mockito.times;
  36. import static org.mockito.Mockito.verify;
  37. import static org.mockito.Mockito.when;
  38. public class PSPainterTestCase {
  39. private PSDocumentHandler docHandler;
  40. private PSPainter psPainter;
  41. private PSGenerator gen;
  42. private IFState state;
  43. @Before
  44. public void setup() {
  45. state = IFState.create();
  46. FOUserAgent userAgent = mock(FOUserAgent.class);
  47. when(userAgent.getRendererOptions()).thenReturn(Collections.EMPTY_MAP);
  48. IFContext context = mock(IFContext.class);
  49. when(context.getUserAgent()).thenReturn(userAgent);
  50. docHandler = new PSDocumentHandler(context);
  51. gen = mock(PSGenerator.class);
  52. docHandler.gen = gen;
  53. state = IFState.create();
  54. psPainter = new PSPainter(docHandler, state);
  55. }
  56. @Test
  57. public void testNonZeroFontSize() throws IOException {
  58. testFontSize(6, times(1));
  59. }
  60. @Test
  61. public void testZeroFontSize() throws IOException {
  62. testFontSize(0, never());
  63. }
  64. private void testFontSize(int fontSize, VerificationMode test) throws IOException {
  65. state.setFontSize(fontSize);
  66. try {
  67. psPainter.drawText(10, 10, 2, 2, null, "Test");
  68. } catch (Exception ex) {
  69. //Expected
  70. }
  71. verify(gen, test).useColor(state.getTextColor());
  72. }
  73. @Test
  74. public void testDrawBorderRect() {
  75. // the goal of this test is to check that the drawing of rounded corners in PS calls
  76. // PSGraphicsPaiter.cubicBezierTo(); the check is done by verifying that a curveto command is written
  77. // to the PSGenerator
  78. // mock
  79. PSGenerator psGenerator = mock(PSGenerator.class);
  80. when(psGenerator.formatDouble(anyFloat())).thenReturn("20.0"); // simplify!
  81. // mock
  82. PSRenderingUtil psRenderingUtil = mock(PSRenderingUtil.class);
  83. // mock
  84. PSDocumentHandler psDocumentHandler = mock(PSDocumentHandler.class);
  85. when(psDocumentHandler.getGenerator()).thenReturn(psGenerator);
  86. when(psDocumentHandler.getPSUtil()).thenReturn(psRenderingUtil);
  87. // real instance, no mock
  88. PSPainter psPainter = new PSPainter(psDocumentHandler);
  89. // build rectangle 200 x 50 (points, which are converted to milipoints)
  90. Rectangle rectangle = new Rectangle(0, 0, 200000, 50000);
  91. // build border properties: width 4pt, radius 30pt
  92. int style = Constants.EN_SOLID;
  93. BorderProps.Mode mode = BorderProps.Mode.SEPARATE;
  94. Color color = Color.BLACK;
  95. int borderWidth = 4000;
  96. int radiusStart = 30000;
  97. int radiusEnd = 30000;
  98. BorderProps border1 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  99. BorderProps border2 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  100. BorderProps border3 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  101. BorderProps border4 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  102. try {
  103. psPainter.drawBorderRect(rectangle, border1, border2, border3, border4, Color.WHITE);
  104. verify(psGenerator, times(16)).writeln("20.0 20.0 20.0 20.0 20.0 20.0 curveto ");
  105. } catch (Exception e) {
  106. fail("something broke...");
  107. }
  108. }
  109. }