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