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 
23 
24 package ifc.text;
25 
26 import lib.MultiMethodTest;
27 
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.text.XDocumentIndex;
30 import com.sun.star.text.XText;
31 import com.sun.star.text.XTextContent;
32 import com.sun.star.text.XTextDocument;
33 import com.sun.star.text.XTextRange;
34 import com.sun.star.uno.UnoRuntime;
35 
36 /**
37  * Testing <code>com.sun.star.text.XDocumentIndex</code>
38  * interface methods :
39  * <ul>
40  *  <li><code> getServiceName()</code></li>
41  *  <li><code> update()</code></li>
42  * </ul> <p>
43  *
44  * This test needs the following object relations :
45  * <ul>
46  *  <li> <code>'TextDoc'</code> (of type <code>XTextDocument</code>):
47  *   the text document for creating and inserting index mark.</li>
48  * <ul> <p>
49  *
50  * Test is <b> NOT </b> multithread compilant. <p>
51  * @see com.sun.star.text.XDocumentIndex
52  */
53 public class _XDocumentIndex extends MultiMethodTest {
54 
55     public XDocumentIndex oObj = null;
56 
57     /**
58      * Test calls the method. <p>
59      * Has <b> OK </b> status if the retruned service name
60      * is equal to 'com.sun.star.text.DocumentIndex'.
61      */
_getServiceName()62     public void _getServiceName() {
63         String serv = oObj.getServiceName();
64         tRes.tested("getServiceName()",
65             serv.equals("com.sun.star.text.DocumentIndex"));
66     }
67 
68     /**
69      * Gets the document from relation and insert a new index mark.
70      * Then it stores the text content of document index before
71      * update and after.<p>
72      *
73      * Has <b> OK </b> status if index content is changed and
74      * new index contains index mark inserted. <p>
75      */
_update()76      public void _update() {
77         boolean bOK = true;
78 
79         try {
80             XTextDocument xTextDoc = (XTextDocument)
81                 tEnv.getObjRelation("TextDoc");
82             XText xText = xTextDoc.getText();
83             XTextRange xTR = xText.getEnd();
84             xTR.setString("IndexMark");
85 
86             XMultiServiceFactory xDocMSF = (XMultiServiceFactory)
87                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
88 
89             Object idxMark = xDocMSF.createInstance
90                 ("com.sun.star.text.DocumentIndexMark");
91             XTextContent xTC = (XTextContent) UnoRuntime.queryInterface
92                 (XTextContent.class, idxMark);
93             xText.insertTextContent(xTR, xTC, true);
94         } catch (com.sun.star.uno.Exception e) {
95             log.println("Couldn't insert index mark.");
96             e.printStackTrace(log);
97             bOK = false ;
98         }
99 
100         String contentBefore = oObj.getAnchor().getString();
101         log.println("Content before: '" + contentBefore + "'");
102 
103         oObj.update();
104 
105         try {
106             Thread.sleep(1000);
107         }
108         catch (InterruptedException ex) {
109         }
110 
111 
112         String contentAfter = oObj.getAnchor().getString();
113         log.println("Content after: '" + contentAfter + "'");
114 
115         bOK &= !contentAfter.equals(contentBefore);
116         bOK &= contentAfter.indexOf("IndexMark") > -1;
117 
118         tRes.tested("update()",bOK);
119     }
120 
121 
122 
123 }  // finish class _XDocumentIndex
124 
125 
126