1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.cts.verifier.p2p.testcase;
17 
18 import java.util.List;
19 
20 import android.content.Context;
21 import android.net.wifi.p2p.nsd.WifiP2pServiceRequest;
22 
23 import com.android.cts.verifier.R;
24 import com.android.cts.verifier.p2p.testcase.ListenerTest.ListenerArgument;
25 
26 /**
27  * The base class for service discovery requester test case.
28  * The common functions are defined in this class.
29  */
30 public abstract class ServReqTestCase extends ReqTestCase {
31 
ServReqTestCase(Context context)32     public ServReqTestCase(Context context) {
33         super(context);
34     }
35 
36     /**
37      * Search test.
38      *
39      * 1. Add the specified service requests to the framework.<br>
40      * 2. Call discoverService().<br>
41      * 3. Check the appropriate responses can be received.<br>
42      *
43      * @param targetAddr target device address
44      * @param reqList service discovery request list to send.
45      * @param dnsPtrArgList the expected result list of dns sd callback function
46      * @param dnsTxtArgList the expected result list of txt record callback function
47      * @param upnpArgList the expected result list of upnp callback function
48      */
searchTest(String targetAddr, List<WifiP2pServiceRequest> reqList, List<ListenerArgument> dnsPtrArgList, List<ListenerArgument> dnsTxtArgList, List<ListenerArgument> upnpArgList)49     protected boolean searchTest(String targetAddr,
50             List<WifiP2pServiceRequest> reqList,
51             List<ListenerArgument> dnsPtrArgList,
52             List<ListenerArgument> dnsTxtArgList,
53             List<ListenerArgument> upnpArgList)
54             throws InterruptedException {
55 
56         ActionListenerTest actionListener = new ActionListenerTest();
57         UPnPServiceResponseListenerTest upnpListener =
58                 new UPnPServiceResponseListenerTest(targetAddr);
59         DnsSdResponseListenerTest dnsSdListener =
60                 new DnsSdResponseListenerTest(targetAddr);
61         DnsSdTxtRecordListenerTest txtListener =
62                 new DnsSdTxtRecordListenerTest(targetAddr);
63 
64         /*
65          * Set service discovery request to the framework.
66          */
67         for (WifiP2pServiceRequest req: reqList) {
68             mP2pMgr.addServiceRequest(mChannel, req, actionListener);
69             if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
70                 mReason = mContext.getString(R.string.p2p_add_service_request_error);
71                 return false;
72             }
73         }
74 
75         /*
76          * set service response callback function to framework
77          */
78         mP2pMgr.setUpnpServiceResponseListener(mChannel, upnpListener);
79         mP2pMgr.setDnsSdResponseListeners(mChannel, dnsSdListener, txtListener);
80 
81         /*
82          * execute service find operation.
83          */
84         mP2pMgr.discoverServices(mChannel, actionListener);
85         if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
86             mReason = mContext.getString(R.string.p2p_discover_services_error);
87             return false;
88         }
89 
90         /*
91          * If needed, check that the expected callback is invoked correctly
92          * Otherwise, check that no callback is invoked.
93          */
94         Timeout t = new Timeout(TIMEOUT);
95         if (!dnsSdListener.check(dnsPtrArgList, t.getRemainTime())) {
96             mReason = getListenerError(dnsSdListener);
97             return false;
98         }
99         if (!txtListener.check(dnsTxtArgList, t.getRemainTime())) {
100             mReason = getListenerError(txtListener);
101             return false;
102         }
103         if (!upnpListener.check(upnpArgList, t.getRemainTime())) {
104             mReason = getListenerError(upnpListener);
105             return false;
106         }
107 
108         return true;
109     }
110 
getListenerError(ListenerTest listener)111     protected String getListenerError(ListenerTest listener) {
112         StringBuilder sb = new StringBuilder();
113         sb.append(mContext.getText(R.string.p2p_receive_invalid_response_error));
114         sb.append(listener.getReason());
115         return sb.toString();
116     }
117 }
118