Home | History | Annotate | Download | only in cts
      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 
     17 package android.permission.cts;
     18 
     19 import android.content.Intent;
     20 import android.os.Bundle;
     21 import android.test.AndroidTestCase;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 
     24 /**
     25  * Verify Context related methods without specific BROADCAST series permissions.
     26  */
     27 public class NoBroadcastPackageRemovedPermissionTest extends AndroidTestCase {
     28     private static final String TEST_RECEIVER_PERMISSION = "receiverPermission";
     29 
     30     /**
     31      * Verify that Context#sendStickyBroadcast(Intent),
     32      * Context#removeStickyBroadcast(Intent)
     33      * requires permissions.
     34      * <p>Requires Permission:
     35      *   {@link android.Manifest.permission#BROADCAST_STICKY }.
     36      */
     37     @SmallTest
     38     public void testSendOrRemoveStickyBroadcast() {
     39         try {
     40             mContext.sendStickyBroadcast(createIntent(Intent.ACTION_WALLPAPER_CHANGED));
     41             fail("Context.sendStickyBroadcast did not throw SecurityException as expected");
     42         } catch (SecurityException e) {
     43             // expected
     44         }
     45 
     46         try {
     47             mContext.removeStickyBroadcast(createIntent(Intent.ACTION_WALLPAPER_CHANGED));
     48             fail("Context.removeStickyBroadcast did not throw SecurityException as expected");
     49         } catch (SecurityException e) {
     50             // expected
     51         }
     52     }
     53 
     54     /**
     55      * Verify that Context#sendBroadcast(Intent),
     56      * Context#sendBroadcast(Intent, String)
     57      * Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver,
     58      *                              Handler, int, String, Bundle)
     59      * Context#sendOrderedBroadcast(Intent, String) with ACTION_UID_REMOVED
     60      * with ACTION_PACKAGE_REMOVED requires permissions.
     61      * <p>Requires Permission:
     62      *   {@link android.Manifest.permission#BROADCAST_PACKAGE_REMOVED}.
     63      */
     64     @SmallTest
     65     public void testSendBroadcast() {
     66         try {
     67             mContext.sendBroadcast(createIntent(Intent.ACTION_PACKAGE_REMOVED));
     68             fail("Context.sendBroadcast did not throw SecurityException as expected");
     69         } catch (SecurityException e) {
     70             // expected
     71         }
     72 
     73         try {
     74             mContext.sendBroadcast(createIntent(Intent.ACTION_PACKAGE_REMOVED),
     75                     TEST_RECEIVER_PERMISSION);
     76             fail("Context.sendBroadcast did not throw SecurityException as expected");
     77         } catch (SecurityException e) {
     78             // expected
     79         }
     80 
     81         try {
     82             mContext.sendOrderedBroadcast(createIntent(Intent.ACTION_PACKAGE_REMOVED),
     83                     TEST_RECEIVER_PERMISSION, null, null, 0, "initialData", Bundle.EMPTY);
     84             fail("Context.sendOrderedBroadcast did not throw SecurityException as expected");
     85         } catch (SecurityException e) {
     86             // expected
     87         }
     88 
     89         try {
     90             mContext.sendOrderedBroadcast(createIntent(Intent.ACTION_PACKAGE_REMOVED),
     91                     TEST_RECEIVER_PERMISSION);
     92             fail("Context.sendOrderedBroadcast did not throw SecurityException as expected");
     93         } catch (SecurityException e) {
     94             // expected
     95         }
     96     }
     97 
     98     private Intent createIntent(String action) {
     99         Intent intent = new Intent();
    100         intent.setAction(action);
    101         return intent;
    102     }
    103 }
    104