xref: /AOO41X/test/testuno/source/fvt/uno/sd/bullet/GraphicBulletFromFile.java (revision 95c33bc250915b2aacbd07437eebf550ab475351)
1b4dcab30SLi Feng Wang /**************************************************************
2b4dcab30SLi Feng Wang  *
3b4dcab30SLi Feng Wang  * Licensed to the Apache Software Foundation (ASF) under one
4b4dcab30SLi Feng Wang  * or more contributor license agreements.  See the NOTICE file
5b4dcab30SLi Feng Wang  * distributed with this work for additional information
6b4dcab30SLi Feng Wang  * regarding copyright ownership.  The ASF licenses this file
7b4dcab30SLi Feng Wang  * to you under the Apache License, Version 2.0 (the
8b4dcab30SLi Feng Wang  * "License"); you may not use this file except in compliance
9b4dcab30SLi Feng Wang  * with the License.  You may obtain a copy of the License at
10b4dcab30SLi Feng Wang  *
11b4dcab30SLi Feng Wang  *   http://www.apache.org/licenses/LICENSE-2.0
12b4dcab30SLi Feng Wang  *
13b4dcab30SLi Feng Wang  * Unless required by applicable law or agreed to in writing,
14b4dcab30SLi Feng Wang  * software distributed under the License is distributed on an
15b4dcab30SLi Feng Wang  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b4dcab30SLi Feng Wang  * KIND, either express or implied.  See the License for the
17b4dcab30SLi Feng Wang  * specific language governing permissions and limitations
18b4dcab30SLi Feng Wang  * under the License.
19b4dcab30SLi Feng Wang  *
20b4dcab30SLi Feng Wang  *************************************************************/
21b4dcab30SLi Feng Wang /*
22b4dcab30SLi Feng Wang  * Select a external picture from a file as graphic bullet
23b4dcab30SLi Feng Wang  * */
24b4dcab30SLi Feng Wang package fvt.uno.sd.bullet;
25b4dcab30SLi Feng Wang import static org.junit.Assert.*;
26b4dcab30SLi Feng Wang import static org.openoffice.test.common.Testspace.prepareData;
27b4dcab30SLi Feng Wang import static testlib.uno.PageUtil.getDrawPageByIndex;
28b4dcab30SLi Feng Wang import static testlib.uno.ShapeUtil.addPortion;
29b4dcab30SLi Feng Wang import static testlib.uno.ShapeUtil.getPortion;
30b4dcab30SLi Feng Wang import static testlib.uno.GraphicUtil.getUniqueIDOfGraphicFile;
31b4dcab30SLi Feng Wang 
32b4dcab30SLi Feng Wang import java.io.File;
33b4dcab30SLi Feng Wang 
34b4dcab30SLi Feng Wang import org.junit.After;
35b4dcab30SLi Feng Wang import org.junit.AfterClass;
36b4dcab30SLi Feng Wang import org.junit.Before;
37b4dcab30SLi Feng Wang import org.junit.BeforeClass;
38b4dcab30SLi Feng Wang import org.junit.Test;
39b4dcab30SLi Feng Wang import org.openoffice.test.uno.UnoApp;
40b4dcab30SLi Feng Wang import org.openoffice.test.common.FileUtil;
41b4dcab30SLi Feng Wang import org.openoffice.test.common.Testspace;
42b4dcab30SLi Feng Wang 
43b4dcab30SLi Feng Wang import testlib.uno.SDUtil;
44b4dcab30SLi Feng Wang 
45b4dcab30SLi Feng Wang import com.sun.star.awt.Size;
46b4dcab30SLi Feng Wang import com.sun.star.beans.PropertyValue;
47b4dcab30SLi Feng Wang import com.sun.star.beans.XPropertySet;
48b4dcab30SLi Feng Wang import com.sun.star.container.XIndexReplace;
49b4dcab30SLi Feng Wang import com.sun.star.drawing.XDrawPage;
50b4dcab30SLi Feng Wang import com.sun.star.drawing.XShape;
51b4dcab30SLi Feng Wang import com.sun.star.lang.XComponent;
52b4dcab30SLi Feng Wang import com.sun.star.style.NumberingType;
53b4dcab30SLi Feng Wang import com.sun.star.uno.UnoRuntime;
54b4dcab30SLi Feng Wang 
55b4dcab30SLi Feng Wang public class GraphicBulletFromFile {
56b4dcab30SLi Feng Wang 
57b4dcab30SLi Feng Wang 	private static final UnoApp app = new UnoApp();
58b4dcab30SLi Feng Wang 
59b4dcab30SLi Feng Wang 	private XComponent m_xSDComponent = null;
60b4dcab30SLi Feng Wang 	private String m_filePath = null;
61b4dcab30SLi Feng Wang 	private XPropertySet m_xtextProps = null;
62b4dcab30SLi Feng Wang 	private String m_GraphicPath = null;
63b4dcab30SLi Feng Wang 
64b4dcab30SLi Feng Wang 	@Before
setUpDocument()65b4dcab30SLi Feng Wang 	public void setUpDocument() throws Exception {
66b4dcab30SLi Feng Wang 		m_filePath = Testspace.getPath("temp/GraphicBulletFromFile.odp");
67b4dcab30SLi Feng Wang 		String abslotePath = prepareData("uno/sd/36.gif");
68b4dcab30SLi Feng Wang 		m_GraphicPath = FileUtil.getUrl(new File(abslotePath));
69b4dcab30SLi Feng Wang //		m_GraphicPath = "file:///F:/work/36.gif";
70b4dcab30SLi Feng Wang 		if (FileUtil.fileExists(m_filePath)) {//load
71b4dcab30SLi Feng Wang 			m_xtextProps = load();
72b4dcab30SLi Feng Wang 		} else {//new
73b4dcab30SLi Feng Wang 			m_xSDComponent = (XComponent) UnoRuntime.queryInterface(
74b4dcab30SLi Feng Wang 					XComponent.class, app.newDocument("simpress"));
75b4dcab30SLi Feng Wang 			Object firstPage = getDrawPageByIndex(m_xSDComponent, 0);
76b4dcab30SLi Feng Wang 			Object firstTextBox = SDUtil.getShapeOfPageByIndex(firstPage, 0);
77b4dcab30SLi Feng Wang 			XShape xfirstTextBox = (XShape)UnoRuntime.queryInterface(XShape.class, firstTextBox);
78b4dcab30SLi Feng Wang 			m_xtextProps = addPortion(xfirstTextBox, "test Graphic Bullet From a File", false);
79b4dcab30SLi Feng Wang 		}
80b4dcab30SLi Feng Wang 	}
81b4dcab30SLi Feng Wang 
load()82b4dcab30SLi Feng Wang 	private XPropertySet load() throws Exception{
83b4dcab30SLi Feng Wang 		m_xSDComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
84b4dcab30SLi Feng Wang 				app.loadDocument(m_filePath));
85b4dcab30SLi Feng Wang 		Object firstPage = getDrawPageByIndex(m_xSDComponent, 0);
86b4dcab30SLi Feng Wang 		XDrawPage firstpage = getDrawPageByIndex(m_xSDComponent, 0);
87b4dcab30SLi Feng Wang 		Object firstTextBox = SDUtil.getShapeOfPageByIndex(firstPage, 0);
88b4dcab30SLi Feng Wang 		XShape xfirstTextBox = (XShape)UnoRuntime.queryInterface(XShape.class, firstTextBox);
89b4dcab30SLi Feng Wang 		return getPortion(xfirstTextBox, 0);
90b4dcab30SLi Feng Wang 	}
91b4dcab30SLi Feng Wang 
92b4dcab30SLi Feng Wang 	@After
tearDownDocument()93b4dcab30SLi Feng Wang 	public void tearDownDocument() {
94b4dcab30SLi Feng Wang 		app.closeDocument(m_xSDComponent);
95b4dcab30SLi Feng Wang 
96b4dcab30SLi Feng Wang 	}
97b4dcab30SLi Feng Wang 
98b4dcab30SLi Feng Wang 	@BeforeClass
setUpConnection()99b4dcab30SLi Feng Wang 	public static void setUpConnection() throws Exception {
100b4dcab30SLi Feng Wang 		app.start();
101b4dcab30SLi Feng Wang 	}
102b4dcab30SLi Feng Wang 
103b4dcab30SLi Feng Wang 	@AfterClass
tearDownConnection()104b4dcab30SLi Feng Wang 	public static void tearDownConnection() throws InterruptedException,
105b4dcab30SLi Feng Wang 			Exception {
106b4dcab30SLi Feng Wang 		app.close();
107b4dcab30SLi Feng Wang 		//remove the temp file
108b4dcab30SLi Feng Wang 		FileUtil.deleteFile(Testspace.getPath("temp"));
109b4dcab30SLi Feng Wang 	}
110b4dcab30SLi Feng Wang 
111b4dcab30SLi Feng Wang 	@Test
testGraphicBulletFromFile()112b4dcab30SLi Feng Wang 	public void testGraphicBulletFromFile() throws Exception {
113b4dcab30SLi Feng Wang 
114b4dcab30SLi Feng Wang 		Object numberingrules = m_xtextProps.getPropertyValue("NumberingRules");
115b4dcab30SLi Feng Wang 
116b4dcab30SLi Feng Wang 		XIndexReplace xReplace = (XIndexReplace) UnoRuntime.queryInterface(
117b4dcab30SLi Feng Wang 	             XIndexReplace.class, numberingrules);
118b4dcab30SLi Feng Wang 
119b4dcab30SLi Feng Wang 		PropertyValue[] props = new PropertyValue[3];
120b4dcab30SLi Feng Wang 		props[0] = new PropertyValue();
121b4dcab30SLi Feng Wang 	    props[0].Name = "NumberingType";
122b4dcab30SLi Feng Wang 	    props[0].Value = new Short(NumberingType.BITMAP );
123b4dcab30SLi Feng Wang 
124b4dcab30SLi Feng Wang 	    props[1] = new PropertyValue();
125b4dcab30SLi Feng Wang 	    props[1].Name = "GraphicURL";
126b4dcab30SLi Feng Wang 	    props[1].Value = "vnd.sun.star.GraphicObject:"+getUniqueIDOfGraphicFile(app, m_GraphicPath);
127b4dcab30SLi Feng Wang 
128b4dcab30SLi Feng Wang 	    props[2] = new PropertyValue();
129b4dcab30SLi Feng Wang 	    props[2].Name = "GraphicSize";
130b4dcab30SLi Feng Wang 	    props[2].Value = new Size(1000,1000);
131b4dcab30SLi Feng Wang 
132b4dcab30SLi Feng Wang 	    xReplace.replaceByIndex(0, props);
133b4dcab30SLi Feng Wang 
134b4dcab30SLi Feng Wang 	    m_xtextProps.setPropertyValue("NumberingRules", numberingrules);
135b4dcab30SLi Feng Wang 		  //set numbering level to 0
136b4dcab30SLi Feng Wang 		m_xtextProps.setPropertyValue("NumberingLevel", new Short((short)0));
137b4dcab30SLi Feng Wang 
138b4dcab30SLi Feng Wang 
139b4dcab30SLi Feng Wang 		app.saveDocument(m_xSDComponent, m_filePath);
140b4dcab30SLi Feng Wang //			app.closeDocument(m_xSDComponent);
141b4dcab30SLi Feng Wang 		m_xSDComponent.dispose();
142b4dcab30SLi Feng Wang 			//reopen
143b4dcab30SLi Feng Wang 		m_xtextProps = load();
144b4dcab30SLi Feng Wang 
145b4dcab30SLi Feng Wang 		Object numberingrules2 = m_xtextProps.getPropertyValue("NumberingRules");
146b4dcab30SLi Feng Wang 
147b4dcab30SLi Feng Wang 		XIndexReplace xReplace2 = (XIndexReplace) UnoRuntime.queryInterface(
148b4dcab30SLi Feng Wang 		            XIndexReplace.class, numberingrules2);
149b4dcab30SLi Feng Wang 
150b4dcab30SLi Feng Wang 		PropertyValue[] proValues2 = (PropertyValue[])xReplace2.getByIndex(0);
151*f331e23fSHerbert Dürr 		int nNumTypeItemIndex = -1;
152*f331e23fSHerbert Dürr 		int nGraphObjItemIndex = -1;
153*f331e23fSHerbert Dürr 		for( int i = 0; i < proValues2.length; ++i) {
154*f331e23fSHerbert Dürr 				final String aPropName = proValues2[i].Name;
155*f331e23fSHerbert Dürr 				if( aPropName.equals( "GraphicURL"))
156*f331e23fSHerbert Dürr 						nGraphObjItemIndex = i;
157*f331e23fSHerbert Dürr 				else if( aPropName.equals( "NumberingType"))
158*f331e23fSHerbert Dürr 						nNumTypeItemIndex = i;
159*f331e23fSHerbert Dürr 		}
160*f331e23fSHerbert Dürr 		assertEquals("NumberingType should be BITMAP",
161*f331e23fSHerbert Dürr 				NumberingType.BITMAP, proValues2[ nNumTypeItemIndex].Value);
162b4dcab30SLi Feng Wang 		String uniqueID = getUniqueIDOfGraphicFile(app, m_GraphicPath);
163*f331e23fSHerbert Dürr 		assertEquals("Graphic should be the one with uniqueID"+uniqueID,
164*f331e23fSHerbert Dürr 				"vnd.sun.star.GraphicObject:"+uniqueID, proValues2[ nGraphObjItemIndex].Value);
165*f331e23fSHerbert Dürr 	}
166*f331e23fSHerbert Dürr }
167b4dcab30SLi Feng Wang 
168