xref: /trunk/test/testuno/source/fvt/uno/sw/field/SubjectField.java (revision 42a976b737d4e2b88cfbe2fe1cd610fe957bdc84)
1eba4d44aSLiu Zhe /**************************************************************
2eba4d44aSLiu Zhe  *
3eba4d44aSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4eba4d44aSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5eba4d44aSLiu Zhe  * distributed with this work for additional information
6eba4d44aSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7eba4d44aSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8eba4d44aSLiu Zhe  * "License"); you may not use this file except in compliance
9eba4d44aSLiu Zhe  * with the License.  You may obtain a copy of the License at
10eba4d44aSLiu Zhe  *
11eba4d44aSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12eba4d44aSLiu Zhe  *
13eba4d44aSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14eba4d44aSLiu Zhe  * software distributed under the License is distributed on an
15eba4d44aSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16eba4d44aSLiu Zhe  * KIND, either express or implied.  See the License for the
17eba4d44aSLiu Zhe  * specific language governing permissions and limitations
18eba4d44aSLiu Zhe  * under the License.
19eba4d44aSLiu Zhe  *
20eba4d44aSLiu Zhe  *************************************************************/
21eba4d44aSLiu Zhe package fvt.uno.sw.field;
22eba4d44aSLiu Zhe 
23eba4d44aSLiu Zhe import static org.junit.Assert.assertTrue;
24eba4d44aSLiu Zhe 
25eba4d44aSLiu Zhe import org.junit.After;
26eba4d44aSLiu Zhe import org.junit.AfterClass;
27eba4d44aSLiu Zhe import org.junit.Before;
28eba4d44aSLiu Zhe import org.junit.BeforeClass;
29eba4d44aSLiu Zhe import org.junit.Test;
30eba4d44aSLiu Zhe import org.openoffice.test.common.Testspace;
31eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
32eba4d44aSLiu Zhe 
33eba4d44aSLiu Zhe import testlib.uno.SWUtil;
34eba4d44aSLiu Zhe 
35eba4d44aSLiu Zhe import com.sun.star.beans.XPropertySet;
36eba4d44aSLiu Zhe import com.sun.star.container.XEnumeration;
37eba4d44aSLiu Zhe import com.sun.star.container.XEnumerationAccess;
38eba4d44aSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
39eba4d44aSLiu Zhe import com.sun.star.text.XTextDocument;
40eba4d44aSLiu Zhe import com.sun.star.text.XTextField;
41eba4d44aSLiu Zhe import com.sun.star.text.XTextFieldsSupplier;
42eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime;
43eba4d44aSLiu Zhe public class SubjectField {
44eba4d44aSLiu Zhe 
45eba4d44aSLiu Zhe     private static final UnoApp app = new UnoApp();
46eba4d44aSLiu Zhe     private static XTextDocument odtDocument = null;
47eba4d44aSLiu Zhe     private static XTextDocument docDocument = null;
48eba4d44aSLiu Zhe     private  static String odtSample = "uno/sw/field/SubjectField.odt";
49eba4d44aSLiu Zhe     private static String docSample = "uno/sw/field/Test_Sample.doc";
50eba4d44aSLiu Zhe 
51eba4d44aSLiu Zhe     private  static String odtSaveAsDocSample = "uno/sw/field/odtSaveAsDOC.doc";
52eba4d44aSLiu Zhe     private static String docSaveAsODTSample = "uno/sw/field/docSaveAsODT.odt";
53eba4d44aSLiu Zhe 
54eba4d44aSLiu Zhe     @Before
setUpDocument()55eba4d44aSLiu Zhe     public void setUpDocument() throws Exception {
56eba4d44aSLiu Zhe 
57eba4d44aSLiu Zhe     }
58eba4d44aSLiu Zhe 
59eba4d44aSLiu Zhe     @After
tearDownDocument()60eba4d44aSLiu Zhe     public void tearDownDocument() {
61eba4d44aSLiu Zhe 
62eba4d44aSLiu Zhe 
63eba4d44aSLiu Zhe     }
64eba4d44aSLiu Zhe 
65eba4d44aSLiu Zhe     @BeforeClass
setUpConnection()66eba4d44aSLiu Zhe     public static void setUpConnection() throws Exception {
67eba4d44aSLiu Zhe         app.start();
68eba4d44aSLiu Zhe     }
69eba4d44aSLiu Zhe 
70eba4d44aSLiu Zhe     @AfterClass
tearDownConnection()71eba4d44aSLiu Zhe     public static void tearDownConnection() throws InterruptedException,
72eba4d44aSLiu Zhe             Exception {
73eba4d44aSLiu Zhe         app.close();
74eba4d44aSLiu Zhe     }
75eba4d44aSLiu Zhe 
76eba4d44aSLiu Zhe     /**
77eba4d44aSLiu Zhe      *
78eba4d44aSLiu Zhe      * Test Subject Field Can created and Saved in odt file
79eba4d44aSLiu Zhe      * 1.launch a odt document
80eba4d44aSLiu Zhe      * 2.Create a page count field at end of this page
81eba4d44aSLiu Zhe      * 3.Save and Reopen this document
82eba4d44aSLiu Zhe      * 4.Save it as doc format and reload
83eba4d44aSLiu Zhe      * @throws Throwable
84eba4d44aSLiu Zhe      */
85eba4d44aSLiu Zhe     @Test
testSubjectFieldODT()86eba4d44aSLiu Zhe     public void testSubjectFieldODT() throws Throwable {
87eba4d44aSLiu Zhe         odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app);
88eba4d44aSLiu Zhe 
89eba4d44aSLiu Zhe 
90eba4d44aSLiu Zhe         String subjectStr = "Test Subject Field";
91eba4d44aSLiu Zhe         createSubjectFiled(odtDocument);
92eba4d44aSLiu Zhe 
93*07225a51SJohn Bampton         assertTrue("Verify Subject field is created use exist subject", isContainSubjectField(odtDocument, "UNO API Subject"));
94*07225a51SJohn Bampton         assertTrue("Verify Subject field is created use exist subject, can get text from document",
95eba4d44aSLiu Zhe                 isContainSubjectText(odtDocument, "UNO API Subject"));
96eba4d44aSLiu Zhe         //Set subject
97eba4d44aSLiu Zhe         SWUtil.setDocumentProperty(odtDocument, "Subject", subjectStr);
98*07225a51SJohn Bampton         //Verify after set subject
99eba4d44aSLiu Zhe         assertTrue("Verify Subject field is created, can get text from document",
100eba4d44aSLiu Zhe                 isContainSubjectText(odtDocument, subjectStr));
101eba4d44aSLiu Zhe 
102eba4d44aSLiu Zhe         odtDocument = SWUtil.saveAndReload(odtDocument, app);
103eba4d44aSLiu Zhe         //verify after save and reload
104eba4d44aSLiu Zhe         assertTrue("Verify Subject field is created, after save, still exist.", isContainSubjectField(odtDocument, subjectStr));
105eba4d44aSLiu Zhe         assertTrue("Verify Subject field is created, can get text from document after saved.",
106eba4d44aSLiu Zhe                 isContainSubjectText(odtDocument, subjectStr));
107eba4d44aSLiu Zhe         SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample));
108eba4d44aSLiu Zhe         app.closeDocument(odtDocument);
109eba4d44aSLiu Zhe         docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app);
110eba4d44aSLiu Zhe         assertTrue("Verify Subject field is created, after saved to doc format, field still exist.", isContainSubjectField(docDocument, subjectStr));
111eba4d44aSLiu Zhe         assertTrue("Verify Subject field is created, after saved to doc format, can get text from document",
112eba4d44aSLiu Zhe                 isContainSubjectText(docDocument, subjectStr));
113eba4d44aSLiu Zhe         app.closeDocument(docDocument);
114eba4d44aSLiu Zhe     }
115eba4d44aSLiu Zhe 
116eba4d44aSLiu Zhe     /**
117eba4d44aSLiu Zhe      *  Test Subject Field Can created and Saved in Doc file
118eba4d44aSLiu Zhe      * 1.launch a doc document
119eba4d44aSLiu Zhe      * 2.Create a Subject field at end of this page
120eba4d44aSLiu Zhe      * 3.Save and Reopen this document, check Subject field
121eba4d44aSLiu Zhe      * 3.Save as odt format and reload
122eba4d44aSLiu Zhe      * @throws Throwable
123eba4d44aSLiu Zhe      */
1243ecf08d2SLi Feng Wang //  @Test
1253ecf08d2SLi Feng Wang //  public void testPageCountFieldDOC() throws Throwable {
1263ecf08d2SLi Feng Wang //      docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app);
1273ecf08d2SLi Feng Wang //      String subjectStr = "Test Subject Field";
1283ecf08d2SLi Feng Wang //      SWUtil.setDocumentProperty(docDocument, "Subject", subjectStr);
1293ecf08d2SLi Feng Wang //      createSubjectFiled(docDocument);
1303ecf08d2SLi Feng Wang //
1313ecf08d2SLi Feng Wang //      assertTrue("Verify Subject field is created.", isContainSubjectField(docDocument, subjectStr));
1323ecf08d2SLi Feng Wang //      assertTrue("Verify Subject field is created, can get text from document",
1333ecf08d2SLi Feng Wang //              isContainSubjectText(docDocument, subjectStr));
1343ecf08d2SLi Feng Wang //
1353ecf08d2SLi Feng Wang //      docDocument = SWUtil.saveAndReload(docDocument, app);
1363ecf08d2SLi Feng Wang //      //verify after save and reload
1373ecf08d2SLi Feng Wang //      assertTrue("Verify Subject field is created, after save, still exist.", isContainSubjectField(docDocument, subjectStr));
1383ecf08d2SLi Feng Wang //      assertTrue("Verify Subject field is created, can get text from document after saved.",
1393ecf08d2SLi Feng Wang //              isContainSubjectText(docDocument, subjectStr));
1403ecf08d2SLi Feng Wang //      SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample));
1413ecf08d2SLi Feng Wang //      app.closeDocument(docDocument);
1423ecf08d2SLi Feng Wang //      odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app);
1433ecf08d2SLi Feng Wang //      assertTrue("Verify Subject field is created, after saved to doc format, field still exist.", isContainSubjectField(odtDocument, subjectStr));
1443ecf08d2SLi Feng Wang //      assertTrue("Verify Subject field is created, after saved to doc format, can get text from document",
1453ecf08d2SLi Feng Wang //              isContainSubjectText(odtDocument, subjectStr));
1463ecf08d2SLi Feng Wang //      app.closeDocument(odtDocument);
1473ecf08d2SLi Feng Wang //  }
148eba4d44aSLiu Zhe 
149eba4d44aSLiu Zhe 
150eba4d44aSLiu Zhe 
151eba4d44aSLiu Zhe     /**
152eba4d44aSLiu Zhe      * Create a subject field at start of this document
153eba4d44aSLiu Zhe      * @param document
154eba4d44aSLiu Zhe      * @throws Exception
155eba4d44aSLiu Zhe      */
createSubjectFiled(XTextDocument document)156eba4d44aSLiu Zhe     private void createSubjectFiled(XTextDocument document) throws Exception {
157eba4d44aSLiu Zhe 
158eba4d44aSLiu Zhe         XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
159eba4d44aSLiu Zhe         XTextField  subjectField = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.docinfo.Subject"));
160eba4d44aSLiu Zhe 
161eba4d44aSLiu Zhe 
162eba4d44aSLiu Zhe         SWUtil.moveCuror2Start(document);
163eba4d44aSLiu Zhe         document.getText().insertTextContent(document.getText().getStart(), subjectField, true);
164eba4d44aSLiu Zhe 
165eba4d44aSLiu Zhe     }
166eba4d44aSLiu Zhe 
167eba4d44aSLiu Zhe 
168eba4d44aSLiu Zhe     /**
169eba4d44aSLiu Zhe      * Check is contain subject content at start of this document
170eba4d44aSLiu Zhe      *
171eba4d44aSLiu Zhe      * @param document
172eba4d44aSLiu Zhe      * @param content
173eba4d44aSLiu Zhe      * @return
174eba4d44aSLiu Zhe      */
isContainSubjectText(XTextDocument document, String content)175eba4d44aSLiu Zhe     private boolean isContainSubjectText(XTextDocument document, String content) {
176eba4d44aSLiu Zhe         String documentString = document.getText().getString().trim();
177eba4d44aSLiu Zhe         return documentString.indexOf(content) == 0;
178eba4d44aSLiu Zhe     }
179eba4d44aSLiu Zhe 
180eba4d44aSLiu Zhe     /**
181eba4d44aSLiu Zhe      * Check is contain subject field
182eba4d44aSLiu Zhe      * @param document
183eba4d44aSLiu Zhe      * @throws Exception
184eba4d44aSLiu Zhe      */
isContainSubjectField(XTextDocument document, String content)185eba4d44aSLiu Zhe     private boolean isContainSubjectField(XTextDocument document, String content) throws Exception {
186eba4d44aSLiu Zhe         XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
187eba4d44aSLiu Zhe         XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields();
188eba4d44aSLiu Zhe         XEnumeration enumeration = xEnumeratedFields.createEnumeration();
189eba4d44aSLiu Zhe         while (enumeration.hasMoreElements()) {
190eba4d44aSLiu Zhe                 Object field =  enumeration.nextElement();
191eba4d44aSLiu Zhe                 XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
192eba4d44aSLiu Zhe                 String strContent = (String) props.getPropertyValue("Content");
193eba4d44aSLiu Zhe                 return content.equals(strContent);
194eba4d44aSLiu Zhe 
195eba4d44aSLiu Zhe         }
196eba4d44aSLiu Zhe         return false;
197eba4d44aSLiu Zhe 
198eba4d44aSLiu Zhe     }
199eba4d44aSLiu Zhe 
200eba4d44aSLiu Zhe }
201