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.

PageSequenceLayoutManagerTestCase.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.layoutmgr;
  19. import org.junit.Test;
  20. import static org.junit.Assert.assertEquals;
  21. import static org.mockito.Matchers.anyInt;
  22. import static org.mockito.Mockito.mock;
  23. import static org.mockito.Mockito.when;
  24. import org.apache.fop.area.AreaTreeHandler;
  25. import org.apache.fop.area.PageViewport;
  26. import org.apache.fop.fo.pagination.Flow;
  27. import org.apache.fop.fo.pagination.PageSequence;
  28. import org.apache.fop.fo.pagination.Region;
  29. import org.apache.fop.fo.pagination.RegionBody;
  30. import org.apache.fop.fo.pagination.Root;
  31. import org.apache.fop.fo.pagination.SimplePageMaster;
  32. public class PageSequenceLayoutManagerTestCase {
  33. private static final String MAIN_FLOW_NAME = "main";
  34. private static final String EMPTY_FLOW_NAME = "empty";
  35. /**
  36. * Blank pages can be created from empty pages
  37. *
  38. * @throws Exception
  39. */
  40. @Test
  41. public void testGetNextPageBlank() throws Exception {
  42. final Page expectedPage = createPageForRegionName(EMPTY_FLOW_NAME);
  43. final Page[] providedPages = new Page[]{expectedPage};
  44. testGetNextPage(providedPages, expectedPage, true);
  45. }
  46. /**
  47. * Empty pages should not be provided by the PageSequenceLayoutManager
  48. * to layout the main flow
  49. *
  50. * @throws Exception
  51. */
  52. @Test
  53. public void testGetNextPageFirstEmpty() throws Exception {
  54. final Page emptyPage = createPageForRegionName(EMPTY_FLOW_NAME);
  55. final Page expectedPage = createPageForRegionName(MAIN_FLOW_NAME);
  56. final Page[] providedPages = new Page[]{emptyPage, expectedPage};
  57. testGetNextPage(providedPages, expectedPage, false);
  58. }
  59. private void testGetNextPage(final Page[] providedPages, Page expectedPage, boolean isBlank) {
  60. final Flow flow = mock(Flow.class);
  61. final PageSequence pseq = mock(PageSequence.class);
  62. final Root root = mock(Root.class);
  63. final AreaTreeHandler ath = mock(AreaTreeHandler.class);
  64. when(flow.getFlowName()).thenReturn(MAIN_FLOW_NAME);
  65. when(pseq.getMainFlow()).thenReturn(flow);
  66. when(pseq.getRoot()).thenReturn(root);
  67. PageSequenceLayoutManager sut = new PageSequenceLayoutManager(ath, pseq) {
  68. @Override
  69. protected Page createPage(int i, boolean b) {
  70. return providedPages[i - 1];
  71. }
  72. @Override
  73. protected void finishPage() {
  74. //nop
  75. }
  76. // Expose the protected method for testing
  77. public Page makeNewPage(boolean isBlank) {
  78. return super.makeNewPage(isBlank);
  79. }
  80. };
  81. assertEquals(expectedPage, sut.makeNewPage(isBlank));
  82. }
  83. private static Page createPageForRegionName(final String regionName) {
  84. final Page page = mock(Page.class);
  85. final SimplePageMaster spm = mock(SimplePageMaster.class);
  86. final PageViewport pageViewport = mock(PageViewport.class);
  87. final Region region = mock(RegionBody.class);
  88. when(page.getSimplePageMaster()).thenReturn(spm);
  89. when(page.getPageViewport()).thenReturn(pageViewport);
  90. when(spm.getRegion(anyInt())).thenReturn(region);
  91. when(region.getRegionName()).thenReturn(regionName);
  92. return page;
  93. }
  94. @Test
  95. public void testRegionNameNotFound() {
  96. final PageSequence pseq = mock(PageSequence.class);
  97. final AreaTreeHandler ath = mock(AreaTreeHandler.class);
  98. final Flow flow = mock(Flow.class);
  99. final Root root = mock(Root.class);
  100. when(flow.getFlowName()).thenReturn(MAIN_FLOW_NAME);
  101. when(pseq.getRoot()).thenReturn(root);
  102. when(pseq.hasPagePositionLast()).thenReturn(true);
  103. when(pseq.getMainFlow()).thenReturn(flow);
  104. PageSequenceLayoutManager pageSequenceLayoutManager = new PageSequenceLayoutManager(ath, pseq) {
  105. public void activateLayout() {
  106. makeNewPage(false);
  107. }
  108. protected Page createPage(int pageNumber, boolean isBlank) {
  109. return createPageForRegionName("test");
  110. }
  111. protected void finishPage() {
  112. }
  113. };
  114. RuntimeException re = null;
  115. try {
  116. pageSequenceLayoutManager.activateLayout();
  117. } catch (RuntimeException e) {
  118. re = e;
  119. }
  120. assertEquals(re.getMessage(),
  121. "The flow-name \"test\" could not be mapped to a region-name in the layout-master-set");
  122. }
  123. }