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 android.content.Context; 19 20 import com.android.settings.applications.ApplicationFeatureProvider; 21 import com.android.settings.bluetooth.BluetoothFeatureProvider; 22 import com.android.settings.connecteddevice.SmsMirroringFeatureProvider; 23 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 24 import com.android.settings.dashboard.DashboardFeatureProvider; 25 import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider; 26 import com.android.settings.datausage.DataPlanFeatureProvider; 27 import com.android.settings.enterprise.EnterprisePrivacyFeatureProvider; 28 import com.android.settings.fuelgauge.PowerUsageFeatureProvider; 29 import com.android.settings.gestures.AssistGestureFeatureProvider; 30 import com.android.settings.localepicker.LocaleFeatureProvider; 31 import com.android.settings.overlay.FeatureFactory; 32 import com.android.settings.overlay.SupportFeatureProvider; 33 import com.android.settings.security.SecurityFeatureProvider; 34 import com.android.settings.search.SearchFeatureProvider; 35 import com.android.settings.overlay.SurveyFeatureProvider; 36 import com.android.settings.users.UserFeatureProvider; 37 38 import static org.mockito.Matchers.anyString; 39 import static org.mockito.Mockito.mock; 40 import static org.mockito.Mockito.when; 41 42 /** 43 * Test util to provide fake FeatureFactory. To use this factory, call {@code setupForTest} in 44 * {@code @Before} method of the test class. 45 */ 46 public class FakeFeatureFactory extends FeatureFactory { 47 48 public final SupportFeatureProvider supportFeatureProvider; 49 public final MetricsFeatureProvider metricsFeatureProvider; 50 public final PowerUsageFeatureProvider powerUsageFeatureProvider; 51 public final DashboardFeatureProvider dashboardFeatureProvider; 52 public final LocaleFeatureProvider localeFeatureProvider; 53 public final ApplicationFeatureProvider applicationFeatureProvider; 54 public final EnterprisePrivacyFeatureProvider enterprisePrivacyFeatureProvider; 55 public final SearchFeatureProvider searchFeatureProvider; 56 public final SurveyFeatureProvider surveyFeatureProvider; 57 public final SecurityFeatureProvider securityFeatureProvider; 58 public final SuggestionFeatureProvider suggestionsFeatureProvider; 59 public final UserFeatureProvider userFeatureProvider; 60 public final AssistGestureFeatureProvider assistGestureFeatureProvider; 61 public final BluetoothFeatureProvider bluetoothFeatureProvider; 62 public final DataPlanFeatureProvider dataPlanFeatureProvider; 63 public final SmsMirroringFeatureProvider smsMirroringFeatureProvider; 64 65 /** 66 * Call this in {@code @Before} method of the test class to use fake factory. 67 * 68 * @param context The context must be a deep mock. 69 */ 70 public static FakeFeatureFactory setupForTest(Context context) { 71 sFactory = null; 72 when(context.getString(com.android.settings.R.string.config_featureFactory)) 73 .thenReturn(FakeFeatureFactory.class.getName()); 74 try { 75 Class c = FakeFeatureFactory.class; 76 when(context.getClassLoader().loadClass(anyString())).thenReturn(c); 77 } catch (ClassNotFoundException e) { 78 // Ignore. 79 } 80 return (FakeFeatureFactory) FakeFeatureFactory.getFactory(context); 81 } 82 83 /** 84 * Used by reflection. Do not call directly. 85 */ 86 public FakeFeatureFactory() { 87 supportFeatureProvider = mock(SupportFeatureProvider.class); 88 metricsFeatureProvider = mock(MetricsFeatureProvider.class); 89 powerUsageFeatureProvider = mock(PowerUsageFeatureProvider.class); 90 dashboardFeatureProvider = mock(DashboardFeatureProvider.class); 91 localeFeatureProvider = mock(LocaleFeatureProvider.class); 92 applicationFeatureProvider = mock(ApplicationFeatureProvider.class); 93 enterprisePrivacyFeatureProvider = mock(EnterprisePrivacyFeatureProvider.class); 94 searchFeatureProvider = mock(SearchFeatureProvider.class); 95 surveyFeatureProvider = mock(SurveyFeatureProvider.class); 96 securityFeatureProvider = mock(SecurityFeatureProvider.class); 97 suggestionsFeatureProvider = mock(SuggestionFeatureProvider.class); 98 userFeatureProvider = mock(UserFeatureProvider.class); 99 assistGestureFeatureProvider = mock(AssistGestureFeatureProvider.class); 100 bluetoothFeatureProvider = mock(BluetoothFeatureProvider.class); 101 dataPlanFeatureProvider = mock(DataPlanFeatureProvider.class); 102 smsMirroringFeatureProvider = mock(SmsMirroringFeatureProvider.class); 103 } 104 105 @Override 106 public SuggestionFeatureProvider getSuggestionFeatureProvider(Context context) { 107 return suggestionsFeatureProvider; 108 } 109 110 @Override 111 public SupportFeatureProvider getSupportFeatureProvider(Context context) { 112 return supportFeatureProvider; 113 } 114 115 @Override 116 public MetricsFeatureProvider getMetricsFeatureProvider() { 117 return metricsFeatureProvider; 118 } 119 120 @Override 121 public PowerUsageFeatureProvider getPowerUsageFeatureProvider(Context context) { 122 return powerUsageFeatureProvider; 123 } 124 125 @Override 126 public DashboardFeatureProvider getDashboardFeatureProvider(Context context) { 127 return dashboardFeatureProvider; 128 } 129 130 @Override 131 public ApplicationFeatureProvider getApplicationFeatureProvider(Context context) { 132 return applicationFeatureProvider; 133 } 134 135 @Override 136 public LocaleFeatureProvider getLocaleFeatureProvider() { 137 return localeFeatureProvider; 138 } 139 140 @Override 141 public EnterprisePrivacyFeatureProvider getEnterprisePrivacyFeatureProvider(Context context) { 142 return enterprisePrivacyFeatureProvider; 143 } 144 145 @Override 146 public SearchFeatureProvider getSearchFeatureProvider() { 147 return searchFeatureProvider; 148 } 149 150 @Override 151 public SurveyFeatureProvider getSurveyFeatureProvider(Context context) { 152 return surveyFeatureProvider; 153 } 154 155 @Override 156 public SecurityFeatureProvider getSecurityFeatureProvider() { 157 return securityFeatureProvider; 158 } 159 160 @Override 161 public UserFeatureProvider getUserFeatureProvider(Context context) { 162 return userFeatureProvider; 163 } 164 165 @Override 166 public BluetoothFeatureProvider getBluetoothFeatureProvider(Context context) { 167 return bluetoothFeatureProvider; 168 } 169 170 @Override 171 public DataPlanFeatureProvider getDataPlanFeatureProvider() { 172 return dataPlanFeatureProvider; 173 } 174 175 @Override 176 public AssistGestureFeatureProvider getAssistGestureFeatureProvider() { 177 return assistGestureFeatureProvider; 178 } 179 180 @Override 181 public SmsMirroringFeatureProvider getSmsMirroringFeatureProvider() { 182 return smsMirroringFeatureProvider; 183 } 184 } 185