1 /* 2 * Copyright (C) 2008 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.Context; 20 import android.net.wifi.WifiManager; 21 import android.net.wifi.WifiManager.WifiLock; 22 import android.test.AndroidTestCase; 23 24 public class WifiManager_WifiLockTest extends AndroidTestCase { 25 26 private static final String WIFI_TAG = "WifiManager_WifiLockTest"; 27 28 public void testWifiLock() { 29 if (!WifiFeature.isWifiSupported(getContext())) { 30 // skip the test if WiFi is not supported 31 return; 32 } 33 WifiManager wm = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); 34 WifiLock wl = wm.createWifiLock(WIFI_TAG); 35 36 wl.setReferenceCounted(true); 37 assertFalse(wl.isHeld()); 38 wl.acquire(); 39 assertTrue(wl.isHeld()); 40 wl.release(); 41 assertFalse(wl.isHeld()); 42 wl.acquire(); 43 wl.acquire(); 44 assertTrue(wl.isHeld()); 45 wl.release(); 46 assertTrue(wl.isHeld()); 47 wl.release(); 48 assertFalse(wl.isHeld()); 49 assertNotNull(wl.toString()); 50 try { 51 wl.release(); 52 fail("should throw out exception because release is called" 53 +" a greater number of times than acquire"); 54 } catch (RuntimeException e) { 55 // expected 56 } 57 58 wl = wm.createWifiLock(WIFI_TAG); 59 wl.setReferenceCounted(false); 60 assertFalse(wl.isHeld()); 61 wl.acquire(); 62 assertTrue(wl.isHeld()); 63 wl.release(); 64 assertFalse(wl.isHeld()); 65 wl.acquire(); 66 wl.acquire(); 67 assertTrue(wl.isHeld()); 68 wl.release(); 69 assertFalse(wl.isHeld()); 70 assertNotNull(wl.toString()); 71 // should be ignored 72 wl.release(); 73 } 74 } 75