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 package ifc.i18n;
24 
25 import com.sun.star.i18n.CollatorOptions;
26 import com.sun.star.i18n.XExtendedIndexEntrySupplier;
27 import com.sun.star.lang.Locale;
28 
29 import java.util.HashMap;
30 
31 import lib.MultiMethodTest;
32 
33 
34 public class _XExtendedIndexEntrySupplier extends MultiMethodTest {
35     public XExtendedIndexEntrySupplier oObj;
36     protected Locale[] locales = null;
37     protected HashMap algorithms = new HashMap();
38 
39     public void _compareIndexEntry() {
40         requiredMethod("getIndexKey()");
41         Locale locale = new Locale("zh", "CN", "");
42         String val1 = new String(new char[]{UnicodeStringPair.getUnicodeValue(0), UnicodeStringPair.getUnicodeValue(1)});
43         String val2 = new String(new char[]{UnicodeStringPair.getUnicodeValue(1), UnicodeStringPair.getUnicodeValue(0)});
44         short result1 = oObj.compareIndexEntry(val1, "", locale, val1, "", locale);
45         short result2 = oObj.compareIndexEntry(val1, "", locale, val2, "", locale);
46         short result3 = oObj.compareIndexEntry(val2, "", locale, val1, "", locale);
47 
48         tRes.tested("compareIndexEntry()", result1 == 0 && result2 + result3 == 0);
49     }
50 
51     /*
52      * gets the list of all algorithms for each listed language
53      * is OK if everyone of the returned lists are filled
54      */
55     public void _getAlgorithmList() {
56         requiredMethod("getLocaleList()");
57 
58         boolean result = true;
59         boolean locResult = false;
60 
61         for (int i = 0; i < locales.length; i++) {
62             String[] algNames = oObj.getAlgorithmList(locales[i]);
63             algorithms.put(new Integer(i), algNames);
64 
65             locResult = algNames != null && algNames.length > 0;
66             System.out.println("Locale " + i + ": " + locales[i].Country+","+locales[i].Language);
67 
68             for (int j=0; j<algNames.length; j++) {
69                 System.out.println("\tAlgorithm " + j + ": " + algNames[j]);
70             }
71 
72             if (!locResult) {
73                 log.println("No Algorithm found for " + locales[i].Country +
74                             "," + locales[i].Language);
75             }
76 
77             result &= locResult;
78         }
79 
80         tRes.tested("getAlgorithmList()", result);
81     }
82 
83     public void _getIndexKey() {
84         requiredMethod("loadAlgorithm()");
85         char[] characters = new char[] { 19968 };
86         String getIndexFor = new String(characters);
87         for (int i = 0; i < locales.length; i++) {
88             log.println("Language: " + locales[i].Language);
89 
90             for (int j = 0; j < algorithms.size(); j++) {
91                 String[] algs = (String[])algorithms.get(new Integer(j));
92                 for (int k=0;k<algs.length;k++) {
93                 log.println("\t Algorythm :" +
94                             algs[k]);
95                 oObj.loadAlgorithm(locales[i], algs[k], CollatorOptions.CollatorOptions_IGNORE_CASE);
96                 log.println("\t\t Get: " +
97                             oObj.getIndexKey(getIndexFor, "", locales[i]));
98                 }
99             }
100         }
101         tRes.tested("getIndexKey()", true);
102     }
103 
104     /*
105      * gets a list of all locales, is OK if this list isn't empty
106      */
107     public void _getLocaleList() {
108         locales = oObj.getLocaleList();
109         tRes.tested("getLocaleList()", locales.length > 0);
110     }
111 
112     /*
113      * gets one phonetic canidate for the chinese local
114      * is ok if 'yi' is returned as expected.
115      */
116     public void _getPhoneticCandidate() {
117         requiredMethod("getLocaleList()");
118 
119         boolean res = true;
120 
121         Locale loc = new Locale("zh", "CN", "");
122 
123         for (int i = 0;i<UnicodeStringPair.getValCount();i++) {
124 
125             char[] c = new char[]{UnicodeStringPair.getUnicodeValue(i)};
126 
127             String getting = oObj.getPhoneticCandidate(new String(c), loc);
128 
129             boolean locResult = getting.equals(UnicodeStringPair.getExpectedPhoneticString(i));
130 
131             if (!locResult) {
132                 log.println("Char: "+ c[0] + " (" + (int)c[0] + ")");
133                 log.println("Expected " + UnicodeStringPair.getExpectedPhoneticString(i));
134                 log.println("Getting " + getting);
135             }
136 
137             res &= locResult;
138        }
139         tRes.tested("getPhoneticCandidate()", res);
140     }
141 
142     /*
143      * loads all algorithms available in all language.
144      * Is OK if no exception occurs and the method returns
145      * true for each valid algorithm and false otherwise
146      */
147     public void _loadAlgorithm() {
148         requiredMethod("getAlgorithmList()");
149 
150         boolean res = true;
151 
152         for (int i = 0; i < algorithms.size(); i++) {
153             String[] names = (String[]) algorithms.get(new Integer(i));
154             log.println("loading algorithms for " + locales[i].Country +
155                         "," + locales[i].Language);
156 
157             for (int j = 0; j < names.length; j++) {
158                 log.println("\t Loading " + names[j]);
159 
160                 boolean localres = oObj.loadAlgorithm(locales[i], names[j],
161                                                       CollatorOptions.CollatorOptions_IGNORE_CASE);
162 
163                 if (!localres) {
164                     log.println("\t ... didn't work - FAILED");
165                 } else {
166                     log.println("\t ... worked - OK");
167                 }
168 
169                 res &= localres;
170             }
171 
172 /*            log.println("\tTrying to load 'dummy' algorithm");
173 
174             boolean localres = !oObj.loadAlgorithm(locales[i], "dummy",
175                                                    CollatorOptions.CollatorOptions_IGNORE_WIDTH);
176 
177             if (!localres) {
178                 log.println("\t ... didn't work as expected - FAILED");
179             } else {
180                 log.println("\t ... worked - OK");
181             }
182 
183             res &= localres;*/
184         }
185 
186         tRes.tested("loadAlgorithm()", res);
187     }
188 
189     /*
190      * checks the method usePhoneticEntry(). Only the languages ja, ko and zh
191      * should return true. Has OK state if exactly this is the case.
192      */
193     public void _usePhoneticEntry() {
194         requiredMethod("getLocaleList()");
195 
196         boolean res = true;
197 
198         for (int i = 0; i < locales.length; i++) {
199             boolean expected = false;
200 
201             if (locales[i].Language.equals("ja") ||
202                     locales[i].Language.equals("ko") ||
203                     locales[i].Language.equals("zh")) {
204                 expected = true;
205             }
206 
207             boolean locResult = oObj.usePhoneticEntry(locales[i]) == expected;
208 
209             if (!locResult) {
210                 log.println("Failure for language " + locales[i].Language);
211                 log.println("Expected " + expected);
212                 log.println("Getting " + oObj.usePhoneticEntry(locales[i]));
213             }
214 
215             res &= locResult;
216         }
217 
218         tRes.tested("usePhoneticEntry()", res);
219     }
220 
221     /**
222      * Helper class to handle the phonetic equivalence of unicode characters
223      * This class delivers an amount oif unicode characters and the equivalent phonetics
224      * for the "getPhoneticCandidate" test. Euivalents are only usable for zh,CN locale.
225      */
226     public static class UnicodeStringPair {
227         final static int valCount = 78;
228         static String[] sStringEquivalence = null;
229         static char[] iUnicodeEquivalence = null;
230 
231         static {
232             sStringEquivalence = new String[valCount];
233             iUnicodeEquivalence = new char[valCount];
234             fillValues();
235         }
236 
237         public static int getValCount() {
238             return valCount;
239         }
240 
241         public static String getExpectedPhoneticString(int index) {
242             if (index >= valCount) return null;
243             return sStringEquivalence[index];
244         }
245 
246         public static char getUnicodeValue(int index) {
247             if (index > valCount) return 0;
248             return iUnicodeEquivalence[index];
249         }
250 
251         private static void fillValues() {
252             iUnicodeEquivalence[0] = 20049; sStringEquivalence[0] = "zhong";
253             iUnicodeEquivalence[1] = 19968; sStringEquivalence[1] = "yi";
254             iUnicodeEquivalence[2] = 19969; sStringEquivalence[2] = "ding";
255             iUnicodeEquivalence[3] = 19970; sStringEquivalence[3] = "kao";
256             iUnicodeEquivalence[4] = 19971; sStringEquivalence[4] = "qi";
257             iUnicodeEquivalence[5] = 19972; sStringEquivalence[5] = "shang";
258             iUnicodeEquivalence[6] = 19973; sStringEquivalence[6] = "xia";
259             iUnicodeEquivalence[7] = 19975; sStringEquivalence[7] = "wan";
260             iUnicodeEquivalence[8] = 19976; sStringEquivalence[8] = "zhang";
261             iUnicodeEquivalence[9] = 19977; sStringEquivalence[9] = "san";
262             iUnicodeEquivalence[10] = 19978; sStringEquivalence[10] = "shang";
263             iUnicodeEquivalence[11] = 19979; sStringEquivalence[11] = "xia";
264             iUnicodeEquivalence[12] = 19980; sStringEquivalence[12] = "ji";
265             iUnicodeEquivalence[13] = 19981; sStringEquivalence[13] = "bu";
266             iUnicodeEquivalence[14] = 19982; sStringEquivalence[14] = "yu";
267             iUnicodeEquivalence[15] = 19983; sStringEquivalence[15] = "mian";
268             iUnicodeEquivalence[16] = 19984; sStringEquivalence[16] = "gai";
269             iUnicodeEquivalence[17] = 19985; sStringEquivalence[17] = "chou";
270             iUnicodeEquivalence[18] = 19986; sStringEquivalence[18] = "chou";
271             iUnicodeEquivalence[19] = 19987; sStringEquivalence[19] = "zhuan";
272             iUnicodeEquivalence[20] = 19988; sStringEquivalence[20] = "qie";
273             iUnicodeEquivalence[21] = 19989; sStringEquivalence[21] = "pi";
274             iUnicodeEquivalence[22] = 19990; sStringEquivalence[22] = "shi";
275             iUnicodeEquivalence[23] = 19991; sStringEquivalence[23] = "shi";
276             iUnicodeEquivalence[24] = 19992; sStringEquivalence[24] = "qiu";
277             iUnicodeEquivalence[25] = 19993; sStringEquivalence[25] = "bing";
278             iUnicodeEquivalence[26] = 19994; sStringEquivalence[26] = "ye";
279             iUnicodeEquivalence[27] = 19995; sStringEquivalence[27] = "cong";
280             iUnicodeEquivalence[28] = 19996; sStringEquivalence[28] = "dong";
281             iUnicodeEquivalence[29] = 19997; sStringEquivalence[29] = "si";
282             iUnicodeEquivalence[30] = 19998; sStringEquivalence[30] = "cheng";
283             iUnicodeEquivalence[31] = 19999; sStringEquivalence[31] = "diu";
284             iUnicodeEquivalence[32] = 20000; sStringEquivalence[32] = "qiu";
285             iUnicodeEquivalence[33] = 20001; sStringEquivalence[33] = "liang";
286             iUnicodeEquivalence[34] = 20002; sStringEquivalence[34] = "diu";
287             iUnicodeEquivalence[35] = 20003; sStringEquivalence[35] = "you";
288             iUnicodeEquivalence[36] = 20004; sStringEquivalence[36] = "liang";
289             iUnicodeEquivalence[37] = 20005; sStringEquivalence[37] = "yan";
290             iUnicodeEquivalence[38] = 20006; sStringEquivalence[38] = "bing";
291             iUnicodeEquivalence[39] = 20007; sStringEquivalence[39] = "sang";
292             iUnicodeEquivalence[40] = 20008; sStringEquivalence[40] = "shu";
293             iUnicodeEquivalence[41] = 20009; sStringEquivalence[41] = "jiu";
294             iUnicodeEquivalence[42] = 20010; sStringEquivalence[42] = "ge";
295             iUnicodeEquivalence[43] = 20011; sStringEquivalence[43] = "ya";
296             iUnicodeEquivalence[44] = 20012; sStringEquivalence[44] = "qiang";
297             iUnicodeEquivalence[45] = 20013; sStringEquivalence[45] = "zhong";
298             iUnicodeEquivalence[46] = 20014; sStringEquivalence[46] = "ji";
299             iUnicodeEquivalence[47] = 20015; sStringEquivalence[47] = "jie";
300             iUnicodeEquivalence[48] = 20016; sStringEquivalence[48] = "feng";
301             iUnicodeEquivalence[49] = 20017; sStringEquivalence[49] = "guan";
302             iUnicodeEquivalence[50] = 20018; sStringEquivalence[50] = "chuan";
303             iUnicodeEquivalence[51] = 20019; sStringEquivalence[51] = "chan";
304             iUnicodeEquivalence[52] = 20020; sStringEquivalence[52] = "lin";
305             iUnicodeEquivalence[53] = 20021; sStringEquivalence[53] = "zhuo";
306             iUnicodeEquivalence[54] = 20022; sStringEquivalence[54] = "zhu";
307             iUnicodeEquivalence[55] = 20024; sStringEquivalence[55] = "wan";
308             iUnicodeEquivalence[56] = 20025; sStringEquivalence[56] = "dan";
309             iUnicodeEquivalence[57] = 20026; sStringEquivalence[57] = "wei";
310             iUnicodeEquivalence[58] = 20027; sStringEquivalence[58] = "zhu";
311             iUnicodeEquivalence[59] = 20028; sStringEquivalence[59] = "jing";
312             iUnicodeEquivalence[60] = 20029; sStringEquivalence[60] = "li";
313             iUnicodeEquivalence[61] = 20030; sStringEquivalence[61] = "ju";
314             iUnicodeEquivalence[62] = 20031; sStringEquivalence[62] = "pie";
315             iUnicodeEquivalence[63] = 20032; sStringEquivalence[63] = "fu";
316             iUnicodeEquivalence[64] = 20033; sStringEquivalence[64] = "yi";
317             iUnicodeEquivalence[65] = 20034; sStringEquivalence[65] = "yi";
318             iUnicodeEquivalence[66] = 20035; sStringEquivalence[66] = "nai";
319             iUnicodeEquivalence[67] = 20037; sStringEquivalence[67] = "jiu";
320             iUnicodeEquivalence[68] = 20038; sStringEquivalence[68] = "jiu";
321             iUnicodeEquivalence[69] = 20039; sStringEquivalence[69] = "tuo";
322             iUnicodeEquivalence[70] = 20040; sStringEquivalence[70] = "me";
323             iUnicodeEquivalence[71] = 20041; sStringEquivalence[71] = "yi";
324             iUnicodeEquivalence[72] = 20043; sStringEquivalence[72] = "zhi";
325             iUnicodeEquivalence[73] = 20044; sStringEquivalence[73] = "wu";
326             iUnicodeEquivalence[74] = 20045; sStringEquivalence[74] = "zha";
327             iUnicodeEquivalence[75] = 20046; sStringEquivalence[75] = "hu";
328             iUnicodeEquivalence[76] = 20047; sStringEquivalence[76] = "fa";
329             iUnicodeEquivalence[77] = 20048; sStringEquivalence[77] = "le";
330         }
331     }
332 }
333