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