Home | History | Annotate | Download | only in testcase
      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 android.content.Context;
     19 import android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceRequest;
     20 import android.net.wifi.p2p.nsd.WifiP2pUpnpServiceRequest;
     21 
     22 import com.android.cts.verifier.R;
     23 
     24 /**
     25  * Service discovery requester test case to check removeServiceRequest() works well.
     26  */
     27 public class ServReqRemoveRequestTestCase extends ServReqTestCase {
     28 
     29     public ServReqRemoveRequestTestCase(Context context) {
     30         super(context);
     31     }
     32 
     33     @Override
     34     protected boolean executeTest() throws InterruptedException {
     35 
     36         notifyTestMsg(R.string.p2p_checking_serv_capab);
     37 
     38         ActionListenerTest actionListener = new ActionListenerTest();
     39 
     40         /*
     41          * create some service requests.
     42          */
     43         WifiP2pUpnpServiceRequest upnpReq1 =
     44                 WifiP2pUpnpServiceRequest.newInstance();
     45         WifiP2pUpnpServiceRequest upnpReq2 =
     46                 WifiP2pUpnpServiceRequest.newInstance("ssdp:all");
     47         WifiP2pDnsSdServiceRequest bonjourReq1 =
     48                 WifiP2pDnsSdServiceRequest.newInstance();
     49         WifiP2pDnsSdServiceRequest bonjourReq2 =
     50                 WifiP2pDnsSdServiceRequest.newInstance("_ipp._tcp");
     51 
     52         /*
     53          * add request
     54          */
     55         mP2pMgr.addServiceRequest(mChannel, upnpReq1, actionListener);
     56         if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
     57             mReason = mContext.getString(R.string.p2p_add_service_request_error);
     58             return false;
     59         }
     60         mP2pMgr.addServiceRequest(mChannel, upnpReq2, actionListener);
     61         if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
     62             mReason = mContext.getString(R.string.p2p_add_service_request_error);
     63             return false;
     64         }
     65         mP2pMgr.addServiceRequest(mChannel, bonjourReq1, actionListener);
     66         if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
     67             mReason = mContext.getString(R.string.p2p_add_service_request_error);
     68             return false;
     69         }
     70         mP2pMgr.addServiceRequest(mChannel, bonjourReq2, actionListener);
     71         if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
     72             mReason = mContext.getString(R.string.p2p_add_service_request_error);
     73             return false;
     74         }
     75 
     76         /*
     77          * remove requests except for UPnP ssdp:all.
     78          */
     79         mP2pMgr.removeServiceRequest(mChannel, upnpReq1, actionListener);
     80         if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
     81             mReason = mContext.getString(R.string.p2p_remove_service_request_error);
     82             return false;
     83         }
     84         mP2pMgr.removeServiceRequest(mChannel, bonjourReq1, actionListener);
     85         if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
     86             mReason = mContext.getString(R.string.p2p_remove_service_request_error);
     87             return false;
     88         }
     89         mP2pMgr.removeServiceRequest(mChannel, bonjourReq2, actionListener);
     90         if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
     91             mReason = mContext.getString(R.string.p2p_remove_service_request_error);
     92             return false;
     93         }
     94 
     95         /*
     96          * initialize test listener.
     97          */
     98         UPnPServiceResponseListenerTest upnpListener =
     99                 new UPnPServiceResponseListenerTest(mTargetAddress);
    100         DnsSdResponseListenerTest dnsSdListener =
    101                 new DnsSdResponseListenerTest(mTargetAddress);
    102         DnsSdTxtRecordListenerTest txtListener =
    103                 new DnsSdTxtRecordListenerTest(mTargetAddress);
    104 
    105         mP2pMgr.setUpnpServiceResponseListener(mChannel, upnpListener);
    106         mP2pMgr.setDnsSdResponseListeners(mChannel, dnsSdListener, txtListener);
    107 
    108         /*
    109          * search UPnP service with ssdp:all
    110          */
    111         mP2pMgr.discoverServices(mChannel, actionListener);
    112         if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
    113             mReason = mContext.getString(R.string.p2p_discover_services_error);
    114             return false;
    115         }
    116 
    117         /*
    118          * check that bonjour response NEVER received.
    119          * check that just UPnP response is received
    120          */
    121         Timeout t = new Timeout(TIMEOUT);
    122         if (!dnsSdListener.check(DnsSdResponseListenerTest.NO_DNS_PTR,
    123                 t.getRemainTime())) {
    124             mReason = getListenerError(dnsSdListener);
    125             return false;
    126         }
    127         if (!txtListener.check(DnsSdTxtRecordListenerTest.NO_DNS_TXT,
    128                 t.getRemainTime())) {
    129             mReason = getListenerError(txtListener);
    130             return false;
    131         }
    132         if (!upnpListener.check(
    133                 UPnPServiceResponseListenerTest.ALL_UPNP_SERVICES,
    134                 t.getRemainTime())) {
    135             mReason = getListenerError(upnpListener);
    136             return false;
    137         }
    138 
    139         return true;
    140     }
    141 
    142     @Override
    143     public String getTestName() {
    144         return "Remove service requests test";
    145     }
    146 }
    147