package io.card.payment;

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

import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;

import java.util.Iterator;

/**
 * This class is used for Robotium testing ONLY!
 * <p/>
 * ALL classes that match *Tester are excluded from the library jar. As such, they should only be
 * accessed by reflection.
 */
public class CardScannerTester extends CardScanner {
    private static final String TAG = CardScannerTester.class.getSimpleName();
    private static Iterator<byte[]> recording = null;

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

    private Handler mHandler = new Handler();
    private int testFrameCount = 0;
    private boolean scanAllowed = false;

    public CardScannerTester(CardIOActivity scanActivity, int currentFrameOrientation) {
        super(scanActivity, currentFrameOrientation);
        // TODO Auto-generated constructor stub
        useCamera = false;
    }

    private Runnable frameRunner = new Runnable() {
        @Override
        public void run() {
            if (!scanAllowed) {
                return;
            }
            if (recording == null) {
                Log.e(TAG, "null recording!");
                return;
            }
            if (recording.hasNext()) {
                Log.i(TAG, "Setting test frame: " + testFrameCount++);
                onPreviewFrame(recording.next(), null);
                mHandler.postDelayed(this, FRAME_INTERVAL);
            } else {
                Log.w(TAG, "No more frames left at " + testFrameCount);
                mHandler.postDelayed(expireRunner, 5000); // give up after 5 sec.
            }
        }
    };

    private Runnable expireRunner = new Runnable() {
        @Override
        public void run() {
            if (!scanAllowed) {
                return;
            }
            mScanActivityRef.get().finish();
        }
    };

    public static void setRecording(Iterator<byte[]> r) {
        recording = r;
    }

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

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

@@ -947,56 +947,8 @@
 n);%0A
-        // TODO Auto-generated constructor stub%0A
     
@@ -1021,32 +1021,74 @@
 ew Runnable() %7B%0A
+        private byte%5B%5D lastFrame = null;%0A%0A
         @Overrid
@@ -1439,23 +1439,20 @@
     
-onPreview
+last
 Frame
-(
+ = 
 reco
@@ -1467,75 +1467,8 @@
 xt()
-, null);%0A                mHandler.postDelayed(this, FRAME_INTERVAL)
 ;%0A  
@@ -1559,106 +1559,165 @@
 ount
-);%0A                mHandler.postDelayed(expireRunner, 5000); // give up after 5 sec.%0A            %7D
+ + %22 repeating last frame indefinitely.%22);%0A            %7D%0A            onPreviewFrame(lastFrame, null);%0A            mHandler.postDelayed(this, FRAME_INTERVAL);
 %0A   

modify scanner to scan forever so that expiry scanning has a chance to do its thing