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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. PSGenerator psGenerator = mock(PSGenerator.class);
  79. when(psGenerator.formatDouble(anyFloat())).thenReturn("20.0"); // simplify!
  80. PSRenderingUtil psRenderingUtil = mock(PSRenderingUtil.class);
  81. PSDocumentHandler psDocumentHandler = mock(PSDocumentHandler.class);
  82. when(psDocumentHandler.getGenerator()).thenReturn(psGenerator);
  83. when(psDocumentHandler.getPSUtil()).thenReturn(psRenderingUtil);
  84. PSPainter psPainter = new PSPainter(psDocumentHandler);
  85. // build rectangle 200 x 50 (points, which are converted to milipoints)
  86. Rectangle rectangle = new Rectangle(0, 0, 200000, 50000);
  87. // build border properties: width 4pt, radius 30pt
  88. BorderProps border = new BorderProps(Constants.EN_SOLID, 4000, 30000, 30000, Color.BLACK,
  89. BorderProps.Mode.SEPARATE);
  90. try {
  91. psPainter.drawBorderRect(rectangle, border, border, border, border, Color.WHITE);
  92. verify(psGenerator, times(16)).writeln("20.0 20.0 20.0 20.0 20.0 20.0 curveto ");
  93. } catch (Exception e) {
  94. fail("something broke...");
  95. }
  96. }
  97. }