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.BroadcastReceiver;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.IntentFilter;
     22 import android.net.wifi.p2p.WifiP2pInfo;
     23 import android.net.wifi.p2p.WifiP2pManager;
     24 
     25 /**
     26  * Test case for go negotiation response.
     27  *
     28  * The requester devices tries to connect this device.
     29  */
     30 public class GoNegRespTestCase extends TestCase {
     31 
     32     private final IntentFilter mIntentFilter = new IntentFilter();
     33     private WifiP2pBroadcastReceiver mReceiver;
     34 
     35     public GoNegRespTestCase(Context context) {
     36         super(context);
     37         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
     38         mReceiver = new WifiP2pBroadcastReceiver();
     39     }
     40 
     41     @Override
     42     protected void setUp() {
     43         mContext.registerReceiver(mReceiver, mIntentFilter);
     44         super.setUp();
     45     }
     46 
     47     @Override
     48     protected boolean executeTest() throws InterruptedException {
     49 
     50         mP2pMgr.discoverPeers(mChannel, null);
     51 
     52         // wait until p2p device is trying go negotiation.
     53         return true;
     54     }
     55 
     56 
     57     @Override
     58     protected void tearDown() {
     59         // wait until p2p device is trying go negotiation.
     60         synchronized(this) {
     61             try {
     62                 wait();
     63             } catch (InterruptedException e) {
     64                 e.printStackTrace();
     65             }
     66         }
     67         if (mP2pMgr != null) {
     68             mP2pMgr.cancelConnect(mChannel, null);
     69             mP2pMgr.removeGroup(mChannel, null);
     70         }
     71         mContext.unregisterReceiver(mReceiver);
     72 
     73         super.tearDown();
     74     }
     75 
     76 
     77     @Override
     78     public String getTestName() {
     79         return "Go negotiation responder test";
     80     }
     81 
     82     private class WifiP2pBroadcastReceiver extends BroadcastReceiver {
     83         @Override
     84         public void onReceive(Context context, Intent intent) {
     85             String action = intent.getAction();
     86             if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
     87                 synchronized(this) {
     88                     WifiP2pInfo p2pInfo = (WifiP2pInfo)intent.getParcelableExtra(
     89                             WifiP2pManager.EXTRA_WIFI_P2P_INFO);
     90                     if (p2pInfo.groupFormed && !p2pInfo.isGroupOwner) {
     91                         /*
     92                          * Remove p2p group for next test once your device became p2p client.
     93                          * In the case of GO, p2p group will be removed automatically because
     94                          * target device will cut the connection.
     95                          */
     96                         mP2pMgr.removeGroup(mChannel, null);
     97                     } else if (!p2pInfo.groupFormed) {
     98                         /*
     99                          * find again.
    100                          */
    101                         mP2pMgr.discoverPeers(mChannel, null);
    102                     }
    103                 }
    104             }
    105         }
    106     }
    107 }
    108