• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 package com.android.cts.deviceandprofileowner;
17 
18 import android.content.BroadcastReceiver;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.SystemClock;
24 
25 public class PersistentIntentResolvingTest extends BaseDeviceAdminTest {
26     static final String EXAMPLE_ACTION = "com.android.cts.deviceandprofileowner.EXAMPLE_ACTION";
27 
28     private boolean mReceivedConfirmationFrom1;
29     private boolean mReceivedConfirmationFrom2;
30     private BroadcastReceiver mReceiver;
31 
32     @Override
setUp()33     protected void setUp() throws Exception {
34         super.setUp();
35 
36         IntentFilter filter = new IntentFilter();
37         filter.addAction(ExampleIntentReceivingActivity1.CONFIRM_ACTION);
38         filter.addAction(ExampleIntentReceivingActivity2.CONFIRM_ACTION);
39 
40         mReceiver = new ConfirmReceiver();
41         mContext.registerReceiver(mReceiver, filter);
42 
43         synchronized(this) {
44             mReceivedConfirmationFrom1 = false;
45             mReceivedConfirmationFrom2 = false;
46         }
47     }
48 
49     @Override
tearDown()50     protected void tearDown() throws Exception {
51         mDevicePolicyManager.clearPackagePersistentPreferredActivities(ADMIN_RECEIVER_COMPONENT,
52                 PACKAGE_NAME);
53         mContext.unregisterReceiver(mReceiver);
54 
55         super.tearDown();
56     }
57 
testNoPersistentPreferredActivityYieldsResolverActivity()58     public void testNoPersistentPreferredActivityYieldsResolverActivity() {
59         sendExampleIntent();
60         SystemClock.sleep(5000);
61 
62         // Default behavior: intent results in resolver activity, since there are two potential
63         // receivers. No intent is received.
64         synchronized(this) {
65             assertFalse(mReceivedConfirmationFrom1);
66             assertFalse(mReceivedConfirmationFrom2);
67         }
68     }
69 
testAddPersistentPreferredActivityYieldsReceptionAtTarget()70     public void testAddPersistentPreferredActivityYieldsReceptionAtTarget() {
71         addPersistentPreferredActivity();
72         sendExampleIntent();
73         SystemClock.sleep(5000);
74 
75         // Persistent preferred activity present: intent should be received by activity 2.
76         synchronized(this) {
77             assertFalse(mReceivedConfirmationFrom1);
78             assertTrue(mReceivedConfirmationFrom2);
79         }
80     }
81 
testAddAndClearPersistentPreferredActivitiesYieldsResolverActivity()82     public void testAddAndClearPersistentPreferredActivitiesYieldsResolverActivity() {
83         addPersistentPreferredActivity();
84         mDevicePolicyManager.clearPackagePersistentPreferredActivities(ADMIN_RECEIVER_COMPONENT,
85                 PACKAGE_NAME);
86 
87         sendExampleIntent();
88         SystemClock.sleep(5000);
89 
90         // Default behavior: intent results in resolver activity, since there are two potential
91         // receivers. No intent is received.
92         synchronized(this) {
93             assertFalse(mReceivedConfirmationFrom1);
94             assertFalse(mReceivedConfirmationFrom2);
95         }
96     }
97 
98     public class ConfirmReceiver extends BroadcastReceiver {
99         @Override
onReceive(Context context, Intent intent)100         public void onReceive(Context context, Intent intent) {
101             if (intent.getAction().equals(ExampleIntentReceivingActivity1.CONFIRM_ACTION)) {
102                 synchronized (PersistentIntentResolvingTest.this) {
103                     mReceivedConfirmationFrom1 = true;
104                 }
105             } else if (intent.getAction().equals(ExampleIntentReceivingActivity2
106                             .CONFIRM_ACTION)) {
107                 synchronized (PersistentIntentResolvingTest.this) {
108                     mReceivedConfirmationFrom2 = true;
109                 }
110             }
111         }
112     }
113 
sendExampleIntent()114     private void sendExampleIntent() {
115         Intent exampleIntent = new Intent(EXAMPLE_ACTION);
116         exampleIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
117         mContext.startActivity(exampleIntent);
118     }
119 
addPersistentPreferredActivity()120     private void addPersistentPreferredActivity() {
121         IntentFilter filter = new IntentFilter();
122         filter.addAction(EXAMPLE_ACTION);
123         filter.addCategory(Intent.CATEGORY_DEFAULT);
124         ComponentName targetComponent = new ComponentName(PACKAGE_NAME,
125                 ExampleIntentReceivingActivity2.class.getName());
126         mDevicePolicyManager.addPersistentPreferredActivity(ADMIN_RECEIVER_COMPONENT, filter,
127                 targetComponent);
128     }
129 }
130