From 596d6dd99b89538911d067a3f26f74d8e76016cf Mon Sep 17 00:00:00 2001 From: Fan Wu Date: Thu, 11 Apr 2024 03:29:19 +0000 Subject: [PATCH] Remove LooperMode.LEGACY usage from settings/notification Bug: 333808868 Test: atest Change-Id: I11d68462adf60a02224306e848f9280cd1bdad4f --- .../NotificationAccessSettingsTest.java | 15 ++++----- ...sBypassingDndPreferenceControllerTest.java | 32 ++++++++++++------ ...versationListPreferenceControllerTest.java | 33 +++++++++++++++---- 3 files changed, 55 insertions(+), 25 deletions(-) diff --git a/tests/robotests/src/com/android/settings/notification/NotificationAccessSettingsTest.java b/tests/robotests/src/com/android/settings/notification/NotificationAccessSettingsTest.java index d19f17d733b..61294bbc6a1 100644 --- a/tests/robotests/src/com/android/settings/notification/NotificationAccessSettingsTest.java +++ b/tests/robotests/src/com/android/settings/notification/NotificationAccessSettingsTest.java @@ -44,26 +44,27 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.google.common.base.Strings; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; import org.mockito.stubbing.Answer; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; -import org.robolectric.annotation.LooperMode; import org.robolectric.shadows.ShadowApplication; +import org.robolectric.shadows.ShadowLooper; import java.util.ArrayList; @RunWith(RobolectricTestRunner.class) -@LooperMode(LooperMode.Mode.LEGACY) @Config(shadows = {ShadowBluetoothUtils.class}) public class NotificationAccessSettingsTest { + @Rule + public final MockitoRule mMockitoRule = MockitoJUnit.rule(); - private Context mContext; private NotificationAccessSettings mAccessSettings; @Mock private NotificationManager mNotificationManager; @@ -72,9 +73,6 @@ public class NotificationAccessSettingsTest { @Before public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - - mContext = RuntimeEnvironment.application; ShadowApplication shadowApp = ShadowApplication.getInstance(); shadowApp.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); @@ -92,6 +90,7 @@ public class NotificationAccessSettingsTest { mAccessSettings.mNm = mNotificationManager; mAccessSettings.mPm = mPackageManager; ShadowBluetoothUtils.sLocalBluetoothManager = mock(LocalBluetoothManager.class); + ShadowLooper.idleMainLooper(); } @Test diff --git a/tests/robotests/src/com/android/settings/notification/app/AppChannelsBypassingDndPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/app/AppChannelsBypassingDndPreferenceControllerTest.java index 053b3520cbe..b8c9b5b81f4 100644 --- a/tests/robotests/src/com/android/settings/notification/app/AppChannelsBypassingDndPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/app/AppChannelsBypassingDndPreferenceControllerTest.java @@ -42,21 +42,25 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.LooperMode; -import org.robolectric.shadows.ShadowApplication; +import org.robolectric.android.util.concurrent.PausedExecutorService; +import org.robolectric.shadows.ShadowLooper; +import org.robolectric.shadows.ShadowPausedAsyncTask; import java.util.ArrayList; import java.util.Collections; @RunWith(RobolectricTestRunner.class) -@LooperMode(LooperMode.Mode.LEGACY) public class AppChannelsBypassingDndPreferenceControllerTest { @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); + @Rule + public final MockitoRule mMockitoRule = MockitoJUnit.rule(); + @Mock private NotificationBackend mBackend; @@ -65,11 +69,13 @@ public class AppChannelsBypassingDndPreferenceControllerTest { private PreferenceScreen mPreferenceScreen; private PreferenceCategory mCategory; + private PausedExecutorService mExecutorService; @Before public void setUp() { - MockitoAnnotations.initMocks(this); Context context = ApplicationProvider.getApplicationContext(); + mExecutorService = new PausedExecutorService(); + ShadowPausedAsyncTask.overrideExecutor(mExecutorService); mAppRow = new NotificationBackend.AppRow(); mAppRow.uid = 42; @@ -91,7 +97,8 @@ public class AppChannelsBypassingDndPreferenceControllerTest { buildGroupList(true, true, false)); mController.displayPreference(mPreferenceScreen); - ShadowApplication.runBackgroundTasks(); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); assertThat(mCategory.getPreferenceCount()).isEqualTo(4); // "All" + 3 channels assertThat(mCategory.getPreference(0).getTitle().toString()).isEqualTo( @@ -107,7 +114,8 @@ public class AppChannelsBypassingDndPreferenceControllerTest { buildGroupList(true, true, false)); mController.displayPreference(mPreferenceScreen); - ShadowApplication.runBackgroundTasks(); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); assertThat(mCategory.getPreference(0).isEnabled()).isTrue(); } @@ -118,7 +126,8 @@ public class AppChannelsBypassingDndPreferenceControllerTest { buildGroupList(true, false, true)); mController.displayPreference(mPreferenceScreen); - ShadowApplication.runBackgroundTasks(); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); assertThat(((PrimarySwitchPreference) mCategory.getPreference( 1)).isSwitchEnabled()).isTrue(); @@ -135,7 +144,8 @@ public class AppChannelsBypassingDndPreferenceControllerTest { buildGroupList(true, false, true)); mController.displayPreference(mPreferenceScreen); - ShadowApplication.runBackgroundTasks(); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); assertThat(mCategory.getPreference(0).isEnabled()).isFalse(); assertThat(((PrimarySwitchPreference) mCategory.getPreference( @@ -185,7 +195,9 @@ public class AppChannelsBypassingDndPreferenceControllerTest { when(mBackend.getGroups(eq(mAppRow.pkg), eq(mAppRow.uid))).thenReturn(groups); mController.displayPreference(mPreferenceScreen); - ShadowApplication.runBackgroundTasks(); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); + // Check that we've added the group name as a summary to channels that have identical names. // Channels are also alphabetized. assertThat(mCategory.getPreference(1).getTitle().toString()).isEqualTo("Mail"); diff --git a/tests/robotests/src/com/android/settings/notification/app/AppConversationListPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/app/AppConversationListPreferenceControllerTest.java index 916452cace3..c56a54f2728 100644 --- a/tests/robotests/src/com/android/settings/notification/app/AppConversationListPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/app/AppConversationListPreferenceControllerTest.java @@ -37,20 +37,24 @@ import androidx.test.core.app.ApplicationProvider; import com.android.settings.notification.NotificationBackend; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.LooperMode; -import org.robolectric.shadows.ShadowApplication; +import org.robolectric.android.util.concurrent.PausedExecutorService; +import org.robolectric.shadows.ShadowLooper; +import org.robolectric.shadows.ShadowPausedAsyncTask; import java.util.ArrayList; import java.util.Arrays; @RunWith(RobolectricTestRunner.class) -@LooperMode(LooperMode.Mode.LEGACY) public class AppConversationListPreferenceControllerTest { + @Rule + public final MockitoRule mMockitoRule = MockitoJUnit.rule(); private Context mContext; @@ -60,11 +64,13 @@ public class AppConversationListPreferenceControllerTest { private AppConversationListPreferenceController mController; private NotificationBackend.AppRow mAppRow; private PreferenceCategory mPreference; + private PausedExecutorService mExecutorService; @Before public void setUp() { - MockitoAnnotations.initMocks(this); mContext = ApplicationProvider.getApplicationContext(); + mExecutorService = new PausedExecutorService(); + ShadowPausedAsyncTask.overrideExecutor(mExecutorService); mAppRow = new NotificationBackend.AppRow(); mAppRow.uid = 42; @@ -88,7 +94,8 @@ public class AppConversationListPreferenceControllerTest { )); mController.updateState(mPreference); - ShadowApplication.runBackgroundTasks(); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); assertThat(mPreference.isVisible()).isTrue(); assertThat(mPreference.getPreferenceCount()).isEqualTo(2); @@ -107,7 +114,8 @@ public class AppConversationListPreferenceControllerTest { )); mController.updateState(mPreference); - ShadowApplication.runBackgroundTasks(); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); assertThat(mPreference.isVisible()).isTrue(); assertThat(mPreference.getPreferenceCount()).isEqualTo(1); @@ -120,6 +128,8 @@ public class AppConversationListPreferenceControllerTest { .thenReturn(conversationList()); mController.updateState(mPreference); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); assertThat(mPreference.isVisible()).isFalse(); assertThat(mPreference.getPreferenceCount()).isEqualTo(0); @@ -130,6 +140,9 @@ public class AppConversationListPreferenceControllerTest { when(mBackend.getConversations(eq(mAppRow.pkg), eq(mAppRow.uid))) .thenReturn(conversationList()); mController.updateState(mPreference); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); + assertThat(mPreference.isVisible()).isFalse(); // Empty -> present @@ -138,6 +151,9 @@ public class AppConversationListPreferenceControllerTest { conversationChannel("1", "msg", "Mario", "Messages", "M", false) )); mController.updateState(mPreference); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); + assertThat(mPreference.isVisible()).isTrue(); assertThat(mPreference.getPreferenceCount()).isEqualTo(1); @@ -145,6 +161,9 @@ public class AppConversationListPreferenceControllerTest { when(mBackend.getConversations(eq(mAppRow.pkg), eq(mAppRow.uid))) .thenReturn(conversationList()); mController.updateState(mPreference); + mExecutorService.runAll(); + ShadowLooper.idleMainLooper(); + assertThat(mPreference.isVisible()).isFalse(); assertThat(mPreference.getPreferenceCount()).isEqualTo(0); }