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.

AFPPainterTestCase.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.afp;
  19. import java.awt.Color;
  20. import java.awt.Rectangle;
  21. import java.util.Map;
  22. import org.junit.Test;
  23. import static org.junit.Assert.fail;
  24. import static org.mockito.Matchers.any;
  25. import static org.mockito.Mockito.mock;
  26. import static org.mockito.Mockito.verify;
  27. import static org.mockito.Mockito.when;
  28. import org.apache.xmlgraphics.image.loader.Image;
  29. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  30. import org.apache.xmlgraphics.image.loader.ImageManager;
  31. import org.apache.xmlgraphics.image.loader.impl.DefaultImageContext;
  32. import org.apache.xmlgraphics.image.loader.impl.DefaultImageSessionContext;
  33. import org.apache.xmlgraphics.image.loader.impl.ImageBuffered;
  34. import org.apache.fop.afp.AFPEventProducer;
  35. import org.apache.fop.afp.AFPPaintingState;
  36. import org.apache.fop.afp.AFPResourceManager;
  37. import org.apache.fop.apps.FOUserAgent;
  38. import org.apache.fop.events.EventBroadcaster;
  39. import org.apache.fop.fo.Constants;
  40. import org.apache.fop.render.ImageHandlerRegistry;
  41. import org.apache.fop.render.intermediate.IFContext;
  42. import org.apache.fop.traits.BorderProps;
  43. public class AFPPainterTestCase {
  44. @Test
  45. public void testDrawBorderRect() {
  46. // the goal of this test is to check that the drawing of rounded corners in AFP uses a bitmap of the
  47. // rounded corners (in fact the whole rectangle with rounded corners). the check is done by verifying
  48. // that the AFPImageHandlerRenderedImage.handleImage() method is called
  49. // mock
  50. AFPPaintingState afpPaintingState = mock(AFPPaintingState.class);
  51. when(afpPaintingState.getResolution()).thenReturn(72);
  52. // mock
  53. EventBroadcaster eventBroadcaster = mock(EventBroadcaster.class);
  54. // mock
  55. DefaultImageContext defaultImageContext = mock(DefaultImageContext.class);
  56. when(defaultImageContext.getSourceResolution()).thenReturn(72000f);
  57. // mock
  58. DefaultImageSessionContext defaultImageSessionContxt = mock(DefaultImageSessionContext.class);
  59. when(defaultImageSessionContxt.getParentContext()).thenReturn(defaultImageContext);
  60. when(defaultImageSessionContxt.getTargetResolution()).thenReturn(72000f);
  61. // mock
  62. ImageBuffered imageBuffered = mock(ImageBuffered.class);
  63. // mock
  64. ImageManager imageManager = mock(ImageManager.class);
  65. // mock
  66. AFPImageHandlerRenderedImage afpImageHandlerRenderedImage = mock(AFPImageHandlerRenderedImage.class);
  67. // mock
  68. ImageHandlerRegistry imageHandlerRegistry = mock(ImageHandlerRegistry.class);
  69. when(imageHandlerRegistry.getHandler(any(AFPRenderingContext.class), any(Image.class))).thenReturn(
  70. afpImageHandlerRenderedImage);
  71. // mock
  72. FOUserAgent foUserAgent = mock(FOUserAgent.class);
  73. when(foUserAgent.getEventBroadcaster()).thenReturn(eventBroadcaster);
  74. when(foUserAgent.getImageSessionContext()).thenReturn(defaultImageSessionContxt);
  75. when(foUserAgent.getImageManager()).thenReturn(imageManager);
  76. when(foUserAgent.getImageHandlerRegistry()).thenReturn(imageHandlerRegistry);
  77. // mock
  78. AFPEventProducer afpEventProducer = mock(AFPEventProducer.class);
  79. when(AFPEventProducer.Provider.get(eventBroadcaster)).thenReturn(afpEventProducer);
  80. // mock
  81. AFPResourceManager afpResourceManager = mock(AFPResourceManager.class);
  82. when(afpResourceManager.isObjectCached(null)).thenReturn(false);
  83. // mock
  84. IFContext ifContext = mock(IFContext.class);
  85. when(ifContext.getUserAgent()).thenReturn(foUserAgent);
  86. // mock
  87. AFPDocumentHandler afpDocumentHandler = mock(AFPDocumentHandler.class);
  88. when(afpDocumentHandler.getPaintingState()).thenReturn(afpPaintingState);
  89. when(afpDocumentHandler.getContext()).thenReturn(ifContext);
  90. when(afpDocumentHandler.getResourceManager()).thenReturn(afpResourceManager);
  91. when(afpDocumentHandler.cacheRoundedCorner("a2a48964ba2d")).thenReturn("RC000000");
  92. // real instance, no mock
  93. AFPPainter afpPainter = new AFPPainter(afpDocumentHandler);
  94. // build rectangle 200 x 50 (points, which are converted to millipoints)
  95. Rectangle rectangle = new Rectangle(0, 0, 200000, 50000);
  96. // build border properties
  97. int style = Constants.EN_SOLID;
  98. BorderProps.Mode mode = BorderProps.Mode.SEPARATE;
  99. Color color = Color.BLACK;
  100. int borderWidth = 4000;
  101. int radiusStart = 30000;
  102. int radiusEnd = 30000;
  103. BorderProps border1 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  104. BorderProps border2 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  105. BorderProps border3 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  106. BorderProps border4 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  107. try {
  108. when(imageManager.convertImage(any(Image.class), any(ImageFlavor[].class), any(Map.class)))
  109. .thenReturn(imageBuffered);
  110. afpPainter.drawBorderRect(rectangle, border1, border2, border3, border4, Color.WHITE);
  111. // note: here we would really like to verify that the second and third arguments passed to
  112. // handleImage() are the instances ib and rect declared above but that causes mockito to throw
  113. // an exception, probably because we cannot declare the AFPRenderingContext and are forced to
  114. // use any(), which forces the use of any() for all arguments
  115. verify(afpImageHandlerRenderedImage).handleImage(any(AFPRenderingContext.class),
  116. any(Image.class), any(Rectangle.class));
  117. } catch (Exception e) {
  118. fail("something broke...");
  119. }
  120. }
  121. }