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