xref: /trunk/test/testuno/source/fvt/uno/sd/character/CheckCharacterStyle.java (revision de51bb974913f850f1fcb3f9223d11f8f1c098f2)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 /**
22  * check character style
23  * 1. new a impress
24  * 2. insert one line text in the first textbox
25  * 3. set the font color to red
26  * 4. save, close, reopen, then check the font color
27  * 5. set the underline to single
28  * 6. save, close, reopen, then check the underline
29  * 7. set the font size to 12
30  * 8. save, close, reopen, then check the font size
31  * 9. set font style to Bold, Italic
32  * 10. save, close, reopen, then check the font style
33  */
34 package fvt.uno.sd.character;
35 
36 import static org.junit.Assert.*;
37 
38 import java.io.File;
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.openoffice.test.common.FileUtil;
45 import org.openoffice.test.common.Testspace;
46 import org.openoffice.test.uno.UnoApp;
47 
48 import testlib.uno.SDUtil;
49 
50 import com.sun.star.beans.PropertyValue;
51 import com.sun.star.beans.XPropertySet;
52 import com.sun.star.container.XIndexAccess;
53 import com.sun.star.drawing.XDrawPage;
54 import com.sun.star.drawing.XDrawPages;
55 import com.sun.star.drawing.XDrawPagesSupplier;
56 import com.sun.star.drawing.XShapes;
57 import com.sun.star.frame.XStorable;
58 
59 import com.sun.star.lang.XComponent;
60 
61 import com.sun.star.text.XText;
62 import com.sun.star.uno.UnoRuntime;
63 
64 /**
65  * @author LouQL
66  *
67  */
68 public class CheckCharacterStyle {
69 
70     private static final UnoApp app = new UnoApp();
71     private XComponent m_xSDComponent = null;
72     private XText xShapeText = null;
73     private String filePath = null;
74     private XPropertySet xtextProps = null;
75     /**
76      * @throws java.lang.Exception
77      */
78     @BeforeClass
79     public static void setUpConnection() throws Exception {
80         app.start();
81         File temp = new File(Testspace.getPath("temp"));
82         temp.mkdirs();
83     }
84 
85     @AfterClass
86     public static void tearDownConnection() throws Exception {
87         app.close();
88         //remove the temp file
89         FileUtil.deleteFile(Testspace.getPath("temp"));
90     }
91 
92     /**
93      * @throws java.lang.Exception
94      */
95     @Before
96     public void setUp() throws Exception {
97         filePath = Testspace.getPath("temp/CheckCharacterStyle.odt");
98         if(FileUtil.fileExists(filePath))
99         {   //load
100             m_xSDComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
101                         app.loadDocument(filePath));
102             xShapeText = getFirstTextbox();
103         }
104         else{
105             //create a sd
106             m_xSDComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, app.newDocument("simpress"));
107             xShapeText = getFirstTextbox();
108             xShapeText.setString("test");
109         }
110         xtextProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShapeText);
111     }
112 
113     private XText getFirstTextbox() throws Exception
114     {
115         Object firstPage = SDUtil.getPageByIndex(m_xSDComponent, 0);
116         Object firstTextBox = SDUtil.getShapeOfPageByIndex(firstPage, 0);
117         return (XText)UnoRuntime.queryInterface(XText.class, firstTextBox);
118     }
119 
120     /**
121      * @throws java.lang.Exception
122      */
123     @After
124     public void tearDown() throws Exception {
125         //close odp after each test
126         m_xSDComponent.dispose();
127     }
128 
129     @Test
130     public void testFontColor() throws Exception{
131         //set font color to red
132         xtextProps.setPropertyValue("CharColor", 0xFF0000);
133         app.saveDocument(m_xSDComponent, filePath);
134         m_xSDComponent.dispose();
135         //reopen
136         m_xSDComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
137                     app.loadDocument(filePath));
138         xShapeText = getFirstTextbox();
139         xtextProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShapeText);
140         //check character styles
141         assertEquals("character color should be red", 0xFF0000,xtextProps.getPropertyValue("CharColor"));
142 
143     }
144     @Test
145     public void testFontUnderline() throws Exception{
146         //set font color to red
147         xtextProps.setPropertyValue("CharUnderline", com.sun.star.awt.FontUnderline.SINGLE);
148         app.saveDocument(m_xSDComponent, filePath);
149         m_xSDComponent.dispose();
150         //reopen
151         m_xSDComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
152                     app.loadDocument(filePath));
153         xShapeText = getFirstTextbox();
154         xtextProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShapeText);
155         //check character styles
156         assertEquals("character should be underlined", com.sun.star.awt.FontUnderline.SINGLE, xtextProps.getPropertyValue("CharUnderline"));
157     }
158 
159     @Test
160     public void testFontSize() throws Exception{
161         //set font color to red
162         xtextProps.setPropertyValue("CharHeight", 12);
163         app.saveDocument(m_xSDComponent, filePath);
164         m_xSDComponent.dispose();
165         //reopen
166         m_xSDComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
167                     app.loadDocument(filePath));
168         xShapeText = getFirstTextbox();
169         xtextProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShapeText);
170         //check character styles
171         assertEquals("font size should be 12.0", "12.0", xtextProps.getPropertyValue("CharHeight").toString());
172     }
173     @Test
174     public void testFontBoldStyle() throws Exception  {
175         //change the font style to Bold
176         xtextProps.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.BOLD);
177         app.saveDocument(m_xSDComponent, filePath);
178         m_xSDComponent.dispose();
179         //reopen
180         m_xSDComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
181                     app.loadDocument(filePath));
182         xShapeText = getFirstTextbox();
183         xtextProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShapeText);
184         assertEquals("font style should be bold", com.sun.star.awt.FontWeight.BOLD, xtextProps.getPropertyValue("CharWeight"));
185     }
186 
187     @Test
188     public void testFontItalicStyle() throws Exception  {
189         //change the font style to Bold
190         xtextProps.setPropertyValue("CharPosture", com.sun.star.awt.FontSlant.ITALIC);
191         app.saveDocument(m_xSDComponent, filePath);
192         m_xSDComponent.dispose();
193         //reopen
194         m_xSDComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
195                     app.loadDocument(filePath));
196         xShapeText = getFirstTextbox();
197         xtextProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShapeText);
198         assertEquals("font style should be bold", com.sun.star.awt.FontSlant.ITALIC, xtextProps.getPropertyValue("CharPosture"));
199     }
200 }
201