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