121f666c7SLiu Zhe /* Licensed to the Apache Software Foundation (ASF) under one
221f666c7SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
321f666c7SLiu Zhe  * distributed with this work for additional information
421f666c7SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
521f666c7SLiu Zhe  * to you under the Apache License, Version 2.0 (the
621f666c7SLiu Zhe  * "License"); you may not use this file except in compliance
721f666c7SLiu Zhe  * with the License.  You may obtain a copy of the License at
821f666c7SLiu Zhe  *
921f666c7SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
1021f666c7SLiu Zhe  *
1121f666c7SLiu Zhe  * Unless required by applicable law or agreed to in writing,
1221f666c7SLiu Zhe  * software distributed under the License is distributed on an
1321f666c7SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1421f666c7SLiu Zhe  * KIND, either express or implied.  See the License for the
1521f666c7SLiu Zhe  * specific language governing permissions and limitations
1621f666c7SLiu Zhe  * under the License.
1721f666c7SLiu Zhe  *
1821f666c7SLiu Zhe  *************************************************************/
1921f666c7SLiu Zhe 
20*eba4d44aSLiu Zhe package fvt.uno.sc.sheet;
2121f666c7SLiu Zhe 
2221f666c7SLiu Zhe import static org.junit.Assert.*;
2321f666c7SLiu Zhe import org.junit.After;
2421f666c7SLiu Zhe import org.junit.AfterClass;
2521f666c7SLiu Zhe import org.junit.Before;
2621f666c7SLiu Zhe import org.junit.BeforeClass;
2721f666c7SLiu Zhe import org.junit.Test;
2821f666c7SLiu Zhe import org.openoffice.test.common.FileUtil;
2921f666c7SLiu Zhe import org.openoffice.test.common.Testspace;
3021f666c7SLiu Zhe import org.openoffice.test.uno.UnoApp;
31d01630b6SLiu Zhe import testlib.uno.SCUtil;
32d01630b6SLiu Zhe import com.sun.star.beans.Property;
33d01630b6SLiu Zhe import com.sun.star.beans.PropertyAttribute;
3421f666c7SLiu Zhe import com.sun.star.beans.PropertyValue;
35d01630b6SLiu Zhe import com.sun.star.beans.XPropertySet;
36d01630b6SLiu Zhe import com.sun.star.beans.XPropertySetInfo;
37d01630b6SLiu Zhe import com.sun.star.container.XEnumerationAccess;
3821f666c7SLiu Zhe import com.sun.star.container.XIndexAccess;
3921f666c7SLiu Zhe import com.sun.star.container.XNamed;
4021f666c7SLiu Zhe import com.sun.star.frame.XModel;
4121f666c7SLiu Zhe import com.sun.star.frame.XStorable;
4221f666c7SLiu Zhe import com.sun.star.io.IOException;
4321f666c7SLiu Zhe import com.sun.star.lang.IndexOutOfBoundsException;
4421f666c7SLiu Zhe import com.sun.star.lang.WrappedTargetException;
4521f666c7SLiu Zhe import com.sun.star.lang.XComponent;
46d01630b6SLiu Zhe import com.sun.star.sheet.SheetLinkMode;
47d01630b6SLiu Zhe import com.sun.star.sheet.XCalculatable;
48d01630b6SLiu Zhe import com.sun.star.sheet.XExternalDocLink;
49d01630b6SLiu Zhe import com.sun.star.sheet.XExternalDocLinks;
50d01630b6SLiu Zhe import com.sun.star.sheet.XSheetLinkable;
5121f666c7SLiu Zhe import com.sun.star.sheet.XSpreadsheet;
5221f666c7SLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
5321f666c7SLiu Zhe import com.sun.star.sheet.XSpreadsheetView;
5421f666c7SLiu Zhe import com.sun.star.sheet.XSpreadsheets;
5521f666c7SLiu Zhe import com.sun.star.table.XCell;
5621f666c7SLiu Zhe import com.sun.star.uno.UnoRuntime;
57d01630b6SLiu Zhe import com.sun.star.util.XRefreshable;
5821f666c7SLiu Zhe 
5921f666c7SLiu Zhe public class SheetBasicTest {
6021f666c7SLiu Zhe 	UnoApp unoApp = new UnoApp();
6121f666c7SLiu Zhe 
6221f666c7SLiu Zhe 	XSpreadsheetDocument scDocument = null;
6321f666c7SLiu Zhe 	XComponent scComponent = null;
6421f666c7SLiu Zhe 
6521f666c7SLiu Zhe 	@BeforeClass
6621f666c7SLiu Zhe 	public static void setUpBeforeClass() throws Exception {
6721f666c7SLiu Zhe 
6821f666c7SLiu Zhe 	}
6921f666c7SLiu Zhe 
7021f666c7SLiu Zhe 	@AfterClass
7121f666c7SLiu Zhe 	public static void tearDownAfterClass() throws Exception {
7221f666c7SLiu Zhe 	}
7321f666c7SLiu Zhe 
7421f666c7SLiu Zhe 	@Before
7521f666c7SLiu Zhe 	public void setUp() throws Exception {
7621f666c7SLiu Zhe 		unoApp.start();
7721f666c7SLiu Zhe 		// New a SC document
7821f666c7SLiu Zhe 		scComponent = unoApp.newDocument("scalc");
7921f666c7SLiu Zhe 	}
8021f666c7SLiu Zhe 
8121f666c7SLiu Zhe 	@After
8221f666c7SLiu Zhe 	public void tearDown() throws Exception {
8321f666c7SLiu Zhe 		unoApp.closeDocument(scComponent);
8421f666c7SLiu Zhe 		unoApp.close();
8521f666c7SLiu Zhe 	}
8621f666c7SLiu Zhe 
8721f666c7SLiu Zhe 	@Test
8821f666c7SLiu Zhe 	public void insertRenameDeleteSheet() throws Exception {
8921f666c7SLiu Zhe 		// Insert a sheet named aa after first sheet
9021f666c7SLiu Zhe 		String sheetname = "aa";
91d01630b6SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
9221f666c7SLiu Zhe 		XSpreadsheets spreadsheets = scDocument.getSheets();
9321f666c7SLiu Zhe 		spreadsheets.insertNewByName(sheetname, (short) 1);
9421f666c7SLiu Zhe 
9521f666c7SLiu Zhe 		// active the sheet second sheet aa
96d01630b6SLiu Zhe 		XSpreadsheet newSpreadSheet = SCUtil.getSCSheetByIndex(scDocument,
97d01630b6SLiu Zhe 				(short) 1);
98d01630b6SLiu Zhe 		SCUtil.setCurrentSheet(scDocument, newSpreadSheet);
9921f666c7SLiu Zhe 
10021f666c7SLiu Zhe 		// get the new speadsheet name
101d01630b6SLiu Zhe 		assertEquals("actual should equals aa", sheetname,
102d01630b6SLiu Zhe 				SCUtil.getSCSheetNameByIndex(scDocument, (short) 1));
10321f666c7SLiu Zhe 
10421f666c7SLiu Zhe 		// Change the Spreadsheet name
10521f666c7SLiu Zhe 		String changedname = "SpeadsheetAfterChange";
106d01630b6SLiu Zhe 		SCUtil.setSCSheetNameByIndex(scDocument, (short) 1, changedname);
10721f666c7SLiu Zhe 
10821f666c7SLiu Zhe 		// Save and reload document
109d01630b6SLiu Zhe 		SCUtil.saveFileAs(scComponent, "TestSpreadsheet", "ods");
110d01630b6SLiu Zhe 		XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp,
111d01630b6SLiu Zhe 				scDocument, "TestSpreadsheet.ods");
112d01630b6SLiu Zhe 
113d01630b6SLiu Zhe 		scDocument = scDocumentTemp;
114d01630b6SLiu Zhe 		String sheetnameaftermove = SCUtil.getSCSheetNameByIndex(scDocument,
115d01630b6SLiu Zhe 				(short) 1);
11621f666c7SLiu Zhe 
11721f666c7SLiu Zhe 		// Verify the changed Spreadsheet name
11821f666c7SLiu Zhe 		assertEquals("actual should equals SpeadsheetAfterChange", changedname,
119d01630b6SLiu Zhe 				sheetnameaftermove);
12021f666c7SLiu Zhe 
121d01630b6SLiu Zhe 		scDocument.getSheets().removeByName(changedname);
12221f666c7SLiu Zhe 
12321f666c7SLiu Zhe 		assertFalse("actual should equals false",
12421f666c7SLiu Zhe 				spreadsheets.hasByName(changedname));
125d01630b6SLiu Zhe 		SCUtil.save(scDocumentTemp);
12621f666c7SLiu Zhe 	}
12721f666c7SLiu Zhe 
12821f666c7SLiu Zhe 	@Test
12921f666c7SLiu Zhe 	public void copypastesheet() throws Exception {
13021f666c7SLiu Zhe 		// Insert some value into cells
131d01630b6SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
132d01630b6SLiu Zhe 		String souceSheetName = "sourcesheet";
133d01630b6SLiu Zhe 		SCUtil.setSCSheetNameByIndex(scDocument, (short) 0, souceSheetName);
13421f666c7SLiu Zhe 		String[][] stringValues = { { "", "Jan", "Feb", "Mar", "Apr", "Mai" },
13521f666c7SLiu Zhe 				{ "Profit", "12.3", "43.2", "5.1", "76", "56.8" },
13621f666c7SLiu Zhe 				{ "Rival in business", "12.2", "12.6", "17.7", "20.4", "100" }, };
137d01630b6SLiu Zhe 		XSpreadsheet sourceSpreadSheet = SCUtil.getSCSheetByName(scDocument,
138d01630b6SLiu Zhe 				souceSheetName);
13921f666c7SLiu Zhe 		// input strings into sheet1
140d01630b6SLiu Zhe 		SCUtil.setTextToCellRange(sourceSpreadSheet, 0, 0, 5, 2, stringValues);
14121f666c7SLiu Zhe 		// copy the sheet from sourcesheet to copysheet
14221f666c7SLiu Zhe 		String newcopysheet = "copysheet";
143d01630b6SLiu Zhe 		XSpreadsheets spreadsheets = scDocument.getSheets();
144d01630b6SLiu Zhe 		spreadsheets.copyByName(souceSheetName, newcopysheet, (short) 2);
14521f666c7SLiu Zhe 
14621f666c7SLiu Zhe 		// Save and reload document
147d01630b6SLiu Zhe 		SCUtil.saveFileAs(scComponent, "TestCopysheet", "xls");
148d01630b6SLiu Zhe 		XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp,
149d01630b6SLiu Zhe 				scDocument, "TestCopysheet.xls");
150d01630b6SLiu Zhe 		scDocument = scDocumentTemp;
151d01630b6SLiu Zhe 
152d01630b6SLiu Zhe 		XSpreadsheet copysheet = SCUtil
153d01630b6SLiu Zhe 				.getSCSheetByIndex(scDocument, (short) 2);
154d01630b6SLiu Zhe 		String[][] CopystringValues = SCUtil.getTextFromCellRange(copysheet, 0,
155d01630b6SLiu Zhe 				0, 5, 2);
156d01630b6SLiu Zhe 		assertArrayEquals("Expect string value should be stringValues",
157d01630b6SLiu Zhe 				stringValues, CopystringValues);
15821f666c7SLiu Zhe 
15921f666c7SLiu Zhe 	}
16021f666c7SLiu Zhe 
16121f666c7SLiu Zhe 	@Test
16221f666c7SLiu Zhe 	public void movesheet() throws Exception {
16321f666c7SLiu Zhe 
16421f666c7SLiu Zhe 		// new sc document
165d01630b6SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
166d01630b6SLiu Zhe 		XSpreadsheets spreadsheets = scDocument.getSheets();
16721f666c7SLiu Zhe 
16821f666c7SLiu Zhe 		// change the first sheet name and input same value into the sheet cell
16921f666c7SLiu Zhe 		String sheetname = "sourcesheet";
170d01630b6SLiu Zhe 		SCUtil.setSCSheetNameByIndex(scDocument, (short) 0, sheetname);
17121f666c7SLiu Zhe 		String[][] stringValues = { { "", "Jan", "Feb", "Mar", "Apr", "Mai" },
17221f666c7SLiu Zhe 				{ "Profit", "12.3", "43.2", "5.1", "76", "56.8" },
17321f666c7SLiu Zhe 				{ "Rival in business", "12.2", "12.6", "17.7", "20.4", "100" }, };
174d01630b6SLiu Zhe 		XSpreadsheet movesheet = SCUtil
175d01630b6SLiu Zhe 				.getSCSheetByIndex(scDocument, (short) 0);
176d01630b6SLiu Zhe 		SCUtil.setTextToCellRange(movesheet, 0, 0, 5, 2, stringValues);
17721f666c7SLiu Zhe 
17821f666c7SLiu Zhe 		// Before move, get the 2nd sheet name
179d01630b6SLiu Zhe 		String secondSheetNameBeforeMove = SCUtil.getSCSheetNameByIndex(
180d01630b6SLiu Zhe 				scDocument, (short) 1);
18121f666c7SLiu Zhe 
18221f666c7SLiu Zhe 		// move the first sheet
18321f666c7SLiu Zhe 		spreadsheets.moveByName(sheetname, (short) 2);
18421f666c7SLiu Zhe 
18521f666c7SLiu Zhe 		// Save and reload document
186d01630b6SLiu Zhe 		SCUtil.saveFileAs(scComponent, "Testmovesheet", "xls");
187d01630b6SLiu Zhe 		XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp,
188d01630b6SLiu Zhe 				scDocument, "Testmovesheet.xls");
189d01630b6SLiu Zhe 		scDocument = scDocumentTemp;
19021f666c7SLiu Zhe 
19121f666c7SLiu Zhe 		// After move, get the first sheet name, and verify it same as 2nd sheet
19221f666c7SLiu Zhe 		// name before move
193d01630b6SLiu Zhe 		String firstsheetnameAfterMove = SCUtil.getSCSheetNameByIndex(
194d01630b6SLiu Zhe 				scDocument, (short) 0);
19521f666c7SLiu Zhe 		assertEquals("Expect result should be Sheet2",
196d01630b6SLiu Zhe 				secondSheetNameBeforeMove, firstsheetnameAfterMove);
19721f666c7SLiu Zhe 
19821f666c7SLiu Zhe 		// Get the target sheet name after move
199d01630b6SLiu Zhe 		String sheetnameAfterMove = SCUtil.getSCSheetNameByIndex(scDocument,
200d01630b6SLiu Zhe 				(short) 1);
20121f666c7SLiu Zhe 		assertEquals("Expect result should be sourcesheet", sheetname,
202d01630b6SLiu Zhe 				sheetnameAfterMove);
20321f666c7SLiu Zhe 
20421f666c7SLiu Zhe 		// Check the cell value after move
205d01630b6SLiu Zhe 		XSpreadsheet sheetaftermove = SCUtil.getSCSheetByIndex(scDocument,
206d01630b6SLiu Zhe 				(short) 1);
207d01630b6SLiu Zhe 		String[][] stringValuesaftermove = SCUtil.getTextFromCellRange(
208d01630b6SLiu Zhe 				sheetaftermove, 0, 0, 5, 2);
209d01630b6SLiu Zhe 
210d01630b6SLiu Zhe 		assertArrayEquals("Expect result should be stringValues", stringValues,
211d01630b6SLiu Zhe 				stringValuesaftermove);
21221f666c7SLiu Zhe 
21321f666c7SLiu Zhe 	}
21421f666c7SLiu Zhe 
215d01630b6SLiu Zhe 	@Test
216d01630b6SLiu Zhe 	public void hideShowSheet() throws Exception {
217d01630b6SLiu Zhe 		// Insert a sheet named hide sheet after first sheet
218d01630b6SLiu Zhe 		String sheetname = "hide sheet";
219d01630b6SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
220d01630b6SLiu Zhe 		XSpreadsheets spreadsheets = scDocument.getSheets();
221d01630b6SLiu Zhe 		spreadsheets.insertNewByName(sheetname, (short) 1);
222d01630b6SLiu Zhe 
223d01630b6SLiu Zhe 		// active the sheet second sheet "hide sheet"
224d01630b6SLiu Zhe 		XSpreadsheet secondSpreadSheet = SCUtil.getSCSheetByIndex(scDocument,
225d01630b6SLiu Zhe 				(short) 1);
226d01630b6SLiu Zhe 		SCUtil.setCurrentSheet(scDocument, secondSpreadSheet);
227d01630b6SLiu Zhe 		// get second sheet name and verify it should be "hide sheet"
228d01630b6SLiu Zhe 		assertEquals("expect active sheet name will be hide sheet", sheetname,
229d01630b6SLiu Zhe 				SCUtil.getSCSheetNameByIndex(scDocument, (short) 1));
230d01630b6SLiu Zhe 
231d01630b6SLiu Zhe 		// hide the sheet you insert
232d01630b6SLiu Zhe 		XPropertySet sheetPropertySet = (XPropertySet) UnoRuntime
233d01630b6SLiu Zhe 				.queryInterface(XPropertySet.class, secondSpreadSheet);
234d01630b6SLiu Zhe 		boolean isvisiable = false;
235d01630b6SLiu Zhe 		sheetPropertySet.setPropertyValue("IsVisible", isvisiable);
236d01630b6SLiu Zhe 
237d01630b6SLiu Zhe 		// Save and reload document
238d01630b6SLiu Zhe 		SCUtil.saveFileAs(scComponent, "Testhideshowsheet", "xls");
239d01630b6SLiu Zhe 		XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp,
240d01630b6SLiu Zhe 				scDocument, "Testhideshowsheet.xls");
241d01630b6SLiu Zhe 		scDocument = scDocumentTemp;
242d01630b6SLiu Zhe 
243d01630b6SLiu Zhe 		// get the active sheet name after hide sheet, it should be Sheet2
244d01630b6SLiu Zhe 		String sheet2Name = SCUtil.getSCSheetNameByIndex(scDocument, (short) 2);
245d01630b6SLiu Zhe 		String activesheetname = SCUtil.getSCActiveSheetName(scDocument);
246d01630b6SLiu Zhe 		assertEquals("Expect sheet name should be Sheet2", sheet2Name,
247d01630b6SLiu Zhe 				activesheetname);
248d01630b6SLiu Zhe 
249d01630b6SLiu Zhe 		// show sheet "hide sheet"
250d01630b6SLiu Zhe 		sheetPropertySet = (XPropertySet) UnoRuntime.queryInterface(
251d01630b6SLiu Zhe 				XPropertySet.class,
252d01630b6SLiu Zhe 				SCUtil.getSCSheetByIndex(scDocument, (short) 1));
253d01630b6SLiu Zhe 		isvisiable = true;
254d01630b6SLiu Zhe 		sheetPropertySet.setPropertyValue("IsVisible", isvisiable);
255d01630b6SLiu Zhe 
256d01630b6SLiu Zhe 		// active sheet "hide sheet"
257d01630b6SLiu Zhe 		secondSpreadSheet = SCUtil.getSCSheetByIndex(scDocument, (short) 1);
258d01630b6SLiu Zhe 		SCUtil.setCurrentSheet(scDocument, secondSpreadSheet);
259d01630b6SLiu Zhe 
260d01630b6SLiu Zhe 		// Get current active sheet name, verify it same as "hide sheet"
261d01630b6SLiu Zhe 		String currentactivesheetname = SCUtil.getSCActiveSheetName(scDocument);
262d01630b6SLiu Zhe 		assertEquals("Expect active sheet name is hidesheet", sheetname,
263d01630b6SLiu Zhe 				currentactivesheetname);
264d01630b6SLiu Zhe 		SCUtil.save(scDocument);
265d01630b6SLiu Zhe 	}
266d01630b6SLiu Zhe 
267d01630b6SLiu Zhe 	@Test
268d01630b6SLiu Zhe 	public void sheetColor() throws Exception {
269d01630b6SLiu Zhe 		// get first sheet propertyset
270d01630b6SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
271d01630b6SLiu Zhe 		XSpreadsheets spreadsheets = scDocument.getSheets();
272d01630b6SLiu Zhe 		XSpreadsheet firstSpreadSheet = SCUtil.getSCSheetByIndex(scDocument,
273d01630b6SLiu Zhe 				(short) 0);
274d01630b6SLiu Zhe 		XPropertySet sheet1PropertySet = (XPropertySet) UnoRuntime
275d01630b6SLiu Zhe 				.queryInterface(XPropertySet.class, firstSpreadSheet);
276d01630b6SLiu Zhe 
277d01630b6SLiu Zhe 		// Set tabcolor to 111
278d01630b6SLiu Zhe 		sheet1PropertySet.setPropertyValue("TabColor", 111);
279d01630b6SLiu Zhe 
280d01630b6SLiu Zhe 		// copy the color sheet to new sheet
281d01630b6SLiu Zhe 		spreadsheets.copyByName(
282d01630b6SLiu Zhe 				SCUtil.getSCSheetNameByIndex(scDocument, (short) 0),
283d01630b6SLiu Zhe 				"newsheet", (short) 3);
284d01630b6SLiu Zhe 
285d01630b6SLiu Zhe 		// Save and reopen the document
286d01630b6SLiu Zhe 		SCUtil.saveFileAs(scComponent, "Testcolorsheet", "ods");
287d01630b6SLiu Zhe 		XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp,
288d01630b6SLiu Zhe 				scDocument, "Testcolorsheet.ods");
289d01630b6SLiu Zhe 		scDocument = scDocumentTemp;
290d01630b6SLiu Zhe 
291d01630b6SLiu Zhe 		// Get first sheet color
292d01630b6SLiu Zhe 		sheet1PropertySet = (XPropertySet) UnoRuntime.queryInterface(
293d01630b6SLiu Zhe 				XPropertySet.class,
294d01630b6SLiu Zhe 				SCUtil.getSCSheetByIndex(scDocument, (short) 0));
295d01630b6SLiu Zhe 		int firstSheetcolorid = (Integer) sheet1PropertySet
296d01630b6SLiu Zhe 				.getPropertyValue("TabColor");
297d01630b6SLiu Zhe 
298d01630b6SLiu Zhe 		// Get the copyed sheet color
299d01630b6SLiu Zhe 		XPropertySet newsheetPropertySet = (XPropertySet) UnoRuntime
300d01630b6SLiu Zhe 				.queryInterface(XPropertySet.class,
301d01630b6SLiu Zhe 						SCUtil.getSCSheetByIndex(scDocument, (short) 3));
302d01630b6SLiu Zhe 		int copySheetcolorid = (Integer) newsheetPropertySet
303d01630b6SLiu Zhe 				.getPropertyValue("TabColor");
304d01630b6SLiu Zhe 
305d01630b6SLiu Zhe 		// Verify first sheet color changed successfully
306d01630b6SLiu Zhe 		assertEquals("Expect color should be 111", 111, firstSheetcolorid);
307d01630b6SLiu Zhe 
308d01630b6SLiu Zhe 		// Verify first sheet color same as copy sheet color
309d01630b6SLiu Zhe 		assertEquals("Expect color should be 111", firstSheetcolorid,
310d01630b6SLiu Zhe 				copySheetcolorid);
31121f666c7SLiu Zhe 	}
31221f666c7SLiu Zhe 
313d01630b6SLiu Zhe 	@Test
314d01630b6SLiu Zhe 	public void insertSheetFromfile() throws Exception {
315d01630b6SLiu Zhe 		// New a document source.xls, add value to 3 sheet
316d01630b6SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
317d01630b6SLiu Zhe 		XSpreadsheets spreadsheets = scDocument.getSheets();
318d01630b6SLiu Zhe 		XSpreadsheet firstSheet = SCUtil.getSCSheetByIndex(scDocument,
319d01630b6SLiu Zhe 				(short) 0);
320d01630b6SLiu Zhe 		XSpreadsheet secondSheet = SCUtil.getSCSheetByIndex(scDocument,
321d01630b6SLiu Zhe 				(short) 1);
322d01630b6SLiu Zhe 		XSpreadsheet thirdSheet = SCUtil.getSCSheetByIndex(scDocument,
323d01630b6SLiu Zhe 				(short) 2);
324d01630b6SLiu Zhe 		SCUtil.setFormulaToCell(firstSheet, 1, 2, "=2*2");
325d01630b6SLiu Zhe 		SCUtil.setFormulaToCell(secondSheet, 1, 2, "=2*2");
326d01630b6SLiu Zhe 		SCUtil.setFormulaToCell(thirdSheet, 1, 2, "=2*2");
327d01630b6SLiu Zhe 
328d01630b6SLiu Zhe 		// Save and close this document
329d01630b6SLiu Zhe 		SCUtil.saveFileAs(scComponent, "source", "xls");
330d01630b6SLiu Zhe 		SCUtil.closeFile(scDocument);
331d01630b6SLiu Zhe 
332d01630b6SLiu Zhe 		// get source document URL
333dded528dSLiu Zhe 		String SourcestoreUrl = Testspace.getUrl("output/sc/" + "source" + "."
334d01630b6SLiu Zhe 				+ "xls");
335d01630b6SLiu Zhe 
336d01630b6SLiu Zhe 		// New a document
337d01630b6SLiu Zhe 		scComponent = unoApp.newDocument("scalc");
338d01630b6SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
339d01630b6SLiu Zhe 		spreadsheets = scDocument.getSheets();
340d01630b6SLiu Zhe 		// Insert firstexternalsheet sheet, link with Sheet1 in source document
341d01630b6SLiu Zhe 		// and the link mode is NORMAL
342d01630b6SLiu Zhe 		spreadsheets.insertNewByName("firstexternalsheet", (short) 3);
343d01630b6SLiu Zhe 		XSpreadsheet firstexternalsheet = SCUtil.getSCSheetByIndex(scDocument,
344d01630b6SLiu Zhe 				(short) 3);
345d01630b6SLiu Zhe 		XSheetLinkable xfirstSheetLinkable = (XSheetLinkable) UnoRuntime
346d01630b6SLiu Zhe 				.queryInterface(XSheetLinkable.class, firstexternalsheet);
347d01630b6SLiu Zhe 		xfirstSheetLinkable.link(SourcestoreUrl, "", "MS Excel 97", "",
348d01630b6SLiu Zhe 				SheetLinkMode.NORMAL);
349d01630b6SLiu Zhe 
350d01630b6SLiu Zhe 		// Insert secondexternalsheet sheet, link with Sheet2 in source document
351d01630b6SLiu Zhe 		// and the link mode is VALUE
352d01630b6SLiu Zhe 		spreadsheets.insertNewByName("secondexternalsheet", (short) 4);
353d01630b6SLiu Zhe 		XSpreadsheet secondexternalsheet = SCUtil.getSCSheetByIndex(scDocument,
354d01630b6SLiu Zhe 				(short) 4);
355d01630b6SLiu Zhe 		XSheetLinkable xsecondSheetLinkable = (XSheetLinkable) UnoRuntime
356d01630b6SLiu Zhe 				.queryInterface(XSheetLinkable.class, secondexternalsheet);
357d01630b6SLiu Zhe 		xsecondSheetLinkable.link(SourcestoreUrl, "Sheet2", "MS Excel 97", "",
358d01630b6SLiu Zhe 				SheetLinkMode.VALUE);
359d01630b6SLiu Zhe 
360d01630b6SLiu Zhe 		// Insert secondexternalsheet sheet, link with Sheet2 in source document
361d01630b6SLiu Zhe 		// and the link mode is NONE
362d01630b6SLiu Zhe 		spreadsheets.insertNewByName("thirdexternalsheet", (short) 5);
363d01630b6SLiu Zhe 		XSpreadsheet thirdexternalsheet = SCUtil.getSCSheetByIndex(scDocument,
364d01630b6SLiu Zhe 				(short) 5);
365d01630b6SLiu Zhe 		XSheetLinkable xthirdSheetLinkable = (XSheetLinkable) UnoRuntime
366d01630b6SLiu Zhe 				.queryInterface(XSheetLinkable.class, thirdexternalsheet);
367d01630b6SLiu Zhe 		xthirdSheetLinkable.link(SourcestoreUrl, "Sheet3", "MS Excel 97", "",
368d01630b6SLiu Zhe 				SheetLinkMode.NONE);
369d01630b6SLiu Zhe 
370d01630b6SLiu Zhe 		// Verify firstexternalsheet
371d01630b6SLiu Zhe 		assertEquals("Expect formula should be =2*2", "=2*2",
372d01630b6SLiu Zhe 				SCUtil.getFormulaFromCell(firstexternalsheet, 1, 2));
373d01630b6SLiu Zhe 		assertEquals("Expect formula result should be 4", "4",
374d01630b6SLiu Zhe 				SCUtil.getTextFromCell(firstexternalsheet, 1, 2));
375d01630b6SLiu Zhe 
376d01630b6SLiu Zhe 		// Verify secondexternalsheet
377d01630b6SLiu Zhe 		assertEquals("Expect formula should be 4", "4",
378d01630b6SLiu Zhe 				SCUtil.getFormulaFromCell(secondexternalsheet, 1, 2));
379d01630b6SLiu Zhe 		assertEquals("Expect formula result should be 4", "4",
380d01630b6SLiu Zhe 				SCUtil.getTextFromCell(secondexternalsheet, 1, 2));
381d01630b6SLiu Zhe 
382d01630b6SLiu Zhe 		// Verify thirdexternalsheet
383d01630b6SLiu Zhe 		assertEquals("Expect formula should be blank", "",
384d01630b6SLiu Zhe 				SCUtil.getFormulaFromCell(thirdexternalsheet, 1, 2));
385d01630b6SLiu Zhe 		assertEquals("Expect formula result should be blank", "",
386d01630b6SLiu Zhe 				SCUtil.getTextFromCell(thirdexternalsheet, 1, 2));
387d01630b6SLiu Zhe 
388d01630b6SLiu Zhe 		// save document and verify the linked sheet again
389d01630b6SLiu Zhe 		SCUtil.saveFileAs(scComponent, "linked", "ods");
390d01630b6SLiu Zhe 		XSpreadsheetDocument tempscDocument = SCUtil.reloadFile(unoApp,
391d01630b6SLiu Zhe 				scDocument, "linked.ods");
392d01630b6SLiu Zhe 		scDocument = tempscDocument;
393d01630b6SLiu Zhe 		firstexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 3);
394d01630b6SLiu Zhe 		secondexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 4);
395d01630b6SLiu Zhe 		thirdexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 5);
396d01630b6SLiu Zhe 
397d01630b6SLiu Zhe 		// Verify firstexternalsheet
398d01630b6SLiu Zhe 		assertEquals("Expect formula should be =2*2", "=2*2",
399d01630b6SLiu Zhe 				SCUtil.getFormulaFromCell(firstexternalsheet, 1, 2));
400d01630b6SLiu Zhe 		assertEquals("Expect formula result should be 4", "4",
401d01630b6SLiu Zhe 				SCUtil.getTextFromCell(firstexternalsheet, 1, 2));
402d01630b6SLiu Zhe 
403d01630b6SLiu Zhe 		// Verify secondexternalsheet
404d01630b6SLiu Zhe 		assertEquals("Expect formula should be 4", "4",
405d01630b6SLiu Zhe 				SCUtil.getFormulaFromCell(secondexternalsheet, 1, 2));
406d01630b6SLiu Zhe 		assertEquals("Expect formula result should be 4", "4",
407d01630b6SLiu Zhe 				SCUtil.getTextFromCell(secondexternalsheet, 1, 2));
408d01630b6SLiu Zhe 
409d01630b6SLiu Zhe 		// Verify thirdexternalsheet
410d01630b6SLiu Zhe 		assertEquals("Expect formula should be blank", "",
411d01630b6SLiu Zhe 				SCUtil.getFormulaFromCell(thirdexternalsheet, 1, 2));
412d01630b6SLiu Zhe 		assertEquals("Expect formula result should be blank", "",
413d01630b6SLiu Zhe 				SCUtil.getTextFromCell(thirdexternalsheet, 1, 2));
414d01630b6SLiu Zhe 
415d01630b6SLiu Zhe 		//save and close document
416d01630b6SLiu Zhe 		SCUtil.save(scDocument);
417d01630b6SLiu Zhe 		SCUtil.closeFile(scDocument);
418d01630b6SLiu Zhe 
419d01630b6SLiu Zhe 		//Open souce document and change the value in souce document
420d01630b6SLiu Zhe 		XSpreadsheetDocument sourcescDocument = SCUtil.reloadFile(unoApp,
421d01630b6SLiu Zhe 				scDocument, "source.xls");
422d01630b6SLiu Zhe 		firstSheet = SCUtil.getSCSheetByIndex(sourcescDocument, (short) 0);
423d01630b6SLiu Zhe 		secondSheet = SCUtil.getSCSheetByIndex(sourcescDocument, (short) 1);
424d01630b6SLiu Zhe 		thirdSheet = SCUtil.getSCSheetByIndex(sourcescDocument, (short) 2);
425d01630b6SLiu Zhe 		SCUtil.setFormulaToCell(firstSheet, 1, 2, "=3*3");
426d01630b6SLiu Zhe 		SCUtil.setFormulaToCell(secondSheet, 1, 2, "=3*3");
427d01630b6SLiu Zhe 		SCUtil.setFormulaToCell(thirdSheet, 1, 2, "=3*3");
428d01630b6SLiu Zhe 		SCUtil.save(sourcescDocument);
429d01630b6SLiu Zhe 		SCUtil.closeFile(sourcescDocument);
430d01630b6SLiu Zhe 
431d01630b6SLiu Zhe 		//Open link document
432d01630b6SLiu Zhe 		tempscDocument = SCUtil.reloadFile(unoApp, scDocument, "linked.ods");
433d01630b6SLiu Zhe 		scDocument = tempscDocument;
434d01630b6SLiu Zhe 		spreadsheets = scDocument.getSheets();
435d01630b6SLiu Zhe 
436d01630b6SLiu Zhe 		firstexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 3);
437d01630b6SLiu Zhe 		secondexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 4);
438d01630b6SLiu Zhe 		thirdexternalsheet = SCUtil.getSCSheetByIndex(scDocument, (short) 5);
439d01630b6SLiu Zhe 
440d01630b6SLiu Zhe 		//get Object SheetLinks for document
441d01630b6SLiu Zhe 		XPropertySet sheetpropertyset = (XPropertySet) UnoRuntime
442d01630b6SLiu Zhe 				.queryInterface(XPropertySet.class, scDocument);
443d01630b6SLiu Zhe 		Object sheetLinks = sheetpropertyset.getPropertyValue("SheetLinks");
444d01630b6SLiu Zhe 
445d01630b6SLiu Zhe 		XIndexAccess xsheetlinks = (XIndexAccess) UnoRuntime.queryInterface(
446d01630b6SLiu Zhe 				XIndexAccess.class, sheetLinks);
447d01630b6SLiu Zhe 
448d01630b6SLiu Zhe 		//Refresh all links
449d01630b6SLiu Zhe 		for (int i = 0; i < xsheetlinks.getCount(); i++) {
450d01630b6SLiu Zhe 			Object sheetlink = xsheetlinks.getByIndex(i);
451d01630b6SLiu Zhe 			XRefreshable xsheetRefreshable = (XRefreshable) UnoRuntime
452d01630b6SLiu Zhe 					.queryInterface(XRefreshable.class, sheetlink);
453d01630b6SLiu Zhe 			xsheetRefreshable.refresh();
454d01630b6SLiu Zhe 		}
455d01630b6SLiu Zhe 
456d01630b6SLiu Zhe 		// Verify firstexternalsheet
457d01630b6SLiu Zhe 		assertEquals("Expect formula should be =3*3", "=3*3",
458d01630b6SLiu Zhe 				SCUtil.getFormulaFromCell(firstexternalsheet, 1, 2));
459d01630b6SLiu Zhe 		assertEquals("Expect formula result should be 9", "9",
460d01630b6SLiu Zhe 				SCUtil.getTextFromCell(firstexternalsheet, 1, 2));
461d01630b6SLiu Zhe 
462d01630b6SLiu Zhe 		// Verify secondexternalsheet
463d01630b6SLiu Zhe 		assertEquals("Expect formula should be 9", "9",
464d01630b6SLiu Zhe 				SCUtil.getFormulaFromCell(secondexternalsheet, 1, 2));
465d01630b6SLiu Zhe 		assertEquals("Expect formula result should be 9", "9",
466d01630b6SLiu Zhe 				SCUtil.getTextFromCell(secondexternalsheet, 1, 2));
467d01630b6SLiu Zhe 
468d01630b6SLiu Zhe 		// Verify thirdexternalsheet
469d01630b6SLiu Zhe 		assertEquals("Expect formula should be blank", "",
470d01630b6SLiu Zhe 				SCUtil.getFormulaFromCell(thirdexternalsheet, 1, 2));
471d01630b6SLiu Zhe 		assertEquals("Expect formula result should be blank", "",
472d01630b6SLiu Zhe 				SCUtil.getTextFromCell(thirdexternalsheet, 1, 2));
473d01630b6SLiu Zhe 
474d01630b6SLiu Zhe 		//Save the document before close
475d01630b6SLiu Zhe 		SCUtil.save(scDocument);
476d01630b6SLiu Zhe 
47721f666c7SLiu Zhe 	}
478d01630b6SLiu Zhe 
47921f666c7SLiu Zhe }
480