]> source.dussan.org Git - poi.git/commitdiff
Tests for fixes associated with Bug 39324
authorNick Burch <nick@apache.org>
Fri, 21 Apr 2006 13:45:04 +0000 (13:45 +0000)
committerNick Burch <nick@apache.org>
Fri, 21 Apr 2006 13:45:04 +0000 (13:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@395882 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/testcases/org/apache/poi/hslf/model/TestSetBoldItalic.java [new file with mode: 0644]
src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java

diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestSetBoldItalic.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestSetBoldItalic.java
new file mode 100644 (file)
index 0000000..b6b6d1d
--- /dev/null
@@ -0,0 +1,86 @@
+/* ====================================================================\r
+   Copyright 2002-2004   Apache Software Foundation\r
+\r
+   Licensed under the Apache License, Version 2.0 (the "License");\r
+   you may not use this file except in compliance with the License.\r
+   You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.hslf.model;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.hslf.usermodel.SlideShow;\r
+import org.apache.poi.hslf.usermodel.RichTextRun;\r
+import org.apache.poi.hslf.HSLFSlideShow;\r
+\r
+import java.awt.*;\r
+import java.awt.Rectangle;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.ByteArrayInputStream;\r
+import java.io.FileOutputStream;\r
+import java.util.ArrayList;\r
+\r
+/**\r
+ * Test setting text properties of newly added TextBoxes\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestSetBoldItalic extends TestCase {\r
+    /**\r
+     * Verify that we can add TextBox shapes to a slide\r
+     * and set some of the style attributes\r
+     */\r
+    public void testTextBoxWrite() throws Exception {\r
+        //String dirname = System.getProperty("HSLF.testdata.path");\r
+        //String filename = dirname + "/with_textbox.ppt";\r
+        //new SlideShow(new HSLFSlideShow(filename));\r
+\r
+        SlideShow ppt = new SlideShow();\r
+        Slide sl = ppt.createSlide();\r
+        RichTextRun rt;\r
+\r
+        String val = "Hello, World!";\r
+\r
+        // Create a new textbox, and give it lots of properties\r
+        TextBox txtbox = new TextBox();\r
+        txtbox.setText(val);\r
+        txtbox.setFontSize(42);\r
+        txtbox.setBold(true);\r
+        txtbox.setItalic(true);\r
+        txtbox.setUnderline(false);\r
+        sl.addShape(txtbox);\r
+\r
+        // Check it before save\r
+        rt = txtbox.getRichTextRuns()[0];\r
+        assertEquals(val, rt.getText());\r
+        assertEquals(42, rt.getFontSize());\r
+        assertTrue(rt.isBold());\r
+        assertTrue(rt.isItalic());\r
+\r
+        // Serialize and read again\r
+        ByteArrayOutputStream out = new ByteArrayOutputStream();\r
+        ppt.write(out);\r
+        out.close();\r
+\r
+        ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));\r
+        sl = ppt.getSlides()[0];\r
+\r
+        txtbox = (TextBox)sl.getShapes()[0];\r
+        rt = txtbox.getRichTextRuns()[0];\r
+\r
+        // Check after save\r
+        assertEquals(val, rt.getText());\r
+        assertEquals(42, rt.getFontSize());\r
+        assertTrue(rt.isBold());\r
+        assertTrue(rt.isItalic());\r
+        assertFalse(rt.isUnderlined());\r
+    }\r
+\r
+}\r
index bcecb790518b288dba609f49e8d66f2c09916289..83e4636a94e7663163da5e074e10d93357f281e1 100644 (file)
@@ -52,26 +52,55 @@ public class TestStyleTextPropAtom extends TestCase {
        // left aligned+bold (30)
        // centre aligned+italic+blue (28)
        // right aligned+red (25)
-       // left aligned+underlined+larger font size (97)
+       // left aligned+underlined+larger font size (96)
+       // left aligned+underlined+larger font size+red (1)
        private byte[] data_b = new byte[] { 
-               00, 00, 0xA1-256, 0x0F, 0x80-256, 00, 
-               00, 00, 0x1E, 00, 00, 00, 00, 00,
-               00, 0x18, 00, 00, 00, 00, 0x50, 00,
-               0x1C, 00, 00, 00, 00, 00, 00, 0x10,
-           00, 00, 0x50, 00, 0x19, 00, 00, 00,
-           00, 00, 00, 0x18, 00, 00, 02, 00,
-           0x50, 00, 0x61, 00, 00, 00, 00, 00,
-           00, 0x18, 00, 00, 00, 00, 0x50, 00,
-           0x1E, 00, 00, 00, 01, 00, 02, 00,
-           01, 00, 0x14, 00, 0x1C, 00, 00, 00,
-           02, 00, 06, 00, 02, 00, 0x14, 00,
-           00, 00, 00, 05, 0x19, 00, 00, 00,
-           00, 00, 06, 00, 0x14, 00, 0xFF-256, 0x33,
-           00, 0xFE-256, 0x60, 00, 00, 00, 04, 00,
-           03, 00, 04, 00, 01, 00, 0x18, 00,
-           01, 00, 00, 00, 04, 00, 07, 00,
-           04, 00, 01, 00, 0x18, 00, 0xFF-256, 0x33,
-           00, 0xFE-256
+               0, 0, 0xA1-256, 0x0F, 0x80-256, 0, 0, 0, 
+               0x1E, 00, 00, 00,     // paragraph is 30 long 
+               00, 00,               // paragraph reserved field
+               00, 0x18, 00, 00,     // mask is 0x1800
+               00, 00,               // left aligned
+               0x50, 00,             // line spacing 80
+               0x1C, 00, 00, 00,     // paragprah is 28 long 
+               00, 00,               // paragraph reserved field
+               00, 0x10, 00, 00,     // mask is 0x1000
+               0x50, 00,             // line spacing 80
+               0x19, 00, 00, 00,     // paragraph is 25 long
+           00, 00,               // paragraph reserved field
+           00, 0x18, 00, 00,     // mask is 0x1800 
+           02, 00,               // right aligned
+           0x50, 00,             // line spacing 80
+           0x61, 00, 00, 00,     // paragraph is 97 long
+           00, 00,               // paragraph reserved field
+           00, 0x18, 00, 00,     // mask is 0x1800
+           00, 00,               // left aligned
+           0x50, 00,             // line spacing 80
+           
+           0x1E, 00, 00, 00,     // character run is 30 long
+           01, 00, 02, 00,       // mask is 0x020001
+           01, 00,               // char flags 0x0001 = bold
+           0x14, 00,             // font size 20
+           0x1C, 00, 00, 00,     // character run is 28 long
+           02, 00, 06, 00,       // mask is 0x060002
+           02, 00,               // char flags 0x0002 = italic
+           0x14, 00,             // font size 20
+           00, 00, 00, 05,       // colour blue
+           0x19, 00, 00, 00,     // character run is 25 long
+           00, 00, 06, 00,       // char flags 0x060000
+           0x14, 00,             // font size 20
+           0xFF-256, 0x33, 00, 0xFE-256, // colour red
+           0x60, 00, 00, 00,     // character run is 96 long
+           04, 00, 03, 00,       // mask is 0x030004
+           04, 00,               // char flags 0x0004 = underlined
+           01, 00,               // font index is 1
+           0x18, 00,             // font size 24
+           
+           01, 00, 00, 00,       // character run is 1 long
+           04, 00, 07, 00,       // mask is 0x070004
+           04, 00,               // char flags 0x0004 = underlined
+           01, 00,               // font index is 1 
+           0x18, 00,             // font size 24
+           0xFF-256, 0x33, 00, 0xFE-256 // colour red
        };
        private int data_b_text_len = 0xB3;
        
@@ -94,8 +123,8 @@ public class TestStyleTextPropAtom extends TestCase {
 
                // In case A, there is a single styling of the characters
                assertEquals(3, stpa.getCharacterStyles().size());
-               // In case B, there are 4 different stylings
-               assertEquals(4, stpb.getCharacterStyles().size());
+               // In case B, there are 5 different stylings
+               assertEquals(5, stpb.getCharacterStyles().size());
        }
 
        public void testParagraphStyleCounts() throws Exception {
@@ -371,10 +400,10 @@ public class TestStyleTextPropAtom extends TestCase {
        }
        
        /**
-        * Try to recreate an existing StyleTextPropAtom from the empty
+        * Try to recreate an existing StyleTextPropAtom (a) from the empty
         *  constructor, and setting the required properties
         */
-       public void testCreateFromScatch() throws Exception {
+       public void testCreateAFromScatch() throws Exception {
                // Start with an empty one
                StyleTextPropAtom stpa = new StyleTextPropAtom(54);
                
@@ -387,16 +416,14 @@ public class TestStyleTextPropAtom extends TestCase {
                tpca.updateTextSize(21);
                
                // Second char style is coloured, 00 00 00 05, and 17 long
-               TextPropCollection tpcb = new TextPropCollection(17);
+               TextPropCollection tpcb = stpa.addCharacterTextPropCollection(17);
                TextProp tpb = tpcb.addWithName("font.color");
                tpb.setValue(0x05000000);
-               cs.add(tpcb);
                
                // Third char style is coloured, FF 33 00 FE, and 16 long
-               TextPropCollection tpcc = new TextPropCollection(16);
+               TextPropCollection tpcc = stpa.addCharacterTextPropCollection(16);
                TextProp tpc = tpcc.addWithName("font.color");
                tpc.setValue(0xFE0033FF);
-               cs.add(tpcc);
                
                // Should now be the same as data_a
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -409,6 +436,179 @@ public class TestStyleTextPropAtom extends TestCase {
                }
        }
 
+       /**
+        * Try to recreate an existing StyleTextPropAtom (b) from the empty
+        *  constructor, and setting the required properties
+        */
+       public void testCreateBFromScatch() throws Exception {
+               // Start with an empty one
+               StyleTextPropAtom stpa = new StyleTextPropAtom(data_b_text_len);
+               
+               
+               // Need 4 paragraph styles
+               LinkedList ps = stpa.getParagraphStyles();
+               
+               // First is 30 long, left aligned, normal spacing
+               TextPropCollection tppa = (TextPropCollection)ps.get(0);
+               tppa.updateTextSize(30);
+               
+               TextProp tp = tppa.addWithName("alignment");
+               tp.setValue(0);
+               tp = tppa.addWithName("linespacing");
+               tp.setValue(80);
+
+               // Second is 28 long, centre aligned and normal spacing
+               TextPropCollection tppb = stpa.addParagraphTextPropCollection(28);
+               
+               tp = tppb.addWithName("linespacing");
+               tp.setValue(80);
+               
+               // Third is 25 long, right aligned and normal spacing
+               TextPropCollection tppc = stpa.addParagraphTextPropCollection(25);
+               
+               tp = tppc.addWithName("alignment");
+               tp.setValue(2);
+               tp = tppc.addWithName("linespacing");
+               tp.setValue(80);
+               
+               // Forth is left aligned + normal line spacing (despite differing font)
+               TextPropCollection tppd = stpa.addParagraphTextPropCollection(97);
+               
+               tp = tppd.addWithName("alignment");
+               tp.setValue(0);
+               tp = tppd.addWithName("linespacing");
+               tp.setValue(80);
+               
+               
+               // Now do 4 character styles
+               LinkedList cs = stpa.getCharacterStyles();
+               
+               // First is 30 long, bold and font size
+               TextPropCollection tpca = (TextPropCollection)cs.get(0);
+               tpca.updateTextSize(30);
+               
+               tp = tpca.addWithName("font.size");
+               tp.setValue(20);
+               CharFlagsTextProp cftp = (CharFlagsTextProp)
+                       tpca.addWithName("char_flags");
+               assertEquals(0, cftp.getValue());
+               cftp.setSubValue(true, CharFlagsTextProp.BOLD_IDX);
+               assertEquals(1, cftp.getValue());
+               
+               // Second is 28 long, blue and italic
+               TextPropCollection tpcb = stpa.addCharacterTextPropCollection(28);
+               
+               tp = tpcb.addWithName("font.size");
+               tp.setValue(20);
+               tp = tpcb.addWithName("font.color");
+               tp.setValue(0x05000000);
+               cftp = (CharFlagsTextProp)tpcb.addWithName("char_flags");
+               cftp.setSubValue(true, CharFlagsTextProp.ITALIC_IDX);
+               assertEquals(2, cftp.getValue());
+               
+               // Third is 25 long and red
+               TextPropCollection tpcc = stpa.addCharacterTextPropCollection(25);
+               
+               tp = tpcc.addWithName("font.size");
+               tp.setValue(20);
+               tp = tpcc.addWithName("font.color");
+               tp.setValue(0xfe0033ff);
+               
+               // Fourth is 96 long, underlined and different+bigger font 
+               TextPropCollection tpcd = stpa.addCharacterTextPropCollection(96);
+               
+               tp = tpcd.addWithName("font.size");
+               tp.setValue(24);
+               tp = tpcd.addWithName("font.index");
+               tp.setValue(1);
+               cftp = (CharFlagsTextProp)tpcd.addWithName("char_flags");
+               cftp.setSubValue(true, CharFlagsTextProp.UNDERLINE_IDX);
+               assertEquals(4, cftp.getValue());
+               
+               // Fifth is 1 long, underlined and different+bigger font + red
+               TextPropCollection tpce = stpa.addCharacterTextPropCollection(1);
+               
+               tp = tpce.addWithName("font.size");
+               tp.setValue(24);
+               tp = tpce.addWithName("font.index");
+               tp.setValue(1);
+               tp = tpce.addWithName("font.color");
+               tp.setValue(0xfe0033ff);
+               cftp = (CharFlagsTextProp)tpce.addWithName("char_flags");
+               cftp.setSubValue(true, CharFlagsTextProp.UNDERLINE_IDX);
+               assertEquals(4, cftp.getValue());
+               
+               
+               // Check it's as expected
+               assertEquals(4, stpa.getParagraphStyles().size());
+               assertEquals(5, stpa.getCharacterStyles().size());
+               
+               // Compare in detail to b
+               StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length);
+               stpb.setParentTextSize(data_b_text_len);
+               LinkedList psb = stpb.getParagraphStyles();
+               LinkedList csb = stpb.getCharacterStyles();
+               
+               assertEquals(psb.size(), ps.size());
+               assertEquals(csb.size(), cs.size());
+               
+               // Ensure Paragraph Character styles match
+               for(int z=0; z<2; z++) {
+                       LinkedList lla = cs; 
+                       LinkedList llb = csb;
+                       int upto = 5;
+                       if(z == 1) {
+                               lla = ps;
+                               llb = psb;
+                               upto = 4;
+                       }
+                       
+                       for(int i=0; i<upto; i++) {
+                               TextPropCollection ca = (TextPropCollection)lla.get(i);
+                               TextPropCollection cb = (TextPropCollection)llb.get(i);
+                               
+                               assertEquals(ca.getCharactersCovered(), cb.getCharactersCovered());
+                               assertEquals(ca.getTextPropList().size(), cb.getTextPropList().size());
+                               
+                               for(int j=0; j<ca.getTextPropList().size(); j++) {
+                                       TextProp tpa = (TextProp)ca.getTextPropList().get(j);
+                                       TextProp tpb = (TextProp)cb.getTextPropList().get(j);
+                                       //System.out.println("TP " + i + " " + j + " " + tpa.getName() + "\t" + tpa.getValue() );
+                                       assertEquals(tpa.getName(), tpb.getName());
+                                       assertEquals(tpa.getMask(), tpb.getMask());
+                                       assertEquals(tpa.getWriteMask(), tpb.getWriteMask());
+                                       assertEquals(tpa.getValue(), tpb.getValue());
+                               }
+                               
+                               ByteArrayOutputStream ba = new ByteArrayOutputStream();
+                               ByteArrayOutputStream bb = new ByteArrayOutputStream();
+                               
+                               ca.writeOut(ba);
+                               cb.writeOut(bb);
+                               byte[] cab = ba.toByteArray(); 
+                               byte[] cbb = bb.toByteArray();
+                               
+                               assertEquals(cbb.length, cab.length);
+                               for(int j=0; j<cab.length; j++) {
+                                       //System.out.println("On tp " + z + " " + i + " " + j + "\t" + cab[j] + "\t" + cbb[j]);
+                                       assertEquals(cbb[j], cab[j]);
+                               }
+                       }
+               }
+               
+
+               
+               // Check byte level with b
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               stpa.writeOut(baos);
+               byte[] b = baos.toByteArray();
+
+               assertEquals(data_b.length, b.length);
+               for(int i=0; i<data_b.length; i++) {
+                       System.out.println(i + "\t" + b[i] + "\t" + data_b[i] + "\t" + Integer.toHexString(b[i]) );
+                       assertEquals(data_b[i],b[i]);
+               }
+       }
 
        public void testWriteA() throws Exception {
                StyleTextPropAtom stpa = new StyleTextPropAtom(data_a,0,data_a.length);
@@ -459,7 +659,7 @@ public class TestStyleTextPropAtom extends TestCase {
 
                assertEquals(data_b.length, b.length);
                for(int i=0; i<data_b.length; i++) {
-                       System.out.println(i + "\t" + b[i] + "\t" + data_b[i] + "\t" + Integer.toHexString(b[i]) );
+                       //System.out.println(i + "\t" + b[i] + "\t" + data_b[i] + "\t" + Integer.toHexString(b[i]) );
                        assertEquals(data_b[i],b[i]);
                }
        }