Fix ZenModeScheduleRuleSettingsTest null pointer

Test: make ROBOTEST_FILTER=ZenModeScheduleRuleSettingsTest RunSettingsRoboTests -j40
Bug: 63077372
Change-Id: Ib843337e5867507eec1236fb4504035c151c348c
This commit is contained in:
Beverly
2017-12-07 10:04:20 -05:00
parent d7d5754cfd
commit 1aabe820e8

View File

@@ -17,10 +17,10 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.Activity; import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.Intent; import android.content.Intent;
import android.os.UserManager;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
@@ -34,6 +34,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowToast; import org.robolectric.shadows.ShadowToast;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.RuntimeEnvironment;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
@@ -58,14 +60,19 @@ public class ZenModeScheduleRuleSettingsTest {
private Intent mIntent; private Intent mIntent;
@Mock @Mock
private UserManager mUserManager; private NotificationManager mNotificationManager;
private TestFragment mFragment; private TestFragment mFragment;
private Context mContext;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = shadowApplication.getApplicationContext();
mFragment = spy(new TestFragment()); mFragment = spy(new TestFragment());
mFragment.onAttach(application); mFragment.onAttach(application);
@@ -77,13 +84,13 @@ public class ZenModeScheduleRuleSettingsTest {
when(mActivity.getTheme()).thenReturn(res.newTheme()); when(mActivity.getTheme()).thenReturn(res.newTheme());
when(mActivity.getIntent()).thenReturn(mIntent); when(mActivity.getIntent()).thenReturn(mIntent);
when(mActivity.getResources()).thenReturn(res); when(mActivity.getResources()).thenReturn(res);
when(mFragment.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); when(mFragment.getContext()).thenReturn(mContext);
} }
@Test @Test
public void onCreate_noRuleId_shouldToastAndFinishAndNoCrash() { public void onCreate_noRuleId_shouldToastAndFinishAndNoCrash() {
final Context ctx = application.getApplicationContext(); final String expected = mContext.getResources().getString(
final String expected = ctx.getResources().getString(R.string.zen_mode_rule_not_found_text); R.string.zen_mode_rule_not_found_text);
mFragment.onCreate(null); mFragment.onCreate(null);
@@ -93,7 +100,7 @@ public class ZenModeScheduleRuleSettingsTest {
// verify the finish // verify the finish
verify(mActivity).finish(); verify(mActivity).finish();
//shoud not crash //should not crash
} }
public static class TestFragment extends ZenModeScheduleRuleSettings { public static class TestFragment extends ZenModeScheduleRuleSettings {