Home | History | Annotate | Download | only in cts
      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 android.net.wifi.cts;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.net.wifi.WifiManager;
     24 import android.net.wifi.p2p.WifiP2pManager;
     25 import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_DISABLED;
     26 import static android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED;
     27 import android.test.AndroidTestCase;
     28 
     29 public class ConcurrencyTest extends AndroidTestCase {
     30     private class MySync {
     31         int expectedWifiState;
     32         int expectedP2pState;
     33     }
     34 
     35     private WifiManager mWifiManager;
     36     private MySync mMySync = new MySync();
     37 
     38     private static final String TAG = "WifiInfoTest";
     39     private static final int TIMEOUT_MSEC = 6000;
     40     private static final int WAIT_MSEC = 60;
     41     private static final int DURATION = 10000;
     42     private IntentFilter mIntentFilter;
     43     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
     44         @Override
     45         public void onReceive(Context context, Intent intent) {
     46             final String action = intent.getAction();
     47             if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
     48                 synchronized (mMySync) {
     49                     mMySync.expectedWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
     50                             WifiManager.WIFI_STATE_DISABLED);
     51                     mMySync.notify();
     52                 }
     53             } else if(action.equals(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION)) {
     54                 synchronized (mMySync) {
     55                     mMySync.expectedP2pState = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,
     56                             WifiP2pManager.WIFI_P2P_STATE_DISABLED);
     57                     mMySync.notify();
     58                 }
     59             }
     60         }
     61     };
     62 
     63     @Override
     64     protected void setUp() throws Exception {
     65        super.setUp();
     66        if (!WifiFeature.isWifiSupported(getContext()) &&
     67                 !WifiFeature.isP2pSupported(getContext())) {
     68             // skip the test if WiFi && p2p are not supported
     69             return;
     70         }
     71         mIntentFilter = new IntentFilter();
     72         mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
     73         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
     74 
     75         mContext.registerReceiver(mReceiver, mIntentFilter);
     76         mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
     77         assertNotNull(mWifiManager);
     78         if (mWifiManager.isWifiEnabled()) {
     79             assertTrue(mWifiManager.setWifiEnabled(false));
     80             Thread.sleep(DURATION);
     81         }
     82         assertTrue(!mWifiManager.isWifiEnabled());
     83         mMySync.expectedWifiState = WifiManager.WIFI_STATE_DISABLED;
     84         mMySync.expectedP2pState = WifiP2pManager.WIFI_P2P_STATE_DISABLED;
     85     }
     86 
     87     @Override
     88     protected void tearDown() throws Exception {
     89         if (!WifiFeature.isWifiSupported(getContext()) &&
     90                 !WifiFeature.isP2pSupported(getContext())) {
     91             // skip the test if WiFi and p2p are not supported
     92             super.tearDown();
     93             return;
     94         }
     95         mContext.unregisterReceiver(mReceiver);
     96 
     97         if (mWifiManager.isWifiEnabled()) {
     98             assertTrue(mWifiManager.setWifiEnabled(false));
     99             Thread.sleep(DURATION);
    100         }
    101         super.tearDown();
    102     }
    103 
    104     private void waitForBroadcasts() {
    105         synchronized (mMySync) {
    106             long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
    107             while (System.currentTimeMillis() < timeout
    108                     && (mMySync.expectedWifiState != WifiManager.WIFI_STATE_ENABLED ||
    109                     mMySync.expectedP2pState != WifiP2pManager.WIFI_P2P_STATE_ENABLED)) {
    110                 try {
    111                     mMySync.wait(WAIT_MSEC);
    112                 } catch (InterruptedException e) { }
    113             }
    114         }
    115     }
    116 
    117     public void testConcurrency() {
    118         // Cannot support p2p alone
    119         if (!WifiFeature.isWifiSupported(getContext())) {
    120             assertTrue(!WifiFeature.isP2pSupported(getContext()));
    121             return;
    122         }
    123 
    124         if (!WifiFeature.isP2pSupported(getContext())) {
    125             // skip the test if p2p is not supported
    126             return;
    127         }
    128 
    129         // Enable wifi
    130         assertTrue(mWifiManager.setWifiEnabled(true));
    131 
    132         waitForBroadcasts();
    133 
    134         assertTrue(mMySync.expectedWifiState == WifiManager.WIFI_STATE_ENABLED);
    135         assertTrue(mMySync.expectedP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED);
    136     }
    137 
    138 }
    139