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