]> source.dussan.org Git - poi.git/commitdiff
Further XSSF Themes unit testing
authorNick Burch <nick@apache.org>
Tue, 4 Aug 2015 17:50:44 +0000 (17:50 +0000)
committerNick Burch <nick@apache.org>
Tue, 4 Aug 2015 17:50:44 +0000 (17:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1694093 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java

index f443212de92327c955372cb09e65149268e19070..9154e7f7dd9d9ca7a08b63a296e21dee91ee53d5 100644 (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.
index 6be48768edd8090c534d65837b19f82df7337287..77bfdbddf58d90e8d660f9d9df47ce2a956c1710 100644 (file)
@@ -21,11 +21,14 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;\r
 \r
 import java.io.FileOutputStream;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
 \r
 import org.apache.commons.codec.binary.Hex;\r
 import org.apache.poi.ss.usermodel.CellStyle;\r
-import org.apache.poi.ss.usermodel.Row;\r
+import org.apache.poi.ss.util.CellReference;\r
 import org.apache.poi.xssf.XSSFTestDataSamples;\r
+import org.apache.poi.xssf.usermodel.XSSFCell;\r
 import org.apache.poi.xssf.usermodel.XSSFCellStyle;\r
 import org.apache.poi.xssf.usermodel.XSSFColor;\r
 import org.apache.poi.xssf.usermodel.XSSFFont;\r
@@ -35,12 +38,27 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.junit.Test;\r
 \r
 public class TestThemesTable {\r
-    private String testFile = "Themes.xlsx";\r
-\r
-    @Test\r
-    public void testThemesTableColors() throws Exception {\r
-        XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);\r
-        String rgbExpected[] = {\r
+    private String testFileSimple = "Themes.xlsx";\r
+    private String testFileComplex = "Themes2.xlsx";\r
+    // TODO .xls version available too, add HSSF support then check \r
+    \r
+    // What our theme names are\r
+    private static String[] themeEntries = {\r
+        "lt1",\r
+        "dk1",\r
+        "lt2",\r
+        "dk2",\r
+        "accent1",\r
+        "accent2",\r
+        "accent3",\r
+        "accent4",\r
+        "accent5",\r
+        "accent6",\r
+        "hlink",\r
+        "folhlink"\r
+    };\r
+    // What colours they should show up as\r
+    private static String rgbExpected[] = {\r
             "ffffff", // Lt1\r
             "000000", // Dk1\r
             "eeece1", // Lt2\r
@@ -54,34 +72,90 @@ public class TestThemesTable {
             "0000ff", // Hlink\r
             "800080"  // FolHlink\r
         };\r
-        boolean createFile = false;\r
-        int i=0;\r
-        for (Row row : workbook.getSheetAt(0)) {\r
-            XSSFFont font = ((XSSFRow)row).getCell(0).getCellStyle().getFont();\r
-            XSSFColor color = font.getXSSFColor();\r
-            assertEquals("Failed color theme "+i, rgbExpected[i], Hex.encodeHexString(color.getRgb()));\r
-            long themeIdx = font.getCTFont().getColorArray(0).getTheme();\r
-            assertEquals("Failed color theme "+i, i, themeIdx);\r
-            if (createFile) {\r
-                XSSFCellStyle cs = (XSSFCellStyle)row.getSheet().getWorkbook().createCellStyle();\r
-                cs.setFillForegroundColor(color);\r
-                cs.setFillPattern(CellStyle.SOLID_FOREGROUND);\r
-                row.createCell(1).setCellStyle(cs);\r
-            }\r
-            i++;\r
-        }\r
+\r
+    @Test\r
+    public void testThemesTableColors() throws Exception {\r
+        // Load our two test workbooks\r
+        XSSFWorkbook simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);\r
+        XSSFWorkbook complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);\r
+        // Save and re-load them, to check for stability across that\r
+        XSSFWorkbook simpleRS = XSSFTestDataSamples.writeOutAndReadBack(simple);\r
+        XSSFWorkbook complexRS = XSSFTestDataSamples.writeOutAndReadBack(complex);\r
+        // Fetch fresh copies to test with\r
+        simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);\r
+        complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);\r
+        // Files and descriptions\r
+        Map<String,XSSFWorkbook> workbooks = new HashMap<String, XSSFWorkbook>();\r
+        workbooks.put(testFileSimple, simple);\r
+        workbooks.put("Re-Saved_" + testFileSimple, simpleRS);\r
+        // TODO Fix these to work!\r
+//        workbooks.put(testFileComplex, complex);\r
+//        workbooks.put("Re-Saved_" + testFileComplex, complexRS);\r
+        \r
+        // Sanity check\r
+        assertEquals(themeEntries.length, rgbExpected.length);\r
         \r
-        if (createFile) {\r
-            FileOutputStream fos = new FileOutputStream("foobaa.xlsx");\r
-            workbook.write(fos);\r
-            fos.close();\r
+        // For offline testing\r
+        boolean createFiles = false;\r
+        \r
+        // Check each workbook in turn, and verify that the colours\r
+        //  for the theme-applied cells in Column A are correct\r
+        for (String whatWorkbook : workbooks.keySet()) {\r
+            XSSFWorkbook workbook = workbooks.get(whatWorkbook);\r
+            XSSFSheet sheet = workbook.getSheetAt(0);\r
+            int startRN = 0;\r
+            if (whatWorkbook.endsWith(testFileComplex)) startRN++;\r
+            \r
+            for (int rn=startRN; rn<themeEntries.length+startRN; rn++) {\r
+                XSSFRow row = sheet.getRow(rn);\r
+                assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);\r
+                String ref = (new CellReference(rn, 0)).formatAsString();\r
+                XSSFCell cell = row.getCell(0);\r
+                assertNotNull(\r
+                        "Missing cell " + ref + " in " + whatWorkbook, cell);\r
+                assertEquals(\r
+                        "Wrong theme at " + ref + " in " + whatWorkbook,\r
+                        themeEntries[rn], cell.getStringCellValue());\r
+\r
+                XSSFFont font = cell.getCellStyle().getFont();\r
+                XSSFColor color = font.getXSSFColor();\r
+                \r
+                // Theme colours aren't tinted\r
+                assertEquals(false, color.hasTint());\r
+                // Check the RGB part (no tint)\r
+                assertEquals(\r
+                        "Wrong theme colour " + themeEntries[rn] + " on " + whatWorkbook,\r
+                        rgbExpected[rn], Hex.encodeHexString(color.getRGB()));\r
+                // Check the Theme ID\r
+                int expectedThemeIdx = rn - startRN;\r
+                long themeIdx = font.getCTFont().getColorArray(0).getTheme();\r
+                assertEquals(\r
+                        "Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook,\r
+                        expectedThemeIdx, themeIdx);\r
+                \r
+                if (createFiles) {\r
+                    XSSFCellStyle cs = row.getSheet().getWorkbook().createCellStyle();\r
+                    cs.setFillForegroundColor(color);\r
+                    cs.setFillPattern(CellStyle.SOLID_FOREGROUND);\r
+                    row.createCell(1).setCellStyle(cs);\r
+                }\r
+            }\r
+            \r
+            if (createFiles) {\r
+                FileOutputStream fos = new FileOutputStream("Generated_"+whatWorkbook);\r
+                workbook.write(fos);\r
+                fos.close();\r
+            }\r
         }\r
     }\r
     \r
+    // TODO Check the complex parts \r
+    \r
     @Test\r
+    @SuppressWarnings("resource")\r
     public void testAddNew() throws Exception {\r
         XSSFWorkbook wb = new XSSFWorkbook();\r
-        XSSFSheet s = wb.createSheet();\r
+        wb.createSheet();\r
         assertEquals(null, wb.getTheme());\r
         \r
         StylesTable styles = wb.getStylesSource();\r
@@ -97,4 +171,4 @@ public class TestThemesTable {
         assertNotNull(styles.getTheme());\r
         assertNotNull(wb.getTheme());\r
     }\r
-}
\ No newline at end of file
+}\r