1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.applications.appinfo; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.Mockito.doNothing; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 import static org.robolectric.Shadows.shadowOf; 26 27 import android.app.Application; 28 import android.content.Intent; 29 import android.content.pm.ActivityInfo; 30 import android.content.pm.ResolveInfo; 31 import android.support.v7.preference.Preference; 32 33 import com.android.settings.testutils.SettingsRobolectricTestRunner; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.shadows.ShadowPackageManager; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 public class AppSettingPreferenceControllerTest { 45 46 private static final String TEST_PKG_NAME = "test_pkg"; 47 private static final String TEST_CLASS_NAME = "name"; 48 private static final Intent TEST_INTENT = 49 new Intent(Intent.ACTION_APPLICATION_PREFERENCES) 50 .setClassName(TEST_PKG_NAME, TEST_CLASS_NAME); 51 private static final Intent RESOLVED_INTENT = 52 new Intent(Intent.ACTION_APPLICATION_PREFERENCES) 53 .setPackage(TEST_PKG_NAME); 54 55 @Mock 56 private AppInfoDashboardFragment mParent; 57 private Application mApplication; 58 private ShadowPackageManager mPackageManager; 59 private AppSettingPreferenceController mController; 60 private Preference mPreference; 61 62 @Before 63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mApplication = RuntimeEnvironment.application; 66 mPackageManager = shadowOf(mApplication.getPackageManager()); 67 mController = new AppSettingPreferenceController(mApplication, "test_key"); 68 mController.setPackageName(TEST_PKG_NAME).setParentFragment(mParent); 69 mPreference = new Preference(mApplication); 70 mPreference.setKey(mController.getPreferenceKey()); 71 } 72 73 @Test 74 public void getAvailabilityStatus_noAppSetting_shouldNotBeAvailable() { 75 assertThat(mController.isAvailable()) 76 .isFalse(); 77 } 78 79 @Test 80 public void getAvailabilityStatus_noPackageName_shouldNotBeAvailable() { 81 mController.setPackageName(null); 82 83 assertThat(mController.isAvailable()) 84 .isFalse(); 85 } 86 87 @Test 88 public void getAvailabilityStatus_hasAppSetting_shouldBeAvailable() { 89 final ResolveInfo info = new ResolveInfo(); 90 info.activityInfo = new ActivityInfo(); 91 info.activityInfo.packageName = TEST_PKG_NAME; 92 info.activityInfo.name = TEST_CLASS_NAME; 93 94 mPackageManager.addResolveInfoForIntent(RESOLVED_INTENT, info); 95 96 assertThat(mController.isAvailable()) 97 .isTrue(); 98 } 99 100 @Test 101 public void clickPreference_noAppSetting_shouldDoNothing() { 102 assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); 103 } 104 105 @Test 106 public void clickPreference_hasAppSetting_shouldLaunchIntent() { 107 final ResolveInfo info = new ResolveInfo(); 108 info.activityInfo = new ActivityInfo(); 109 info.activityInfo.packageName = TEST_PKG_NAME; 110 info.activityInfo.name = TEST_CLASS_NAME; 111 112 mPackageManager.addResolveInfoForIntent(RESOLVED_INTENT, info); 113 114 assertThat(mController.handlePreferenceTreeClick(mPreference)).isTrue(); 115 assertThat(shadowOf(mApplication).getNextStartedActivity().getComponent()) 116 .isEqualTo(TEST_INTENT.getComponent()); 117 } 118 } 119