Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TestXSLFTableCell.java 11KB

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