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.

TestThemesTable.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.xssf.model;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import java.io.FileOutputStream;
  19. import java.util.LinkedHashMap;
  20. import java.util.Map;
  21. import org.apache.commons.codec.binary.Hex;
  22. import org.apache.poi.ss.usermodel.CellStyle;
  23. import org.apache.poi.ss.util.CellReference;
  24. import org.apache.poi.xssf.XSSFTestDataSamples;
  25. import org.apache.poi.xssf.model.ThemesTable.ThemeElement;
  26. import org.apache.poi.xssf.usermodel.XSSFCell;
  27. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  28. import org.apache.poi.xssf.usermodel.XSSFColor;
  29. import org.apache.poi.xssf.usermodel.XSSFFont;
  30. import org.apache.poi.xssf.usermodel.XSSFRow;
  31. import org.apache.poi.xssf.usermodel.XSSFSheet;
  32. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  33. import org.junit.Test;
  34. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  35. public class TestThemesTable {
  36. private String testFileSimple = "Themes.xlsx";
  37. private String testFileComplex = "Themes2.xlsx";
  38. // TODO .xls version available too, add HSSF support then check
  39. // What colours they should show up as
  40. private static String rgbExpected[] = {
  41. "ffffff", // Lt1
  42. "000000", // Dk1
  43. "eeece1", // Lt2
  44. "1f497d", // DK2
  45. "4f81bd", // Accent1
  46. "c0504d", // Accent2
  47. "9bbb59", // Accent3
  48. "8064a2", // Accent4
  49. "4bacc6", // Accent5
  50. "f79646", // Accent6
  51. "0000ff", // Hlink
  52. "800080" // FolHlink
  53. };
  54. @Test
  55. public void testThemesTableColors() throws Exception {
  56. // Load our two test workbooks
  57. XSSFWorkbook simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
  58. XSSFWorkbook complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
  59. // Save and re-load them, to check for stability across that
  60. XSSFWorkbook simpleRS = XSSFTestDataSamples.writeOutAndReadBack(simple);
  61. XSSFWorkbook complexRS = XSSFTestDataSamples.writeOutAndReadBack(complex);
  62. // Fetch fresh copies to test with
  63. simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
  64. complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
  65. // Files and descriptions
  66. Map<String,XSSFWorkbook> workbooks = new LinkedHashMap<String, XSSFWorkbook>();
  67. workbooks.put(testFileSimple, simple);
  68. workbooks.put("Re-Saved_" + testFileSimple, simpleRS);
  69. workbooks.put(testFileComplex, complex);
  70. workbooks.put("Re-Saved_" + testFileComplex, complexRS);
  71. // Sanity check
  72. assertEquals(rgbExpected.length, rgbExpected.length);
  73. // For offline testing
  74. boolean createFiles = false;
  75. // Check each workbook in turn, and verify that the colours
  76. // for the theme-applied cells in Column A are correct
  77. for (String whatWorkbook : workbooks.keySet()) {
  78. XSSFWorkbook workbook = workbooks.get(whatWorkbook);
  79. XSSFSheet sheet = workbook.getSheetAt(0);
  80. int startRN = 0;
  81. if (whatWorkbook.endsWith(testFileComplex)) startRN++;
  82. for (int rn=startRN; rn<rgbExpected.length+startRN; rn++) {
  83. XSSFRow row = sheet.getRow(rn);
  84. assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);
  85. String ref = (new CellReference(rn, 0)).formatAsString();
  86. XSSFCell cell = row.getCell(0);
  87. assertNotNull(
  88. "Missing cell " + ref + " in " + whatWorkbook, cell);
  89. int expectedThemeIdx = rn-startRN;
  90. ThemeElement themeElem = ThemeElement.byId(expectedThemeIdx);
  91. assertEquals(
  92. "Wrong theme at " + ref + " in " + whatWorkbook,
  93. themeElem.name.toLowerCase(), cell.getStringCellValue());
  94. // Fonts are theme-based in their colours
  95. XSSFFont font = cell.getCellStyle().getFont();
  96. CTColor ctColor = font.getCTFont().getColorArray(0);
  97. assertNotNull(ctColor);
  98. assertEquals(true, ctColor.isSetTheme());
  99. assertEquals(themeElem.idx, ctColor.getTheme());
  100. // Get the colour, via the theme
  101. XSSFColor color = font.getXSSFColor();
  102. // Theme colours aren't tinted
  103. assertEquals(false, color.hasTint());
  104. // Check the RGB part (no tint)
  105. assertEquals(
  106. "Wrong theme colour " + themeElem.name + " on " + whatWorkbook,
  107. rgbExpected[expectedThemeIdx], Hex.encodeHexString(color.getRGB()));
  108. long themeIdx = font.getCTFont().getColorArray(0).getTheme();
  109. assertEquals(
  110. "Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook,
  111. expectedThemeIdx, themeIdx);
  112. if (createFiles) {
  113. XSSFCellStyle cs = row.getSheet().getWorkbook().createCellStyle();
  114. cs.setFillForegroundColor(color);
  115. cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
  116. row.createCell(1).setCellStyle(cs);
  117. }
  118. }
  119. if (createFiles) {
  120. FileOutputStream fos = new FileOutputStream("Generated_"+whatWorkbook);
  121. workbook.write(fos);
  122. fos.close();
  123. }
  124. }
  125. }
  126. /**
  127. * Ensure that, for a file with themes, we can correctly
  128. * read both the themed and non-themed colours back.
  129. * Column A = Theme Foreground
  130. * Column B = Theme Foreground
  131. * Column C = Explicit Colour Foreground
  132. * Column E = Explicit Colour Background, Black Foreground
  133. * Column G = Conditional Formatting Backgrounds
  134. */
  135. @Test
  136. public void themedAndNonThemedColours() {
  137. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
  138. XSSFSheet sheet = wb.getSheetAt(0);
  139. XSSFCellStyle style;
  140. XSSFColor color;
  141. XSSFCell cell;
  142. String[] names = {"White","Black","Grey","Dark Blue","Blue","Red","Green"};
  143. assertEquals(7, names.length);
  144. // Check the non-CF colours in Columns A, B, C and E
  145. for (int rn=1; rn<8; rn++) {
  146. int idx = rn-1;
  147. XSSFRow row = sheet.getRow(rn);
  148. assertNotNull("Missing row " + rn, row);
  149. // Theme cells come first
  150. XSSFCell themeCell = row.getCell(0);
  151. ThemeElement themeElem = ThemeElement.byId(idx);
  152. assertCellContents(themeElem.name, themeCell);
  153. // Sanity check names
  154. assertCellContents(names[idx], row.getCell(1));
  155. assertCellContents(names[idx], row.getCell(2));
  156. assertCellContents(names[idx], row.getCell(4));
  157. // Check the colours
  158. // A: Theme Based, Foreground
  159. style = themeCell.getCellStyle();
  160. color = style.getFont().getXSSFColor();
  161. assertEquals(true, color.isThemed());
  162. assertEquals(idx, color.getTheme());
  163. assertEquals(rgbExpected[idx], Hex.encodeHexString(color.getRGB()));
  164. // B: Theme Based, Foreground
  165. cell = row.getCell(1);
  166. style = cell.getCellStyle();
  167. color = style.getFont().getXSSFColor();
  168. assertEquals(true, color.isThemed());
  169. // TODO Fix the grey theme color in Column B
  170. if (idx != 2) {
  171. assertEquals(idx, color.getTheme());
  172. assertEquals(rgbExpected[idx], Hex.encodeHexString(color.getRGB()));
  173. }
  174. }
  175. // Check the CF colours
  176. // TODO
  177. }
  178. private static void assertCellContents(String expected, XSSFCell cell) {
  179. assertNotNull(cell);
  180. assertEquals(expected.toLowerCase(), cell.getStringCellValue().toLowerCase());
  181. }
  182. @Test
  183. @SuppressWarnings("resource")
  184. public void testAddNew() throws Exception {
  185. XSSFWorkbook wb = new XSSFWorkbook();
  186. wb.createSheet();
  187. assertEquals(null, wb.getTheme());
  188. StylesTable styles = wb.getStylesSource();
  189. assertEquals(null, styles.getTheme());
  190. styles.ensureThemesTable();
  191. assertNotNull(styles.getTheme());
  192. assertNotNull(wb.getTheme());
  193. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  194. styles = wb.getStylesSource();
  195. assertNotNull(styles.getTheme());
  196. assertNotNull(wb.getTheme());
  197. }
  198. }