• 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.deviceowner;
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 BaseDeviceOwnerTest {
26     public static final String EXAMPLE_ACTION = "com.android.cts.deviceowner.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(getWho(), PACKAGE_NAME);
52         mContext.unregisterReceiver(mReceiver);
53 
54         super.tearDown();
55     }
56 
testNoPersistentPreferredActivityYieldsResolverActivity()57     public void testNoPersistentPreferredActivityYieldsResolverActivity() {
58         sendExampleIntent();
59         SystemClock.sleep(5000);
60 
61         // Default behavior: intent results in resolver activity, since there are two potential
62         // receivers. No intent is received.
63         synchronized(this) {
64             assertFalse(mReceivedConfirmationFrom1);
65             assertFalse(mReceivedConfirmationFrom2);
66         }
67     }
68 
testAddPersistentPreferredActivityYieldsReceptionAtTarget()69     public void testAddPersistentPreferredActivityYieldsReceptionAtTarget() {
70         addPersistentPreferredActivity();
71         sendExampleIntent();
72         SystemClock.sleep(5000);
73 
74         // Persistent preferred activity present: intent should be received by activity 2.
75         synchronized(this) {
76             assertFalse(mReceivedConfirmationFrom1);
77             assertTrue(mReceivedConfirmationFrom2);
78         }
79     }
80 
testAddAndClearPersistentPreferredActivitiesYieldsResolverActivity()81     public void testAddAndClearPersistentPreferredActivitiesYieldsResolverActivity() {
82         addPersistentPreferredActivity();
83         mDevicePolicyManager.clearPackagePersistentPreferredActivities(getWho(), PACKAGE_NAME);
84 
85         sendExampleIntent();
86         SystemClock.sleep(5000);
87 
88         // Default behavior: intent results in resolver activity, since there are two potential
89         // receivers. No intent is received.
90         synchronized(this) {
91             assertFalse(mReceivedConfirmationFrom1);
92             assertFalse(mReceivedConfirmationFrom2);
93         }
94     }
95 
96     public class ConfirmReceiver extends BroadcastReceiver {
97         @Override
onReceive(Context context, Intent intent)98         public void onReceive(Context context, Intent intent) {
99             if (intent.getAction().equals(ExampleIntentReceivingActivity1.CONFIRM_ACTION)) {
100                 synchronized (PersistentIntentResolvingTest.this) {
101                     mReceivedConfirmationFrom1 = true;
102                 }
103             } else if (intent.getAction().equals(ExampleIntentReceivingActivity2
104                             .CONFIRM_ACTION)) {
105                 synchronized (PersistentIntentResolvingTest.this) {
106                     mReceivedConfirmationFrom2 = true;
107                 }
108             }
109         }
110     }
111 
sendExampleIntent()112     private void sendExampleIntent() {
113         Intent exampleIntent = new Intent(EXAMPLE_ACTION);
114         exampleIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
115         mContext.startActivity(exampleIntent);
116     }
117 
addPersistentPreferredActivity()118     private void addPersistentPreferredActivity() {
119         IntentFilter filter = new IntentFilter();
120         filter.addAction(EXAMPLE_ACTION);
121         filter.addCategory(Intent.CATEGORY_DEFAULT);
122         ComponentName targetComponent = new ComponentName(PACKAGE_NAME,
123                 ExampleIntentReceivingActivity2.class.getName());
124         mDevicePolicyManager.addPersistentPreferredActivity(getWho(), filter, targetComponent);
125     }
126 }
127