Test that appOps added to notifs from mainThread
Test: atest ForegroundServiceControllerTest
Fixes: 144170894
Change-Id: If9ae7dc81e1b853b2904821baf23eaf55b289fc6
diff --git a/packages/SystemUI/src/com/android/systemui/ForegroundServiceController.java b/packages/SystemUI/src/com/android/systemui/ForegroundServiceController.java
index 15a5c27..c1a23c8 100644
--- a/packages/SystemUI/src/com/android/systemui/ForegroundServiceController.java
+++ b/packages/SystemUI/src/com/android/systemui/ForegroundServiceController.java
@@ -27,6 +27,7 @@
import com.android.systemui.dagger.qualifiers.MainHandler;
import com.android.systemui.statusbar.notification.NotificationEntryManager;
import com.android.systemui.statusbar.notification.collection.NotificationEntry;
+import com.android.systemui.util.Assert;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -121,6 +122,8 @@
* @param active whether the appOpCode is active or not
*/
void onAppOpChanged(int appOpCode, int uid, String packageName, boolean active) {
+ Assert.isMainThread();
+
int userId = UserHandle.getUserId(uid);
// Record active app ops
synchronized (mMutex) {
diff --git a/packages/SystemUI/tests/src/com/android/systemui/ForegroundServiceControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/ForegroundServiceControllerTest.java
index 02a3766..0990e22 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/ForegroundServiceControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/ForegroundServiceControllerTest.java
@@ -18,6 +18,7 @@
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
+import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -34,12 +35,14 @@
import android.app.NotificationManager;
import android.os.Bundle;
import android.os.Handler;
+import android.os.Looper;
import android.os.UserHandle;
import android.service.notification.StatusBarNotification;
+import android.testing.AndroidTestingRunner;
+import android.testing.TestableLooper;
import android.widget.RemoteViews;
import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
import com.android.internal.messages.nano.SystemMessageProto;
import com.android.systemui.appops.AppOpsController;
@@ -58,7 +61,8 @@
import org.mockito.MockitoAnnotations;
@SmallTest
-@RunWith(AndroidJUnit4.class)
+@RunWith(AndroidTestingRunner.class)
+@TestableLooper.RunWithLooper
public class ForegroundServiceControllerTest extends SysuiTestCase {
private ForegroundServiceController mFsc;
private ForegroundServiceNotificationListener mListener;
@@ -69,6 +73,9 @@
@Before
public void setUp() throws Exception {
+ // assume the TestLooper is the main looper for these tests
+ com.android.systemui.util.Assert.sMainLooper = TestableLooper.get(this).getLooper();
+
MockitoAnnotations.initMocks(this);
mFsc = new ForegroundServiceController(mEntryManager, mAppOpsController, mMainHandler);
mListener = new ForegroundServiceNotificationListener(
@@ -81,6 +88,26 @@
}
@Test
+ public void testAppOpsChangedCalledFromBgThread() {
+ try {
+ // WHEN onAppOpChanged is called from a different thread than the MainLooper
+ com.android.systemui.util.Assert.sMainLooper = Looper.getMainLooper();
+ NotificationEntry entry = createFgEntry();
+ mFsc.onAppOpChanged(
+ AppOpsManager.OP_CAMERA,
+ entry.getSbn().getUid(),
+ entry.getSbn().getPackageName(),
+ true);
+
+ // This test is run on the TestableLooper, which is not the MainLooper, so
+ // we expect an exception to be thrown
+ fail("onAppOpChanged shouldn't be allowed to be called from a bg thread.");
+ } catch (IllegalStateException e) {
+ // THEN expect an exception
+ }
+ }
+
+ @Test
public void testAppOps_appOpChangedBeforeNotificationExists() {
// GIVEN app op exists, but notification doesn't exist in NEM yet
NotificationEntry entry = createFgEntry();