1/* 2 * Copyright (C) 2009 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 17package com.android.framework.permission.tests; 18 19import junit.framework.TestCase; 20 21import android.os.Binder; 22import android.os.IVibratorService; 23import android.os.RemoteException; 24import android.os.ServiceManager; 25import android.test.suitebuilder.annotation.SmallTest; 26 27/** 28 * Verify that Hardware apis cannot be called without required permissions. 29 */ 30@SmallTest 31public class VibratorServicePermissionTest extends TestCase { 32 33 private IVibratorService mVibratorService; 34 35 @Override 36 protected void setUp() throws Exception { 37 mVibratorService = IVibratorService.Stub.asInterface( 38 ServiceManager.getService("vibrator")); 39 } 40 41 /** 42 * Test that calling {@link android.os.IVibratorService#vibrate(long)} requires permissions. 43 * <p>Tests permission: 44 * {@link android.Manifest.permission#VIBRATE} 45 * @throws RemoteException 46 */ 47 public void testVibrate() throws RemoteException { 48 try { 49 mVibratorService.vibrate(2000, new Binder()); 50 fail("vibrate did not throw SecurityException as expected"); 51 } catch (SecurityException e) { 52 // expected 53 } 54 } 55 56 /** 57 * Test that calling {@link android.os.IVibratorService#vibratePattern(long[], 58 * int, android.os.IBinder)} requires permissions. 59 * <p>Tests permission: 60 * {@link android.Manifest.permission#VIBRATE} 61 * @throws RemoteException 62 */ 63 public void testVibratePattern() throws RemoteException { 64 try { 65 mVibratorService.vibratePattern(new long[] {0}, 0, new Binder()); 66 fail("vibratePattern did not throw SecurityException as expected"); 67 } catch (SecurityException e) { 68 // expected 69 } 70 } 71 72 /** 73 * Test that calling {@link android.os.IVibratorService#cancelVibrate()} requires permissions. 74 * <p>Tests permission: 75 * {@link android.Manifest.permission#VIBRATE} 76 * @throws RemoteException 77 */ 78 public void testCancelVibrate() throws RemoteException { 79 try { 80 mVibratorService.cancelVibrate(new Binder()); 81 fail("cancelVibrate did not throw SecurityException as expected"); 82 } catch (SecurityException e) { 83 // expected 84 } 85 } 86} 87