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  * Select a external picture from a file as graphic bullet
23  * */
24 package fvt.uno.sd.bullet;
25 import static org.junit.Assert.*;
26 import static org.openoffice.test.common.Testspace.prepareData;
27 import static testlib.uno.PageUtil.getDrawPageByIndex;
28 import static testlib.uno.ShapeUtil.addPortion;
29 import static testlib.uno.ShapeUtil.getPortion;
30 import static testlib.uno.GraphicUtil.getUniqueIDOfGraphicFile;
31 
32 import java.io.File;
33 
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.openoffice.test.uno.UnoApp;
40 import org.openoffice.test.common.FileUtil;
41 import org.openoffice.test.common.Testspace;
42 
43 import testlib.uno.SDUtil;
44 
45 import com.sun.star.awt.Size;
46 import com.sun.star.beans.PropertyValue;
47 import com.sun.star.beans.XPropertySet;
48 import com.sun.star.container.XIndexReplace;
49 import com.sun.star.drawing.XDrawPage;
50 import com.sun.star.drawing.XShape;
51 import com.sun.star.lang.XComponent;
52 import com.sun.star.style.NumberingType;
53 import com.sun.star.uno.UnoRuntime;
54 
55 public class GraphicBulletFromFile {
56 
57 	private static final UnoApp app = new UnoApp();
58 
59 	private XComponent m_xSDComponent = null;
60 	private String m_filePath = null;
61 	private XPropertySet m_xtextProps = null;
62 	private String m_GraphicPath = null;
63 
64 	@Before
setUpDocument()65 	public void setUpDocument() throws Exception {
66 		m_filePath = Testspace.getPath("temp/GraphicBulletFromFile.odp");
67 		String abslotePath = prepareData("uno/sd/36.gif");
68 		m_GraphicPath = FileUtil.getUrl(new File(abslotePath));
69 //		m_GraphicPath = "file:///F:/work/36.gif";
70 		if (FileUtil.fileExists(m_filePath)) {//load
71 			m_xtextProps = load();
72 		} else {//new
73 			m_xSDComponent = (XComponent) UnoRuntime.queryInterface(
74 					XComponent.class, app.newDocument("simpress"));
75 			Object firstPage = getDrawPageByIndex(m_xSDComponent, 0);
76 			Object firstTextBox = SDUtil.getShapeOfPageByIndex(firstPage, 0);
77 			XShape xfirstTextBox = (XShape)UnoRuntime.queryInterface(XShape.class, firstTextBox);
78 			m_xtextProps = addPortion(xfirstTextBox, "test Graphic Bullet From a File", false);
79 		}
80 	}
81 
load()82 	private XPropertySet load() throws Exception{
83 		m_xSDComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
84 				app.loadDocument(m_filePath));
85 		Object firstPage = getDrawPageByIndex(m_xSDComponent, 0);
86 		XDrawPage firstpage = getDrawPageByIndex(m_xSDComponent, 0);
87 		Object firstTextBox = SDUtil.getShapeOfPageByIndex(firstPage, 0);
88 		XShape xfirstTextBox = (XShape)UnoRuntime.queryInterface(XShape.class, firstTextBox);
89 		return getPortion(xfirstTextBox, 0);
90 	}
91 
92 	@After
tearDownDocument()93 	public void tearDownDocument() {
94 		app.closeDocument(m_xSDComponent);
95 
96 	}
97 
98 	@BeforeClass
setUpConnection()99 	public static void setUpConnection() throws Exception {
100 		app.start();
101 	}
102 
103 	@AfterClass
tearDownConnection()104 	public static void tearDownConnection() throws InterruptedException,
105 			Exception {
106 		app.close();
107 		//remove the temp file
108 		FileUtil.deleteFile(Testspace.getPath("temp"));
109 	}
110 
111 	@Test
testGraphicBulletFromFile()112 	public void testGraphicBulletFromFile() throws Exception {
113 
114 		Object numberingrules = m_xtextProps.getPropertyValue("NumberingRules");
115 
116 		XIndexReplace xReplace = (XIndexReplace) UnoRuntime.queryInterface(
117 	             XIndexReplace.class, numberingrules);
118 
119 		PropertyValue[] props = new PropertyValue[3];
120 		props[0] = new PropertyValue();
121 	    props[0].Name = "NumberingType";
122 	    props[0].Value = new Short(NumberingType.BITMAP );
123 
124 	    props[1] = new PropertyValue();
125 	    props[1].Name = "GraphicURL";
126 	    props[1].Value = "vnd.sun.star.GraphicObject:"+getUniqueIDOfGraphicFile(app, m_GraphicPath);
127 
128 	    props[2] = new PropertyValue();
129 	    props[2].Name = "GraphicSize";
130 	    props[2].Value = new Size(1000,1000);
131 
132 	    xReplace.replaceByIndex(0, props);
133 
134 	    m_xtextProps.setPropertyValue("NumberingRules", numberingrules);
135 		  //set numbering level to 0
136 		m_xtextProps.setPropertyValue("NumberingLevel", new Short((short)0));
137 
138 
139 		app.saveDocument(m_xSDComponent, m_filePath);
140 //			app.closeDocument(m_xSDComponent);
141 		m_xSDComponent.dispose();
142 			//reopen
143 		m_xtextProps = load();
144 
145 		Object numberingrules2 = m_xtextProps.getPropertyValue("NumberingRules");
146 
147 		XIndexReplace xReplace2 = (XIndexReplace) UnoRuntime.queryInterface(
148 		            XIndexReplace.class, numberingrules2);
149 
150 		PropertyValue[] proValues2 = (PropertyValue[])xReplace2.getByIndex(0);
151 		int nNumTypeItemIndex = -1;
152 		int nGraphObjItemIndex = -1;
153 		for( int i = 0; i < proValues2.length; ++i) {
154 				final String aPropName = proValues2[i].Name;
155 				if( aPropName.equals( "GraphicURL"))
156 						nGraphObjItemIndex = i;
157 				else if( aPropName.equals( "NumberingType"))
158 						nNumTypeItemIndex = i;
159 		}
160 		assertEquals("NumberingType should be BITMAP",
161 				NumberingType.BITMAP, proValues2[ nNumTypeItemIndex].Value);
162 		String uniqueID = getUniqueIDOfGraphicFile(app, m_GraphicPath);
163 		assertEquals("Graphic should be the one with uniqueID"+uniqueID,
164 				"vnd.sun.star.GraphicObject:"+uniqueID, proValues2[ nGraphObjItemIndex].Value);
165 	}
166 }
167 
168