mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-05-18 05:36:39 -04:00
Unit test
This commit is contained in:
@@ -13,7 +13,7 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class PkpassImporter {
|
||||
Context context;
|
||||
private Context context;
|
||||
|
||||
public PkpassImporter(Context context) {
|
||||
this.context = context;
|
||||
@@ -28,60 +28,55 @@ public class PkpassImporter {
|
||||
|
||||
ZipEntry entry;
|
||||
|
||||
while((entry = zipInputStream.getNextEntry()) != null)
|
||||
{
|
||||
if(!entry.getName().equals("pass.json"))
|
||||
{
|
||||
while ((entry = zipInputStream.getNextEntry()) != null) {
|
||||
if (!entry.getName().equals("pass.json")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int c = zipInputStream.read(); c != -1; c = zipInputStream.read())
|
||||
{
|
||||
for (int c = zipInputStream.read(); c != -1; c = zipInputStream.read()) {
|
||||
sb.append((char) c);
|
||||
}
|
||||
|
||||
String readData = sb.toString();
|
||||
|
||||
JSONObject json = new JSONObject(readData);
|
||||
|
||||
String store = json.getString("organizationName");
|
||||
String note = json.getString("description");
|
||||
|
||||
// https://developer.apple.com/library/archive/documentation/UserExperience/Reference/PassKit_Bundle/Chapters/TopLevel.html#//apple_ref/doc/uid/TP40012026-CH2-SW1
|
||||
// barcodes is the new field
|
||||
// barcode is deprecated, but used on old iOS versions, so we do fall back to it
|
||||
JSONObject barcode = null;
|
||||
JSONArray barcodes = null;
|
||||
|
||||
try
|
||||
{
|
||||
barcodes = json.getJSONArray("barcodes");
|
||||
} catch (JSONException ex) { }
|
||||
|
||||
if(barcodes != null)
|
||||
{
|
||||
barcode = barcodes.getJSONObject(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
barcode = json.getJSONObject("barcode");
|
||||
}
|
||||
|
||||
if(barcode == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
String cardId = barcode.getString("message");
|
||||
String barcodeType = barcode.getString("format").substring("PKBarcodeFormat".length());
|
||||
if(barcodeType.equals("QR"))
|
||||
{
|
||||
barcodeType = "QR_CODE";
|
||||
}
|
||||
return new LoyaltyCard(-1, store, note, cardId, barcodeType, null, null);
|
||||
return fromPassJSON(new JSONObject(readData));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public LoyaltyCard fromPassJSON(JSONObject json) throws JSONException {
|
||||
|
||||
String store = json.getString("organizationName");
|
||||
String note = json.getString("description");
|
||||
|
||||
// https://developer.apple.com/library/archive/documentation/UserExperience/Reference/PassKit_Bundle/Chapters/TopLevel.html#//apple_ref/doc/uid/TP40012026-CH2-SW1
|
||||
// barcodes is the new field
|
||||
// barcode is deprecated, but used on old iOS versions, so we do fall back to it
|
||||
JSONObject barcode = null;
|
||||
JSONArray barcodes = null;
|
||||
|
||||
try {
|
||||
barcodes = json.getJSONArray("barcodes");
|
||||
} catch (JSONException ex) {
|
||||
}
|
||||
|
||||
if (barcodes != null) {
|
||||
barcode = barcodes.getJSONObject(0);
|
||||
} else {
|
||||
barcode = json.getJSONObject("barcode");
|
||||
}
|
||||
|
||||
if (barcode == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String cardId = barcode.getString("message");
|
||||
String barcodeType = barcode.getString("format").substring("PKBarcodeFormat".length());
|
||||
if (barcodeType.equals("QR")) {
|
||||
barcodeType = "QR_CODE";
|
||||
}
|
||||
return new LoyaltyCard(-1, store, note, cardId, barcodeType, null, null);
|
||||
}
|
||||
}
|
||||
114
app/src/test/java/protect/card_locker/PkpassTest.java
Normal file
114
app/src/test/java/protect/card_locker/PkpassTest.java
Normal file
@@ -0,0 +1,114 @@
|
||||
package protect.card_locker;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(constants = BuildConfig.class, sdk = 23)
|
||||
public class PkpassTest {
|
||||
private PkpassImporter pkpassImporter;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
Activity activity = Robolectric.setupActivity(MainActivity.class);
|
||||
pkpassImporter = new PkpassImporter(activity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseGenericExample() throws JSONException
|
||||
{
|
||||
// Generic example from https://github.com/keefmoon/Passbook-Example-Code/blob/master/Pass-Example-Generic/Pass-Example-Generic.pkpass
|
||||
JSONObject json = new JSONObject("{\n" +
|
||||
" \"passTypeIdentifier\" : \"pass.keefmoon.example-generic\",\n" +
|
||||
" \"formatVersion\" : 1,\n" +
|
||||
" \"teamIdentifier\" : \"YAXJZJ267E\",\n" +
|
||||
" \"organizationName\" : \"Passbook Example Company\",\n" +
|
||||
" \"serialNumber\" : \"0000001\",\n" +
|
||||
" \"description\" : \"Staff Pass for Employee Number 001\",\n" +
|
||||
" \"associatedStoreIdentifiers\" : [\n" +
|
||||
" \t 284971959\n" +
|
||||
" ],\n" +
|
||||
" \"locations\" : [\n" +
|
||||
" \t{\"latitude\" : 51.50506, \"longitude\" : -0.01960, \"relevantText\" : \"Company Offices\" }\n" +
|
||||
" ],\n" +
|
||||
" \"foregroundColor\" : \"rgb(255, 255, 255)\",\n" +
|
||||
" \"backgroundColor\" : \"rgb(90, 90, 90)\",\n" +
|
||||
" \"labelColor\" : \"rgb(255, 255, 255)\",\n" +
|
||||
" \"logoText\" : \"Company Staff ID\",\n" +
|
||||
" \"barcode\" : {\n" +
|
||||
" \t\t\"format\" : \"PKBarcodeFormatQR\",\n" +
|
||||
" \t\t\"message\" : \"0000001\",\n" +
|
||||
" \t\t\"messageEncoding\" : \"iso-8859-1\",\n" +
|
||||
" \t\t\"altText\" : \"Staff ID 0000001\"\n" +
|
||||
" },\n" +
|
||||
" \"generic\" : {\n" +
|
||||
" \t\"headerFields\" : [\n" +
|
||||
" \t\t{\n" +
|
||||
" \t\t\t\"key\" : \"staffNumber\",\n" +
|
||||
" \t\"label\" : \"Staff Number\",\n" +
|
||||
" \t\"value\" : \"001\"\n" +
|
||||
" \t\t}\n" +
|
||||
" \t],\n" +
|
||||
" \"primaryFields\" : [\n" +
|
||||
" {\n" +
|
||||
" \"key\" : \"staffName\",\n" +
|
||||
" \"label\" : \"Name\",\n" +
|
||||
" \"value\" : \"Peter Brooke\"\n" +
|
||||
" }\n" +
|
||||
" ],\n" +
|
||||
" \"secondaryFields\" : [\n" +
|
||||
" {\n" +
|
||||
" \"key\" : \"telephoneExt\",\n" +
|
||||
" \"label\" : \"Extension\",\n" +
|
||||
" \"value\" : \"9779\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"key\" : \"jobTitle\",\n" +
|
||||
" \"label\" : \"Job Title\",\n" +
|
||||
" \"value\" : \"Chief Pass Creator\"\n" +
|
||||
" }\n" +
|
||||
" ],\n" +
|
||||
" \"auxiliaryFields\" : [\n" +
|
||||
" {\n" +
|
||||
" \"key\" : \"expiryDate\",\n" +
|
||||
" \"dateStyle\" : \"PKDateStyleShort\",\n" +
|
||||
" \"label\" : \"Expiry Date\",\n" +
|
||||
" \"value\" : \"2013-12-31T00:00-23:59\"\n" +
|
||||
" }\n" +
|
||||
" ],\n" +
|
||||
" \"backFields\" : [\n" +
|
||||
" {\n" +
|
||||
" \"key\" : \"managersName\",\n" +
|
||||
" \"label\" : \"Manager's Name\",\n" +
|
||||
" \"value\" : \"Paul Bailey\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"key\" : \"managersExt\",\n" +
|
||||
" \"label\" : \"Manager's Extension\",\n" +
|
||||
" \"value\" : \"9673\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
|
||||
// Parse Pkpass JSON
|
||||
LoyaltyCard card = pkpassImporter.fromPassJSON(json);
|
||||
|
||||
// Compare everything
|
||||
assertEquals(card.barcodeType, "QR_CODE");
|
||||
assertEquals(card.cardId, "0000001");
|
||||
assertEquals(card.note, "Staff Pass for Employee Number 001");
|
||||
assertEquals(card.store, "Passbook Example Company");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user