Further XSSF Themes unit testing

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1694093 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-08-04 17:50:44 +00:00
parent 4f9ed87f6b
commit 885368e2ce
2 changed files with 111 additions and 29 deletions

View File

@ -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.

View File

@ -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<String,XSSFWorkbook> workbooks = new HashMap<String, XSSFWorkbook>();
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<themeEntries.length+startRN; rn++) {
XSSFRow row = sheet.getRow(rn);
assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);
String ref = (new CellReference(rn, 0)).formatAsString();
XSSFCell cell = row.getCell(0);
assertNotNull(
"Missing cell " + ref + " in " + whatWorkbook, cell);
assertEquals(
"Wrong theme at " + ref + " in " + whatWorkbook,
themeEntries[rn], cell.getStringCellValue());
XSSFFont font = cell.getCellStyle().getFont();
XSSFColor color = font.getXSSFColor();
// Theme colours aren't tinted
assertEquals(false, color.hasTint());
// Check the RGB part (no tint)
assertEquals(
"Wrong theme colour " + themeEntries[rn] + " on " + whatWorkbook,
rgbExpected[rn], Hex.encodeHexString(color.getRGB()));
// Check the Theme ID
int expectedThemeIdx = rn - startRN;
long themeIdx = font.getCTFont().getColorArray(0).getTheme();
assertEquals(
"Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook,
expectedThemeIdx, themeIdx);
if (createFiles) {
XSSFCellStyle cs = row.getSheet().getWorkbook().createCellStyle();
cs.setFillForegroundColor(color);
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
row.createCell(1).setCellStyle(cs);
}
}
if (createFiles) {
FileOutputStream fos = new FileOutputStream("Generated_"+whatWorkbook);
workbook.write(fos);
fos.close();
}
}
}
// TODO Check the complex parts
@Test
@SuppressWarnings("resource")
public void testAddNew() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet s = wb.createSheet();
wb.createSheet();
assertEquals(null, wb.getTheme());
StylesTable styles = wb.getStylesSource();
@ -97,4 +171,4 @@ public class TestThemesTable {
assertNotNull(styles.getTheme());
assertNotNull(wb.getTheme());
}
}
}