1 /* 2 * Copyright (C) 2006 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.app.activity; 18 19 import android.app.Activity; 20 import android.app.PendingIntent; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.os.Bundle; 24 25 import androidx.test.filters.LargeTest; 26 27 @LargeTest 28 public class IntentSenderTest extends BroadcastTest { 29 testRegisteredReceivePermissionGranted()30 public void testRegisteredReceivePermissionGranted() throws Exception { 31 setExpectedReceivers(new String[]{RECEIVER_REG}); 32 registerMyReceiver(new IntentFilter(BROADCAST_REGISTERED), PERMISSION_GRANTED); 33 addIntermediate("after-register"); 34 PendingIntent is = PendingIntent.getBroadcast(getContext(), 0, 35 makeBroadcastIntent(BROADCAST_REGISTERED), 0); 36 is.send(); 37 waitForResultOrThrow(BROADCAST_TIMEOUT); 38 is.cancel(); 39 } 40 testRegisteredReceivePermissionDenied()41 public void testRegisteredReceivePermissionDenied() throws Exception { 42 final Intent intent = makeBroadcastIntent(BROADCAST_REGISTERED); 43 44 setExpectedReceivers(new String[]{RECEIVER_RESULTS}); 45 registerMyReceiver(new IntentFilter(BROADCAST_REGISTERED), PERMISSION_DENIED); 46 addIntermediate("after-register"); 47 48 PendingIntent.OnFinished finish = new PendingIntent.OnFinished() { 49 public void onSendFinished(PendingIntent pi, Intent intent, 50 int resultCode, String resultData, Bundle resultExtras) { 51 gotReceive(RECEIVER_RESULTS, intent); 52 } 53 }; 54 55 PendingIntent is = PendingIntent.getBroadcast(getContext(), 0, intent, 0); 56 is.send(Activity.RESULT_CANCELED, finish, null); 57 waitForResultOrThrow(BROADCAST_TIMEOUT); 58 is.cancel(); 59 } 60 testLocalReceivePermissionGranted()61 public void testLocalReceivePermissionGranted() throws Exception { 62 setExpectedReceivers(new String[]{RECEIVER_LOCAL}); 63 PendingIntent is = PendingIntent.getBroadcast(getContext(), 0, 64 makeBroadcastIntent(BROADCAST_LOCAL_GRANTED), 0); 65 is.send(); 66 waitForResultOrThrow(BROADCAST_TIMEOUT); 67 is.cancel(); 68 } 69 testLocalReceivePermissionDenied()70 public void testLocalReceivePermissionDenied() throws Exception { 71 final Intent intent = makeBroadcastIntent(BROADCAST_LOCAL_DENIED); 72 73 setExpectedReceivers(new String[]{RECEIVER_RESULTS}); 74 75 PendingIntent.OnFinished finish = new PendingIntent.OnFinished() { 76 public void onSendFinished(PendingIntent pi, Intent intent, 77 int resultCode, String resultData, Bundle resultExtras) { 78 gotReceive(RECEIVER_RESULTS, intent); 79 } 80 }; 81 82 PendingIntent is = PendingIntent.getBroadcast(getContext(), 0, intent, 0); 83 is.send(Activity.RESULT_CANCELED, finish, null); 84 waitForResultOrThrow(BROADCAST_TIMEOUT); 85 is.cancel(); 86 } 87 } 88