Skip to content

Commit

Permalink
Merge pull request card-io#106 from lkorth/tests
Browse files Browse the repository at this point in the history
Update Tests
  • Loading branch information
lkorth authored Nov 4, 2016
2 parents 7277d4e + 52142b6 commit 920f1a7
Show file tree
Hide file tree
Showing 41 changed files with 891 additions and 1,820 deletions.
30 changes: 19 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ sudo: true
addons:
apt:
packages:
- p7zip-full
- p7zip-full
language: android
jdk: oraclejdk8
env:
matrix:
- ANDROID_TARGET=android-24 ANDROID_ABI=armeabi-v7a
android:
components:
- tools
- tools
- android-24
- build-tools-24.0.1
- platform-tools
- tools
- tools
- android-24
- android-22
- sys-img-armeabi-v7a-android-22
- build-tools-24.0.1
- platform-tools
- extra-android-support
- extra-android-m2repository
- extra-google-m2repository
before_install:
- export ANDROID_HOME=/usr/local/android-sdk
- wget https://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip -O ndk.bin
Expand All @@ -23,10 +25,16 @@ before_install:
- export PATH=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools:${ANDROID_NDK_HOME}
- echo "sdk.dir=$ANDROID_HOME" > local.properties
- echo "ndk.dir=$ANDROID_NDK_HOME" >> local.properties
script: ./gradlew clean test assembleAndroidTest :card.io:assembleRelease releaseDoc
before_script:
- echo no | android create avd --force -n test -t android-22 --abi armeabi-v7a
- emulator -avd test -camera-back emulated -skin WXGA720 -no-audio -no-window &
- android-wait-for-emulator
- sleep 60
- adb shell input keyevent 82
script: ./gradlew clean test connectedAndroidTest :card.io:assembleRelease releaseDoc
notifications:
webhooks:
urls:
- secure: A1YKQ32CBvm6s0gfwb+UKZuniYdb5ZUENvSpSt/k7nQmyZ9U0p4Sn8qqSSKfRyYVRLT3C3za53aoqFwSqFj6mZ82YO6HbKl/RqhGQLmdIPpyhXKUE5xKlyMbgmAYRBDh41+eQ1i22mkT+jiqVW06VFSB0YyeDKAZjXxk8hfyIUA=
- secure: A1YKQ32CBvm6s0gfwb+UKZuniYdb5ZUENvSpSt/k7nQmyZ9U0p4Sn8qqSSKfRyYVRLT3C3za53aoqFwSqFj6mZ82YO6HbKl/RqhGQLmdIPpyhXKUE5xKlyMbgmAYRBDh41+eQ1i22mkT+jiqVW06VFSB0YyeDKAZjXxk8hfyIUA=
on_success: change
on_failure: always
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ What it does not yet contain is much in the way of documentation. :crying_cat_fa

Note that this is actual production code, which has been iterated upon by multiple developers over several years. If you see something that could benefit from being tidied up, rewritten, or otherwise improved, your Pull Requests will be welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for details.

Brought to you by
Brought to you by
[![PayPal logo](https://raw.githubusercontent.com/card-io/card.io-iOS-source/master/Resources/pp_h_rgb.png)](https://paypal.com/ "PayPal")


Expand Down Expand Up @@ -44,19 +44,12 @@ There are a few bugs in the build process, so these steps are required for the f

### Testing

#### Setup
Requires a recording of a capture session.

1. Connect an Android 4.0 (or better) device. (You can also use a device back to Gingerbread, but some autotests won't work).
2. Load a card recording by running:
`$ adb push test-data/recording_320455133.550273.zip /storage/sdcard0/card_recordings/recording_320455133.550273.zip`

#### Running

1. `$ cd card.io-Android-source`
1. Connect an Android 18 (or better) device.
2. `$ ./gradlew connectedAndroidTest`

That's it! You should see the app open and run through some tests.
You should see the app open and run through some tests.

### Un-official Release

Expand Down
23 changes: 18 additions & 5 deletions SampleApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.2.2'
}
}

repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}

apply plugin: 'com.android.application'
Expand All @@ -17,6 +20,15 @@ android {
compileSdkVersion 24
buildToolsVersion "24.0.1"

defaultConfig {
applicationId "io.card.development"
minSdkVersion 18
targetSdkVersion 24
versionCode 1
versionName "1.0.0"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}

signingConfigs {
myConfig {
storeFile file("example.keystore")
Expand All @@ -27,10 +39,6 @@ android {
}

buildTypes {
debug {
applicationIdSuffix ".debug"
}

release {
minifyEnabled true
proguardFile getDefaultProguardFile('proguard-android.txt')
Expand All @@ -54,6 +62,11 @@ dependencies {
} else {
compile 'io.card:android-sdk:REPLACE_VERSION'
}

debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4'

androidTestCompile "com.android.support.test.espresso:espresso-core:2.2.2"
androidTestCompile "com.github.lkorth:device-automator:01d85912ec"
}

task wrapper(type: Wrapper) {
Expand Down
4 changes: 4 additions & 0 deletions SampleApp/src/androidTest/assets/test_card_images/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Test Card Images

Card images in this folder are included for automated testing. Images should be 640px x 480px for
tests.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 113 additions & 0 deletions SampleApp/src/androidTest/java/io/card/payment/CardScannerTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package io.card.payment;

/* CardScannerTester.java
* See the file "LICENSE.md" for the full license governing this code.
*/

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.view.SurfaceHolder;

import java.io.IOException;

import static android.support.test.InstrumentationRegistry.getInstrumentation;

public class CardScannerTester extends CardScanner {

private static final long FRAME_INTERVAL = (long) (1000.0 / 30);

private static String sCardAssetName;

private boolean mScanAllowed;
private Handler mHandler;
private byte[] mFrame;

public static void setCardAsset(String cardAssetName) {
sCardAssetName = cardAssetName;
}

public CardScannerTester(CardIOActivity scanActivity, int currentFrameOrientation) {
super(scanActivity, currentFrameOrientation);
useCamera = false;
mScanAllowed = false;
mHandler = new Handler();

try {
Bitmap bitmap = BitmapFactory.decodeStream(getInstrumentation().getContext().getAssets()
.open("test_card_images/" + sCardAssetName));
mFrame = getNV21FormattedImage(bitmap.getWidth(), bitmap.getHeight(), bitmap);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private Runnable mFrameRunnable = new Runnable() {
@Override
public void run() {
if (!mScanAllowed) {
return;
}

onPreviewFrame(mFrame, null);
mHandler.postDelayed(this, FRAME_INTERVAL);
}
};

@Override
boolean resumeScanning(SurfaceHolder holder) {
boolean result = super.resumeScanning(holder);
mScanAllowed = true;
mHandler.postDelayed(mFrameRunnable, FRAME_INTERVAL);
return result;
}

@Override
public void pauseScanning() {
mScanAllowed = false;
super.pauseScanning();
}

private byte[] getNV21FormattedImage(int width, int height, Bitmap bitmap) {
int [] argb = new int[width * height];
byte [] yuv = new byte[width * height * 3 / 2];

bitmap.getPixels(argb, 0, width, 0, 0, width, height);
encodeYUV420SP(yuv, argb, width, height);
bitmap.recycle();

return yuv;
}

private void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;

int R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff);

// well known RGB to YUV algorithm
Y = ( ( 66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ( ( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ( ( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128;

// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every
// other pixel AND every other scanline.
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
}

index ++;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.card.test;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;

import org.junit.Rule;
import org.junit.Test;

import java.lang.reflect.Field;

import io.card.payment.CardIOActivity;
import io.card.payment.CardScannerTester;
import io.card.payment.CreditCard;

import static com.lukekorth.deviceautomator.DeviceAutomator.onDevice;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class CardIOActivityTest {

private CardIOTestActivity mActivity;

@Rule
public final CustomActivityTestRule<CardIOTestActivity> mActivityTestRule =
new CustomActivityTestRule<>(CardIOTestActivity.class, false, false);

@Test(timeout = 30000)
public void scansAmexCards() {
CardScannerTester.setCardAsset("amex.png");

startScan();

waitForActivityToFinish();
CreditCard result = getActivityResultIntent().getParcelableExtra(CardIOActivity.EXTRA_SCAN_RESULT);
assertEquals("3743 260055 74998", result.getFormattedCardNumber());
}

private void startScan() {
mActivityTestRule.launchActivity(null);
mActivity = mActivityTestRule.getActivity();

onDevice().acceptRuntimePermission(Manifest.permission.CAMERA);
}

private void waitForActivityToFinish() {
long endTime = System.currentTimeMillis() + 10000;

do {
try {
if (mActivity.isFinishing()) {
return;
}
} catch (Exception ignored) {}
} while (System.currentTimeMillis() < endTime);

throw new RuntimeException("Maximum wait elapsed (10s) while waiting for activity to finish");
}

private Intent getActivityResultIntent() {
assertThat("Activity did not finish", mActivity.isFinishing(), is(true));

try {
Field resultDataField = Activity.class.getDeclaredField("mResultData");
resultDataField.setAccessible(true);
return (Intent) resultDataField.get(mActivity);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.card.test;


import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;

import static android.support.test.InstrumentationRegistry.getTargetContext;

public class CustomActivityTestRule<T extends Activity> extends android.support.test.rule.ActivityTestRule<T> {

private KeyguardManager.KeyguardLock mKeyguardLock;

public CustomActivityTestRule(Class<T> activityClass) {
super(activityClass);
init();
}

public CustomActivityTestRule(Class<T> activityClass, boolean initialTouchMode) {
super(activityClass, initialTouchMode);
init();
}

public CustomActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity) {
super(activityClass, initialTouchMode, launchActivity);
init();
}

private void init() {
mKeyguardLock = ((KeyguardManager) getTargetContext().getSystemService(Context.KEYGUARD_SERVICE))
.newKeyguardLock("CardIOTest");
mKeyguardLock.disableKeyguard();
}

@Override
protected void afterActivityFinished() {
super.afterActivityFinished();

mKeyguardLock.reenableKeyguard();
}
}
Loading

0 comments on commit 920f1a7

Please sign in to comment.