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.

TestXSLFTableCell.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xslf.usermodel;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertTrue;
  20. import java.awt.Color;
  21. import java.io.IOException;
  22. import java.util.List;
  23. import org.apache.poi.sl.usermodel.PaintStyle;
  24. import org.apache.poi.xslf.XSLFTestDataSamples;
  25. import org.junit.jupiter.api.Test;
  26. class TestXSLFTableCell
  27. {
  28. @Test
  29. void testCorrectlyReadsTextRunStylingForCellsWithNoTheme() throws IOException {
  30. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("table-with-no-theme.pptx");
  31. XSLFSlide slide = ppt.getSlides().get(0);
  32. List<XSLFShape> shapes = slide.getShapes();
  33. assertEquals(1, shapes.size());
  34. assertTrue(shapes.get(0) instanceof XSLFTable);
  35. XSLFTable tbl = (XSLFTable)shapes.get(0);
  36. assertEquals(1, tbl.getNumberOfColumns());
  37. assertEquals(2, tbl.getNumberOfRows());
  38. List<XSLFTableRow> rows = tbl.getRows();
  39. assertEquals(2, rows.size());
  40. // First row has 1 col and 1 runs
  41. XSLFTableRow row0 = rows.get(0);
  42. List<XSLFTableCell> cells0 = row0.getCells();
  43. assertEquals(1, cells0.size());
  44. List<XSLFTextParagraph> paras0 = cells0.get(0).getTextParagraphs();
  45. assertEquals(1, paras0.size());
  46. List<XSLFTextRun> runs0 = paras0.get(0).getTextRuns();
  47. assertEquals(1, runs0.size());
  48. // IMPORTANT -> this should not be a normal text run (was a bug previously)
  49. XSLFTextRun run00 = runs0.get(0);
  50. assertEquals("XSLFCellTextRun", run00.getClass().getSimpleName());
  51. assertFalse(run00.isBold());
  52. assertFalse(run00.isItalic());
  53. assertNotNull(run00.getFontColor());
  54. assertTrue(run00.getFontColor() instanceof PaintStyle.SolidPaint);
  55. assertEquals(Color.black, ((PaintStyle.SolidPaint)run00.getFontColor()).getSolidColor().getColor());
  56. // Second row has 1 col and 3 runs
  57. XSLFTableRow row1 = rows.get(1);
  58. List<XSLFTableCell> cells1 = row1.getCells();
  59. assertEquals(1, cells1.size());
  60. List<XSLFTextParagraph> paras1 = cells1.get(0).getTextParagraphs();
  61. assertEquals(1, paras1.size());
  62. List<XSLFTextRun> runs1 = paras1.get(0).getTextRuns();
  63. assertEquals(3, runs1.size());
  64. // IMPORTANT -> this should not be a normal text run (was a bug previously)
  65. XSLFTextRun run10 = runs1.get(0);
  66. assertEquals("XSLFCellTextRun", run10.getClass().getSimpleName());
  67. assertTrue(run10.isBold());
  68. assertFalse(run10.isItalic());
  69. assertNotNull(run10.getFontColor());
  70. assertTrue(run10.getFontColor() instanceof PaintStyle.SolidPaint);
  71. assertEquals(Color.black, ((PaintStyle.SolidPaint)run10.getFontColor()).getSolidColor().getColor());
  72. XSLFTextRun run11 = runs1.get(1);
  73. assertEquals("XSLFCellTextRun", run11.getClass().getSimpleName());
  74. assertFalse(run11.isBold());
  75. assertFalse(run11.isItalic());
  76. assertNotNull(run11.getFontColor());
  77. assertTrue(run11.getFontColor() instanceof PaintStyle.SolidPaint);
  78. assertEquals(Color.black, ((PaintStyle.SolidPaint)run11.getFontColor()).getSolidColor().getColor());
  79. XSLFTextRun run12 = runs1.get(2);
  80. assertEquals("XSLFCellTextRun", run12.getClass().getSimpleName());
  81. assertFalse(run12.isBold());
  82. assertTrue(run12.isItalic());
  83. assertNotNull(run12.getFontColor());
  84. assertTrue(run12.getFontColor() instanceof PaintStyle.SolidPaint);
  85. assertEquals(Color.black, ((PaintStyle.SolidPaint)run12.getFontColor()).getSolidColor().getColor());
  86. ppt.close();
  87. }
  88. @Test
  89. void testCorrectlyReadsTextRunStylingForCellsWithTheme() throws IOException {
  90. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("table-with-theme.pptx");
  91. XSLFSlide slide = ppt.getSlides().get(0);
  92. List<XSLFShape> shapes = slide.getShapes();
  93. assertEquals(1, shapes.size());
  94. assertTrue(shapes.get(0) instanceof XSLFTable);
  95. XSLFTable tbl = (XSLFTable)shapes.get(0);
  96. assertEquals(1, tbl.getNumberOfColumns());
  97. assertEquals(2, tbl.getNumberOfRows());
  98. List<XSLFTableRow> rows = tbl.getRows();
  99. assertEquals(2, rows.size());
  100. // First row has 1 col and 3 runs
  101. XSLFTableRow row0 = rows.get(0);
  102. List<XSLFTableCell> cells0 = row0.getCells();
  103. assertEquals(1, cells0.size());
  104. List<XSLFTextParagraph> paras0 = cells0.get(0).getTextParagraphs();
  105. assertEquals(1, paras0.size());
  106. List<XSLFTextRun> runs0 = paras0.get(0).getTextRuns();
  107. assertEquals(3, runs0.size());
  108. // IMPORTANT -> this should not be a normal text run (was a bug previously)
  109. XSLFTextRun run00 = runs0.get(0);
  110. assertEquals("XSLFCellTextRun", run00.getClass().getSimpleName());
  111. assertTrue(run00.isBold());
  112. assertFalse(run00.isItalic());
  113. assertNotNull(run00.getFontColor());
  114. assertTrue(run00.getFontColor() instanceof PaintStyle.SolidPaint);
  115. assertEquals(Color.white, ((PaintStyle.SolidPaint)run00.getFontColor()).getSolidColor().getColor());
  116. XSLFTextRun run01 = runs0.get(1);
  117. assertEquals("XSLFCellTextRun", run01.getClass().getSimpleName());
  118. assertTrue(run01.isBold());
  119. assertFalse(run01.isItalic());
  120. assertNotNull(run01.getFontColor());
  121. assertTrue(run01.getFontColor() instanceof PaintStyle.SolidPaint);
  122. assertEquals(Color.white, ((PaintStyle.SolidPaint)run01.getFontColor()).getSolidColor().getColor());
  123. XSLFTextRun run02 = runs0.get(2);
  124. assertEquals("XSLFCellTextRun", run02.getClass().getSimpleName());
  125. assertFalse(run02.isBold());
  126. assertFalse(run02.isItalic());
  127. assertNotNull(run02.getFontColor());
  128. assertTrue(run02.getFontColor() instanceof PaintStyle.SolidPaint);
  129. assertEquals(Color.red, ((PaintStyle.SolidPaint)run02.getFontColor()).getSolidColor().getColor());
  130. // Second row has 1 col and 7 runs
  131. XSLFTableRow row1 = rows.get(1);
  132. List<XSLFTableCell> cells1 = row1.getCells();
  133. assertEquals(1, cells1.size());
  134. List<XSLFTextParagraph> paras1 = cells1.get(0).getTextParagraphs();
  135. assertEquals(1, paras1.size());
  136. List<XSLFTextRun> runs1 = paras1.get(0).getTextRuns();
  137. assertEquals(7, runs1.size());
  138. // IMPORTANT -> this should not be a normal text run (was a bug previously)
  139. XSLFTextRun run10 = runs1.get(0);
  140. assertEquals("XSLFCellTextRun", run10.getClass().getSimpleName());
  141. assertTrue(run10.isBold());
  142. assertFalse(run10.isItalic());
  143. assertNotNull(run10.getFontColor());
  144. assertTrue(run10.getFontColor() instanceof PaintStyle.SolidPaint);
  145. assertEquals(Color.black, ((PaintStyle.SolidPaint)run10.getFontColor()).getSolidColor().getColor());
  146. XSLFTextRun run11 = runs1.get(1);
  147. assertEquals("XSLFCellTextRun", run11.getClass().getSimpleName());
  148. assertFalse(run11.isBold());
  149. assertFalse(run11.isItalic());
  150. assertNotNull(run11.getFontColor());
  151. assertTrue(run11.getFontColor() instanceof PaintStyle.SolidPaint);
  152. assertEquals(Color.black, ((PaintStyle.SolidPaint)run11.getFontColor()).getSolidColor().getColor());
  153. XSLFTextRun run12 = runs1.get(2);
  154. assertEquals("XSLFCellTextRun", run12.getClass().getSimpleName());
  155. assertFalse(run12.isBold());
  156. assertTrue(run12.isItalic());
  157. assertNotNull(run12.getFontColor());
  158. assertTrue(run12.getFontColor() instanceof PaintStyle.SolidPaint);
  159. assertEquals(Color.black, ((PaintStyle.SolidPaint)run12.getFontColor()).getSolidColor().getColor());
  160. XSLFTextRun run13 = runs1.get(3);
  161. assertEquals("XSLFCellTextRun", run13.getClass().getSimpleName());
  162. assertFalse(run13.isBold());
  163. assertFalse(run13.isItalic());
  164. assertNotNull(run13.getFontColor());
  165. assertTrue(run13.getFontColor() instanceof PaintStyle.SolidPaint);
  166. assertEquals(Color.black, ((PaintStyle.SolidPaint)run13.getFontColor()).getSolidColor().getColor());
  167. XSLFTextRun run14 = runs1.get(4);
  168. assertEquals("XSLFCellTextRun", run14.getClass().getSimpleName());
  169. assertFalse(run14.isBold());
  170. assertFalse(run14.isItalic());
  171. assertNotNull(run14.getFontColor());
  172. assertTrue(run14.getFontColor() instanceof PaintStyle.SolidPaint);
  173. assertEquals(Color.yellow, ((PaintStyle.SolidPaint)run14.getFontColor()).getSolidColor().getColor());
  174. XSLFTextRun run15 = runs1.get(5);
  175. assertEquals("XSLFCellTextRun", run15.getClass().getSimpleName());
  176. assertFalse(run15.isBold());
  177. assertFalse(run15.isItalic());
  178. assertNotNull(run15.getFontColor());
  179. assertTrue(run15.getFontColor() instanceof PaintStyle.SolidPaint);
  180. assertEquals(Color.black, ((PaintStyle.SolidPaint)run15.getFontColor()).getSolidColor().getColor());
  181. XSLFTextRun run16 = runs1.get(6);
  182. assertEquals("XSLFCellTextRun", run16.getClass().getSimpleName());
  183. assertFalse(run16.isBold());
  184. assertFalse(run16.isItalic());
  185. assertNotNull(run16.getFontColor());
  186. assertTrue(run16.getFontColor() instanceof PaintStyle.SolidPaint);
  187. assertEquals(Color.black, ((PaintStyle.SolidPaint)run16.getFontColor()).getSolidColor().getColor());
  188. ppt.close();
  189. }
  190. @Test
  191. void testBug68703() throws IOException {
  192. try(XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("bug68703.pptx")) {
  193. XSLFSlide firstSlide = pptx.getSlides().get(0);
  194. XSLFTable table = (XSLFTable) firstSlide.getShapes().get(0);
  195. XSLFTableCell cell = table.getCell(0, 0);
  196. List<XSLFTextParagraph> cellParagraphs = cell.getTextParagraphs();
  197. List<XSLFTextRun> cellTextRuns = cellParagraphs.get(0).getTextRuns();
  198. PaintStyle fontColor = cellTextRuns.get(0).getFontColor();
  199. assertNotNull(fontColor);
  200. assertTrue(fontColor instanceof PaintStyle.SolidPaint);
  201. assertEquals(Color.black, ((PaintStyle.SolidPaint) fontColor).getSolidColor().getColor());
  202. }
  203. }
  204. }