Skip to content

Commit

Permalink
Fix Android tests to not use experimental feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ErisMik committed Jan 17, 2025
1 parent 030614f commit 94d93be
Show file tree
Hide file tree
Showing 4 changed files with 465 additions and 440 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ android {
androidTest {
java {
if (System.getProperty("testBuildType", "debug") == "perf") {
exclude "**/PorcupineTest.java"
exclude "**/StandardTests.java"
exclude "**/LanguageTests.java"
exclude "**/IntegrationTest.java"
} else if (System.getProperty("testBuildType", "debug") == "integ") {
exclude "**/PorcupineTest.java"
exclude "**/StandardTests.java"
exclude "**/LanguageTests.java"
exclude "**/PerformanceTest.java"
} else {
exclude "**/IntegrationTest.java"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Copyright 2025 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/

package ai.picovoice.porcupine.testapp;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import ai.picovoice.porcupine.Porcupine;


@RunWith(Parameterized.class)
public class LanguageTests extends BaseTest {

@Parameterized.Parameter(value = 0)
public String modelFile;

@Parameterized.Parameter(value = 1)
public String[] keywordFiles;

@Parameterized.Parameter(value = 2)
public String testAudioFile;

@Parameterized.Parameter(value = 3)
public int[] expectedResults;

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> initParameters() throws IOException {
String testDataJsonString = getTestDataString();

JsonObject testDataJson = JsonParser.parseString(testDataJsonString).getAsJsonObject();
JsonArray multipleKeywordDataJson = testDataJson.getAsJsonObject("tests").getAsJsonArray("multipleKeyword");

List<Object[]> parameters = new ArrayList<>();
for (int i = 0; i < multipleKeywordDataJson.size(); i++) {
JsonObject testData = multipleKeywordDataJson.get(i).getAsJsonObject();
String language = testData.get("language").getAsString();
JsonArray keywords = testData.getAsJsonArray("wakewords");
JsonArray groundTruthJson = testData.getAsJsonArray("groundTruth");

String modelFile = String.format("model_files/porcupine_params_%s.pv", language);
String[] keywordFiles = new String[keywords.size()];
for (int j = 0; j < keywords.size(); j++) {
String keyword = keywords.get(j).getAsString();
keywordFiles[j] = String.format("keyword_files/%s/%s_android.ppn", language, keyword);
}
String audioFile = String.format("audio_samples/multiple_keywords_%s.wav", language);
int[] groundTruth = new int[groundTruthJson.size()];
for (int j = 0; j < groundTruthJson.size(); j++) {
groundTruth[j] = groundTruthJson.get(j).getAsInt();
}

if (Objects.equals(language, "en")) {
modelFile = "model_files/porcupine_params.pv";
audioFile = "audio_samples/multiple_keywords.wav";
}

parameters.add(new Object[] {
modelFile,
keywordFiles,
audioFile,
groundTruth
});
}

return parameters;
}


@Test
public void testProcess() throws Exception {
String modelPath = new File(testResourcesPath, modelFile).getAbsolutePath();
String[] keywordPaths = new String[keywordFiles.length];
for (int i = 0; i < keywordFiles.length; i++) {
keywordPaths[i] = new File(testResourcesPath, keywordFiles[i]).getAbsolutePath();

}
Porcupine p = new Porcupine.Builder()
.setAccessKey(accessKey)
.setModelPath(modelPath)
.setKeywordPaths(keywordPaths)
.build(appContext);

assertTrue(p.getVersion() != null && !p.getVersion().isEmpty());
assertTrue(p.getFrameLength() > 0);
assertTrue(p.getSampleRate() > 0);

File testAudio = new File(testResourcesPath, testAudioFile);
ArrayList<Integer> detectionResults = processTestAudio(p, testAudio);
p.delete();

assertSame(expectedResults.length, detectionResults.size());
for (int i = 0; i < expectedResults.length; i++) {
assertSame(detectionResults.get(i), expectedResults[i]);
}

p.delete();
}
}
Loading

0 comments on commit 94d93be

Please sign in to comment.