Merge "Fix ZenModeScheduleRuleSettingsTest to test onAttach" into tm-dev am: 29c62e7873

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/17944009

Change-Id: I1fadf7a04b661102fa55e7fec779aaa3e4d83c8c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Yuri Lin
2022-04-27 16:43:46 +00:00
committed by Automerger Merge Worker

View File

@@ -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;
}
}
}