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 package testcase.uno.sw.frame;
23 
24 import static org.junit.Assert.assertArrayEquals;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.openoffice.test.common.Testspace;
34 import org.openoffice.test.uno.UnoApp;
35 
36 import com.sun.star.container.NoSuchElementException;
37 import com.sun.star.container.XIndexAccess;
38 import com.sun.star.container.XNameAccess;
39 import com.sun.star.text.XTextDocument;
40 import com.sun.star.text.XTextFrame;
41 import com.sun.star.text.XTextFramesSupplier;
42 import com.sun.star.uno.UnoRuntime;
43 
44 public class CheckFrames {
45 
46 	private static final UnoApp app = new UnoApp();
47 
48 	private XTextDocument document = null;
49 
50 	@Test(expected = NoSuchElementException.class)
51 	public void testLoadTextFrame() throws Exception {
52 		document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.prepareData("uno/sw/CheckFlies.odt")));
53 		XTextFramesSupplier xTFS = (XTextFramesSupplier) UnoRuntime.queryInterface(XTextFramesSupplier.class, document);
54 		String[] expectedNames = { "Frame1", "Frame2" };
55 		String[] expectedContents = { "PageBoundFrame", "ParaBoundFrame" };
56 		XNameAccess xTextFrames = xTFS.getTextFrames();
57 		assertArrayEquals("Text frame names", expectedNames, xTextFrames.getElementNames());
58 		assertTrue("Has text frame named Frame1", xTextFrames.hasByName(expectedNames[0]));
59 		assertFalse("Has nonexisting text frame.", xTextFrames.hasByName("Nonexisting text frame"));
60 
61 		XIndexAccess xTextFramesIdx = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTextFrames);
62 		assertEquals("Text frame count", expectedNames.length, xTextFramesIdx.getCount());
63 		String[] contents = new String[expectedNames.length];
64 		for (int i = 0; i < xTextFramesIdx.getCount(); i++) {
65 			Object obj = xTextFramesIdx.getByIndex(i);
66 			XTextFrame frame = (XTextFrame) UnoRuntime.queryInterface(XTextFrame.class, obj);
67 			contents[i] = frame.getText().getString();
68 		}
69 		assertArrayEquals("Text frame contents", expectedContents, contents);
70 		xTextFrames.getByName("Nonexisting Textframe");
71 	}
72 
73 	@Before
74 	public void setUp() throws Exception {
75 		app.start();
76 	}
77 
78 	@After
79 	public void tearDown() {
80 		app.closeDocument(document);
81 	}
82 
83 	@AfterClass
84 	public static void tearDownConnection() throws InterruptedException, Exception {
85 		app.close();
86 	}
87 
88 }
89