Update RecentAppsPreferenceControllerTest to sdk26

- Remove the usage of the mock context to avoid null pointers in sdk 26

Test: make RunSettingsRoboTests -j40
Change-Id: I73dac0c91e56d84cdec0314a4ec5754a0a98c54b
This commit is contained in:
jeffreyhuang
2017-11-27 13:37:31 -08:00
parent eeffbf6225
commit 3b0b8b711e
3 changed files with 46 additions and 58 deletions

View File

@@ -18,4 +18,5 @@
<bool name="config_enableColorTemperature">false</bool> <bool name="config_enableColorTemperature">false</bool>
<bool name="config_show_camera_laser_sensor">false</bool> <bool name="config_show_camera_laser_sensor">false</bool>
<bool name="config_show_connectivity_monitor">false</bool> <bool name="config_show_connectivity_monitor">false</bool>
<bool name="config_display_recent_apps">false</bool>
</resources> </resources>

View File

@@ -20,4 +20,5 @@
<bool name="config_enableColorTemperature">true</bool> <bool name="config_enableColorTemperature">true</bool>
<bool name="config_show_camera_laser_sensor">true</bool> <bool name="config_show_camera_laser_sensor">true</bool>
<bool name="config_show_connectivity_monitor">true</bool> <bool name="config_show_connectivity_monitor">true</bool>
<bool name="config_display_recent_apps">true</bool>
</resources> </resources>

View File

@@ -16,13 +16,30 @@
package com.android.settings.applications; package com.android.settings.applications;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Application; import android.app.Application;
import android.app.usage.UsageStats; import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager; import android.app.usage.UsageStatsManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
@@ -31,14 +48,13 @@ import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.applications.ApplicationsState; import com.android.settingslib.applications.ApplicationsState;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.ArgumentMatcher; import org.mockito.ArgumentMatcher;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@@ -47,25 +63,9 @@ import org.robolectric.annotation.Config;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class RecentAppsPreferenceControllerTest { public class RecentAppsPreferenceControllerTest {
@Mock @Mock
@@ -76,14 +76,18 @@ public class RecentAppsPreferenceControllerTest {
private Preference mSeeAllPref; private Preference mSeeAllPref;
@Mock @Mock
private PreferenceCategory mDivider; private PreferenceCategory mDivider;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mMockContext;
@Mock @Mock
private UsageStatsManager mUsageStatsManager; private UsageStatsManager mUsageStatsManager;
@Mock @Mock
private UserManager mUserManager; private UserManager mUserManager;
@Mock @Mock
private ApplicationsState mAppState; private ApplicationsState mAppState;
@Mock
private PackageManager mPackageManager;
@Mock
private ApplicationsState.AppEntry mAppEntry;
@Mock
private ApplicationInfo mApplicationInfo;
private Context mContext; private Context mContext;
private RecentAppsPreferenceController mController; private RecentAppsPreferenceController mController;
@@ -91,12 +95,11 @@ public class RecentAppsPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(mMockContext.getSystemService(Context.USAGE_STATS_SERVICE)) mContext = spy(RuntimeEnvironment.application);
.thenReturn(mUsageStatsManager); doReturn(mUsageStatsManager).when(mContext).getSystemService(Context.USAGE_STATS_SERVICE);
when(mMockContext.getSystemService(Context.USER_SERVICE)) doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE);
.thenReturn(mUserManager); doReturn(mPackageManager).when(mContext).getPackageManager();
mContext = RuntimeEnvironment.application;
mController = new RecentAppsPreferenceController(mContext, mAppState, null); mController = new RecentAppsPreferenceController(mContext, mAppState, null);
when(mScreen.findPreference(anyString())).thenReturn(mCategory); when(mScreen.findPreference(anyString())).thenReturn(mCategory);
@@ -125,7 +128,7 @@ public class RecentAppsPreferenceControllerTest {
@Test @Test
public void onDisplayAndUpdateState_shouldRefreshUi() { public void onDisplayAndUpdateState_shouldRefreshUi() {
mController = spy( mController = spy(
new RecentAppsPreferenceController(mMockContext, (Application) null, null)); new RecentAppsPreferenceController(mContext, (Application) null, null));
doNothing().when(mController).refreshUi(mContext); doNothing().when(mController).refreshUi(mContext);
@@ -136,11 +139,8 @@ public class RecentAppsPreferenceControllerTest {
} }
@Test @Test
@Config(qualifiers = "mcc999")
public void display_shouldNotShowRecents_showAppInfoPreference() { public void display_shouldNotShowRecents_showAppInfoPreference() {
mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps))
.thenReturn(false);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
verify(mCategory, never()).addPreference(any(Preference.class)); verify(mCategory, never()).addPreference(any(Preference.class));
@@ -152,8 +152,6 @@ public class RecentAppsPreferenceControllerTest {
@Test @Test
public void display_showRecents() { public void display_showRecents() {
when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps))
.thenReturn(true);
final List<UsageStats> stats = new ArrayList<>(); final List<UsageStats> stats = new ArrayList<>();
final UsageStats stat1 = new UsageStats(); final UsageStats stat1 = new UsageStats();
final UsageStats stat2 = new UsageStats(); final UsageStats stat2 = new UsageStats();
@@ -172,20 +170,17 @@ public class RecentAppsPreferenceControllerTest {
// stat1, stat2 are valid apps. stat3 is invalid. // stat1, stat2 are valid apps. stat3 is invalid.
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
.thenReturn(mock(ApplicationsState.AppEntry.class)); .thenReturn(mAppEntry);
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
.thenReturn(mock(ApplicationsState.AppEntry.class)); .thenReturn(mAppEntry);
when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat3.mPackageName, UserHandle.myUserId()))
.thenReturn(null); .thenReturn(null);
when(mMockContext.getPackageManager().resolveActivity(any(Intent.class), anyInt())) when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
.thenReturn(new ResolveInfo()); new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
final Configuration configuration = new Configuration(); mAppEntry.info = mApplicationInfo;
configuration.locale = Locale.US;
when(mMockContext.getResources().getConfiguration()).thenReturn(configuration);
mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
verify(mCategory).setTitle(R.string.recent_app_category_title); verify(mCategory).setTitle(R.string.recent_app_category_title);
@@ -200,8 +195,6 @@ public class RecentAppsPreferenceControllerTest {
@Test @Test
public void display_hasRecentButNoneDisplayable_showAppInfo() { public void display_hasRecentButNoneDisplayable_showAppInfo() {
when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps))
.thenReturn(true);
final List<UsageStats> stats = new ArrayList<>(); final List<UsageStats> stats = new ArrayList<>();
final UsageStats stat1 = new UsageStats(); final UsageStats stat1 = new UsageStats();
final UsageStats stat2 = new UsageStats(); final UsageStats stat2 = new UsageStats();
@@ -218,12 +211,11 @@ public class RecentAppsPreferenceControllerTest {
.thenReturn(mock(ApplicationsState.AppEntry.class)); .thenReturn(mock(ApplicationsState.AppEntry.class));
when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat2.mPackageName, UserHandle.myUserId()))
.thenReturn(mock(ApplicationsState.AppEntry.class)); .thenReturn(mock(ApplicationsState.AppEntry.class));
when(mMockContext.getPackageManager().resolveActivity(any(Intent.class), anyInt())) when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
.thenReturn(new ResolveInfo()); new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
verify(mCategory, never()).addPreference(any(Preference.class)); verify(mCategory, never()).addPreference(any(Preference.class));
@@ -234,8 +226,6 @@ public class RecentAppsPreferenceControllerTest {
@Test @Test
public void display_showRecents_formatSummary() { public void display_showRecents_formatSummary() {
when(mMockContext.getResources().getBoolean(R.bool.config_display_recent_apps))
.thenReturn(true);
final List<UsageStats> stats = new ArrayList<>(); final List<UsageStats> stats = new ArrayList<>();
final UsageStats stat1 = new UsageStats(); final UsageStats stat1 = new UsageStats();
stat1.mLastTimeUsed = System.currentTimeMillis(); stat1.mLastTimeUsed = System.currentTimeMillis();
@@ -243,17 +233,13 @@ public class RecentAppsPreferenceControllerTest {
stats.add(stat1); stats.add(stat1);
when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId())) when(mAppState.getEntry(stat1.mPackageName, UserHandle.myUserId()))
.thenReturn(mock(ApplicationsState.AppEntry.class)); .thenReturn(mAppEntry);
when(mMockContext.getPackageManager().resolveActivity(any(Intent.class), anyInt())) when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
.thenReturn(new ResolveInfo()); new ResolveInfo());
when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong())) when(mUsageStatsManager.queryUsageStats(anyInt(), anyLong(), anyLong()))
.thenReturn(stats); .thenReturn(stats);
mAppEntry.info = mApplicationInfo;
final Configuration configuration = new Configuration();
configuration.locale = Locale.US;
when(mMockContext.getResources().getConfiguration()).thenReturn(configuration);
mController = new RecentAppsPreferenceController(mMockContext, mAppState, null);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
verify(mCategory).addPreference(argThat(summaryMatches("0 min. ago"))); verify(mCategory).addPreference(argThat(summaryMatches("0 min. ago")));