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