poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1694093 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/usermodel/XSSFColor.java testcases/org/apache/poi/xssf/model/TestThemesTable.java
Date Tue, 04 Aug 2015 17:50:45 GMT
Author: nick
Date: Tue Aug  4 17:50:44 2015
New Revision: 1694093

URL: http://svn.apache.org/r1694093
Log:
Further XSSF Themes unit testing

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

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java?rev=1694093&r1=1694092&r2=1694093&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java Tue Aug  4 17:50:44
2015
@@ -88,6 +88,14 @@ public class XSSFColor extends ExtendedC
     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.

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java?rev=1694093&r1=1694092&r2=1694093&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java Tue Aug 
4 17:50:44 2015
@@ -21,11 +21,14 @@ import static org.junit.Assert.assertEqu
 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.XSS
 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);
+        
+        // Sanity check
+        assertEquals(themeEntries.length, rgbExpected.length);
         
-        if (createFile) {
-            FileOutputStream fos = new FileOutputStream("foobaa.xlsx");
-            workbook.write(fos);
-            fos.close();
+        // 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());
     }
-}
\ No newline at end of file
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message