1*22a547feSLi Feng Wang package fvt.uno.sd.slideshow;
2*22a547feSLi Feng Wang 
3*22a547feSLi Feng Wang import junit.framework.Assert;
4*22a547feSLi Feng Wang 
5*22a547feSLi Feng Wang import org.junit.After;
6*22a547feSLi Feng Wang import org.junit.Before;
7*22a547feSLi Feng Wang import org.junit.Test;
8*22a547feSLi Feng Wang import org.openoffice.test.common.FileUtil;
9*22a547feSLi Feng Wang import org.openoffice.test.common.Testspace;
10*22a547feSLi Feng Wang import org.openoffice.test.uno.UnoApp;
11*22a547feSLi Feng Wang 
12*22a547feSLi Feng Wang import com.sun.star.awt.Point;
13*22a547feSLi Feng Wang import com.sun.star.awt.Size;
14*22a547feSLi Feng Wang import com.sun.star.beans.PropertyValue;
15*22a547feSLi Feng Wang import com.sun.star.beans.XPropertySet;
16*22a547feSLi Feng Wang import com.sun.star.container.XIndexContainer;
17*22a547feSLi Feng Wang import com.sun.star.container.XNameContainer;
18*22a547feSLi Feng Wang import com.sun.star.container.XNamed;
19*22a547feSLi Feng Wang import com.sun.star.drawing.XDrawPage;
20*22a547feSLi Feng Wang import com.sun.star.drawing.XDrawPages;
21*22a547feSLi Feng Wang import com.sun.star.drawing.XDrawPagesSupplier;
22*22a547feSLi Feng Wang import com.sun.star.drawing.XShape;
23*22a547feSLi Feng Wang import com.sun.star.drawing.XShapes;
24*22a547feSLi Feng Wang import com.sun.star.frame.XStorable;
25*22a547feSLi Feng Wang import com.sun.star.lang.XComponent;
26*22a547feSLi Feng Wang import com.sun.star.lang.XSingleServiceFactory;
27*22a547feSLi Feng Wang import com.sun.star.presentation.XCustomPresentationSupplier;
28*22a547feSLi Feng Wang import com.sun.star.presentation.XPresentation;
29*22a547feSLi Feng Wang import com.sun.star.presentation.XPresentationSupplier;
30*22a547feSLi Feng Wang import com.sun.star.uno.UnoRuntime;
31*22a547feSLi Feng Wang 
32*22a547feSLi Feng Wang import testlib.uno.ShapeUtil;
33*22a547feSLi Feng Wang 
34*22a547feSLi Feng Wang public class CustomShow {
35*22a547feSLi Feng Wang 	XPresentationSupplier sdDocument = null;
36*22a547feSLi Feng Wang 	XPresentation pre = null;
37*22a547feSLi Feng Wang 	XComponent precomp = null;
38*22a547feSLi Feng Wang 	XComponent impressDocument = null;
39*22a547feSLi Feng Wang 	XComponent reLoadFile = null;
40*22a547feSLi Feng Wang 	XDrawPagesSupplier drawsupplier = null;
41*22a547feSLi Feng Wang 	XDrawPages drawpages = null;
42*22a547feSLi Feng Wang 
43*22a547feSLi Feng Wang 	String filePath = null;
44*22a547feSLi Feng Wang 
45*22a547feSLi Feng Wang 	UnoApp unoApp = new UnoApp();
46*22a547feSLi Feng Wang 
47*22a547feSLi Feng Wang 	/**
48*22a547feSLi Feng Wang 	 * @throws java.lang.Exception
49*22a547feSLi Feng Wang 	 */
50*22a547feSLi Feng Wang 	@Before
51*22a547feSLi Feng Wang 	public void setUp() throws Exception {
52*22a547feSLi Feng Wang 		unoApp.start();
53*22a547feSLi Feng Wang 		createDocumentAndSlide();
54*22a547feSLi Feng Wang 	}
55*22a547feSLi Feng Wang 
56*22a547feSLi Feng Wang 	@After
57*22a547feSLi Feng Wang 	public void tearDown() throws Exception {
58*22a547feSLi Feng Wang 		unoApp.closeDocument(impressDocument);
59*22a547feSLi Feng Wang 		unoApp.closeDocument(reLoadFile);
60*22a547feSLi Feng Wang 		unoApp.close();
61*22a547feSLi Feng Wang 	}
62*22a547feSLi Feng Wang 
63*22a547feSLi Feng Wang 	@Test
64*22a547feSLi Feng Wang 	public void testCustomShow() throws Exception {
65*22a547feSLi Feng Wang 
66*22a547feSLi Feng Wang 		String aNameArray[] = { "Page 1", "Page 2",  "Page 3", "Page 4", "Page 5"};
67*22a547feSLi Feng Wang 
68*22a547feSLi Feng Wang 		//insert five  pages
69*22a547feSLi Feng Wang 	    while (drawpages.getCount() < aNameArray.length)
70*22a547feSLi Feng Wang 		    drawpages.insertNewByIndex(0);
71*22a547feSLi Feng Wang 
72*22a547feSLi Feng Wang 	    //add text shape to each page
73*22a547feSLi Feng Wang 		for (int i = 0; i < aNameArray.length; i++) {
74*22a547feSLi Feng Wang 			XDrawPage xDrawPage = (XDrawPage) UnoRuntime.queryInterface(
75*22a547feSLi Feng Wang 					XDrawPage.class, drawpages.getByIndex(i));
76*22a547feSLi Feng Wang 			XNamed xPageName = (XNamed) UnoRuntime.queryInterface(XNamed.class,
77*22a547feSLi Feng Wang 					xDrawPage);
78*22a547feSLi Feng Wang 			xPageName.setName(aNameArray[i]);
79*22a547feSLi Feng Wang 
80*22a547feSLi Feng Wang 			XShape xTextObj = ShapeUtil.createShape(impressDocument, new Point(
81*22a547feSLi Feng Wang 					10000, 9000), new Size(10000, 5000),
82*22a547feSLi Feng Wang 					"com.sun.star.drawing.TextShape");
83*22a547feSLi Feng Wang 			XShapes xShapes = (XShapes) UnoRuntime.queryInterface(
84*22a547feSLi Feng Wang 					XShapes.class, xDrawPage);
85*22a547feSLi Feng Wang 			xShapes.add(xTextObj);
86*22a547feSLi Feng Wang 			ShapeUtil.addPortion(xTextObj, aNameArray[i], true);
87*22a547feSLi Feng Wang 		}
88*22a547feSLi Feng Wang 
89*22a547feSLi Feng Wang 
90*22a547feSLi Feng Wang 		//create two custom shows
91*22a547feSLi Feng Wang 		//one will play slide 3 to 5 and is named "Part"
92*22a547feSLi Feng Wang 		//the other one will play slide 1 t0 5 and is named "All"
93*22a547feSLi Feng Wang 		XCustomPresentationSupplier xCustPresSupplier = (XCustomPresentationSupplier) UnoRuntime
94*22a547feSLi Feng Wang 				.queryInterface(XCustomPresentationSupplier.class, impressDocument);
95*22a547feSLi Feng Wang 
96*22a547feSLi Feng Wang 
97*22a547feSLi Feng Wang 		// the following container is a container for further container which
98*22a547feSLi Feng Wang 		// includes the list of pages that are to play within a custom show
99*22a547feSLi Feng Wang 		XNameContainer xNameContainer = xCustPresSupplier
100*22a547feSLi Feng Wang 				.getCustomPresentations();
101*22a547feSLi Feng Wang 		XSingleServiceFactory xFactory = (XSingleServiceFactory) UnoRuntime
102*22a547feSLi Feng Wang 				.queryInterface(XSingleServiceFactory.class, xNameContainer);
103*22a547feSLi Feng Wang 
104*22a547feSLi Feng Wang 		Object xObj;
105*22a547feSLi Feng Wang 		XIndexContainer xContainer;
106*22a547feSLi Feng Wang 
107*22a547feSLi Feng Wang 		//instantiate an IndexContainer that will take a list of draw pages for
108*22a547feSLi Feng Wang 		//the first custom show
109*22a547feSLi Feng Wang 		xObj = xFactory.createInstance();
110*22a547feSLi Feng Wang 		xContainer = (XIndexContainer) UnoRuntime.queryInterface(
111*22a547feSLi Feng Wang 				XIndexContainer.class, xObj);
112*22a547feSLi Feng Wang 		for (int i = 3; i < 5; i++)
113*22a547feSLi Feng Wang 			xContainer.insertByIndex(xContainer.getCount(),
114*22a547feSLi Feng Wang 					drawpages.getByIndex(i));
115*22a547feSLi Feng Wang 		xNameContainer.insertByName("Part", xContainer);
116*22a547feSLi Feng Wang 
117*22a547feSLi Feng Wang 
118*22a547feSLi Feng Wang 		//instantiate an IndexContainer that will take a list of draw page for
119*22a547feSLi Feng Wang 		//the second custom show
120*22a547feSLi Feng Wang 		xObj = xFactory.createInstance();
121*22a547feSLi Feng Wang 		xContainer = (XIndexContainer) UnoRuntime.queryInterface(
122*22a547feSLi Feng Wang 				XIndexContainer.class, xObj);
123*22a547feSLi Feng Wang 		for (int i = 1; i < 5; i++)
124*22a547feSLi Feng Wang 			xContainer.insertByIndex(xContainer.getCount(),
125*22a547feSLi Feng Wang 					drawpages.getByIndex(i));
126*22a547feSLi Feng Wang 		xNameContainer.insertByName("All", xContainer);
127*22a547feSLi Feng Wang 
128*22a547feSLi Feng Wang 
129*22a547feSLi Feng Wang 		//set which custom show is to used
130*22a547feSLi Feng Wang 		XPresentationSupplier xPresSupplier = (XPresentationSupplier) UnoRuntime
131*22a547feSLi Feng Wang 				.queryInterface(XPresentationSupplier.class, impressDocument);
132*22a547feSLi Feng Wang 		XPresentation xPresentation = xPresSupplier.getPresentation();
133*22a547feSLi Feng Wang 		XPropertySet xPresPropSet = (XPropertySet) UnoRuntime.queryInterface(
134*22a547feSLi Feng Wang 				XPropertySet.class, xPresentation);
135*22a547feSLi Feng Wang 		xPresPropSet.setPropertyValue("CustomShow", "Part");
136*22a547feSLi Feng Wang 
137*22a547feSLi Feng Wang 		saveAndLoadSlide();
138*22a547feSLi Feng Wang 
139*22a547feSLi Feng Wang 		//assert if custom show is set
140*22a547feSLi Feng Wang 		Assert.assertEquals("Set Part as CustomSHow", xPresPropSet.getPropertyValue("CustomShow"), "Part");
141*22a547feSLi Feng Wang 	}
142*22a547feSLi Feng Wang 
143*22a547feSLi Feng Wang 	/**
144*22a547feSLi Feng Wang 	 * create a new presentation document and insert a new slide.
145*22a547feSLi Feng Wang 	 *
146*22a547feSLi Feng Wang 	 * @throws Exception
147*22a547feSLi Feng Wang 	 */
148*22a547feSLi Feng Wang 	public void createDocumentAndSlide() throws Exception {
149*22a547feSLi Feng Wang 		impressDocument = (XComponent) UnoRuntime.queryInterface(
150*22a547feSLi Feng Wang 				XComponent.class, unoApp.newDocument("simpress"));
151*22a547feSLi Feng Wang 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
152*22a547feSLi Feng Wang 				XDrawPagesSupplier.class, impressDocument);
153*22a547feSLi Feng Wang 		drawpages = drawsupplier.getDrawPages();
154*22a547feSLi Feng Wang 
155*22a547feSLi Feng Wang 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
156*22a547feSLi Feng Wang 				XPresentationSupplier.class, impressDocument);
157*22a547feSLi Feng Wang 		pre = sdDocument.getPresentation();
158*22a547feSLi Feng Wang 	}
159*22a547feSLi Feng Wang 
160*22a547feSLi Feng Wang 	/**
161*22a547feSLi Feng Wang 	 * Save presentation and reLoad the slide.
162*22a547feSLi Feng Wang 	 *
163*22a547feSLi Feng Wang 	 * @param no
164*22a547feSLi Feng Wang 	 * @return void
165*22a547feSLi Feng Wang 	 * @throws Exception
166*22a547feSLi Feng Wang 	 */
167*22a547feSLi Feng Wang 	public void saveAndLoadSlide() throws Exception {
168*22a547feSLi Feng Wang 		reLoadFile = saveAndReloadDoc(impressDocument,
169*22a547feSLi Feng Wang 				"StarOffice XML (Impress)", "odp");
170*22a547feSLi Feng Wang 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
171*22a547feSLi Feng Wang 				XDrawPagesSupplier.class, reLoadFile);
172*22a547feSLi Feng Wang 		drawpages = drawsupplier.getDrawPages();
173*22a547feSLi Feng Wang 
174*22a547feSLi Feng Wang 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
175*22a547feSLi Feng Wang 				XPresentationSupplier.class, reLoadFile);
176*22a547feSLi Feng Wang 		pre = sdDocument.getPresentation();
177*22a547feSLi Feng Wang 	}
178*22a547feSLi Feng Wang 
179*22a547feSLi Feng Wang 	/**
180*22a547feSLi Feng Wang 	 * save and reload Presentation document.
181*22a547feSLi Feng Wang 	 *
182*22a547feSLi Feng Wang 	 * @param presentationDocument
183*22a547feSLi Feng Wang 	 * @param sFilter
184*22a547feSLi Feng Wang 	 * @param sExtension
185*22a547feSLi Feng Wang 	 * @return
186*22a547feSLi Feng Wang 	 * @throws Exception
187*22a547feSLi Feng Wang 	 */
188*22a547feSLi Feng Wang 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
189*22a547feSLi Feng Wang 			String sFilter, String sExtension) throws Exception {
190*22a547feSLi Feng Wang 		filePath = Testspace.getPath("tmp/customshow." + sExtension);
191*22a547feSLi Feng Wang 		PropertyValue[] aStoreProperties = new PropertyValue[2];
192*22a547feSLi Feng Wang 		aStoreProperties[0] = new PropertyValue();
193*22a547feSLi Feng Wang 		aStoreProperties[1] = new PropertyValue();
194*22a547feSLi Feng Wang 		aStoreProperties[0].Name = "Override";
195*22a547feSLi Feng Wang 		aStoreProperties[0].Value = true;
196*22a547feSLi Feng Wang 		aStoreProperties[1].Name = "FilterName";
197*22a547feSLi Feng Wang 		aStoreProperties[1].Value = sFilter;
198*22a547feSLi Feng Wang 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
199*22a547feSLi Feng Wang 				XStorable.class, presentationDocument);
200*22a547feSLi Feng Wang 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
201*22a547feSLi Feng Wang 
202*22a547feSLi Feng Wang 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
203*22a547feSLi Feng Wang 				unoApp.loadDocument(filePath));
204*22a547feSLi Feng Wang 	}
205*22a547feSLi Feng Wang }
206