Skip to content

Commit

Permalink
Merge pull request #12567 from keymanapp/fix/android/json-utils-listf…
Browse files Browse the repository at this point in the history
…iles

fix(android/engine): Increase robustness when checking package kmp.json languages
  • Loading branch information
darcywong00 authored Oct 24, 2024
2 parents d1b373f + 5ef503c commit 1f47374
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public static JSONArray getLanguages() {
return new JSONArray();
}
File[] packages = resourceRoot.listFiles();
if (packages == null) {
return new JSONArray();
}
JSONArray languagesArray = new JSONArray();
JSONParser parser = new JSONParser();

Expand All @@ -60,6 +63,9 @@ public static JSONArray getLanguages() {
String kbdFilename = pkg.getName() + "/" + kbdID + ".js";

// Merge languages
if (!kmpKeyboardObj.has("languages")) {
continue;
}
JSONArray kmpLanguageArray = kmpKeyboardObj.getJSONArray("languages");
for (int j=0; j<kmpLanguageArray.length(); j++) {
JSONObject languageObj = kmpLanguageArray.getJSONObject(j);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@RunWith(RobolectricTestRunner.class)
public class JSONUtilsTest {
private static final File TEST_RESOURCE_ROOT = new File("test_resources", "packages");
private static final File TEST_INVALID_RESOURCE_ROOT = new File("test_resources", "invalid_packages");
private static final String TAG = "JSONUtilsTest";

@Before
Expand Down Expand Up @@ -52,6 +53,18 @@ public void test_getLanguages() {
}
}

/**
* Test parsing null_languages/kmp.json which has no languages
*/
@Test
public void test_invalid_getLanguages() {
JSONUtils.initialize(TEST_INVALID_RESOURCE_ROOT);
JSONArray result = JSONUtils.getLanguages();
final int EXPECTED_NUM_LANGUAGES = 0;

Assert.assertEquals(EXPECTED_NUM_LANGUAGES, result.length());
}

@Test
public void test_findID() {
JSONParser parser = new JSONParser();
Expand Down
127 changes: 127 additions & 0 deletions android/KMEA/test_resources/invalid_packages/null_languages/kmp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"system": {
"keymanDeveloperVersion": "11.0.1304.0",
"fileVersion": "7.0"
},
"options": {
"graphicFile": "splash.bmp"
},
"info": {
"name": {
"description": "Cameroon QWERTY"
},
"version": {
"description": "6.0.2"
},
"copyright": {
"description": "\u00A92018 SIL Cameroon"
},
"author": {
"description": "Matthew Lee",
"url": "mailto:[email protected]"
},
"website": {
"description": "LangTechCameroon.info",
"url": "LangTechCameroon.info"
}
},
"files": [
{
"name": "Cameroon.ico",
"description": "File Cameroon.ico"
},
{
"name": "sil_cameroon_qwerty.css",
"description": "File sil_cameroon_qwerty.css"
},
{
"name": "AndikaAfr-R.ttf",
"description": "Font Andika Afr"
},
{
"name": "sil_cameroon_qwerty.kvk",
"description": "File sil_cameroon_qwerty.kvk"
},
{
"name": "sil_cameroon_qwerty.kmx",
"description": "Keyboard Cameroon QWERTY"
},
{
"name": "splash.bmp",
"description": "File splash.bmp"
},
{
"name": "welcome.htm",
"description": "File welcome.htm"
},
{
"name": "e_chart.png",
"description": "File e_chart.png"
},
{
"name": "e_tableau.png",
"description": "File e_tableau.png"
},
{
"name": "helpIcon.png",
"description": "File helpIcon.png"
},
{
"name": "ic_launcher.png",
"description": "File ic_launcher.png"
},
{
"name": "icon.png",
"description": "File icon.png"
},
{
"name": "QwertyColor.png",
"description": "File QwertyColor.png"
},
{
"name": "sil-logo-blue.png",
"description": "File sil-logo-blue.png"
},
{
"name": "kb.css",
"description": "File kb.css"
},
{
"name": "sil_cameroon_qwerty.js",
"description": "File sil_cameroon_qwerty.js"
},
{
"name": "globe.png",
"description": "File globe.png"
},
{
"name": "longpress-qwerty.png",
"description": "File longpress-qwerty.png"
},
{
"name": "modkeys-en-q.png",
"description": "File modkeys-en-q.png"
},
{
"name": "modkeys-fr-q.png",
"description": "File modkeys-fr-q.png"
},
{
"name": "kmp.inf",
"description": "Package information"
},
{
"name": "kmp.json",
"description": "Package information (JSON)"
}
],
"keyboards": [
{
"name": "Cameroon QWERTY",
"id": "sil_cameroon_qwerty",
"version": "6.0.2",
"oskFont": "AndikaAfr-R.ttf",
"displayFont": "AndikaAfr-R.ttf"
}
]
}

0 comments on commit 1f47374

Please sign in to comment.