Add appops testcase for location disabled

Bug: 231496105
Test: this test case
Change-Id: I084aa0ceabeee99b71969ba948068f8eb8a418f5
diff --git a/tests/location/location_none/src/android/location/cts/none/LocationDisabledAppOpsTest.java b/tests/location/location_none/src/android/location/cts/none/LocationDisabledAppOpsTest.java
new file mode 100644
index 0000000..990d1de
--- /dev/null
+++ b/tests/location/location_none/src/android/location/cts/none/LocationDisabledAppOpsTest.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.location.cts.none;
+
+import static android.app.AppOpsManager.MODE_ALLOWED;
+import static android.app.AppOpsManager.OPSTR_FINE_LOCATION;
+
+import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
+
+import android.app.ActivityManager;
+import android.app.AppOpsManager;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.location.LocationManager;
+import android.os.PackageTagsList;
+import android.os.Process;
+import android.os.UserHandle;
+
+import androidx.test.InstrumentationRegistry;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class LocationDisabledAppOpsTest {
+
+    private final Context mContext = InstrumentationRegistry.getContext();
+    private LocationManager mLm;
+    private AppOpsManager mAom;
+
+    @Before
+    public void setUp() {
+        mLm = mContext.getSystemService(LocationManager.class);
+        mAom = mContext.getSystemService(AppOpsManager.class);
+    }
+
+    @Test
+    public void testLocationAppOpIsIgnoredForAppsWhenLocationIsDisabled() {
+        PackageTagsList ignoreList = mLm.getIgnoreSettingsAllowlist();
+
+        UserHandle[] userArr = {UserHandle.SYSTEM};
+        runWithShellPermissionIdentity(() -> {
+            userArr[0] = UserHandle.of(ActivityManager.getCurrentUser());
+        });
+
+        UserHandle user = userArr[0];
+
+        boolean wasEnabled = mLm.isLocationEnabledForUser(user);
+
+        try {
+            runWithShellPermissionIdentity(() -> {
+                mLm.setLocationEnabledForUser(false, user);
+            });
+
+            List<String> bypassedNoteOps = new ArrayList<>();
+            List<String> bypassedCheckOps = new ArrayList<>();
+            for (PackageInfo pi : mContext.getPackageManager().getInstalledPackagesAsUser(
+                            0, user.getIdentifier())) {
+                ApplicationInfo ai = pi.applicationInfo;
+                if (ai.uid != Process.SYSTEM_UID) {
+                    final int[] mode = {MODE_ALLOWED};
+                    runWithShellPermissionIdentity(() -> {
+                        mode[0] = mAom.noteOpNoThrow(
+                                OPSTR_FINE_LOCATION, ai.uid, ai.packageName);
+                    });
+                    if (mode[0] == MODE_ALLOWED && !ignoreList.containsAll(pi.packageName)) {
+                        bypassedNoteOps.add(pi.packageName);
+                    }
+
+
+                    mode[0] = MODE_ALLOWED;
+                    runWithShellPermissionIdentity(() -> {
+                        mode[0] = mAom.checkOpNoThrow(OPSTR_FINE_LOCATION, ai.uid, ai.packageName);
+                    });
+                    if (mode[0] == MODE_ALLOWED && !ignoreList.includes(pi.packageName)) {
+                        bypassedCheckOps.add(pi.packageName);
+                    }
+
+                }
+            }
+
+            String msg = "";
+            if (!bypassedNoteOps.isEmpty()) {
+                msg += "Apps which still have access from noteOp " + bypassedNoteOps;
+            }
+            if (!bypassedCheckOps.isEmpty()) {
+                msg += (msg.isEmpty() ? "" : "\n\n")
+                        +  "Apps which still have access from checkOp " + bypassedCheckOps;
+            }
+            if(!msg.isEmpty()) {
+                Assert.fail(msg);
+            }
+
+        } finally {
+            runWithShellPermissionIdentity(() -> {
+                mLm.setLocationEnabledForUser(wasEnabled, user);
+            });
+        }
+    }
+
+}