diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java index f443212de..9154e7f7d 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java @@ -88,6 +88,14 @@ public class XSSFColor extends ExtendedColor { public boolean isThemed() { return ctColor.isSetTheme(); } + + /** + * A boolean value indicating if the ctColor has a tint or not + */ + public boolean hasTint() { + if (! ctColor.isSetRgb()) return false; + return ctColor.getRgb().length == 4; + } /** * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors. diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java index 6be48768e..77bfdbddf 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java @@ -21,11 +21,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.FileOutputStream; +import java.util.HashMap; +import java.util.Map; import org.apache.commons.codec.binary.Hex; import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFFont; @@ -35,12 +38,27 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Test; public class TestThemesTable { - private String testFile = "Themes.xlsx"; - - @Test - public void testThemesTableColors() throws Exception { - XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile); - String rgbExpected[] = { + private String testFileSimple = "Themes.xlsx"; + private String testFileComplex = "Themes2.xlsx"; + // TODO .xls version available too, add HSSF support then check + + // What our theme names are + private static String[] themeEntries = { + "lt1", + "dk1", + "lt2", + "dk2", + "accent1", + "accent2", + "accent3", + "accent4", + "accent5", + "accent6", + "hlink", + "folhlink" + }; + // What colours they should show up as + private static String rgbExpected[] = { "ffffff", // Lt1 "000000", // Dk1 "eeece1", // Lt2 @@ -54,34 +72,90 @@ public class TestThemesTable { "0000ff", // Hlink "800080" // FolHlink }; - boolean createFile = false; - int i=0; - for (Row row : workbook.getSheetAt(0)) { - XSSFFont font = ((XSSFRow)row).getCell(0).getCellStyle().getFont(); - XSSFColor color = font.getXSSFColor(); - assertEquals("Failed color theme "+i, rgbExpected[i], Hex.encodeHexString(color.getRgb())); - long themeIdx = font.getCTFont().getColorArray(0).getTheme(); - assertEquals("Failed color theme "+i, i, themeIdx); - if (createFile) { - XSSFCellStyle cs = (XSSFCellStyle)row.getSheet().getWorkbook().createCellStyle(); - cs.setFillForegroundColor(color); - cs.setFillPattern(CellStyle.SOLID_FOREGROUND); - row.createCell(1).setCellStyle(cs); - } - i++; - } + + @Test + public void testThemesTableColors() throws Exception { + // Load our two test workbooks + XSSFWorkbook simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple); + XSSFWorkbook complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex); + // Save and re-load them, to check for stability across that + XSSFWorkbook simpleRS = XSSFTestDataSamples.writeOutAndReadBack(simple); + XSSFWorkbook complexRS = XSSFTestDataSamples.writeOutAndReadBack(complex); + // Fetch fresh copies to test with + simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple); + complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex); + // Files and descriptions + Map workbooks = new HashMap(); + workbooks.put(testFileSimple, simple); + workbooks.put("Re-Saved_" + testFileSimple, simpleRS); + // TODO Fix these to work! +// workbooks.put(testFileComplex, complex); +// workbooks.put("Re-Saved_" + testFileComplex, complexRS); - if (createFile) { - FileOutputStream fos = new FileOutputStream("foobaa.xlsx"); - workbook.write(fos); - fos.close(); + // Sanity check + assertEquals(themeEntries.length, rgbExpected.length); + + // For offline testing + boolean createFiles = false; + + // Check each workbook in turn, and verify that the colours + // for the theme-applied cells in Column A are correct + for (String whatWorkbook : workbooks.keySet()) { + XSSFWorkbook workbook = workbooks.get(whatWorkbook); + XSSFSheet sheet = workbook.getSheetAt(0); + int startRN = 0; + if (whatWorkbook.endsWith(testFileComplex)) startRN++; + + for (int rn=startRN; rn