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 package com.android.settings.testutils; 17 18 import static org.mockito.ArgumentMatchers.anyString; 19 import static org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.when; 21 22 import android.content.Context; 23 24 import com.android.settings.accounts.AccountFeatureProvider; 25 import com.android.settings.applications.ApplicationFeatureProvider; 26 import com.android.settings.aware.AwareFeatureProvider; 27 import com.android.settings.biometrics.face.FaceFeatureProvider; 28 import com.android.settings.bluetooth.BluetoothFeatureProvider; 29 import com.android.settings.dashboard.DashboardFeatureProvider; 30 import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider; 31 import com.android.settings.enterprise.EnterprisePrivacyFeatureProvider; 32 import com.android.settings.fuelgauge.PowerUsageFeatureProvider; 33 import com.android.settings.gestures.AssistGestureFeatureProvider; 34 import com.android.settings.homepage.contextualcards.ContextualCardFeatureProvider; 35 import com.android.settings.localepicker.LocaleFeatureProvider; 36 import com.android.settings.overlay.DockUpdaterFeatureProvider; 37 import com.android.settings.overlay.FeatureFactory; 38 import com.android.settings.overlay.SupportFeatureProvider; 39 import com.android.settings.overlay.SurveyFeatureProvider; 40 import com.android.settings.panel.PanelFeatureProvider; 41 import com.android.settings.search.SearchFeatureProvider; 42 import com.android.settings.security.SecurityFeatureProvider; 43 import com.android.settings.slices.SlicesFeatureProvider; 44 import com.android.settings.users.UserFeatureProvider; 45 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 46 47 import org.mockito.Answers; 48 49 /** 50 * Test util to provide fake FeatureFactory. To use this factory, call {@code setupForTest} in 51 * {@code @Before} method of the test class. 52 */ 53 public class FakeFeatureFactory extends FeatureFactory { 54 55 public final SupportFeatureProvider supportFeatureProvider; 56 public final MetricsFeatureProvider metricsFeatureProvider; 57 public final PowerUsageFeatureProvider powerUsageFeatureProvider; 58 public final DashboardFeatureProvider dashboardFeatureProvider; 59 public final DockUpdaterFeatureProvider dockUpdaterFeatureProvider; 60 public final LocaleFeatureProvider localeFeatureProvider; 61 public final ApplicationFeatureProvider applicationFeatureProvider; 62 public final EnterprisePrivacyFeatureProvider enterprisePrivacyFeatureProvider; 63 public final SurveyFeatureProvider surveyFeatureProvider; 64 public final SecurityFeatureProvider securityFeatureProvider; 65 public final SuggestionFeatureProvider suggestionsFeatureProvider; 66 public final UserFeatureProvider userFeatureProvider; 67 public final AssistGestureFeatureProvider assistGestureFeatureProvider; 68 public final AccountFeatureProvider mAccountFeatureProvider; 69 public final ContextualCardFeatureProvider mContextualCardFeatureProvider; 70 public final BluetoothFeatureProvider mBluetoothFeatureProvider; 71 public final AwareFeatureProvider mAwareFeatureProvider; 72 public final FaceFeatureProvider mFaceFeatureProvider; 73 74 public PanelFeatureProvider panelFeatureProvider; 75 public SlicesFeatureProvider slicesFeatureProvider; 76 public SearchFeatureProvider searchFeatureProvider; 77 78 /** 79 * Call this in {@code @Before} method of the test class to use fake factory. 80 */ setupForTest()81 public static FakeFeatureFactory setupForTest() { 82 final Context context = mock(Context.class, Answers.RETURNS_DEEP_STUBS); 83 sFactory = null; 84 when(context.getString(com.android.settings.R.string.config_featureFactory)) 85 .thenReturn(FakeFeatureFactory.class.getName()); 86 try { 87 Class c = FakeFeatureFactory.class; 88 when(context.getClassLoader().loadClass(anyString())).thenReturn(c); 89 } catch (ClassNotFoundException e) { 90 // Ignore. 91 } 92 return (FakeFeatureFactory) FakeFeatureFactory.getFactory(context); 93 } 94 95 /** 96 * Used by reflection. Do not call directly. 97 */ FakeFeatureFactory()98 public FakeFeatureFactory() { 99 supportFeatureProvider = mock(SupportFeatureProvider.class); 100 metricsFeatureProvider = mock(MetricsFeatureProvider.class); 101 powerUsageFeatureProvider = mock(PowerUsageFeatureProvider.class); 102 dashboardFeatureProvider = mock(DashboardFeatureProvider.class); 103 dockUpdaterFeatureProvider = mock(DockUpdaterFeatureProvider.class); 104 localeFeatureProvider = mock(LocaleFeatureProvider.class); 105 applicationFeatureProvider = mock(ApplicationFeatureProvider.class); 106 enterprisePrivacyFeatureProvider = mock(EnterprisePrivacyFeatureProvider.class); 107 searchFeatureProvider = mock(SearchFeatureProvider.class); 108 surveyFeatureProvider = mock(SurveyFeatureProvider.class); 109 securityFeatureProvider = mock(SecurityFeatureProvider.class); 110 suggestionsFeatureProvider = mock(SuggestionFeatureProvider.class); 111 userFeatureProvider = mock(UserFeatureProvider.class); 112 assistGestureFeatureProvider = mock(AssistGestureFeatureProvider.class); 113 slicesFeatureProvider = mock(SlicesFeatureProvider.class); 114 mAccountFeatureProvider = mock(AccountFeatureProvider.class); 115 mContextualCardFeatureProvider = mock(ContextualCardFeatureProvider.class); 116 panelFeatureProvider = mock(PanelFeatureProvider.class); 117 mBluetoothFeatureProvider = mock(BluetoothFeatureProvider.class); 118 mAwareFeatureProvider = mock(AwareFeatureProvider.class); 119 mFaceFeatureProvider = mock(FaceFeatureProvider.class); 120 } 121 122 @Override getSuggestionFeatureProvider(Context context)123 public SuggestionFeatureProvider getSuggestionFeatureProvider(Context context) { 124 return suggestionsFeatureProvider; 125 } 126 127 @Override getSupportFeatureProvider(Context context)128 public SupportFeatureProvider getSupportFeatureProvider(Context context) { 129 return supportFeatureProvider; 130 } 131 132 @Override getMetricsFeatureProvider()133 public MetricsFeatureProvider getMetricsFeatureProvider() { 134 return metricsFeatureProvider; 135 } 136 137 @Override getPowerUsageFeatureProvider(Context context)138 public PowerUsageFeatureProvider getPowerUsageFeatureProvider(Context context) { 139 return powerUsageFeatureProvider; 140 } 141 142 @Override getDashboardFeatureProvider(Context context)143 public DashboardFeatureProvider getDashboardFeatureProvider(Context context) { 144 return dashboardFeatureProvider; 145 } 146 147 @Override getDockUpdaterFeatureProvider()148 public DockUpdaterFeatureProvider getDockUpdaterFeatureProvider() { 149 return dockUpdaterFeatureProvider; 150 } 151 152 @Override getApplicationFeatureProvider(Context context)153 public ApplicationFeatureProvider getApplicationFeatureProvider(Context context) { 154 return applicationFeatureProvider; 155 } 156 157 @Override getLocaleFeatureProvider()158 public LocaleFeatureProvider getLocaleFeatureProvider() { 159 return localeFeatureProvider; 160 } 161 162 @Override getEnterprisePrivacyFeatureProvider(Context context)163 public EnterprisePrivacyFeatureProvider getEnterprisePrivacyFeatureProvider(Context context) { 164 return enterprisePrivacyFeatureProvider; 165 } 166 167 @Override getSearchFeatureProvider()168 public SearchFeatureProvider getSearchFeatureProvider() { 169 return searchFeatureProvider; 170 } 171 172 @Override getSurveyFeatureProvider(Context context)173 public SurveyFeatureProvider getSurveyFeatureProvider(Context context) { 174 return surveyFeatureProvider; 175 } 176 177 @Override getSecurityFeatureProvider()178 public SecurityFeatureProvider getSecurityFeatureProvider() { 179 return securityFeatureProvider; 180 } 181 182 @Override getUserFeatureProvider(Context context)183 public UserFeatureProvider getUserFeatureProvider(Context context) { 184 return userFeatureProvider; 185 } 186 187 @Override getAssistGestureFeatureProvider()188 public AssistGestureFeatureProvider getAssistGestureFeatureProvider() { 189 return assistGestureFeatureProvider; 190 } 191 192 @Override getSlicesFeatureProvider()193 public SlicesFeatureProvider getSlicesFeatureProvider() { 194 return slicesFeatureProvider; 195 } 196 197 @Override getAccountFeatureProvider()198 public AccountFeatureProvider getAccountFeatureProvider() { 199 return mAccountFeatureProvider; 200 } 201 202 @Override getPanelFeatureProvider()203 public PanelFeatureProvider getPanelFeatureProvider() { 204 return panelFeatureProvider; 205 } 206 207 @Override getContextualCardFeatureProvider(Context context)208 public ContextualCardFeatureProvider getContextualCardFeatureProvider(Context context) { 209 return mContextualCardFeatureProvider; 210 } 211 212 @Override getBluetoothFeatureProvider(Context context)213 public BluetoothFeatureProvider getBluetoothFeatureProvider(Context context) { 214 return mBluetoothFeatureProvider; 215 } 216 217 @Override getAwareFeatureProvider()218 public AwareFeatureProvider getAwareFeatureProvider() { 219 return mAwareFeatureProvider; 220 } 221 222 @Override getFaceFeatureProvider()223 public FaceFeatureProvider getFaceFeatureProvider() { 224 return mFaceFeatureProvider; 225 } 226 } 227