1 package ai.picovoice.rhinodemo;
3 import android.content.Context;
4 import android.content.res.AssetManager;
6 import androidx.test.ext.junit.runners.AndroidJUnit4;
7 import androidx.test.platform.app.InstrumentationRegistry;
9 import com.microsoft.appcenter.espresso.Factory;
10 import com.microsoft.appcenter.espresso.ReportHelper;
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Rule;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.nio.ByteBuffer;
27 import java.nio.ByteOrder;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertFalse;
36 import static org.junit.Assert.assertTrue;
39 @RunWith(AndroidJUnit4.class)
42 public ReportHelper reportHelper = Factory.getReportHelper();
45 AssetManager assetManager;
46 String testResourcesPath;
51 reportHelper.label(
"Stopping App");
55 public void Setup() throws IOException {
56 testContext = InstrumentationRegistry.getInstrumentation().getContext();
57 appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
58 assetManager = testContext.getAssets();
59 extractAssetsRecursively(
"test_resources");
60 testResourcesPath =
new File(appContext.getFilesDir(),
"test_resources").getAbsolutePath();
62 accessKey = appContext.getString(
R.string.pvTestingAccessKey);
67 File contextPath =
new File(testResourcesPath,
"context_files/coffee_maker_android.rhn");
83 File contextPath =
new File(testResourcesPath,
"context_files/coffee_maker_android.rhn");
84 File modelPath =
new File(testResourcesPath,
"model_files/rhino_params.pv");
97 File contextPath =
new File(testResourcesPath,
"context_files/coffee_maker_android.rhn");
110 File contextPath =
new File(testResourcesPath,
"context_files/coffee_maker_android.rhn");
123 File contextPath =
new File(testResourcesPath,
"context_files/test_de_android.rhn");
124 File modelPath =
new File(testResourcesPath,
"model_files/rhino_params_de.pv");
137 File contextPath =
new File(testResourcesPath,
"context_files/test_es_android.rhn");
138 File modelPath =
new File(testResourcesPath,
"model_files/rhino_params_es.pv");
151 File contextPath =
new File(testResourcesPath,
"context_files/test_fr_android.rhn");
152 File modelPath =
new File(testResourcesPath,
"model_files/rhino_params_fr.pv");
165 boolean didFail =
false;
166 File contextPath =
new File(testResourcesPath,
"context_files/test_de_android.rhn");
167 File modelPath =
new File(testResourcesPath,
"model_files/rhino_params.pv");
183 boolean didFail =
false;
184 File contextPath =
new File(testResourcesPath,
"context_files/test_fr_android.rhn");
198 boolean didFail =
false;
212 boolean didFail =
false;
213 File contextPath =
new File(testResourcesPath,
"bad_path/bad_path.rhn");
228 boolean didFail =
false;
229 File contextPath =
new File(testResourcesPath,
"context_files/coffee_maker_android.rhn");
230 File modelPath =
new File(testResourcesPath,
"bad_path/bad_path.pv");
246 boolean didFail =
false;
247 File contextPath =
new File(testResourcesPath,
"context_files/coffee_maker_android.rhn");
263 boolean didFail =
false;
264 File contextPath =
new File(testResourcesPath,
"context_files/coffee_maker_linux.rhn");
280 File contextPath =
new File(testResourcesPath,
"context_files/coffee_maker_android.rhn");
286 File testAudio =
new File(testResourcesPath,
"audio_samples/test_within_context.wav");
287 FileInputStream audioInputStream =
new FileInputStream(testAudio);
291 ByteBuffer pcmBuff = ByteBuffer.wrap(rawData).order(ByteOrder.LITTLE_ENDIAN);
293 assertEquals(44, audioInputStream.skip(44));
295 boolean isFinalized =
false;
296 while (audioInputStream.available() > 0) {
297 int numRead = audioInputStream.read(pcmBuff.array());
299 pcmBuff.asShortBuffer().get(pcm);
307 assertTrue(isFinalized);
311 assertEquals(
"orderBeverage", inference.
getIntent());
313 Map<String, String> slots = inference.
getSlots();
314 assertEquals(
"medium", slots.get(
"size"));
315 assertEquals(
"double shot", slots.get(
"numberOfShots"));
316 assertEquals(
"americano", slots.get(
"beverage"));
324 File contextPath =
new File(testResourcesPath,
"context_files/coffee_maker_android.rhn");
330 File testAudio =
new File(testResourcesPath,
"audio_samples/test_out_of_context.wav");
331 FileInputStream audioInputStream =
new FileInputStream(testAudio);
335 ByteBuffer pcmBuff = ByteBuffer.wrap(rawData).order(ByteOrder.LITTLE_ENDIAN);
337 assertEquals(44, audioInputStream.skip(44));
339 boolean isFinalized =
false;
340 while (audioInputStream.available() > 0) {
341 int numRead = audioInputStream.read(pcmBuff.array());
343 pcmBuff.asShortBuffer().get(pcm);
351 assertTrue(isFinalized);
361 String[] list = assetManager.list(path);
362 if (list.length > 0) {
363 File outputFile =
new File(appContext.getFilesDir(), path);
364 if (!outputFile.exists()) {
365 if (!outputFile.mkdirs()) {
366 throw new IOException(
"Couldn't create output directory " + outputFile.getAbsolutePath());
370 for (String file : list) {
371 String filepath = path +
"/" + file;
372 extractAssetsRecursively(filepath);
375 extractTestFile(path);
381 InputStream is =
new BufferedInputStream(assetManager.open(filepath), 256);
382 File absPath =
new File(appContext.getFilesDir(), filepath);
383 OutputStream os =
new BufferedOutputStream(
new FileOutputStream(absPath), 256);
385 while ((r = is.read()) != -1) {