Create AppDataUsageListController
For better organization and testings. Bug: 240931350 Test: manual - on AppDataUsage Test: unit test Change-Id: I77ceeccc7055fcd948fe40d5dfb9cc4a9b9ad2ee
This commit is contained in:
@@ -60,6 +60,7 @@ import com.android.settings.widget.EntityHeaderController;
|
||||
import com.android.settingslib.AppItem;
|
||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.net.NetworkCycleDataForUid;
|
||||
import com.android.settingslib.net.NetworkCycleDataForUidLoader;
|
||||
import com.android.settingslib.net.UidDetail;
|
||||
@@ -109,10 +110,7 @@ public class AppDataUsageTest {
|
||||
@Test
|
||||
@Config(shadows = ShadowFragment.class)
|
||||
public void onCreate_appUid_shouldGetAppLabelFromAppInfo() throws NameNotFoundException {
|
||||
mFragment = spy(new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
});
|
||||
mFragment = spy(new TestFragment());
|
||||
final FragmentActivity activity = spy(Robolectric.setupActivity(FragmentActivity.class));
|
||||
doReturn(mPackageManager).when(activity).getPackageManager();
|
||||
doReturn(activity).when(mFragment).getActivity();
|
||||
@@ -142,10 +140,7 @@ public class AppDataUsageTest {
|
||||
@Test
|
||||
@Config(shadows = ShadowFragment.class)
|
||||
public void onCreate_notAppUid_shouldGetAppLabelFromUidDetailProvider() {
|
||||
mFragment = spy(new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
});
|
||||
mFragment = spy(new TestFragment());
|
||||
ReflectionHelpers.setField(mFragment, "mDashboardFeatureProvider",
|
||||
FakeFeatureFactory.setupForTest().dashboardFeatureProvider);
|
||||
doReturn(Robolectric.setupActivity(FragmentActivity.class)).when(mFragment).getActivity();
|
||||
@@ -172,10 +167,7 @@ public class AppDataUsageTest {
|
||||
|
||||
@Test
|
||||
public void bindAppHeader_allWorkApps_shouldNotShowAppInfoLink() {
|
||||
mFragment = spy(new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
});
|
||||
mFragment = spy(new TestFragment());
|
||||
|
||||
when(mFragment.getPreferenceManager())
|
||||
.thenReturn(mock(PreferenceManager.class, RETURNS_DEEP_STUBS));
|
||||
@@ -192,10 +184,7 @@ public class AppDataUsageTest {
|
||||
throws PackageManager.NameNotFoundException {
|
||||
final int fakeUserId = 100;
|
||||
|
||||
mFragment = spy(new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
});
|
||||
mFragment = spy(new TestFragment());
|
||||
final ArraySet<String> packages = new ArraySet<>();
|
||||
packages.add("pkg");
|
||||
final AppItem appItem = new AppItem(123456789);
|
||||
@@ -221,10 +210,7 @@ public class AppDataUsageTest {
|
||||
|
||||
@Test
|
||||
public void changePreference_backgroundData_shouldUpdateUI() {
|
||||
mFragment = spy(new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
});
|
||||
mFragment = spy(new TestFragment());
|
||||
final AppItem appItem = new AppItem(123456789);
|
||||
final RestrictedSwitchPreference pref = mock(RestrictedSwitchPreference.class);
|
||||
final DataSaverBackend dataSaverBackend = mock(DataSaverBackend.class);
|
||||
@@ -241,10 +227,7 @@ public class AppDataUsageTest {
|
||||
|
||||
@Test
|
||||
public void updatePrefs_restrictedByAdmin_shouldDisablePreference() {
|
||||
mFragment = spy(new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
});
|
||||
mFragment = spy(new TestFragment());
|
||||
final int testUid = 123123;
|
||||
final AppItem appItem = new AppItem(testUid);
|
||||
final RestrictedSwitchPreference restrictBackgroundPref
|
||||
@@ -272,10 +255,7 @@ public class AppDataUsageTest {
|
||||
|
||||
@Test
|
||||
public void bindData_noAppUsageData_shouldHideCycleSpinner() {
|
||||
mFragment = spy(new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
});
|
||||
mFragment = spy(new TestFragment());
|
||||
final SpinnerPreference cycle = mock(SpinnerPreference.class);
|
||||
ReflectionHelpers.setField(mFragment, "mCycle", cycle);
|
||||
final Preference preference = mock(Preference.class);
|
||||
@@ -291,10 +271,7 @@ public class AppDataUsageTest {
|
||||
|
||||
@Test
|
||||
public void bindData_hasAppUsageData_shouldShowCycleSpinnerAndUpdateUsageSummary() {
|
||||
mFragment = spy(new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
});
|
||||
mFragment = spy(new TestFragment());
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
ReflectionHelpers.setField(mFragment, "mContext", context);
|
||||
final long backgroundBytes = 1234L;
|
||||
@@ -323,10 +300,7 @@ public class AppDataUsageTest {
|
||||
|
||||
@Test
|
||||
public void onCreateLoader_categoryApp_shouldQueryDataUsageUsingAppKey() {
|
||||
mFragment = new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
};
|
||||
mFragment = new TestFragment();
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
final int testUid = 123123;
|
||||
final AppItem appItem = new AppItem(testUid);
|
||||
@@ -349,10 +323,7 @@ public class AppDataUsageTest {
|
||||
|
||||
@Test
|
||||
public void onCreateLoader_categoryUser_shouldQueryDataUsageUsingAssociatedUids() {
|
||||
mFragment = new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
};
|
||||
mFragment = new TestFragment();
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
final int testUserId = 11;
|
||||
final AppItem appItem = new AppItem(testUserId);
|
||||
@@ -389,10 +360,7 @@ public class AppDataUsageTest {
|
||||
appItem.category = AppItem.CATEGORY_APP;
|
||||
appItem.addUid(uid);
|
||||
|
||||
mFragment = new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
};
|
||||
mFragment = new TestFragment();
|
||||
ReflectionHelpers.setField(mFragment, "mContext", RuntimeEnvironment.application);
|
||||
ReflectionHelpers.setField(mFragment, "mCycles", testCycles);
|
||||
ReflectionHelpers.setField(mFragment, "mAppItem", appItem);
|
||||
@@ -425,10 +393,7 @@ public class AppDataUsageTest {
|
||||
builder.setStartTime(tenDaysAgo).setEndTime(now).setTotalUsage(1234L);
|
||||
data.add(builder.build());
|
||||
|
||||
mFragment = new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
};
|
||||
mFragment = new TestFragment();
|
||||
ReflectionHelpers.setField(mFragment, "mContext", RuntimeEnvironment.application);
|
||||
ReflectionHelpers.setField(mFragment, "mCycleAdapter", mock(CycleAdapter.class));
|
||||
ReflectionHelpers.setField(mFragment, "mSelectedCycle", tenDaysAgo);
|
||||
@@ -455,10 +420,7 @@ public class AppDataUsageTest {
|
||||
ShadowDataUsageUtils.HAS_SIM = false;
|
||||
ShadowSubscriptionManager.setDefaultDataSubscriptionId(
|
||||
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
|
||||
mFragment = spy(new AppDataUsage() {
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) { return true; }
|
||||
});
|
||||
mFragment = spy(new TestFragment());
|
||||
doReturn(Robolectric.setupActivity(FragmentActivity.class)).when(mFragment).getActivity();
|
||||
doReturn(RuntimeEnvironment.application).when(mFragment).getContext();
|
||||
final UidDetailProvider uidDetailProvider = mock(UidDetailProvider.class);
|
||||
@@ -478,4 +440,16 @@ public class AppDataUsageTest {
|
||||
assertTrue(mFragment.mTemplate.getSubscriberIds().isEmpty());
|
||||
assertTrue(mFragment.mTemplate.getWifiNetworkKeys().isEmpty());
|
||||
}
|
||||
|
||||
private static class TestFragment extends AppDataUsage {
|
||||
@Override
|
||||
protected <T extends AbstractPreferenceController> T use(Class<T> clazz) {
|
||||
return mock(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimHardwareVisible(Context context) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (C) 2023 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.datausage
|
||||
|
||||
import android.content.Context
|
||||
import android.util.SparseBooleanArray
|
||||
import androidx.lifecycle.testing.TestLifecycleOwner
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceCategory
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.android.settings.datausage.lib.AppPreferenceRepository
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.doAnswer
|
||||
import org.mockito.kotlin.mock
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class AppDataUsageListControllerTest {
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
|
||||
private val repository = mock<AppPreferenceRepository> {
|
||||
on { loadAppPreferences(any()) } doAnswer {
|
||||
val uids = it.arguments[0] as List<*>
|
||||
uids.map { Preference(context) }
|
||||
}
|
||||
}
|
||||
|
||||
private val controller = AppDataUsageListController(
|
||||
context = context,
|
||||
preferenceKey = KEY,
|
||||
repository = repository,
|
||||
)
|
||||
|
||||
private val preference = PreferenceCategory(context).apply { key = KEY }
|
||||
|
||||
private val preferenceScreen = PreferenceManager(context).createPreferenceScreen(context)
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
preferenceScreen.addPreference(preference)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onViewCreated_singleUid_hidePreference(): Unit = runBlocking {
|
||||
controller.init(SparseBooleanArray().apply {
|
||||
put(UID_0, true)
|
||||
})
|
||||
controller.displayPreference(preferenceScreen)
|
||||
|
||||
controller.onViewCreated(TestLifecycleOwner())
|
||||
delay(100)
|
||||
|
||||
assertThat(preference.isVisible).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onViewCreated_twoUid_showPreference(): Unit = runBlocking {
|
||||
controller.init(SparseBooleanArray().apply {
|
||||
put(UID_0, true)
|
||||
put(UID_1, true)
|
||||
})
|
||||
controller.displayPreference(preferenceScreen)
|
||||
|
||||
controller.onViewCreated(TestLifecycleOwner())
|
||||
delay(100)
|
||||
|
||||
assertThat(preference.isVisible).isTrue()
|
||||
assertThat(preference.preferenceCount).isEqualTo(2)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val KEY = "test_key"
|
||||
const val UID_0 = 10000
|
||||
const val UID_1 = 10001
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2023 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.datausage.lib
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.ApplicationInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.UserHandle
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.doReturn
|
||||
import org.mockito.kotlin.doThrow
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.spy
|
||||
import org.mockito.kotlin.stub
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class AppPreferenceRepositoryTest {
|
||||
private val packageManager = mock<PackageManager> {
|
||||
on { getPackagesForUid(UID) } doReturn arrayOf(PACKAGE_NAME)
|
||||
}
|
||||
|
||||
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
|
||||
on { packageManager } doReturn packageManager
|
||||
}
|
||||
|
||||
private val repository = AppPreferenceRepository(context)
|
||||
|
||||
@Test
|
||||
fun loadAppPreferences_packageNotFound_returnEmpty() {
|
||||
packageManager.stub {
|
||||
on {
|
||||
getApplicationInfoAsUser(PACKAGE_NAME, 0, UserHandle.getUserId(UID))
|
||||
} doThrow PackageManager.NameNotFoundException()
|
||||
}
|
||||
|
||||
val preferences = repository.loadAppPreferences(listOf(UID))
|
||||
|
||||
assertThat(preferences).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun loadAppPreferences_packageFound_returnPreference() {
|
||||
val app = mock<ApplicationInfo> {
|
||||
on { loadUnbadgedIcon(any()) } doReturn UNBADGED_ICON
|
||||
on { loadLabel(any()) } doReturn LABEL
|
||||
}
|
||||
packageManager.stub {
|
||||
on {
|
||||
getApplicationInfoAsUser(PACKAGE_NAME, 0, UserHandle.getUserId(UID))
|
||||
} doReturn app
|
||||
}
|
||||
|
||||
val preferences = repository.loadAppPreferences(listOf(UID))
|
||||
|
||||
assertThat(preferences).hasSize(1)
|
||||
preferences[0].apply {
|
||||
assertThat(title).isEqualTo(LABEL)
|
||||
assertThat(icon).isNotNull()
|
||||
assertThat(isSelectable).isFalse()
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val UID = 10000
|
||||
const val PACKAGE_NAME = "package.name"
|
||||
const val LABEL = "Label"
|
||||
val UNBADGED_ICON = mock<Drawable>()
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.datausage;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class AppPrefLoaderTest {
|
||||
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
private AppPrefLoader mLoader;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
final ArraySet<String> pkgs = new ArraySet<>(2);
|
||||
pkgs.add("pkg0");
|
||||
pkgs.add("pkg1");
|
||||
mLoader = new AppPrefLoader(
|
||||
ApplicationProvider.getApplicationContext(), pkgs, mPackageManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadInBackground_packageNotFound_shouldReturnEmptySet()
|
||||
throws NameNotFoundException {
|
||||
when(mPackageManager.getApplicationInfo(anyString(), anyInt()))
|
||||
.thenThrow(new NameNotFoundException());
|
||||
|
||||
assertThat(mLoader.loadInBackground()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadInBackground_shouldReturnPreference() throws NameNotFoundException {
|
||||
ApplicationInfo info = mock(ApplicationInfo.class);
|
||||
when(mPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(info);
|
||||
final Drawable drawable = mock(Drawable.class);
|
||||
final String label = "Label1";
|
||||
when(info.loadIcon(mPackageManager)).thenReturn(drawable);
|
||||
when(info.loadLabel(mPackageManager)).thenReturn(label);
|
||||
|
||||
Preference preference = mLoader.loadInBackground().valueAt(0);
|
||||
assertThat(preference.getTitle()).isEqualTo(label);
|
||||
assertThat(preference.getIcon()).isEqualTo(drawable);
|
||||
assertThat(preference.isSelectable()).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user