Debian Codegraph @

android/eSpeakTests/src/com/reecedunn/espeak/test/VoiceSettingsTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2013 Reece H. Dunn
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.reecedunn.espeak.test;
18
19 import android.content.SharedPreferences;
20 import android.preference.PreferenceManager;
21
22 import com.reecedunn.espeak.SpeechSynthesis;
23 import com.reecedunn.espeak.VoiceSettings;
24
25 import org.json.JSONException;
26 import org.json.JSONObject;
27
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.hamcrest.Matchers.*;
30
31 public class VoiceSettingsTest extends TextToSpeechTestCase
32 {
33 private SpeechSynthesis.SynthReadyCallback mCallback = new SpeechSynthesis.SynthReadyCallback()
34 {
35 @Override
36 public void onSynthDataReady(byte[] audioData)
37 {
38 }
39
40 @Override
41 public void onSynthDataComplete()
42 {
43 }
44 };
45
46 // No Settings (New Install)
47
48 public void testNoPreferences()
49 {
50 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
51 SharedPreferences.Editor editor = prefs.edit();
52 editor.clear();
53 editor.commit();
54
55 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
56 VoiceSettings settings = new VoiceSettings(prefs, synth);
57 assertThat(settings.getVoiceVariant().toString(), is("male"));
58 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
59 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
60 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
61 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
62 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
63 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
64
65 try {
66 JSONObject json = settings.toJSON();
67 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
68 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
69 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
70 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
71 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
72 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
73 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
74 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
75 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
76 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
77 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
78 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
79 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
80 } catch (JSONException e) {
81 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
82 }
83 }
84
85 // Old Settings
86
87 public void testDefaultGenderMale()
88 {
89 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
90 SharedPreferences.Editor editor = prefs.edit();
91 editor.clear();
92 editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_MALE));
93 editor.commit();
94
95 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
96 VoiceSettings settings = new VoiceSettings(prefs, synth);
97 assertThat(settings.getVoiceVariant().toString(), is("male"));
98 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
99 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
100 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
101 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
102 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
103 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
104
105 try {
106 JSONObject json = settings.toJSON();
107 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
108 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
109 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
110 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
111 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
112 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
113 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
114 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
115 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
116 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
117 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
118 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
119 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
120 } catch (JSONException e) {
121 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
122 }
123 }
124
125 public void testDefaultGenderFemale()
126 {
127 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
128 SharedPreferences.Editor editor = prefs.edit();
129 editor.clear();
130 editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_FEMALE));
131 editor.commit();
132
133 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
134 VoiceSettings settings = new VoiceSettings(prefs, synth);
135 assertThat(settings.getVoiceVariant().toString(), is("female"));
136 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
137 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
138 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
139 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
140 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
141 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
142
143 try {
144 JSONObject json = settings.toJSON();
145 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
146 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("female"));
147 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
148 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
149 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
150 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
151 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
152 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
153 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
154 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
155 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
156 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
157 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
158 } catch (JSONException e) {
159 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
160 }
161 }
162
163 public void defaultRateTest(int prefValue, int settingValue, SpeechSynthesis synth)
164 {
165 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
166 SharedPreferences.Editor editor = prefs.edit();
167 editor.clear();
168 editor.putString("default_rate", Integer.toString(prefValue));
169 editor.commit();
170
171 VoiceSettings settings = new VoiceSettings(prefs, synth);
172 assertThat(settings.getVoiceVariant().toString(), is("male"));
173 assertThat(settings.getRate(), is(settingValue));
174 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
175 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
176 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
177 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
178 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
179
180 try {
181 JSONObject json = settings.toJSON();
182 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
183 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
184 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
185 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(settingValue));
186 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
187 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
188 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
189 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
190 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
191 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
192 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
193 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
194 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
195 } catch (JSONException e) {
196 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
197 }
198 }
199
200 public void testDefaultRate()
201 {
202 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
203 defaultRateTest(300, 449, synth); // clamped to maximum value
204 defaultRateTest(200, 350, synth);
205 defaultRateTest(100, 175, synth); // default value
206 defaultRateTest( 50, 87, synth);
207 defaultRateTest( 25, 80, synth); // clamped to minimum value
208 }
209
210 public void defaultPitchTest(int prefValue, int settingValue, SpeechSynthesis synth)
211 {
212 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
213 SharedPreferences.Editor editor = prefs.edit();
214 editor.clear();
215 editor.putString("default_pitch", Integer.toString(prefValue));
216 editor.commit();
217
218 VoiceSettings settings = new VoiceSettings(prefs, synth);
219 assertThat(settings.getVoiceVariant().toString(), is("male"));
220 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
221 assertThat(settings.getPitch(), is(settingValue));
222 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
223 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
224 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
225 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
226
227 try {
228 JSONObject json = settings.toJSON();
229 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
230 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
231 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
232 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
233 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
234 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(settingValue));
235 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
236 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
237 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
238 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
239 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
240 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
241 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
242 } catch (JSONException e) {
243 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
244 }
245 }
246
247 public void testDefaultPitch()
248 {
249 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
250 defaultPitchTest(250, 100, synth); // clamped to maximum value
251 defaultPitchTest(200, 100, synth);
252 defaultPitchTest(100, 50, synth); // default value
253 defaultPitchTest( 50, 25, synth);
254 defaultPitchTest( 0, 0, synth);
255 defaultPitchTest( -5, 0, synth); // clamped to minimum value
256 }
257
258 // New Settings
259
260 public void testEspeakVariant()
261 {
262 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
263 SharedPreferences.Editor editor = prefs.edit();
264 editor.clear();
265 editor.putString("espeak_variant", "klatt2-old");
266 editor.commit();
267
268 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
269 VoiceSettings settings = new VoiceSettings(prefs, synth);
270 assertThat(settings.getVoiceVariant().toString(), is("klatt2-old"));
271 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
272 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
273 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
274 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
275 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
276 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
277
278 try {
279 JSONObject json = settings.toJSON();
280 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
281 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("klatt2-old"));
282 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
283 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
284 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
285 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
286 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
287 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
288 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
289 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
290 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
291 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
292 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
293 } catch (JSONException e) {
294 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
295 }
296 }
297
298 public void espeakRateTest(int prefValue, int settingValue, SpeechSynthesis synth)
299 {
300 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
301 SharedPreferences.Editor editor = prefs.edit();
302 editor.clear();
303 editor.putString("espeak_rate", Integer.toString(prefValue));
304 editor.commit();
305
306 VoiceSettings settings = new VoiceSettings(prefs, synth);
307 assertThat(settings.getVoiceVariant().toString(), is("male"));
308 assertThat(settings.getRate(), is(settingValue));
309 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
310 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
311 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
312 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
313 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
314
315 try {
316 JSONObject json = settings.toJSON();
317 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
318 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
319 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
320 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(settingValue));
321 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
322 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
323 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
324 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
325 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
326 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
327 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
328 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
329 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
330 } catch (JSONException e) {
331 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
332 }
333 }
334
335 public void testEspeakRate()
336 {
337 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
338 espeakRateTest(500, 449, synth); // clamped to maximum value
339 espeakRateTest(400, 400, synth);
340 espeakRateTest(200, 200, synth);
341 espeakRateTest(175, 175, synth); // default value
342 espeakRateTest(150, 150, synth);
343 espeakRateTest( 70, 80, synth); // clamped to minimum value
344 }
345
346 public void espeakPitchTest(int prefValue, int settingValue, SpeechSynthesis synth)
347 {
348 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
349 SharedPreferences.Editor editor = prefs.edit();
350 editor.clear();
351 editor.putString("espeak_pitch", Integer.toString(prefValue));
352 editor.commit();
353
354 VoiceSettings settings = new VoiceSettings(prefs, synth);
355 assertThat(settings.getVoiceVariant().toString(), is("male"));
356 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
357 assertThat(settings.getPitch(), is(settingValue));
358 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
359 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
360 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
361 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
362
363 try {
364 JSONObject json = settings.toJSON();
365 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
366 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
367 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
368 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
369 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
370 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(settingValue));
371 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
372 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
373 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
374 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
375 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
376 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
377 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
378 } catch (JSONException e) {
379 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
380 }
381 }
382
383 public void testEspeakPitch()
384 {
385 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
386 espeakPitchTest(110, 100, synth); // clamped to maximum value
387 espeakPitchTest(100, 100, synth);
388 espeakPitchTest( 50, 50, synth); // default value
389 espeakPitchTest( 10, 10, synth);
390 espeakPitchTest( -5, 0, synth); // clamped to minimum value
391 }
392
393 public void espeakPitchRangeTest(int prefValue, int settingValue, SpeechSynthesis synth)
394 {
395 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
396 SharedPreferences.Editor editor = prefs.edit();
397 editor.clear();
398 editor.putString("espeak_pitch_range", Integer.toString(prefValue));
399 editor.commit();
400
401 VoiceSettings settings = new VoiceSettings(prefs, synth);
402 assertThat(settings.getVoiceVariant().toString(), is("male"));
403 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
404 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
405 assertThat(settings.getPitchRange(), is(settingValue));
406 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
407 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
408 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
409
410 try {
411 JSONObject json = settings.toJSON();
412 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
413 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
414 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
415 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
416 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
417 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
418 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
419 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(settingValue));
420 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
421 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
422 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
423 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
424 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
425 } catch (JSONException e) {
426 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
427 }
428 }
429
430 public void testEspeakPitchRange()
431 {
432 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
433 espeakPitchRangeTest(110, 100, synth); // clamped to maximum value
434 espeakPitchRangeTest(100, 100, synth);
435 espeakPitchRangeTest( 50, 50, synth); // default value
436 espeakPitchRangeTest( 10, 10, synth);
437 espeakPitchRangeTest( -5, 0, synth); // clamped to minimum value
438 }
439
440 public void espeakVolumeTest(int prefValue, int settingValue, SpeechSynthesis synth)
441 {
442 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
443 SharedPreferences.Editor editor = prefs.edit();
444 editor.clear();
445 editor.putString("espeak_volume", Integer.toString(prefValue));
446 editor.commit();
447
448 VoiceSettings settings = new VoiceSettings(prefs, synth);
449 assertThat(settings.getVoiceVariant().toString(), is("male"));
450 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
451 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
452 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
453 assertThat(settings.getVolume(), is(settingValue));
454 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
455 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
456
457 try {
458 JSONObject json = settings.toJSON();
459 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
460 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
461 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
462 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
463 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
464 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
465 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
466 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
467 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
468 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(settingValue));
469 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
470 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
471 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
472 } catch (JSONException e) {
473 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
474 }
475 }
476
477 public void testEspeakVolume()
478 {
479 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
480 espeakVolumeTest(210, 200, synth); // clamped to maximum value
481 espeakVolumeTest(150, 150, synth);
482 espeakVolumeTest(100, 100, synth); // default value
483 espeakVolumeTest( 50, 50, synth);
484 espeakVolumeTest( -5, 0, synth); // clamped to minimum value
485 }
486
487 public void espeakPunctuationLevelTest(int prefValue, int settingValue, String jsonValue, SpeechSynthesis synth)
488 {
489 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
490 SharedPreferences.Editor editor = prefs.edit();
491 editor.clear();
492 editor.putString("espeak_punctuation_level", Integer.toString(prefValue));
493 editor.commit();
494
495 VoiceSettings settings = new VoiceSettings(prefs, synth);
496 assertThat(settings.getVoiceVariant().toString(), is("male"));
497 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
498 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
499 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
500 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
501 assertThat(settings.getPunctuationLevel(), is(settingValue));
502 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
503
504 try {
505 JSONObject json = settings.toJSON();
506 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
507 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
508 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
509 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
510 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
511 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
512 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
513 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
514 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
515 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
516 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
517 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(jsonValue));
518 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
519 } catch (JSONException e) {
520 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
521 }
522 }
523
524 public void testEspeakPunctuationLevel()
525 {
526 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
527 espeakPunctuationLevelTest( 3, SpeechSynthesis.PUNCT_SOME, "some", synth); // clamped to maximum value
528 espeakPunctuationLevelTest( 2, SpeechSynthesis.PUNCT_SOME, "some", synth);
529 espeakPunctuationLevelTest( 1, SpeechSynthesis.PUNCT_ALL, "all", synth);
530 espeakPunctuationLevelTest( 0, SpeechSynthesis.PUNCT_NONE, "none", synth);
531 espeakPunctuationLevelTest(-1, SpeechSynthesis.PUNCT_NONE, "none", synth); // clamped to minimum value
532 }
533
534 public void testEspeakPunctuationCharacters()
535 {
536 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
537 SharedPreferences.Editor editor = prefs.edit();
538 editor.clear();
539 editor.putString("espeak_punctuation_characters", ".?!");
540 editor.commit();
541
542 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
543 VoiceSettings settings = new VoiceSettings(prefs, synth);
544 assertThat(settings.getVoiceVariant().toString(), is("male"));
545 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
546 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
547 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
548 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
549 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
550 assertThat(settings.getPunctuationCharacters(), is(".?!"));
551
552 try {
553 JSONObject json = settings.toJSON();
554 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
555 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
556 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
557 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
558 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
559 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
560 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
561 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
562 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
563 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
564 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
565 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
566 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(instanceOf(String.class)));
567 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(".?!"));
568 } catch (JSONException e) {
569 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
570 }
571 }
572
573 // Mixed (Old and New) Settings
574
575 public void testEspeakVariantWithDefaultGenderFemale()
576 {
577 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
578 SharedPreferences.Editor editor = prefs.edit();
579 editor.clear();
580 editor.putString("default_gender", Integer.toString(SpeechSynthesis.GENDER_FEMALE));
581 editor.putString("espeak_variant", "klatt4");
582 editor.commit();
583
584 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
585 VoiceSettings settings = new VoiceSettings(prefs, synth);
586 assertThat(settings.getVoiceVariant().toString(), is("klatt4"));
587 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
588 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
589 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
590 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
591 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
592 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
593
594 try {
595 JSONObject json = settings.toJSON();
596 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
597 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("klatt4"));
598 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
599 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
600 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
601 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
602 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
603 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
604 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
605 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
606 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
607 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
608 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
609 } catch (JSONException e) {
610 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
611 }
612 }
613
614 public void testEspeakRateWithDefaultRate()
615 {
616 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
617 SharedPreferences.Editor editor = prefs.edit();
618 editor.clear();
619 editor.putString("default_rate", Integer.toString(50));
620 editor.putString("espeak_rate", Integer.toString(200));
621 editor.commit();
622
623 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
624 VoiceSettings settings = new VoiceSettings(prefs, synth);
625 assertThat(settings.getVoiceVariant().toString(), is("male"));
626 assertThat(settings.getRate(), is(200));
627 assertThat(settings.getPitch(), is(synth.Pitch.getDefaultValue()));
628 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
629 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
630 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
631 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
632
633 try {
634 JSONObject json = settings.toJSON();
635 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
636 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
637 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
638 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(200));
639 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
640 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(synth.Pitch.getDefaultValue()));
641 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
642 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
643 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
644 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
645 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
646 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
647 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
648 } catch (JSONException e) {
649 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
650 }
651 }
652
653 public void testEspeakPitchWithDefaultPitch()
654 {
655 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
656 SharedPreferences.Editor editor = prefs.edit();
657 editor.clear();
658 editor.putString("default_pitch", Integer.toString(50));
659 editor.putString("espeak_pitch", Integer.toString(75));
660 editor.commit();
661
662 SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
663 VoiceSettings settings = new VoiceSettings(prefs, synth);
664 assertThat(settings.getVoiceVariant().toString(), is("male"));
665 assertThat(settings.getRate(), is(synth.Rate.getDefaultValue()));
666 assertThat(settings.getPitch(), is(75));
667 assertThat(settings.getPitchRange(), is(synth.PitchRange.getDefaultValue()));
668 assertThat(settings.getVolume(), is(synth.Volume.getDefaultValue()));
669 assertThat(settings.getPunctuationLevel(), is(SpeechSynthesis.PUNCT_NONE));
670 assertThat(settings.getPunctuationCharacters(), is(nullValue()));
671
672 try {
673 JSONObject json = settings.toJSON();
674 assertThat(json.opt(VoiceSettings.PRESET_VARIANT), is(instanceOf(String.class)));
675 assertThat((String)json.opt(VoiceSettings.PRESET_VARIANT), is("male"));
676 assertThat(json.opt(VoiceSettings.PRESET_RATE), is(instanceOf(Integer.class)));
677 assertThat((Integer)json.opt(VoiceSettings.PRESET_RATE), is(synth.Rate.getDefaultValue()));
678 assertThat(json.opt(VoiceSettings.PRESET_PITCH), is(instanceOf(Integer.class)));
679 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH), is(75));
680 assertThat(json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(instanceOf(Integer.class)));
681 assertThat((Integer)json.opt(VoiceSettings.PRESET_PITCH_RANGE), is(synth.PitchRange.getDefaultValue()));
682 assertThat(json.opt(VoiceSettings.PRESET_VOLUME), is(instanceOf(Integer.class)));
683 assertThat((Integer)json.opt(VoiceSettings.PRESET_VOLUME), is(synth.Volume.getDefaultValue()));
684 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is(instanceOf(String.class)));
685 assertThat((String)json.opt(VoiceSettings.PRESET_PUNCTUATION_LEVEL), is("none"));
686 assertThat(json.opt(VoiceSettings.PRESET_PUNCTUATION_CHARACTERS), is(nullValue()));
687 } catch (JSONException e) {
688 assertThat(e.toString(), is(nullValue())); // This will be false; used to report exception.
689 }
690 }
691 }