1 /*
2  * Copyright (C) 2019 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 static com.google.common.truth.Truth.assertWithMessage;
19 
20 import android.app.admin.DevicePolicyManager;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.location.LocationManager;
26 import android.util.Log;
27 
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30 
31 /**
32  * Test {@link DevicePolicyManager#setLocationEnabled}.
33  */
34 public final class SetLocationEnabledTest extends BaseDeviceOwnerTest {
35 
36     private static final String TAG = SetLocationEnabledTest.class.getSimpleName();
37 
38     private static final long TIMEOUT_MS = 5000;
39 
40     private static final boolean ENABLED = true;
41     private static final boolean DISABLED = false;
42 
43     private LocationManager mLocationManager;
44 
45     @Override
setUp()46     protected void setUp() throws Exception {
47         super.setUp();
48 
49         mLocationManager = mContext.getSystemService(LocationManager.class);
50     }
51 
testSetLocationEnabled()52     public void testSetLocationEnabled() throws Exception {
53         boolean enabled = mLocationManager.isLocationEnabled();
54         if (enabled) {
55             Log.d(TAG, "location is initially enabled for user " + mUserId);
56             runDisableLocationFirst();
57         } else {
58             Log.d(TAG, "location is initially disabled for user " + mUserId);
59             runEnableLocationFirst();
60         }
61     }
62 
runDisableLocationFirst()63     private void runDisableLocationFirst() throws Exception {
64         Log.v(TAG, "runDisableLocationFirst()");
65 
66         setLocationEnabledAndWaitIfNecessary(DISABLED, /* wait= */ !mIsAutomotive);
67         assertWithMessage("isLocationEnabled()").that(mLocationManager.isLocationEnabled())
68                 .isEqualTo(mIsAutomotive ? ENABLED : DISABLED);
69 
70         setLocationEnabledAndWaitIfNecessary(ENABLED, /* wait= */ !mIsAutomotive);
71         assertWithMessage("isLocationEnabled()").that(mLocationManager.isLocationEnabled())
72                 .isTrue();
73     }
74 
runEnableLocationFirst()75     private void runEnableLocationFirst() throws Exception {
76         Log.v(TAG, "runEnableLocationFirst()");
77 
78         setLocationEnabledAndWaitIfNecessary(ENABLED, /* wait= */ true);
79         assertWithMessage("isLocationEnabled()").that(mLocationManager.isLocationEnabled())
80                 .isTrue();
81 
82         setLocationEnabledAndWaitIfNecessary(DISABLED, /* wait= */ !mIsAutomotive);
83         assertWithMessage("isLocationEnabled()").that(mLocationManager.isLocationEnabled())
84                 .isEqualTo(mIsAutomotive ? ENABLED : DISABLED);
85     }
86 
setLocationEnabledAndWaitIfNecessary(boolean enabled, boolean wait)87     private void setLocationEnabledAndWaitIfNecessary(boolean enabled, boolean wait)
88             throws Exception {
89         if (!wait) {
90             Log.d(TAG, "setting location to " + enabled);
91             mDevicePolicyManager.setLocationEnabled(getWho(), enabled);
92             Log.d(TAG, "not waiting for " + LocationManager.MODE_CHANGED_ACTION + " intent");
93             return;
94         }
95 
96         CountDownLatch latch = new CountDownLatch(1);
97         BroadcastReceiver receiver = new BroadcastReceiver() {
98             @Override
99             public void onReceive(Context context, Intent intent) {
100                 boolean actualEnabled = intent
101                         .getBooleanExtra(LocationManager.EXTRA_LOCATION_ENABLED, enabled);
102                 Log.d(TAG, "received intent " + intent.getAction() + ": enabled=" + actualEnabled);
103                 if (actualEnabled != enabled) {
104                     Log.e(TAG, "Invalid value on extra " + LocationManager.EXTRA_LOCATION_ENABLED
105                             + ": " + actualEnabled);
106                     return;
107                 }
108                 latch.countDown();
109             }
110         };
111         mContext.registerReceiver(receiver, new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
112         try {
113             Log.d(TAG, "setting location to " + enabled);
114             mDevicePolicyManager.setLocationEnabled(getWho(), enabled);
115             Log.d(TAG, "Waiting for " + LocationManager.MODE_CHANGED_ACTION + " intent");
116             assertWithMessage("%s intent reveiced in %sms", LocationManager.MODE_CHANGED_ACTION,
117                     TIMEOUT_MS).that(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
118         } finally {
119             mContext.unregisterReceiver(receiver);
120         }
121     }
122 }
123