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.WifiP2pConfig; 22 import android.net.wifi.p2p.WifiP2pDevice; 23 import android.net.wifi.p2p.WifiP2pInfo; 24 import android.net.wifi.p2p.WifiP2pManager; 25 26 import com.android.cts.verifier.R; 27 28 import java.lang.reflect.Method; 29 30 /** 31 * A base class for connection request test. 32 */ 33 public abstract class ConnectReqTestCase extends ReqTestCase { 34 35 protected P2pBroadcastReceiverTest mReceiverTest; 36 37 public ConnectReqTestCase(Context context) { 38 super(context); 39 } 40 41 /** 42 * Set up the test case. 43 */ 44 protected void setUp() { 45 super.setUp(); 46 mReceiverTest = new P2pBroadcastReceiverTest(mContext); 47 mReceiverTest.init(mChannel); 48 49 try { 50 Method[] methods = WifiP2pManager.class.getMethods(); 51 for (int i = 0; i < methods.length; i++) { 52 if (methods[i].getName().equals("deletePersistentGroup")) { 53 // Delete any persistent group 54 for (int netid = 0; netid < 32; netid++) { 55 methods[i].invoke(mP2pMgr, mChannel, netid, null); 56 } 57 } 58 } 59 } catch(Exception e) { 60 e.printStackTrace(); 61 } 62 // Disconnect from wifi to avoid channel conflict 63 mWifiMgr.disconnect(); 64 } 65 66 /** 67 * Tear down the test case. 68 */ 69 protected void tearDown() { 70 super.tearDown(); 71 if (mReceiverTest != null) { 72 mReceiverTest.close(); 73 } 74 if (mP2pMgr != null) { 75 mP2pMgr.cancelConnect(mChannel, null); 76 mP2pMgr.removeGroup(mChannel, null); 77 } 78 } 79 80 /** 81 * Tries to connect the target devices. 82 * @param isJoin if true, try to join the group. otherwise, try go negotiation. 83 * @param wpsConfig wps configuration method. 84 * @return true if succeeded. 85 * @throws InterruptedException 86 */ 87 protected boolean connectTest(boolean isJoin, int wpsConfig) throws InterruptedException { 88 notifyTestMsg(R.string.p2p_searching_target); 89 90 /* 91 * Search target device and check its capability. 92 */ 93 ActionListenerTest actionListener = new ActionListenerTest(); 94 mP2pMgr.discoverPeers(mChannel, actionListener); 95 if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) { 96 mReason = mContext.getString(R.string.p2p_discover_peers_error); 97 return false; 98 } 99 100 WifiP2pDevice dev = mReceiverTest.waitDeviceFound(mTargetAddress, TIMEOUT); 101 if (dev == null) { 102 mReason = mContext.getString(R.string.p2p_target_not_found_error); 103 return false; 104 } 105 106 if (!isJoin && dev.isGroupOwner()) { 107 // target device should be p2p device. 108 mReason = mContext.getString(R.string.p2p_target_invalid_role_error); 109 return false; 110 } else if (isJoin && !dev.isGroupOwner()) { 111 //target device should be group owner. 112 mReason = mContext.getString(R.string.p2p_target_invalid_role_error2); 113 return false; 114 } 115 116 if (wpsConfig == WpsInfo.PBC) { 117 notifyTestMsg(R.string.p2p_connecting_with_pbc); 118 } else { 119 notifyTestMsg(R.string.p2p_connecting_with_pin); 120 } 121 122 /* 123 * Try to connect the target device. 124 */ 125 WifiP2pConfig config = new WifiP2pConfig(); 126 config.deviceAddress = dev.deviceAddress; 127 config.wps.setup = wpsConfig; 128 mP2pMgr.connect(mChannel, config, actionListener); 129 if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) { 130 mReason = mContext.getString(R.string.p2p_connect_error); 131 return false; 132 } 133 134 /* 135 * Check if the connection broadcast is received. 136 */ 137 WifiP2pInfo p2pInfo = mReceiverTest.waitConnectionNotice(TIMEOUT_FOR_USER_ACTION); 138 if (p2pInfo == null) { 139 mReason = mContext.getString(R.string.p2p_connection_error); 140 return false; 141 } 142 143 /* 144 * Wait until peer gets marked conencted. 145 */ 146 notifyTestMsg(R.string.p2p_waiting_for_peer_to_connect); 147 if (mReceiverTest.waitPeerConnected(mTargetAddress, TIMEOUT) != true) { 148 mReason = mContext.getString(R.string.p2p_connection_error); 149 return false; 150 } 151 152 /* 153 * Remove the p2p group manualy. 154 */ 155 mP2pMgr.removeGroup(mChannel, actionListener); 156 if (!actionListener.check(ActionListenerTest.SUCCESS, TIMEOUT)) { 157 mReason = mContext.getString(R.string.p2p_remove_group_error); 158 return false; 159 } 160 161 notifyTestMsg(R.string.p2p_waiting_for_peer_to_disconnect); 162 163 /* 164 * Check if p2p disconnection broadcast is received 165 */ 166 p2pInfo = mReceiverTest.waitDisconnectionNotice(TIMEOUT); 167 if (p2pInfo == null) { 168 mReason = mContext.getString(R.string.p2p_connection_error); 169 return false; 170 } 171 172 /* Wait until peer gets marked disconnected */ 173 174 if (mReceiverTest.waitPeerDisconnected(mTargetAddress, TIMEOUT) != true) { 175 mReason = mContext.getString(R.string.p2p_detect_disconnection_error); 176 return false; 177 } 178 179 return true; 180 } 181 } 182