Add preference controller to launch app-time-spent UI.

Bug: 74580195
Test: robotests
Change-Id: Iba9338f04cc037a134ef9445ce97cbbe21f53833
This commit is contained in:
Fan Zhang
2018-03-23 13:02:24 -07:00
parent 7d66123f4c
commit 64a8f70a7f
5 changed files with 198 additions and 18 deletions

View File

@@ -0,0 +1,98 @@
/*
* 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.applications.appinfo;
import static android.content.Intent.EXTRA_PACKAGE_NAME;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
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.Shadows;
import org.robolectric.shadows.ShadowPackageManager;
@RunWith(SettingsRobolectricTestRunner.class)
public class TimeSpentInAppPreferenceControllerTest {
private static final String TEST_KEY = "test_tey";
private static final Intent TEST_INTENT = new Intent(
TimeSpentInAppPreferenceController.SEE_TIME_IN_APP_TEMPLATE)
.putExtra(EXTRA_PACKAGE_NAME, "com.android.settings");
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private ShadowPackageManager mPackageManager;
private TimeSpentInAppPreferenceController mController;
private Preference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPackageManager = Shadows.shadowOf(mContext.getPackageManager());
mController = new TimeSpentInAppPreferenceController(mContext, TEST_KEY);
mPreference = new Preference(mContext);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
}
@Test
public void noPackageName_shouldBeDisabled() {
mController.setPackageName(null);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
}
@Test
public void noIntentHandler_shouldBeDisabled() {
mController.setPackageName(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
}
@Test
public void hasIntentHandler_shouldBeAvailable() {
mPackageManager.addResolveInfoForIntent(TEST_INTENT, new ResolveInfo());
mController.setPackageName(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
mController.displayPreference(mScreen);
final Intent intent = mPreference.getIntent();
assertThat(intent.getAction()).isEqualTo(TEST_INTENT.getAction());
assertThat(intent.getStringExtra(EXTRA_PACKAGE_NAME))
.isEqualTo(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
}
}