mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-04-04 23:53:51 -04:00
Create export->import test
This commit is contained in:
@@ -309,7 +309,6 @@ public class Utils {
|
||||
FileOutputStream out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
|
||||
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||
|
||||
}
|
||||
|
||||
static public void saveCardImage(Context context, Bitmap bitmap, int loyaltyCardId, boolean front) throws FileNotFoundException {
|
||||
@@ -331,11 +330,15 @@ public class Utils {
|
||||
return retrieveCardImage(context, getCardImageFileName(loyaltyCardId, front));
|
||||
}
|
||||
|
||||
static public Object hashmapGetOrDefault(HashMap hashMap, String key, Object defaultValue) {
|
||||
Object value = hashMap.get(key);
|
||||
static public Object hashmapGetOrDefault(HashMap hashMap, Object key, Object defaultValue, Class keyType) {
|
||||
Object value = hashMap.get(keyType.cast(key));
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static public Object hashmapGetOrDefault(HashMap hashMap, String key, Object defaultValue) {
|
||||
return hashmapGetOrDefault(hashMap, key, defaultValue, String.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="192dp"
|
||||
android:height="192dp"
|
||||
android:viewportWidth="50.8"
|
||||
android:viewportHeight="50.8">
|
||||
<path
|
||||
android:pathData="M14.3354,20.1954l20.7318,-9.2304l5.7612,12.9398l-20.7318,9.2304z"
|
||||
android:strokeLineJoin="miter"
|
||||
android:strokeWidth="0.529167"
|
||||
android:fillColor="#f0f0f0"
|
||||
android:strokeColor="#c80000"/>
|
||||
<path
|
||||
android:pathData="M14.8755,10.9648l23.2041,10.3311l-6.8874,15.4694l-23.2041,-10.3311z"
|
||||
android:strokeLineJoin="miter"
|
||||
android:strokeWidth="0.529167"
|
||||
android:fillColor="#f0f0f0"
|
||||
android:strokeColor="#c80000"/>
|
||||
<path
|
||||
android:pathData="M16.5599,16.1348l26.5459,7.6119l-4.5489,15.8639l-26.5459,-7.6119z"
|
||||
android:strokeLineJoin="round"
|
||||
android:strokeWidth="1.5875"
|
||||
android:fillColor="#c80000"
|
||||
android:strokeColor="#c80000"
|
||||
android:fillType="evenOdd"/>
|
||||
<path
|
||||
android:pathData="M12.011,15.4955h27.6157v16.5032h-27.6157z"
|
||||
android:strokeLineJoin="round"
|
||||
android:strokeWidth="1.5875"
|
||||
android:fillColor="#ff0000"
|
||||
android:strokeColor="#ff0000"
|
||||
android:fillType="evenOdd"/>
|
||||
<path
|
||||
android:pathData="M7.8471,23.7471a4.3659,8.5899 0,1 0,8.7317 0a4.3659,8.5899 0,1 0,-8.7317 0z"
|
||||
android:strokeLineJoin="round"
|
||||
android:strokeWidth="0.91078"
|
||||
android:fillColor="#ff0000"
|
||||
android:strokeColor="#ff0000"/>
|
||||
<path
|
||||
android:pathData="m24.4983,25.781a1.6711,1.6711 0,0 1,-1.3809 1.6457,1.6711 1.6711,0 0,1 -1.8605,-1.0741"
|
||||
android:strokeLineJoin="round"
|
||||
android:strokeWidth="0.529167"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeColor="#f0f0f0"
|
||||
android:strokeLineCap="round"/>
|
||||
<path
|
||||
android:pathData="m27.7991,26.333a1.6711,1.6711 0,0 1,-1.8605 1.0741,1.6711 1.6711,0 0,1 -1.3809,-1.6457"
|
||||
android:strokeLineJoin="round"
|
||||
android:strokeWidth="0.529167"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeColor="#f0f0f0"
|
||||
android:strokeLineCap="round"/>
|
||||
<path
|
||||
android:pathData="m16.0606,22.271 l2.6458,-2.6458 2.6458,2.6458"
|
||||
android:strokeLineJoin="miter"
|
||||
android:strokeWidth="0.529167"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeColor="#f0f0f0"
|
||||
android:strokeLineCap="butt"/>
|
||||
<path
|
||||
android:pathData="m27.7023,22.271 l2.6458,-2.6458 2.6458,2.6458"
|
||||
android:strokeLineJoin="miter"
|
||||
android:strokeWidth="0.529167"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeColor="#f0f0f0"
|
||||
android:strokeLineCap="butt"/>
|
||||
</vector>
|
||||
@@ -1,5 +0,0 @@
|
||||
<vector android:height="24dp" android:tint="#FFFFFF"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M20,3h-1L19,1h-2v2L7,3L7,1L5,1v2L4,3c-1.1,0 -2,0.9 -2,2v16c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,5c0,-1.1 -0.9,-2 -2,-2zM20,21L4,21L4,8h16v13z"/>
|
||||
</vector>
|
||||
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="@android:color/darker_gray"/>
|
||||
<size android:height="1dp"/>
|
||||
</shape>
|
||||
@@ -29,6 +29,7 @@ import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -38,6 +39,7 @@ import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Currency;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.core.content.res.ResourcesCompat;
|
||||
@@ -867,46 +869,106 @@ public class ImportExportTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exportV2() throws FileNotFoundException
|
||||
public void exportImportV2Zip() throws FileNotFoundException
|
||||
{
|
||||
db.insertGroup("Example");
|
||||
|
||||
// Prepare images
|
||||
BitmapDrawable launcher = (BitmapDrawable) ResourcesCompat.getDrawableForDensity(activity.getResources(), R.mipmap.ic_launcher, DisplayMetrics.DENSITY_XXXHIGH, activity.getTheme());
|
||||
BitmapDrawable roundLauncher = (BitmapDrawable) ResourcesCompat.getDrawableForDensity(activity.getResources(), R.mipmap.ic_launcher_round, DisplayMetrics.DENSITY_XXXHIGH, activity.getTheme());
|
||||
|
||||
Bitmap frontImage = launcher.getBitmap();
|
||||
Bitmap backImage = roundLauncher.getBitmap();
|
||||
Bitmap launcherBitmap = launcher.getBitmap();
|
||||
Bitmap roundLauncherBitmap = roundLauncher.getBitmap();
|
||||
|
||||
int loyaltyCard = (int) db.insertLoyaltyCard("Card 1", "Note 1", new Date(1618053234), new BigDecimal("100"), Currency.getInstance("USD"), "1234", "5432", BarcodeFormat.QR_CODE, 1, 0);
|
||||
// Set up cards and groups
|
||||
HashMap<Integer, LoyaltyCard> loyaltyCardHashMap = new HashMap<>();
|
||||
HashMap<Integer, List<Group>> loyaltyCardGroups = new HashMap<>();
|
||||
HashMap<Integer, Bitmap> loyaltyCardFrontImages = new HashMap<>();
|
||||
HashMap<Integer, Bitmap> loyaltyCardBackImages = new HashMap<>();
|
||||
|
||||
Utils.saveCardImage(activity.getApplicationContext(), Utils.resizeBitmap(frontImage), loyaltyCard, true);
|
||||
Utils.saveCardImage(activity.getApplicationContext(), Utils.resizeBitmap(backImage), loyaltyCard, false);
|
||||
// Create card 1
|
||||
int loyaltyCardId = (int) db.insertLoyaltyCard("Card 1", "Note 1", new Date(1618053234), new BigDecimal("100"), Currency.getInstance("USD"), "1234", "5432", BarcodeFormat.QR_CODE, 1, 0);
|
||||
loyaltyCardHashMap.put(loyaltyCardId, db.getLoyaltyCard(loyaltyCardId));
|
||||
db.insertGroup("One");
|
||||
List<Group> groups = Arrays.asList(db.getGroup("One"));
|
||||
db.setLoyaltyCardGroups(loyaltyCardId, groups);
|
||||
loyaltyCardGroups.put(loyaltyCardId, groups);
|
||||
Utils.saveCardImage(activity.getApplicationContext(), launcherBitmap, loyaltyCardId, true);
|
||||
Utils.saveCardImage(activity.getApplicationContext(), roundLauncherBitmap, loyaltyCardId, false);
|
||||
loyaltyCardFrontImages.put(loyaltyCardId, launcherBitmap);
|
||||
loyaltyCardBackImages.put(loyaltyCardId, roundLauncherBitmap);
|
||||
|
||||
db.setLoyaltyCardGroups(loyaltyCard, Arrays.asList(db.getGroup("Example")));
|
||||
// Create card 2
|
||||
loyaltyCardId = (int) db.insertLoyaltyCard("Card 2", "", null, new BigDecimal(0), null, "123456", null, null, 2, 1);
|
||||
loyaltyCardHashMap.put(loyaltyCardId, db.getLoyaltyCard(loyaltyCardId));
|
||||
|
||||
// Export everything
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
|
||||
|
||||
MultiFormatExporter.exportData(activity.getApplicationContext(), db, outputStream, DataFormat.Catima);
|
||||
|
||||
String outputCsv = "2\r\n" +
|
||||
"\r\n" +
|
||||
"_id\r\n" +
|
||||
"Example\r\n" +
|
||||
"\r\n" +
|
||||
"_id,store,note,expiry,balance,balancetype,cardid,barcodeid,barcodetype,headercolor,starstatus,frontimage,backimage\r\n" +
|
||||
"1,Card 1,Note 1,1618053234,100,USD,1234,5432,QR_CODE,1,0,\"iVBORw0KGgoAAAANSUhEUgAAAgAAAAIAAQAAAADcA-lXAAAANklEQVR42u3BAQEAAACCIP-vbkhA\n" +
|
||||
"AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB8G4IAAAFjdVCkAAAAAElFTkSuQmCC\n\",\"iVBORw0KGgoAAAANSUhEUgAAAgAAAAIAAQAAAADcA-lXAAAANklEQVR42u3BAQEAAACCIP-vbkhA\n" +
|
||||
"AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB8G4IAAAFjdVCkAAAAAElFTkSuQmCC\n\"\r\n" +
|
||||
"\r\n" +
|
||||
"cardId,groupId\r\n" +
|
||||
"1,Example\r\n";
|
||||
// Wipe database
|
||||
TestHelpers.getEmptyDb(activity);
|
||||
|
||||
assertEquals(outputCsv, outputStream.toString());
|
||||
// Import everything
|
||||
MultiFormatImporter.importData(activity.getApplicationContext(), db, new ByteArrayInputStream(outputStream.toByteArray()), DataFormat.Catima, null);
|
||||
|
||||
// Ensure everything is there
|
||||
assertEquals(loyaltyCardHashMap.size(), db.getLoyaltyCardCount());
|
||||
|
||||
for (Integer loyaltyCardID : loyaltyCardHashMap.keySet()) {
|
||||
LoyaltyCard loyaltyCard = loyaltyCardHashMap.get(loyaltyCardID);
|
||||
|
||||
LoyaltyCard dbLoyaltyCard = db.getLoyaltyCard(loyaltyCardID);
|
||||
|
||||
assertEquals(loyaltyCard.id, dbLoyaltyCard.id);
|
||||
assertEquals(loyaltyCard.store, dbLoyaltyCard.store);
|
||||
assertEquals(loyaltyCard.note, dbLoyaltyCard.note);
|
||||
assertEquals(loyaltyCard.expiry, dbLoyaltyCard.expiry);
|
||||
assertEquals(loyaltyCard.balance, dbLoyaltyCard.balance);
|
||||
assertEquals(loyaltyCard.cardId, dbLoyaltyCard.cardId);
|
||||
assertEquals(loyaltyCard.barcodeId, dbLoyaltyCard.barcodeId);
|
||||
assertEquals(loyaltyCard.starStatus, dbLoyaltyCard.starStatus);
|
||||
assertEquals(loyaltyCard.barcodeType, dbLoyaltyCard.barcodeType);
|
||||
assertEquals(loyaltyCard.balanceType, dbLoyaltyCard.balanceType);
|
||||
assertEquals(loyaltyCard.headerColor, dbLoyaltyCard.headerColor);
|
||||
|
||||
List<Group> emptyGroup = new ArrayList<>();
|
||||
|
||||
assertEquals(
|
||||
groupsToGroupNames(
|
||||
(List<Group>) Utils.hashmapGetOrDefault(
|
||||
loyaltyCardGroups,
|
||||
loyaltyCardID,
|
||||
emptyGroup,
|
||||
Integer.class
|
||||
)
|
||||
),
|
||||
groupsToGroupNames(
|
||||
db.getLoyaltyCardGroups(
|
||||
loyaltyCardID
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
Bitmap expectedFrontImage = loyaltyCardFrontImages.get(loyaltyCardID);
|
||||
Bitmap expectedBackImage = loyaltyCardBackImages.get(loyaltyCardID);
|
||||
Bitmap actualFrontImage = Utils.retrieveCardImage(activity.getApplicationContext(), Utils.getCardImageFileName(loyaltyCardID, true));
|
||||
Bitmap actualBackImage = Utils.retrieveCardImage(activity.getApplicationContext(), Utils.getCardImageFileName(loyaltyCardID, false));
|
||||
|
||||
if (expectedFrontImage != null) {
|
||||
assertTrue(expectedFrontImage.sameAs(actualFrontImage));
|
||||
} else {
|
||||
assertNull(actualFrontImage);
|
||||
}
|
||||
|
||||
if (expectedBackImage != null) {
|
||||
assertTrue(expectedBackImage.sameAs(actualBackImage));
|
||||
} else {
|
||||
assertNull(actualBackImage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void importV2()
|
||||
public void importV2CSV()
|
||||
{
|
||||
String csvText = "2\n" +
|
||||
"\n" +
|
||||
@@ -915,16 +977,14 @@ public class ImportExportTest
|
||||
"Food\n" +
|
||||
"Fashion\n" +
|
||||
"\n" +
|
||||
"_id,store,note,expiry,balance,balancetype,cardid,barcodeid,headercolor,barcodetype,starstatus,frontimage,backimage\n" +
|
||||
"1,Card 1,Note 1,1618053234,100,USD,1234,5432,1,QR_CODE,0,\"iVBORw0KGgoAAAANSUhEUgAAAgAAAAIAAQAAAADcA-lXAAAANklEQVR42u3BAQEAAACCIP-vbkhA\n" +
|
||||
"AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB8G4IAAAFjdVCkAAAAAElFTkSuQmCC\n\",\"iVBORw0KGgoAAAANSUhEUgAAAgAAAAIAAQAAAADcA-lXAAAANklEQVR42u3BAQEAAACCIP-vbkhA\n" +
|
||||
"AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB8G4IAAAFjdVCkAAAAAElFTkSuQmCC\n\"\r\n" +
|
||||
"8,Clothes Store,Note about store,,0,,a,,-5317,,0,,\n" +
|
||||
"2,Department Store,,1618041729,0,,A,,-9977996,,0,,\n" +
|
||||
"3,Grocery Store,,,150,,dhd,,-9977996,,0,,\n" +
|
||||
"4,Pharmacy,,,0,,dhshsvshs,,-10902850,,1,,\n" +
|
||||
"5,Restaurant,Note about restaurant here,,0,,98765432,23456,-10902850,CODE_128,0,,\n" +
|
||||
"6,Shoe Store,,,12.50,EUR,a,-5317,,AZTEC,0,,\n" +
|
||||
"_id,store,note,expiry,balance,balancetype,cardid,barcodeid,headercolor,barcodetype,starstatus\n" +
|
||||
"1,Card 1,Note 1,1618053234,100,USD,1234,5432,1,QR_CODE,0,\r\n" +
|
||||
"8,Clothes Store,Note about store,,0,,a,,-5317,,0,\n" +
|
||||
"2,Department Store,,1618041729,0,,A,,-9977996,,0,\n" +
|
||||
"3,Grocery Store,,,150,,dhd,,-9977996,,0,\n" +
|
||||
"4,Pharmacy,,,0,,dhshsvshs,,-10902850,,1,\n" +
|
||||
"5,Restaurant,Note about restaurant here,,0,,98765432,23456,-10902850,CODE_128,0,\n" +
|
||||
"6,Shoe Store,,,12.50,EUR,a,-5317,,AZTEC,0,\n" +
|
||||
"\n" +
|
||||
"cardId,groupId\n" +
|
||||
"8,Fashion\n" +
|
||||
@@ -958,12 +1018,6 @@ public class ImportExportTest
|
||||
assertEquals(Arrays.asList(8, 6), db.getGroupCardIds("Fashion"));
|
||||
|
||||
// Check all cards
|
||||
BitmapDrawable launcher = (BitmapDrawable) ResourcesCompat.getDrawableForDensity(activity.getResources(), R.mipmap.ic_launcher, DisplayMetrics.DENSITY_XXXHIGH, activity.getTheme());
|
||||
BitmapDrawable roundLauncher = (BitmapDrawable) ResourcesCompat.getDrawableForDensity(activity.getResources(), R.mipmap.ic_launcher_round, DisplayMetrics.DENSITY_XXXHIGH, activity.getTheme());
|
||||
|
||||
Bitmap frontImage = launcher.getBitmap();
|
||||
Bitmap backImage = roundLauncher.getBitmap();
|
||||
|
||||
LoyaltyCard card1 = db.getLoyaltyCard(1);
|
||||
|
||||
assertEquals("Card 1", card1.store);
|
||||
@@ -976,8 +1030,8 @@ public class ImportExportTest
|
||||
assertEquals(BarcodeFormat.QR_CODE, card1.barcodeType);
|
||||
assertEquals(1, (long) card1.headerColor);
|
||||
assertEquals(0, card1.starStatus);
|
||||
assertTrue(Utils.resizeBitmap(frontImage).sameAs(Utils.retrieveCardImage(activity.getApplicationContext(), card1.id, true)));
|
||||
assertTrue(Utils.resizeBitmap(backImage).sameAs(Utils.retrieveCardImage(activity.getApplicationContext(), card1.id, false)));
|
||||
assertEquals(null, Utils.retrieveCardImage(activity.getApplicationContext(), card1.id, true));
|
||||
assertEquals(null, Utils.retrieveCardImage(activity.getApplicationContext(), card1.id, false));
|
||||
|
||||
LoyaltyCard card8 = db.getLoyaltyCard(8);
|
||||
|
||||
|
||||
@@ -1,11 +1,33 @@
|
||||
package protect.card_locker;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TestHelpers {
|
||||
static public DBHelper getEmptyDb(Activity activity) {
|
||||
DBHelper db = new DBHelper(activity);
|
||||
|
||||
// Make sure no files remain
|
||||
Cursor cursor = db.getLoyaltyCardCursor();
|
||||
cursor.moveToFirst();
|
||||
while (!cursor.isAfterLast()) {
|
||||
int cardID = cursor.getColumnIndex(DBHelper.LoyaltyCardDbIds.ID);
|
||||
|
||||
try {
|
||||
Utils.saveCardImage(activity.getApplicationContext(), null, cardID, true);
|
||||
} catch (FileNotFoundException ignored) {}
|
||||
try {
|
||||
Utils.saveCardImage(activity.getApplicationContext(), null, cardID, false);
|
||||
} catch (FileNotFoundException ignored) {}
|
||||
|
||||
cursor.moveToNext();
|
||||
}
|
||||
|
||||
// Make sure DB is empty
|
||||
SQLiteDatabase database = db.getWritableDatabase();
|
||||
database.execSQL("delete from " + DBHelper.LoyaltyCardDbIds.TABLE);
|
||||
|
||||
Reference in New Issue
Block a user