Fix as reviewed and add test

This commit is contained in:
Sylvia van Os
2020-01-02 21:15:06 +01:00
parent cc99af13e4
commit ce81d3afd4
4 changed files with 69 additions and 11 deletions

View File

@@ -145,7 +145,7 @@ public class BarcodeSelectorActivity extends AppCompatActivity
public void onClick(View view) {
Log.d(TAG, "Selected no barcode");
Intent result = new Intent();
result.putExtra(BARCODE_FORMAT, LoyaltyCardEditActivity.NO_BARCODE);
result.putExtra(BARCODE_FORMAT, "");
result.putExtra(BARCODE_CONTENTS, cardId);
BarcodeSelectorActivity.this.setResult(RESULT_OK, result);
finish();

View File

@@ -37,7 +37,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
private static final String TAG = "CardLocker";
protected static final String NO_BARCODE = "_NO_BARCODE_";
private static final int SELECT_BARCODE_REQUEST = 1;
protected static final int SELECT_BARCODE_REQUEST = 1;
EditText storeFieldEdit;
EditText noteFieldEdit;
@@ -499,7 +499,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
}
if(contents != null && contents.isEmpty() == false &&
format != null && format.isEmpty() == false)
format != null)
{
Log.i(TAG, "Read barcode id: " + contents);
Log.i(TAG, "Read format: " + format);
@@ -508,7 +508,8 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
cardIdView.setText(contents);
final TextView barcodeTypeField = findViewById(R.id.barcodeType);
barcodeTypeField.setText(format);
// Set special NO_BARCODE value to prevent onResume from overwriting it
barcodeTypeField.setText(format.isEmpty() ? LoyaltyCardEditActivity.NO_BARCODE : format);
onResume();
}
}

View File

@@ -64,9 +64,8 @@ public class BarcodeSelectorActivityTest {
activity.findViewById(R.id.noBarcode).performClick();
Intent resultIntent = shadowOf(activity).getResultIntent();
// The BarcodeSelectorActivity should return the special NO_BARCODE string to differentiate
// from nothing being set yet
assertEquals(LoyaltyCardEditActivity.NO_BARCODE, resultIntent.getStringExtra(BarcodeSelectorActivity.BARCODE_FORMAT));
// The BarcodeSelectorActivity should return an empty string
assertEquals("", resultIntent.getStringExtra(BarcodeSelectorActivity.BARCODE_FORMAT));
assertEquals("abcdefg", resultIntent.getStringExtra(BarcodeSelectorActivity.BARCODE_CONTENTS));
}

View File

@@ -164,10 +164,10 @@ public class LoyaltyCardViewActivityTest
assertNotNull(bundle);
Intent resultIntent = new Intent(intent);
Bundle resultBuddle = new Bundle();
resultBuddle.putString(Intents.Scan.RESULT, BARCODE_DATA);
resultBuddle.putString(Intents.Scan.RESULT_FORMAT, BARCODE_TYPE);
resultIntent.putExtras(resultBuddle);
Bundle resultBundle = new Bundle();
resultBundle.putString(Intents.Scan.RESULT, BARCODE_DATA);
resultBundle.putString(Intents.Scan.RESULT_FORMAT, BARCODE_TYPE);
resultIntent.putExtras(resultBundle);
// Respond to image capture, success
shadowOf(activity).receiveResult(
@@ -176,6 +176,38 @@ public class LoyaltyCardViewActivityTest
resultIntent);
}
/**
* Initiate and complete a barcode selection, either in success
* or in failure
*/
private void selectBarcodeWithResult(final Activity activity, final int buttonId, final String barcodeData, final String barcodeType, final boolean success) throws IOException
{
// Start image capture
final Button captureButton = activity.findViewById(buttonId);
captureButton.performClick();
ShadowActivity.IntentForResult intentForResult = shadowOf(activity).peekNextStartedActivityForResult();
assertNotNull(intentForResult);
Intent intent = intentForResult.intent;
assertNotNull(intent);
Bundle bundle = intent.getExtras();
assertNotNull(bundle);
Intent resultIntent = new Intent(intent);
Bundle resultBundle = new Bundle();
resultBundle.putString(BarcodeSelectorActivity.BARCODE_FORMAT, barcodeType);
resultBundle.putString(BarcodeSelectorActivity.BARCODE_CONTENTS, barcodeData);
resultIntent.putExtras(resultBundle);
// Respond to barcode selection, success
shadowOf(activity).receiveResult(
intent,
success ? Activity.RESULT_OK : Activity.RESULT_CANCELED,
resultIntent);
}
private void checkFieldProperties(final Activity activity, final int id, final int visibility,
final String contents)
{
@@ -551,6 +583,32 @@ public class LoyaltyCardViewActivityTest
saveLoyaltyCardWithArguments(activity, "store", "note", BARCODE_DATA, LoyaltyCardEditActivity.NO_BARCODE, false);
}
@Test
public void removeBarcodeFromLoyaltyCard() throws IOException
{
ActivityController activityController = createActivityWithLoyaltyCard(true);
Activity activity = (Activity)activityController.get();
DBHelper db = new DBHelper(activity);
db.insertLoyaltyCard("store", "note", BARCODE_DATA, BARCODE_TYPE, Color.BLACK, Color.WHITE);
activityController.start();
activityController.visible();
activityController.resume();
// First check if the card is as expected
checkAllFields(activity, ViewMode.UPDATE_CARD, "store", "note", BARCODE_DATA, BARCODE_TYPE);
// Complete empty barcode selection successfully
selectBarcodeWithResult(activity, R.id.enterButton, BARCODE_DATA, "", true);
// Check if the barcode type is NO_BARCODE as expected
checkAllFields(activity, ViewMode.UPDATE_CARD, "store", "note", BARCODE_DATA, LoyaltyCardEditActivity.NO_BARCODE);
// Check if the special NO_BARCODE string doesn't get saved
saveLoyaltyCardWithArguments(activity, "store", "note", BARCODE_DATA, LoyaltyCardEditActivity.NO_BARCODE, false);
}
@Test
public void startCheckFontSizes()
{