Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE when sharing cards without header values #343

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions app/src/main/java/protect/card_locker/ImportURIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@ public LoyaltyCard parse(Uri uri) throws InvalidObjectException {
}

try {
// These values are allowed to be null
Integer headerColor = null;
Integer headerTextColor = null;

String store = uri.getQueryParameter(STORE);
String note = uri.getQueryParameter(NOTE);
String cardId = uri.getQueryParameter(CARD_ID);
String barcodeType = uri.getQueryParameter(BARCODE_TYPE);
Integer headerColor = Integer.parseInt(uri.getQueryParameter(HEADER_COLOR));
Integer headerTextColor = Integer.parseInt(uri.getQueryParameter(HEADER_TEXT_COLOR));
String unparsedHeaderColor = uri.getQueryParameter(HEADER_COLOR);
if(unparsedHeaderColor != null)
{
headerColor = Integer.parseInt(unparsedHeaderColor);
}
String unparsedHeaderTextColor = uri.getQueryParameter(HEADER_TEXT_COLOR);
if(unparsedHeaderTextColor != null)
{
headerTextColor = Integer.parseInt(unparsedHeaderTextColor);
}
return new LoyaltyCard(-1, store, note, cardId, barcodeType, headerColor, headerTextColor);
} catch (NullPointerException | NumberFormatException ex) {
throw new InvalidObjectException("Not a valid import URI");
Expand All @@ -57,8 +69,14 @@ protected Uri toUri(LoyaltyCard loyaltyCard) {
uriBuilder.appendQueryParameter(NOTE, loyaltyCard.note);
uriBuilder.appendQueryParameter(CARD_ID, loyaltyCard.cardId);
uriBuilder.appendQueryParameter(BARCODE_TYPE, loyaltyCard.barcodeType);
uriBuilder.appendQueryParameter(HEADER_COLOR, loyaltyCard.headerColor.toString());
uriBuilder.appendQueryParameter(HEADER_TEXT_COLOR, loyaltyCard.headerTextColor.toString());
if(loyaltyCard.headerColor != null)
{
uriBuilder.appendQueryParameter(HEADER_COLOR, loyaltyCard.headerColor.toString());
}
if(loyaltyCard.headerTextColor != null)
{
uriBuilder.appendQueryParameter(HEADER_TEXT_COLOR, loyaltyCard.headerTextColor.toString());
}

return uriBuilder.build();
}
Expand Down
25 changes: 25 additions & 0 deletions app/src/test/java/protect/card_locker/ImportURITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.robolectric.annotation.Config;
import java.io.InvalidObjectException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static protect.card_locker.DBHelper.LoyaltyCardDbIds;

Expand Down Expand Up @@ -53,6 +54,30 @@ public void ensureNoDataLoss() throws InvalidObjectException
assertEquals(card.store, parsedCard.store);
}

@Test
public void ensureNoCrashOnMissingHeaderFields() throws InvalidObjectException
{
// Generate card
db.insertLoyaltyCard("store", "note", BarcodeFormat.UPC_A.toString(), LoyaltyCardDbIds.BARCODE_TYPE, null, null);

// Get card
LoyaltyCard card = db.getLoyaltyCard(1);

// Card to URI
Uri cardUri = importURIHelper.toUri(card);

// Parse URI
LoyaltyCard parsedCard = importURIHelper.parse(cardUri);

// Compare everything
assertEquals(card.barcodeType, parsedCard.barcodeType);
assertEquals(card.cardId, parsedCard.cardId);
assertEquals(card.note, parsedCard.note);
assertEquals(card.store, parsedCard.store);
assertNull(parsedCard.headerColor);
assertNull(parsedCard.headerTextColor);
}

@Test
public void failToParseInvalidUri()
{
Expand Down