wangzhibo
2026-08-16 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package nodomain.freeyourgadget.gadgetbridge.test;
 
import android.content.SharedPreferences;
 
import org.junit.Test;
 
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
import nodomain.freeyourgadget.gadgetbridge.util.RtlUtils;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
 
/**
 * Tests RtlUtils
 */
public class RtlUtilsTest extends TestBase {
 
//    @Test
    private Character hebrew1 = 'א';
    private Character hebrew2 = 'ם';
 
    private Character arabic1 = 'ا';
    private Character arabic2 = 'ن';
 
    private Character english1 = 'a';
    private Character english2 = 'N';
 
    private Character punctuation1 = '.';
    private Character punctuation2 = '-';
 
    private Character space = ' ';
 
    private Character endLine1 = '\0';
    private Character endLine2 = '\n';
 
    private Character[] contextualIsolated = {'ء', '\uFE80'};
    private Character[] contextualBeginning = {'س', '\uFEB3'};
    private Character[] contextualMiddle = {'ض', '\uFEC0'};
    private Character[] contextualEndNoMiddle = {'آ', '\uFE82'};
    private Character[] contextualLam = {(char)('ل' + 'أ'), '\uFEF8'};
 
    private String hebrewWord1 = "שלום";
    private String hebrewWord2 = "וברכה";
    private String hebrewPhrase = hebrewWord1 + " " + hebrewWord2;
 
    private String arabicWord1 = "سلام";
    private String arabicWord2 = "البركة";
    private String arabicPhrase = arabicWord1 + " " + arabicWord2;
 
    private String englishWord1 = "Hello";
    private String englishWord2 = "Welcome";
    private String englishPhrase = englishWord1 + " and " + englishWord2;
 
    @Test
    public void testGetCharacterType() throws Exception {
 
        RtlUtils.characterType actual = RtlUtils.getCharacterType(hebrew1);
        RtlUtils.characterType expected = RtlUtils.characterType.rtl;
        assertEquals("Get Hebrew Character type failed", expected, actual);
 
        actual = RtlUtils.getCharacterType(arabic1);
        expected = RtlUtils.characterType.rtl_arabic;
        assertEquals("Get Arabic Character type failed", expected, actual);
 
        actual = RtlUtils.getCharacterType(english1);
        expected = RtlUtils.characterType.ltr;
        assertEquals("Get Hebrew Character type failed", expected, actual);
 
        actual = RtlUtils.getCharacterType(' ');
        expected = RtlUtils.characterType.space;
        assertEquals("Get Hebrew Character type failed", expected, actual);
    }
 
 
    @Test
    public void testIsHebrew() throws Exception {
        boolean result = RtlUtils.isHebrew(hebrew1);
        assertTrue("Is Hebrew Character failed", result);
 
        result = RtlUtils.isHebrew(hebrew2);
        assertTrue("Is Hebrew Character failed", result);
 
        result = RtlUtils.isHebrew(arabic1);
        assertFalse("Is Hebrew Character failed", result);
 
        result = RtlUtils.isHebrew(english1);
        assertFalse("Is Hebrew Character failed", result);
    }
 
    @Test
    public void testIsArabic() throws Exception {
        boolean result = RtlUtils.isArabic(arabic1);
        assertTrue("Is Arabic Character failed", result);
 
        result = RtlUtils.isArabic(arabic2);
        assertTrue("Is Arabic Character failed", result);
 
        result = RtlUtils.isArabic(hebrew1);
        assertFalse("Is Arabic Character failed", result);
 
        result = RtlUtils.isArabic(english1);
        assertFalse("Is Arabic Character failed", result);
    }
 
    @Test
    public void testIsLtr() throws Exception {
        boolean result = RtlUtils.isLtr(english1);
        assertTrue("Is Ltr Character failed", result);
 
        result = RtlUtils.isLtr(english2);
        assertTrue("Is Ltr Character failed", result);
 
        result = RtlUtils.isLtr(hebrew1);
        assertFalse("Is Ltr Character failed", result);
 
        result = RtlUtils.isLtr(arabic1);
        assertFalse("Is Ltr Character failed", result);
    }
 
    @Test
    public void testIsRtl() throws Exception {
        boolean result = RtlUtils.isRtl(hebrew1);
        assertTrue("Is Rtl Character failed", result);
 
        result = RtlUtils.isRtl(arabic1);
        assertTrue("Is Rtl Character failed", result);
 
        result = RtlUtils.isRtl(english1);
        assertFalse("Is Rtl Character failed", result);
 
        result = RtlUtils.isRtl(punctuation1);
        assertFalse("Is Rtl Character failed", result);
    }
 
    @Test
    public void testIsPunctuation() throws Exception {
        boolean result = RtlUtils.isPunctuations(punctuation1);
        assertTrue("Is Punctuation Character failed", result);
 
        result = RtlUtils.isPunctuations(punctuation2);
        assertTrue("Is Punctuation Character failed", result);
 
        result = RtlUtils.isPunctuations(english1);
        assertFalse("Is Punctuation Character failed", result);
 
        result = RtlUtils.isPunctuations(space);
        assertFalse("Is Punctuation Character failed", result);
    }
 
    @Test
    public void testIsSpaceSign() throws Exception {
        boolean result = RtlUtils.isSpaceSign(space);
        assertTrue("Is Space Sign Character failed", result);
 
        result = RtlUtils.isSpaceSign(punctuation1);
        assertFalse("Is Space Sign Character failed", result);
 
        result = RtlUtils.isSpaceSign(english1);
        assertFalse("Is SpaceSign Character failed", result);
 
        result = RtlUtils.isSpaceSign(hebrew1);
        assertFalse("Is SpaceSign Character failed", result);
    }
 
    @Test
    public void testIsEndLineSign() throws Exception {
        boolean result = RtlUtils.isEndLineSign(endLine1);
        assertTrue("Is End Line Sign Character failed", result);
 
        result = RtlUtils.isEndLineSign(endLine2);
        assertTrue("Is End Line Sign Character failed", result);
 
        result = RtlUtils.isEndLineSign(english1);
        assertFalse("Is End Line Sign Character failed", result);
 
        result = RtlUtils.isEndLineSign(space);
        assertFalse("Is End Line Sign Character failed", result);
    }
 
    @Test
    public void testExceptionAfterLam() throws Exception {
        boolean result = RtlUtils.exceptionAfterLam('\u0623');
        assertTrue("Is Exception After Lam failed", result);
 
        result = RtlUtils.exceptionAfterLam('\u0630');
        assertFalse("Is Exception After Lam failed", result);
 
        result = RtlUtils.exceptionAfterLam('\u062A');
        assertFalse("Is Exception After Lam failed", result);
    }
 
 
    @Test
    public void testGetContextualSymbol() throws Exception {
 
        Character actual = RtlUtils.getContextualSymbol(contextualIsolated[0], RtlUtils.contextualState.isolate);
        Character expected = contextualIsolated[1];
        assertEquals("Get Contextual Symbol failed", expected, actual);
 
        actual = RtlUtils.getContextualSymbol(contextualBeginning[0], RtlUtils.contextualState.begin);
        expected = contextualBeginning[1];
        assertEquals("Get Contextual Symbol failed", expected, actual);
 
        actual = RtlUtils.getContextualSymbol(contextualMiddle[0], RtlUtils.contextualState.middle);
        expected = contextualMiddle[1];
        assertEquals("Get Contextual Symbol failed", expected, actual);
 
        actual = RtlUtils.getContextualSymbol(contextualEndNoMiddle[0], RtlUtils.contextualState.end);
        expected = contextualEndNoMiddle[1];
        assertEquals("Get Contextual Symbol failed", expected, actual);
 
        actual = RtlUtils.getContextualSymbol(contextualEndNoMiddle[0], RtlUtils.contextualState.middle);
        expected = contextualEndNoMiddle[0];
        assertEquals("Get Contextual Symbol failed", expected, actual);
    }
 
    @Test
    public void testGetCharContextualState() throws Exception {
 
        RtlUtils.contextualState actual = RtlUtils.getCharContextualState(RtlUtils.contextualState.isolate, contextualBeginning[0], contextualEndNoMiddle[0]);
        RtlUtils.contextualState expected = RtlUtils.contextualState.begin;
        assertEquals("Get Char Contextual State failed", expected, actual);
 
        actual = RtlUtils.getCharContextualState(RtlUtils.contextualState.begin, contextualMiddle[0], contextualEndNoMiddle[0]);
        expected = RtlUtils.contextualState.middle;
        assertEquals("Get Char Contextual State failed", expected, actual);
 
        actual = RtlUtils.getCharContextualState(RtlUtils.contextualState.begin, contextualMiddle[0], contextualIsolated[0]);
        expected = RtlUtils.contextualState.end;
        assertEquals("Get Char Contextual State failed", expected, actual);
 
        actual = RtlUtils.getCharContextualState(RtlUtils.contextualState.begin, contextualIsolated[0], contextualEndNoMiddle[0]);
        expected = RtlUtils.contextualState.isolate;
        assertEquals("Get Char Contextual State failed", expected, actual);
    }
 
    @Test
    public void testConvertToContextual() throws Exception {
 
        String actual = RtlUtils.convertToContextual("");
        String expected = "";
        assertEquals("Convert To Contextual failed", expected, actual);
 
        char[] nonContextual = {'ج', 'ج', 'ج'};
        char[] contextual = {'\uFE9F', '\uFEA0', '\uFE9E'};
        actual = RtlUtils.convertToContextual(new StringBuilder().append(nonContextual).toString());
        expected = new StringBuilder().append(contextual).toString();
        assertEquals("Convert To Contextual failed", expected, actual);
 
        char[] nonContextual2 = {'ج', 'ج', 'ل', '\u0622','ج'};
        char[] contextual2 = {'\uFE9F', '\uFEA0', '\uFEF6', '\uFE9D'};
        actual = RtlUtils.convertToContextual(new StringBuilder().append(nonContextual2).toString());
        expected = new StringBuilder().append(contextual2).toString();
        assertEquals("Convert To Contextual failed", expected, actual);
    }
 
    @Test
    public void testReverse() throws Exception {
 
        String actual = RtlUtils.reverse(hebrewWord1);
        String expected = "םולש";
        assertEquals("Reverse failed", expected, actual);
 
        actual = RtlUtils.reverse(hebrewPhrase);
        expected = "הכרבו םולש";
        assertEquals("Reverse failed", expected, actual);
 
        actual = RtlUtils.reverse("טקסט עם (סוגריים) וסימן שאלה?");
        expected = "?הלאש ןמיסו (םיירגוס) םע טסקט";
        assertEquals("Reverse failed", expected, actual);
    }
 
    @Test
    public void testFixWhitespace() throws Exception {
 
        String actual = RtlUtils.fixWhitespace(englishPhrase + " ");
        String expected = " " + englishPhrase;
        assertEquals("fix whitespace failed", expected, actual);
 
        actual = RtlUtils.fixWhitespace(englishPhrase);
        expected = englishPhrase;
        assertEquals("fix whitespace failed", expected, actual);
    }
 
    @Test
    public void testFixRtl() throws Exception {
 
        String actual = RtlUtils.fixRtl("Only english");
        String expected = "Only english";
        assertEquals("fix rtl failed", expected, actual);
 
        actual = RtlUtils.fixRtl("Only very long english with more than 18 characters");
        expected = " Only very long\n" +
                " english with more\n" +
                " than 18\n" +
                "characters";
        assertEquals("fix rtl failed", expected, actual);
 
        actual = RtlUtils.fixRtl("רק עברית");
        expected = "תירבע קר";
        assertEquals("fix rtl failed", expected, actual);
 
        actual = RtlUtils.fixRtl("English and עברית");
        expected = "תירבע English and";
        assertEquals("fix rtl failed", expected, actual);
 
        actual = RtlUtils.fixRtl("משפט ארוך עם עברית and English וגם קצת סימנים כמו ?!$ (וגם ^.)");
        expected = " םע ךורא טפשמ\n" +
                " and English תירבע\n" +
                " םינמיס תצק םגו\n" +
                "(.^ םגו) $!? ומכ";
        assertEquals("fix rtl failed", expected, actual);
 
 
 
        actual = RtlUtils.fixRtl("משפט עם כותרת\0ושמכיל גם ירידת\nשורה");
        expected = "תרתוכ םע טפשמ\0תדירי םג ליכמשו\n" +
                "הרוש";
        assertEquals("fix rtl failed", expected, actual);
    }
 
    @Test
    public void testRtlSupport() throws Exception {
        setDefaultRtl();
        assertFalse("Rtl option fail! Expected 'Off' by default, but result is 'On'", RtlUtils.rtlSupport());
 
        enableRtl(true);
        assertTrue("Rtl option fail! Expected 'On', but result is 'Off'", RtlUtils.rtlSupport());
    }
 
    private void setDefaultRtl() {
        SharedPreferences settings = GBApplication.getPrefs().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.remove(GBPrefs.RTL_SUPPORT);
        editor.apply();
    }
 
    private void enableRtl(boolean enable) {
        SharedPreferences settings = GBApplication.getPrefs().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(GBPrefs.RTL_SUPPORT, enable);
        editor.apply();
    }
 
    @Test
    public void testContextualSupport() throws Exception {
        setDefaultContextual();
        assertFalse("Contextual option fail! Expected 'Off' by default, but result is 'On'", RtlUtils.contextualSupport());
 
        enableContextual(true);
        assertTrue("Contextual option fail! Expected 'On', but result is 'Off'", RtlUtils.contextualSupport());
    }
 
    private void setDefaultContextual() {
        SharedPreferences settings = GBApplication.getPrefs().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.remove(GBPrefs.RTL_CONTEXTUAL_ARABIC);
        editor.apply();
    }
 
    private void enableContextual(boolean enable) {
        SharedPreferences settings = GBApplication.getPrefs().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(GBPrefs.RTL_CONTEXTUAL_ARABIC, enable);
        editor.apply();
    }
}