1 /* 2 * Copyright (C) 2016 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; 18 19 import android.app.AlertDialog; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageInfo; 23 import android.content.pm.PackageManager; 24 import android.support.v7.preference.PreferenceManager; 25 import android.support.v7.preference.PreferenceScreen; 26 27 import com.android.internal.logging.nano.MetricsProto; 28 import com.android.settings.SettingsRobolectricTestRunner; 29 import com.android.settings.TestConfig; 30 import com.android.settings.testutils.FakeFeatureFactory; 31 import com.android.settingslib.applications.AppUtils; 32 import com.android.settingslib.applications.instantapps.InstantAppDataProvider; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Answers; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.annotation.Config; 41 import org.robolectric.shadows.ShadowApplication; 42 import org.robolectric.util.ReflectionHelpers; 43 44 import static org.mockito.Matchers.any; 45 import static org.mockito.Mockito.mock; 46 import static org.mockito.Mockito.verify; 47 import static org.mockito.Mockito.when; 48 49 @RunWith(SettingsRobolectricTestRunner.class) 50 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 51 public class AppInfoWithHeaderTest { 52 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 53 private Context mContext; 54 55 private FakeFeatureFactory mFactory; 56 private TestFragment mAppInfoWithHeader; 57 58 @Before setUp()59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 FakeFeatureFactory.setupForTest(mContext); 62 63 mFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 64 when(mFactory.metricsFeatureProvider.getMetricsCategory(any(Object.class))) 65 .thenReturn(MetricsProto.MetricsEvent.SETTINGS_APP_NOTIF_CATEGORY); 66 mAppInfoWithHeader = new TestFragment(); 67 } 68 69 @Test testAppHeaderIsAdded()70 public void testAppHeaderIsAdded() { 71 final AppHeaderController appHeaderController = new AppHeaderController( 72 ShadowApplication.getInstance().getApplicationContext(), 73 mAppInfoWithHeader, 74 null); 75 when(mFactory.applicationFeatureProvider.newAppHeaderController(mAppInfoWithHeader, null)) 76 .thenReturn(appHeaderController); 77 mAppInfoWithHeader.onActivityCreated(null); 78 79 verify(mAppInfoWithHeader.mScreen).addPreference(any(LayoutPreference.class)); 80 } 81 82 public static class TestFragment extends AppInfoWithHeader { 83 84 PreferenceManager mManager; 85 PreferenceScreen mScreen; 86 Context mShadowContext; 87 TestFragment()88 public TestFragment() { 89 mPm = mock(PackageManager.class); 90 mManager = mock(PreferenceManager.class); 91 mScreen = mock(PreferenceScreen.class); 92 mPackageInfo = new PackageInfo(); 93 mPackageInfo.applicationInfo = new ApplicationInfo(); 94 mShadowContext = ShadowApplication.getInstance().getApplicationContext(); 95 ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider", 96 (InstantAppDataProvider) (info -> false)); 97 when(mManager.getContext()).thenReturn(mShadowContext); 98 } 99 100 @Override getMetricsCategory()101 public int getMetricsCategory() { 102 return 0; 103 } 104 105 @Override refreshUi()106 protected boolean refreshUi() { 107 return false; 108 } 109 110 @Override createDialog(int id, int errorCode)111 protected AlertDialog createDialog(int id, int errorCode) { 112 return null; 113 } 114 115 @Override getPreferenceScreen()116 public PreferenceScreen getPreferenceScreen() { 117 return mScreen; 118 } 119 120 @Override getPreferenceManager()121 public PreferenceManager getPreferenceManager() { 122 return mManager; 123 } 124 125 @Override getContext()126 public Context getContext() { 127 return mShadowContext; 128 } 129 } 130 131 } 132