Home | History | Annotate | Download | only in tests
      1 package com.android.bluetooth.tests;
      2 
      3 import java.util.ArrayList;
      4 import java.util.concurrent.CountDownLatch;
      5 
      6 import javax.obex.HeaderSet;
      7 import javax.obex.Operation;
      8 import javax.obex.ResponseCodes;
      9 
     10 import junit.framework.Assert;
     11 import android.bluetooth.BluetoothAdapter;
     12 import android.content.Context;
     13 import android.os.Handler;
     14 import android.os.RemoteException;
     15 import android.util.Log;
     16 
     17 import com.android.bluetooth.map.BluetoothMapAccountItem;
     18 import com.android.bluetooth.map.BluetoothMapContentObserver;
     19 import com.android.bluetooth.map.BluetoothMapMasInstance;
     20 import com.android.bluetooth.map.BluetoothMapObexServer;
     21 import com.android.bluetooth.map.BluetoothMapUtils;
     22 import com.android.bluetooth.map.BluetoothMnsObexClient;
     23 
     24 public class MapObexTestServer extends BluetoothMapObexServer {
     25 
     26     private static final String TAG = "MapObexTestServer";
     27     private static final boolean V = true;
     28 
     29     ArrayList<SeqStep> mSequence;
     30     CountDownLatch mStopLatch;
     31 
     32     ObexTestDataHandler mDataHandler;
     33     int mOperationIndex = 0;
     34 
     35     /* This needs to be static, as calling the super-constructor must be the first step.
     36      * Alternatively add the account as constructor parameter, and create a builder
     37      * function - factory pattern. */
     38 //    private static BluetoothMapAccountItem mAccountMock = new BluetoothMapAccountItem("1",
     39 //           "TestAccount",
     40 //           "do.not.exist.package.name.and.never.used.anyway:-)",
     41 //           "info.guardianproject.otr.app.im.provider.bluetoothprovider",
     42 //           null,
     43 //           BluetoothMapUtils.TYPE.IM,
     44 //           null,
     45 //           null);
     46     private static BluetoothMapAccountItem mAccountMock = null;
     47 
     48     /* MAP Specific instance variables
     49     private final BluetoothMapContentObserver mObserver = null;
     50     private final BluetoothMnsObexClient mMnsClient = null;*/
     51 
     52     /* Test values, consider gathering somewhere else */
     53     private static final int MAS_ID = 0;
     54     private static final int REMOTE_FEATURE_MASK = 0x07FFFFFF;
     55     private static final BluetoothMapMasInstance mMasInstance =
     56             new MockMasInstance(MAS_ID, REMOTE_FEATURE_MASK);
     57 
     58     public MapObexTestServer(final Context context, ArrayList<SeqStep> sequence,
     59             CountDownLatch stopLatch) throws RemoteException {
     60 
     61         super(null, context,
     62                 new  BluetoothMapContentObserver(context,
     63                         new BluetoothMnsObexClient(
     64                                 BluetoothAdapter.getDefaultAdapter().
     65                                 getRemoteDevice("12:23:34:45:56:67"), null, null),
     66                                 /* TODO: this will not work for single device test... */
     67                         mMasInstance,
     68                         mAccountMock, /* Account */
     69                         true) /* Enable SMS/MMS*/,
     70                 mMasInstance,
     71                 mAccountMock /* Account */,
     72                 true /* SMS/MMS enabled*/);
     73         mSequence = sequence;
     74         mDataHandler = new ObexTestDataHandler("(Server)");
     75         mStopLatch = stopLatch;
     76     }
     77 
     78     /* OBEX operation handlers */
     79     @Override
     80     public int onConnect(HeaderSet request, HeaderSet reply) {
     81         Log.i(TAG,"onConnect()");
     82         int index;
     83         int result = ResponseCodes.OBEX_HTTP_OK;
     84         try {
     85             index = ((Long)request.getHeader(TestSequencer.STEP_INDEX_HEADER)).intValue();
     86             mOperationIndex = index;
     87             SeqStep step = mSequence.get(mOperationIndex);
     88             Assert.assertNotNull("invalid step index!", step);
     89             if(step.mServerPreAction != null) {
     90                 step.mServerPreAction.execute(step, request, null);
     91             }
     92             result = super.onConnect(request, reply);
     93         } catch (Exception e) {
     94             Log.e(TAG, "Exception in onConnect - aborting...", e);
     95             result = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
     96             // A read from null will produce exception to end the test.
     97         }
     98         return result;
     99     }
    100 
    101     @Override
    102     public void onDisconnect(HeaderSet request, HeaderSet reply) {
    103         Log.i(TAG,"onDisconnect()");
    104         /* TODO: validate request headers, and set response headers */
    105         int index;
    106         int result = ResponseCodes.OBEX_HTTP_OK;
    107         try {
    108             index = ((Long)request.getHeader(TestSequencer.STEP_INDEX_HEADER)).intValue();
    109             mOperationIndex = index;
    110             SeqStep step = mSequence.get(mOperationIndex);
    111             Assert.assertNotNull("invalid step index!", step);
    112             if(step.mServerPreAction != null) {
    113                 step.mServerPreAction.execute(step, request, null);
    114             }
    115             super.onDisconnect(request, reply);
    116         } catch (Exception e) {
    117             Log.e(TAG, "Exception in onDisconnect - aborting...", e);
    118             result = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
    119             // A read from null will produce exception to end the test.
    120         }
    121         if(mOperationIndex >= (mSequence.size()-1)) {
    122             /* End of test, signal test runner thread */
    123             Log.i(TAG, "Sending latch close signal...");
    124             mStopLatch.countDown();
    125         } else {
    126             Log.i(TAG, "Got disconnect with mOperationCounter = " + mOperationIndex);
    127         }
    128         reply.responseCode = result;
    129     }
    130 
    131     @Override
    132     public int onPut(Operation operation) {
    133         Log.i(TAG,"onPut()");
    134         int result = ResponseCodes.OBEX_HTTP_OK;
    135         try{
    136             HeaderSet reqHeaders = operation.getReceivedHeader();
    137             int index = ((Long)reqHeaders.getHeader(TestSequencer.STEP_INDEX_HEADER)).intValue();
    138             mOperationIndex = index;
    139             SeqStep step = mSequence.get(mOperationIndex);
    140             Assert.assertNotNull("invalid step index!", step);
    141             if(step.mServerPreAction != null) {
    142                 step.mServerPreAction.execute(step, reqHeaders, operation);
    143             }
    144             super.onPut(operation);
    145         } catch (Exception e) {
    146             Log.e(TAG, "Exception in onPut - aborting...", e);
    147             result = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
    148             // A read from null will produce exception to end the test.
    149         }
    150         if(result == ResponseCodes.OBEX_HTTP_OK) {
    151             Log.i(TAG, "OBEX-HANDLER: operation complete success");
    152         } else {
    153             Log.e(TAG, "OBEX-HANDLER: operation complete FAILED!");
    154         }
    155         return result;
    156     }
    157 
    158     @Override
    159     public int onGet(Operation operation) {
    160         Log.i(TAG,"onGet()");
    161         int result = ResponseCodes.OBEX_HTTP_OK;
    162         try{
    163             HeaderSet reqHeaders = operation.getReceivedHeader();
    164             int index = ((Long)reqHeaders.getHeader(TestSequencer.STEP_INDEX_HEADER)).intValue();
    165             mOperationIndex = index;
    166             SeqStep step = mSequence.get(mOperationIndex);
    167             Assert.assertNotNull("invalid step index!", step);
    168             if(step.mServerPreAction != null) {
    169                 step.mServerPreAction.execute(step, reqHeaders, operation);
    170             }
    171             super.onGet(operation);
    172         } catch (Exception e) {
    173             Log.e(TAG, "Exception in onGet - aborting...", e);
    174             result = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
    175             // A read from null will produce exception to end the test.
    176         }
    177         if(result == ResponseCodes.OBEX_HTTP_OK) {
    178             Log.i(TAG, "OBEX-HANDLER: operation complete success");
    179         } else {
    180             Log.e(TAG, "OBEX-HANDLER: operation complete FAILED!");
    181         }
    182         return result;
    183     }
    184 
    185 
    186 
    187 }
    188 
    189