1 package com.android.bluetooth.tests; 2 3 import java.io.IOException; 4 import java.io.UnsupportedEncodingException; 5 import java.text.ParseException; 6 import java.util.Arrays; 7 8 import javax.obex.HeaderSet; 9 import javax.obex.Operation; 10 import javax.obex.ResponseCodes; 11 12 import junit.framework.Assert; 13 import android.util.Log; 14 15 import com.android.bluetooth.map.BluetoothMapAppParams; 16 import com.android.bluetooth.map.BluetoothMapConvoListing; 17 import com.android.bluetooth.map.BluetoothMapConvoListingElement; 18 import com.android.bluetooth.map.BluetoothMapFolderElement; 19 import com.android.bluetooth.tests.TestSequencer.OPTYPE; 20 21 public class MapStepsConvo { 22 private static final String TAG = "MapStepsConvo"; 23 24 addConvoListingSteps(TestSequencer sequencer)25 protected static void addConvoListingSteps(TestSequencer sequencer) { 26 SeqStep step; 27 final int count = 5; 28 29 // TODO: As we use the default message database, these tests will fail if the 30 // database has any content. 31 // To cope with this for now, the validation is disabled. 32 33 /* Request the number of messages */ 34 step = addConvoListingStep(sequencer, 35 0 /*maxListCount*/, 36 -1 /*listStartOffset*/, 37 null /*activityBegin*/, 38 null /*activityEnd*/, 39 -1 /*readStatus*/, 40 null /*recipient*/, 41 new MapConvoListValidator() 42 /*new MapConvoListValidator(MapTestData.TEST_NUM_CONTACTS)validator*/); 43 /* Add messages and contacts for the entire sequence of tests */ 44 step.mServerPreAction = new MapTestData.MapAddSmsMessages(count); 45 46 /* Request the XML for all conversations */ 47 step = addConvoListingStep(sequencer, 48 -1 /*maxListCount*/, 49 -1 /*listStartOffset*/, 50 null /*activityBegin*/, 51 null /*activityEnd*/, 52 -1 /*readStatus*/, 53 null /*recipient*/, 54 /*nearly impossible to validate due to auto assigned ID values*/ 55 new MapConvoListValidator() 56 /*new MapConvoListValidator(MapTestData.TEST_NUM_CONTACTS)validator*/); 57 58 step = addConvoListingStep(sequencer, 59 2 /*maxListCount*/, 60 -1 /*listStartOffset*/, 61 null /*activityBegin*/, 62 null /*activityEnd*/, 63 -1 /*readStatus*/, 64 null /*recipient*/, 65 /*nearly impossible to validate due to auto assigned ID values*/ 66 new MapConvoListValidator() 67 /*new MapConvoListValidator(MapTestData.TEST_NUM_CONTACTS)validator*/); 68 69 step = addConvoListingStep(sequencer, 70 2 /*maxListCount*/, 71 1 /*listStartOffset*/, 72 null /*activityBegin*/, 73 null /*activityEnd*/, 74 -1 /*readStatus*/, 75 null /*recipient*/, 76 /*nearly impossible to validate due to auto assigned ID values*/ 77 new MapConvoListValidator() 78 /*new MapConvoListValidator(MapTestData.TEST_NUM_CONTACTS)validator*/); 79 80 step = addConvoListingStep(sequencer, 81 3 /*maxListCount*/, 82 2 /*listStartOffset*/, 83 null /*activityBegin*/, 84 null /*activityEnd*/, 85 -1 /*readStatus*/, 86 null /*recipient*/, 87 /*nearly impossible to validate due to auto assigned ID values*/ 88 new MapConvoListValidator() 89 /*new MapConvoListValidator(MapTestData.TEST_NUM_CONTACTS)validator*/); 90 91 step = addConvoListingStep(sequencer, 92 5 /*maxListCount*/, 93 1 /*listStartOffset*/, 94 MapTestData.TEST_ACTIVITY_BEGIN_STRING /*activityBegin*/, 95 null /*activityEnd*/, 96 -1 /*readStatus*/, 97 null /*recipient*/, 98 /*nearly impossible to validate due to auto assigned ID values*/ 99 new MapConvoListValidator() 100 /*new MapConvoListValidator(MapTestData.TEST_NUM_CONTACTS)validator*/); 101 102 step = addConvoListingStep(sequencer, 103 5 /*maxListCount*/, 104 0 /*listStartOffset*/, 105 MapTestData.TEST_ACTIVITY_BEGIN_STRING /*activityBegin*/, 106 MapTestData.TEST_ACTIVITY_END_STRING /*activityEnd*/, 107 -1 /*readStatus*/, 108 null /*recipient*/, 109 /*nearly impossible to validate due to auto assigned ID values*/ 110 new MapConvoListValidator() 111 /*new MapConvoListValidator(MapTestData.TEST_NUM_CONTACTS)validator*/); 112 113 step = addConvoListingStep(sequencer, 114 5 /*maxListCount*/, 115 1 /*listStartOffset*/, 116 MapTestData.TEST_ACTIVITY_BEGIN_STRING /*activityBegin*/, 117 null /*activityEnd*/, 118 2/* read only */ /*readStatus*/, 119 null /*recipient*/, 120 /*nearly impossible to validate due to auto assigned ID values*/ 121 new MapConvoListValidator() 122 /*new MapConvoListValidator(MapTestData.TEST_NUM_CONTACTS)validator*/); 123 124 /* TODO: Test the different combinations of filtering */ 125 } 126 127 /** 128 * Use -1 or null to omit value in request 129 * @param sequencer 130 * @param maxListCount 131 * @param listStartOffset 132 * @param activityBegin 133 * @param activityEnd 134 * @param readStatus -1 omit value, 0 = no filtering, 1 = get unread only, 2 = get read only, 135 * 3 = 1+2 - hence get none... 136 * @param recipient substring of the recipient name 137 * @param validator 138 * @return a reference to the step added, for further decoration 139 */ addConvoListingStep(TestSequencer sequencer, int maxListCount, int listStartOffset, String activityBegin, String activityEnd, int readStatus, String recipient, ISeqStepValidator validator)140 private static SeqStep addConvoListingStep(TestSequencer sequencer, int maxListCount, 141 int listStartOffset, String activityBegin, String activityEnd, 142 int readStatus, String recipient, ISeqStepValidator validator) { 143 SeqStep step; 144 BluetoothMapAppParams appParams = new BluetoothMapAppParams(); 145 try { 146 if(activityBegin != null) { 147 appParams.setFilterLastActivityBegin(activityBegin); 148 } 149 if(activityEnd != null) { 150 appParams.setFilterLastActivityEnd(activityEnd); 151 } 152 if(readStatus != -1) { 153 appParams.setFilterReadStatus(readStatus); 154 } 155 if(recipient != null) { 156 appParams.setFilterRecipient(recipient); 157 } 158 if(maxListCount != -1) { 159 appParams.setMaxListCount(maxListCount); 160 } 161 if(listStartOffset != -1) { 162 appParams.setStartOffset(listStartOffset); 163 } 164 } catch (ParseException e) { 165 Log.e(TAG, "unable to build appParams", e); 166 } 167 step = sequencer.addStep(OPTYPE.GET, null); 168 HeaderSet hs = new HeaderSet(); 169 hs.setHeader(HeaderSet.TYPE, MapObexLevelTest.TYPE_GET_CONVO_LISTING); 170 try { 171 hs.setHeader(HeaderSet.APPLICATION_PARAMETER, appParams.EncodeParams()); 172 } catch (UnsupportedEncodingException e) { 173 Log.e(TAG, "ERROR", e); 174 Assert.fail(); 175 } 176 step.mReqHeaders = hs; 177 step.mValidator = validator; 178 return step; 179 } 180 181 /* Functions to validate results */ 182 private static class MapConvoListValidator implements ISeqStepValidator { 183 184 final BluetoothMapConvoListing mExpectedListing; 185 final int mExpectedSize; 186 MapConvoListValidator(BluetoothMapConvoListing listing)187 public MapConvoListValidator(BluetoothMapConvoListing listing) { 188 this.mExpectedListing = listing; 189 this.mExpectedSize = -1; 190 } 191 MapConvoListValidator(int convoListingSize)192 public MapConvoListValidator(int convoListingSize) { 193 this.mExpectedListing = null; 194 this.mExpectedSize = convoListingSize; 195 } 196 MapConvoListValidator()197 public MapConvoListValidator() { 198 this.mExpectedListing = null; 199 this.mExpectedSize = -1; 200 } 201 202 @Override validate(SeqStep step, HeaderSet response, Operation op)203 public boolean validate(SeqStep step, HeaderSet response, Operation op) 204 throws IOException { 205 Assert.assertNotNull(op); 206 op.noBodyHeader(); 207 try { 208 // For some odd reason, the request will not be send before we start to read the 209 // reply data, hence we need to do this first? 210 BluetoothMapConvoListing receivedListing = new BluetoothMapConvoListing(); 211 receivedListing.appendFromXml(op.openInputStream()); 212 response = op.getReceivedHeader(); 213 byte[] appParamsRaw = (byte[])response.getHeader(HeaderSet.APPLICATION_PARAMETER); 214 Assert.assertNotNull(appParamsRaw); 215 BluetoothMapAppParams appParams; 216 appParams = new BluetoothMapAppParams(appParamsRaw); 217 Assert.assertNotNull(appParams); 218 Assert.assertNotNull(appParams.getDatabaseIdentifier()); 219 Assert.assertNotSame(BluetoothMapAppParams.INVALID_VALUE_PARAMETER, 220 appParams.getConvoListingSize()); 221 if(mExpectedSize >= 0) { 222 Assert.assertSame(mExpectedSize, appParams.getConvoListingSize()); 223 } 224 if(mExpectedListing != null) { 225 // Recursively compare 226 Assert.assertTrue(mExpectedListing.equals(receivedListing)); 227 Assert.assertSame(mExpectedListing.getList().size(), 228 appParams.getConvoListingSize()); 229 } 230 int responseCode = op.getResponseCode(); 231 Assert.assertEquals(ResponseCodes.OBEX_HTTP_OK, responseCode); 232 op.close(); 233 } catch (Exception e) { 234 Log.e(TAG,"",e); 235 Assert.fail(); 236 } 237 return true; 238 } 239 } 240 } 241