Merge "Add action to open the anomaly detail page"
This commit is contained in:
@@ -18,24 +18,30 @@ package com.android.settings.fuelgauge;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceCategory;
|
||||
import android.support.v7.preference.PreferenceManager;
|
||||
import android.util.IconDrawableFactory;
|
||||
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.fuelgauge.batterytip.AppInfo;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
@@ -43,13 +49,13 @@ import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class RestrictedAppDetailsTest {
|
||||
private static final String PACKAGE_NAME = "com.android.app";
|
||||
private static final String APP_NAME = "app";
|
||||
private static final int UID = 1234;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
@@ -58,25 +64,33 @@ public class RestrictedAppDetailsTest {
|
||||
private IconDrawableFactory mIconDrawableFactory;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private PreferenceManager mPreferenceManager;
|
||||
private RestrictedAppDetails mFragment;
|
||||
@Mock
|
||||
private SettingsActivity mSettingsActivity;
|
||||
@Mock
|
||||
private InstrumentedPreferenceFragment mFragment;
|
||||
private RestrictedAppDetails mRestrictedAppDetails;
|
||||
private Context mContext;
|
||||
private Intent mIntent;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mFragment = spy(new RestrictedAppDetails());
|
||||
mRestrictedAppDetails = spy(new RestrictedAppDetails());
|
||||
|
||||
doReturn(mPreferenceManager).when(mFragment).getPreferenceManager();
|
||||
doReturn(mPreferenceManager).when(mRestrictedAppDetails).getPreferenceManager();
|
||||
doReturn(mContext).when(mPreferenceManager).getContext();
|
||||
mFragment.mPackageManager = mPackageManager;
|
||||
mFragment.mIconDrawableFactory = mIconDrawableFactory;
|
||||
mFragment.mPackageOpsList = new ArrayList<>();
|
||||
mFragment.mPackageOpsList.add(
|
||||
new AppOpsManager.PackageOps(PACKAGE_NAME, UID, null /* entries */));
|
||||
mFragment.mRestrictedAppListGroup = spy(new PreferenceCategory(mContext));
|
||||
doReturn(mPreferenceManager).when(mFragment.mRestrictedAppListGroup).getPreferenceManager();
|
||||
mRestrictedAppDetails.mPackageManager = mPackageManager;
|
||||
mRestrictedAppDetails.mIconDrawableFactory = mIconDrawableFactory;
|
||||
mRestrictedAppDetails.mAppInfos = new ArrayList<>();
|
||||
mRestrictedAppDetails.mAppInfos.add(new AppInfo.Builder()
|
||||
.setPackageName(PACKAGE_NAME)
|
||||
.build());
|
||||
mRestrictedAppDetails.mRestrictedAppListGroup = spy(new PreferenceCategory(mContext));
|
||||
mRestrictedAppDetails.mBatteryUtils = new BatteryUtils(mContext);
|
||||
doReturn(mPreferenceManager).when(
|
||||
mRestrictedAppDetails.mRestrictedAppListGroup).getPreferenceManager();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,12 +98,33 @@ public class RestrictedAppDetailsTest {
|
||||
doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(PACKAGE_NAME, 0);
|
||||
doReturn(APP_NAME).when(mPackageManager).getApplicationLabel(mApplicationInfo);
|
||||
|
||||
mFragment.refreshUi();
|
||||
mRestrictedAppDetails.refreshUi();
|
||||
|
||||
assertThat(mFragment.mRestrictedAppListGroup.getPreferenceCount()).isEqualTo(1);
|
||||
final Preference preference = mFragment.mRestrictedAppListGroup.getPreference(0);
|
||||
assertThat(mRestrictedAppDetails.mRestrictedAppListGroup.getPreferenceCount()).isEqualTo(1);
|
||||
final Preference preference = mRestrictedAppDetails.mRestrictedAppListGroup.getPreference(
|
||||
0);
|
||||
assertThat(preference.getKey()).isEqualTo(PACKAGE_NAME);
|
||||
assertThat(preference.getTitle()).isEqualTo(APP_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartRestrictedAppDetails_startWithCorrectData() {
|
||||
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
|
||||
doAnswer(invocation -> {
|
||||
// Get the intent in which it has the app info bundle
|
||||
mIntent = captor.getValue();
|
||||
return true;
|
||||
}).when(mSettingsActivity).startActivity(captor.capture());
|
||||
|
||||
RestrictedAppDetails.startRestrictedAppDetails(mSettingsActivity, mFragment,
|
||||
mRestrictedAppDetails.mAppInfos);
|
||||
|
||||
final Bundle bundle = mIntent.getBundleExtra(
|
||||
SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
|
||||
// Verify the bundle has the correct info
|
||||
final List<AppInfo> appInfos = bundle.getParcelableArrayList(
|
||||
RestrictedAppDetails.EXTRA_APP_INFO_LIST);
|
||||
assertThat(appInfos).hasSize(1);
|
||||
assertThat(appInfos.get(0).packageName).isEqualTo(PACKAGE_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.fuelgauge.batterytip;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.core.InstrumentedPreferenceFragment;
|
||||
import com.android.settings.fuelgauge.batterytip.actions.OpenRestrictAppFragmentAction;
|
||||
import com.android.settings.fuelgauge.batterytip.actions.RestrictAppAction;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class BatteryTipUtilsTest {
|
||||
@Mock
|
||||
private SettingsActivity mSettingsActivity;
|
||||
@Mock
|
||||
private InstrumentedPreferenceFragment mFragment;
|
||||
private RestrictAppTip mRestrictAppTip;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
FakeFeatureFactory.setupForTest();
|
||||
doReturn(RuntimeEnvironment.application).when(mFragment).getContext();
|
||||
mRestrictAppTip = spy(new RestrictAppTip(BatteryTip.StateType.NEW, new ArrayList<>()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetActionForBatteryTip_typeRestrictStateNew_returnActionRestrict() {
|
||||
doReturn(BatteryTip.StateType.NEW).when(mRestrictAppTip).getState();
|
||||
|
||||
assertThat(BatteryTipUtils.getActionForBatteryTip(mRestrictAppTip, mSettingsActivity,
|
||||
mFragment)).isInstanceOf(RestrictAppAction.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetActionForBatteryTip_typeRestrictStateHandled_returnActionOpen() {
|
||||
doReturn(BatteryTip.StateType.HANDLED).when(mRestrictAppTip).getState();
|
||||
|
||||
assertThat(BatteryTipUtils.getActionForBatteryTip(mRestrictAppTip, mSettingsActivity,
|
||||
mFragment)).isInstanceOf(OpenRestrictAppFragmentAction.class);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user