From e540c8165d02e3e321657a5ee207311c30c0824d Mon Sep 17 00:00:00 2001 From: Yuri Lin Date: Tue, 26 Apr 2022 14:20:09 -0400 Subject: [PATCH] Fix ZenModeScheduleRuleSettingsTest to test onAttach this follows the model of the change to ZenModeEventRuleSettingsTest in ag/17607148 Fixes: 215062934 Test: settings robotests Change-Id: I44abec9316f78be318b51ba541d20b863eb86c8d --- .../zen/ZenModeScheduleRuleSettingsTest.java | 42 +++++-------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/tests/robotests/src/com/android/settings/notification/zen/ZenModeScheduleRuleSettingsTest.java b/tests/robotests/src/com/android/settings/notification/zen/ZenModeScheduleRuleSettingsTest.java index 601ec092fa7..a8a551f64c5 100644 --- a/tests/robotests/src/com/android/settings/notification/zen/ZenModeScheduleRuleSettingsTest.java +++ b/tests/robotests/src/com/android/settings/notification/zen/ZenModeScheduleRuleSettingsTest.java @@ -18,31 +18,27 @@ package com.android.settings.notification.zen; import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.robolectric.RuntimeEnvironment.application; -import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.os.Looper; import androidx.fragment.app.FragmentActivity; +import androidx.test.core.app.ApplicationProvider; import com.android.settings.R; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; -import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowToast; @RunWith(RobolectricTestRunner.class) @@ -54,42 +50,32 @@ public class ZenModeScheduleRuleSettingsTest { @Mock private Intent mIntent; - @Mock - private NotificationManager mNotificationManager; - - private TestFragment mFragment; + private ZenModeScheduleRuleSettings mFragment; private Context mContext; @Before public void setUp() { MockitoAnnotations.initMocks(this); - ShadowApplication shadowApplication = ShadowApplication.getInstance(); - shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); - mContext = application; + mContext = ApplicationProvider.getApplicationContext(); - mFragment = spy(new TestFragment()); - mFragment.onAttach(application); - - doReturn(mActivity).when(mFragment).getActivity(); - - Resources res = application.getResources(); - - doReturn(res).when(mFragment).getResources(); + Resources res = mContext.getResources(); when(mActivity.getTheme()).thenReturn(res.newTheme()); when(mActivity.getIntent()).thenReturn(mIntent); when(mActivity.getResources()).thenReturn(res); when(mActivity.getMainLooper()).thenReturn(mock(Looper.class)); + + mFragment = spy(new ZenModeScheduleRuleSettings()); + when(mFragment.getActivity()).thenReturn(mActivity); when(mFragment.getContext()).thenReturn(mContext); + when(mFragment.getResources()).thenReturn(res); + mFragment.onAttach(mContext); } @Test - @Ignore - public void onCreate_noRuleId_shouldToastAndFinishAndNoCrash() { + public void onAttach_noRuleId_shouldToastAndFinishAndNoCrash() { final String expected = mContext.getString(R.string.zen_mode_rule_not_found_text); - mFragment.onCreate(null); - // verify the toast assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo(expected); @@ -98,12 +84,4 @@ public class ZenModeScheduleRuleSettingsTest { //should not crash } - - public static class TestFragment extends ZenModeScheduleRuleSettings { - - @Override - protected Object getSystemService(final String name) { - return null; - } - } }