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 17 package com.android.cts.verifier.p2p.testcase; 18 19 import com.android.cts.verifier.R; 20 21 import android.content.Context; 22 import android.net.wifi.WpsInfo; 23 import android.net.wifi.p2p.nsd.WifiP2pUpnpServiceRequest; 24 25 /** 26 * Test case to join a p2p group with wps push button. 27 */ 28 public class P2pClientPbcTestCase extends ConnectReqTestCase { 29 P2pClientPbcTestCase(Context context)30 public P2pClientPbcTestCase(Context context) { 31 super(context); 32 } 33 34 @Override executeTest()35 protected boolean executeTest() throws InterruptedException { 36 37 if (!checkUpnpService()) { 38 return false; 39 } 40 41 return connectTest(true, WpsInfo.PBC); 42 } 43 44 /** 45 * Check UPnP service on GO through service discovery request. 46 * @return true if success. 47 * @throws InterruptedException 48 */ checkUpnpService()49 private boolean checkUpnpService() throws InterruptedException { 50 notifyTestMsg(R.string.p2p_checking_serv_capab); 51 ActionListenerTest actionListener = new ActionListenerTest(); 52 53 /* 54 * Check UPnP services. 55 */ 56 WifiP2pUpnpServiceRequest upnpReq = WifiP2pUpnpServiceRequest.newInstance(); 57 58 /* 59 * add UPnP request. 60 */ 61 mP2pMgr.addServiceRequest(mChannel, upnpReq, actionListener); 62 if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) { 63 mReason = mContext.getString(R.string.p2p_add_service_request_error); 64 return false; 65 } 66 67 /* 68 * Initialize listener test objects. 69 */ 70 UPnPServiceResponseListenerTest upnpListener = 71 new UPnPServiceResponseListenerTest(mTargetAddress); 72 DnsSdResponseListenerTest dnsListener = 73 new DnsSdResponseListenerTest(mTargetAddress); 74 DnsSdTxtRecordListenerTest txtListener = 75 new DnsSdTxtRecordListenerTest(mTargetAddress); 76 77 /* 78 * set service response listener callback. 79 */ 80 mP2pMgr.setUpnpServiceResponseListener(mChannel, upnpListener); 81 mP2pMgr.setDnsSdResponseListeners(mChannel, dnsListener, txtListener); 82 83 /* 84 * discover services 85 */ 86 mP2pMgr.discoverServices(mChannel, actionListener); 87 if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) { 88 mReason = mContext.getString(R.string.p2p_discover_services_error); 89 return false; 90 } 91 92 /* 93 * Receive only UPnP service. 94 */ 95 Timeout t = new Timeout(TIMEOUT); 96 if (!dnsListener.check(DnsSdResponseListenerTest.NO_DNS_PTR, 97 t.getRemainTime())) { 98 mReason = getListenerError(dnsListener); 99 return false; 100 } 101 if (!txtListener.check(DnsSdTxtRecordListenerTest.NO_DNS_TXT, 102 t.getRemainTime())) { 103 mReason = getListenerError(txtListener); 104 return false; 105 } 106 if (!upnpListener.check( 107 UPnPServiceResponseListenerTest.ALL_UPNP_SERVICES, 108 t.getRemainTime())) { 109 mReason = getListenerError(upnpListener); 110 return false; 111 } 112 113 return true; 114 } 115 getListenerError(ListenerTest listener)116 private String getListenerError(ListenerTest listener) { 117 StringBuilder sb = new StringBuilder(); 118 sb.append(mContext.getText(R.string.p2p_receive_invalid_response_error)); 119 sb.append(listener.getReason()); 120 return sb.toString(); 121 } 122 123 @Override getTestName()124 public String getTestName() { 125 return "Join p2p group test (push button)"; 126 } 127 } 128