1 /*
2  * Copyright (C) 2015 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 com.android.cts.managedprofile;
18 
19 import android.appwidget.AppWidgetManager;
20 import android.appwidget.AppWidgetProviderInfo;
21 import android.content.BroadcastReceiver;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.os.Process;
27 import android.os.UserHandle;
28 import android.util.Log;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.concurrent.ArrayBlockingQueue;
33 import java.util.concurrent.BlockingQueue;
34 import java.util.concurrent.TimeUnit;
35 
36 /**
37  * This class contains tests for cross profile widget providers that are run on the managed
38  * profile. Policies are set using {@link SetPolicyActivity} and then verified in these tests.
39  * The tests cannot be run independently, but are part of one hostside test.
40  */
41 public class CrossProfileWidgetTest extends BaseManagedProfileTest {
42     static final String WIDGET_PROVIDER_PKG = "com.android.cts.widgetprovider";
43 
44     private AppWidgetManager mAppWidgetManager;
45 
setUp()46     public void setUp() throws Exception {
47         super.setUp();
48         mAppWidgetManager = (AppWidgetManager) mContext.getSystemService(Context.APPWIDGET_SERVICE);
49     }
50 
51     /**
52      * This test checks that the widget provider was successfully whitelisted and verifies that
53      * if was added successfully and can be found inside the profile.
54      */
testCrossProfileWidgetProviderAdded()55     public void testCrossProfileWidgetProviderAdded() {
56         List<String> providers = mDevicePolicyManager.getCrossProfileWidgetProviders(
57                 ADMIN_RECEIVER_COMPONENT);
58         assertEquals(1, providers.size());
59         assertTrue(providers.contains(WIDGET_PROVIDER_PKG));
60         // check that widget can be found inside the profile
61         assertTrue(containsWidgetProviderPkg(mAppWidgetManager.getInstalledProviders()));
62     }
63 
64     /**
65      * This test verifies that the widget provider was successfully removed from the whitelist.
66      */
testCrossProfileWidgetProviderRemoved()67     public void testCrossProfileWidgetProviderRemoved() {
68         List<String> providers = mDevicePolicyManager.getCrossProfileWidgetProviders(
69                 ADMIN_RECEIVER_COMPONENT);
70         assertTrue(providers.isEmpty());
71         // check that widget can still be found inside the profile
72         // This does not currently work correctly: http://b/issues/21180997
73         // assertTrue(containsWidgetProviderPkg(mAppWidgetManager.getInstalledProviders()));
74     }
75 
containsWidgetProviderPkg(List<AppWidgetProviderInfo> widgets)76     private boolean containsWidgetProviderPkg(List<AppWidgetProviderInfo> widgets) {
77         for (AppWidgetProviderInfo widget : widgets) {
78             if (WIDGET_PROVIDER_PKG.equals(widget.provider.getPackageName())) {
79                 return true;
80             }
81         }
82         return false;
83     }
84 }
85