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.appwidget.AppWidgetManager; 20 import android.content.ComponentName; 21 import android.content.pm.PackageManager; 22 import android.test.AndroidTestCase; 23 import android.test.suitebuilder.annotation.SmallTest; 24 25 /** 26 * Test that protected AppWidgetManager APIs cannot be called without permissions 27 */ 28 public class AppWidgetManagerPermissionTest extends AndroidTestCase { 29 30 private AppWidgetManager mAppWidgetManager = null; 31 32 @Override 33 protected void setUp() throws Exception { 34 super.setUp(); 35 mAppWidgetManager = AppWidgetManager.getInstance(getContext()); 36 } 37 38 /** 39 * Verify that calling 40 * {@link AppWidgetManager#bindAppWidgetId(int, android.content.ComponentName)} 41 * requires permissions. 42 * <p>Tests Permission: 43 * {@link android.Manifest.permission#BIND_APP_WIDGET}. 44 */ 45 @SmallTest 46 public void testBindAppWidget() { 47 if (!getContext().getPackageManager() 48 .hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS)) { 49 return; 50 } 51 assertNotNull(mAppWidgetManager); 52 53 try { 54 final boolean bound = mAppWidgetManager.bindAppWidgetIdIfAllowed(1, 55 new ComponentName(mContext, "foo")); 56 assertFalse("Was able to call bindAppWidgetId", bound); 57 } catch (SecurityException e) { 58 // expected 59 } 60 } 61 } 62