• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.bluetooth.tests;
2 
3 import java.io.IOException;
4 
5 import javax.obex.HeaderSet;
6 import javax.obex.Operation;
7 import javax.obex.ResponseCodes;
8 
9 import junit.framework.Assert;
10 import android.util.Log;
11 
12 import com.android.bluetooth.map.BluetoothMapAppParams;
13 import com.android.bluetooth.map.BluetoothMapFolderElement;
14 import com.android.bluetooth.tests.TestSequencer.OPTYPE;
15 
16 public class MapStepsFolder {
17     private final static String TAG = "MapStepsFolder";
18     /**
19      * Request and expect the following folder structure:
20      * root
21      *   telecom
22      *     msg
23      *       inbox
24      *       outbox
25      *       draft
26      *       sent
27      *       deleted
28      *
29      * The order in which they occur in the listing will not matter.
30      * @param sequencer
31      */
addGoToMsgFolderSteps(TestSequencer sequencer)32     protected static void addGoToMsgFolderSteps(TestSequencer sequencer) {
33         SeqStep step;
34         //BluetoothMapFolderElement rootDir = new BluetoothMapFolderElement("root", null);
35 
36         // MAP Get Folder Listing Steps
37         // The telecom folder
38         step = sequencer.addStep(OPTYPE.GET, null);
39         HeaderSet hs = new HeaderSet();
40         hs.setHeader(HeaderSet.TYPE, MapObexLevelTest.TYPE_GET_FOLDER_LISTING);
41         step.mReqHeaders = hs;
42         step.mValidator = new MapBuildFolderStructurValidator(1, null);
43 
44         step = sequencer.addStep(OPTYPE.SET_PATH, ObexTest.getResponsecodevalidator());
45         hs = new HeaderSet();
46         hs.setHeader(HeaderSet.NAME, "telecom");
47         step.mReqHeaders = hs;
48         step.mClientPostAction = new MapSetClientFolder("telecom");
49 
50 
51         // The msg folder
52         step = sequencer.addStep(OPTYPE.GET, null);
53         hs = new HeaderSet();
54         hs.setHeader(HeaderSet.TYPE, MapObexLevelTest.TYPE_GET_FOLDER_LISTING);
55         step.mReqHeaders = hs;
56         step.mValidator = new MapBuildFolderStructurValidator(1, null);
57 
58         step = sequencer.addStep(OPTYPE.SET_PATH, ObexTest.getResponsecodevalidator());
59         hs = new HeaderSet();
60         hs.setHeader(HeaderSet.NAME, "msg");
61         step.mReqHeaders = hs;
62         step.mClientPostAction = new MapSetClientFolder("msg");
63 
64         // The msg folder
65         step = sequencer.addStep(OPTYPE.GET, null);
66         hs = new HeaderSet();
67         hs.setHeader(HeaderSet.TYPE, MapObexLevelTest.TYPE_GET_FOLDER_LISTING);
68         step.mReqHeaders = hs;
69         step.mValidator = new MapBuildFolderStructurValidator(5, buildDefaultFolderStructure());
70     }
71 
72     /**
73      * Sets the current folder on the client, to the folder name specified in the constructor.
74      * TODO: Could be extended to be able to navigate back and forth in the folder structure.
75      */
76     private static class MapSetClientFolder implements ISeqStepAction {
77         final String mFolderName;
MapSetClientFolder(String folderName)78         public MapSetClientFolder(String folderName) {
79             super();
80             this.mFolderName = folderName;
81         }
82         @Override
execute(SeqStep step, HeaderSet request, Operation op)83         public void execute(SeqStep step, HeaderSet request, Operation op)
84                 throws IOException {
85             MapBuildFolderStructurValidator.sCurrentFolder =
86                     MapBuildFolderStructurValidator.sCurrentFolder.getSubFolder(mFolderName);
87             Assert.assertNotNull(MapBuildFolderStructurValidator.sCurrentFolder);
88             Log.i(TAG, "MapSetClientFolder(): Current path: " +
89                     MapBuildFolderStructurValidator.sCurrentFolder.getFullPath());
90         }
91     }
92 
93     /* Functions to validate results */
94     private static class MapBuildFolderStructurValidator implements ISeqStepValidator {
95 
96         final int mExpectedListingSize;
97         static BluetoothMapFolderElement sCurrentFolder = null;
98         final BluetoothMapFolderElement mExpectedFolderElement;
99 
MapBuildFolderStructurValidator(int mExpectedListingSize, BluetoothMapFolderElement folderElement)100         public MapBuildFolderStructurValidator(int mExpectedListingSize,
101                 BluetoothMapFolderElement folderElement) {
102             super();
103             if(sCurrentFolder == null) {
104                 sCurrentFolder = new BluetoothMapFolderElement("root", null);
105             }
106             this.mExpectedListingSize = mExpectedListingSize;
107             this.mExpectedFolderElement = folderElement;
108         }
109 
110 
111         @Override
validate(SeqStep step, HeaderSet response, Operation op)112         public boolean validate(SeqStep step, HeaderSet response, Operation op)
113                 throws IOException {
114             Assert.assertNotNull(op);
115             op.noBodyHeader();
116             try {
117                 // For some odd reason, the request will not be send before we start to read the
118                 // reply data, hence we need to do this first?
119                 sCurrentFolder.appendSubfolders(op.openInputStream());
120                 response = op.getReceivedHeader();
121                 byte[] appParamsRaw = (byte[])response.getHeader(HeaderSet.APPLICATION_PARAMETER);
122                 Assert.assertNotNull(appParamsRaw);
123                 BluetoothMapAppParams appParams;
124                 appParams = new BluetoothMapAppParams(appParamsRaw);
125                 Assert.assertNotNull(appParams);
126                 if(mExpectedFolderElement != null) {
127                     // Recursively compare
128                     Assert.assertTrue(mExpectedFolderElement.compareTo(sCurrentFolder.getRoot())
129                             == 0);
130                 }
131                 int responseCode = op.getResponseCode();
132                 Assert.assertEquals(ResponseCodes.OBEX_HTTP_OK, responseCode);
133                 op.close();
134             } catch (Exception e) {
135                 Log.e(TAG,"",e);
136                 Assert.fail();
137             }
138             return true;
139         }
140 
141     }
142 
143 
buildDefaultFolderStructure()144     private static BluetoothMapFolderElement buildDefaultFolderStructure(){
145         BluetoothMapFolderElement root =
146                 new BluetoothMapFolderElement("root", null); // This will be the root element
147         BluetoothMapFolderElement tmpFolder;
148         tmpFolder = root.addFolder("telecom"); // root/telecom
149         tmpFolder = tmpFolder.addFolder("msg");         // root/telecom/msg
150         tmpFolder.addFolder("inbox");                   // root/telecom/msg/inbox
151         tmpFolder.addFolder("outbox");                  // root/telecom/msg/outbox
152         tmpFolder.addFolder("sent");                    // root/telecom/msg/sent
153         tmpFolder.addFolder("deleted");                 // root/telecom/msg/deleted
154         tmpFolder.addFolder("draft");                   // root/telecom/msg/draft
155         return root;
156     }
157 
158 
159 }
160