aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/speech/TextFactoryTest.java
blob: c01fbd0c110f73c767fce9b262e8df101df7b2a9 (plain)
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
package cgeo.geocaching.speech;

import static org.assertj.core.api.Assertions.assertThat;

import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.location.Geopoint;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.settings.TestSettings;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.test.AndroidTestCase;

import java.util.Locale;

public class TextFactoryTest extends AndroidTestCase {

    private static final Geopoint MY_POSITION = new Geopoint(15, -86);
    private static final Geopoint NORTH_6100M = new Geopoint(15.054859, -86);
    private static final Geopoint WEST_1MILE = new Geopoint(15, -86.014984);
    private static final Geopoint SOUTH_1020M = new Geopoint(14.990827, -86);
    private static final Geopoint EAST_123M = new Geopoint(15, -85.998855);
    private static final Geopoint WEST_34M = new Geopoint(15, -86.000317);
    private static final Geopoint EAST_1M = new Geopoint(15, -85.999990);
    private static final Geopoint EAST_1FT = new Geopoint(15, -85.999996);

    private Locale defaultLocale1;
    private Locale defaultLocale2;
    private boolean defaultMetric;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        final Resources resources = CgeoApplication.getInstance().getResources();
        final Configuration config = resources.getConfiguration();
        defaultLocale1 = config.locale;
        defaultLocale2 = Locale.getDefault();
        defaultMetric = !Settings.useImperialUnits();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        setLocale(defaultLocale1, defaultLocale2, defaultMetric);
    }

    public static void testGetTextEn() {
        setLocale(Locale.UK, true);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("one o'clock. 6 kilometers");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("9 o'clock. 1.6 kilometers");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("6 o'clock. one kilometer");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("3 o'clock. 120 meters");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("11 o'clock. 34 meters");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1M, 180)).isEqualTo("9 o'clock. one meter");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("12 o'clock. 0 meters");

        setLocale(Locale.UK, false);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("one o'clock. 4 miles");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("9 o'clock. one mile");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("6 o'clock. 0.6 miles");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("3 o'clock. 400 feet");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("11 o'clock. 111 feet");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1FT, 180)).isEqualTo("9 o'clock. one foot");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("12 o'clock. 0 feet");
    }

    public static void testGetTextDe() {
        setLocale(Locale.GERMANY, true);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("ein Uhr. 6 Kilometer");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("9 Uhr. 1,6 Kilometer");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("6 Uhr. ein Kilometer");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("3 Uhr. 120 Meter");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("11 Uhr. 34 Meter");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1M, 180)).isEqualTo("9 Uhr. ein Meter");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("12 Uhr. 0 Meter");

        setLocale(Locale.GERMANY, false);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("ein Uhr. 4 Meilen");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("9 Uhr. eine Meile");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("6 Uhr. 0,6 Meilen");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("3 Uhr. 400 Fuß");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("11 Uhr. 111 Fuß");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1FT, 180)).isEqualTo("9 Uhr. ein Fuß");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("12 Uhr. 0 Fuß");
    }

    public static void testGetTextFr() {
        setLocale(Locale.FRANCE, true);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("une heure. 6 kilomètres");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("9 heures. 1,6 kilomètres");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("6 heures. un kilomètre");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("3 heures. 120 mètres");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("11 heures. 34 mètres");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1M, 180)).isEqualTo("9 heures. un mètre");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("12 heures. 0 mètre");

        setLocale(Locale.FRANCE, false);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("une heure. 4 milles");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("9 heures. un mille");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("6 heures. 0,6 milles");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("3 heures. 400 pieds");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("11 heures. 111 pieds");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1FT, 180)).isEqualTo("9 heures. un pied");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("12 heures. 0 pied");
    }

    public static void testGetTextIt() {
        setLocale(Locale.ITALY, true);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("a ore una. 6 chilometri");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("a ore 9. 1,6 chilometri");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("a ore 6. un chilometro");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("a ore 3. 120 metri");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("a ore 11. 34 metri");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1M, 180)).isEqualTo("a ore 9. un metro");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("a ore 12. 0 metri");

        setLocale(Locale.ITALY, false);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("a ore una. 4 miglia");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("a ore 9. un miglio");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("a ore 6. 0,6 miglia");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("a ore 3. 400 piedi");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("a ore 11. 111 piedi");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1FT, 180)).isEqualTo("a ore 9. un piede");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("a ore 12. 0 piedi");
    }

    public static void testGetTextSv() {
        setLocale(new Locale("sv", "SE"), true);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("Klockan ett. 6 kilometer");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("Klockan 9. 1,6 kilometer");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("Klockan 6. en kilometer");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("Klockan 3. 120 meter");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("Klockan 11. 34 meter");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1M, 180)).isEqualTo("Klockan 9. en meter");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("Klockan 12. 0 meter");

        setLocale(new Locale("sv", "SE"), false);
        assertThat(TextFactory.getText(MY_POSITION, NORTH_6100M, 330)).isEqualTo("Klockan ett. 4 engelsk mil");
        assertThat(TextFactory.getText(MY_POSITION, WEST_1MILE, 0)).isEqualTo("Klockan 9. en engelsk mil");
        assertThat(TextFactory.getText(MY_POSITION, SOUTH_1020M, 0)).isEqualTo("Klockan 6. 0,6 engelsk mil");
        assertThat(TextFactory.getText(MY_POSITION, EAST_123M, 0)).isEqualTo("Klockan 3. 400 fot");
        assertThat(TextFactory.getText(MY_POSITION, WEST_34M, 290)).isEqualTo("Klockan 11. 111 fot");
        assertThat(TextFactory.getText(MY_POSITION, EAST_1FT, 180)).isEqualTo("Klockan 9. en fot");
        assertThat(TextFactory.getText(MY_POSITION, MY_POSITION, 0)).isEqualTo("Klockan 12. 0 fot");
    }

    private static void setLocale(Locale locale, boolean metric) {
        setLocale(locale, locale, metric);
    }

    private static void setLocale(Locale locale1, Locale locale2, boolean metric) {
        final Configuration config = new Configuration();
        config.locale = locale1;
        final Resources resources = CgeoApplication.getInstance().getResources();
        resources.updateConfiguration(config, resources.getDisplayMetrics());

        Locale.setDefault(locale2);
        TestSettings.setUseImperialUnits(!metric);
    }
}