1 /*
2  * Copyright (C) 2015 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.messaging;
18 
19 import android.content.ContentProvider;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.provider.Settings;
23 import android.telephony.SubscriptionInfo;
24 import android.telephony.SubscriptionManager;
25 
26 import com.android.messaging.datamodel.DataModel;
27 import com.android.messaging.datamodel.MemoryCacheManager;
28 import com.android.messaging.datamodel.ParticipantRefresh.ContactContentObserver;
29 import com.android.messaging.datamodel.media.MediaCacheManager;
30 import com.android.messaging.datamodel.media.MediaResourceManager;
31 import com.android.messaging.sms.ApnDatabase;
32 import com.android.messaging.sms.BugleCarrierConfigValuesLoader;
33 import com.android.messaging.ui.UIIntents;
34 import com.android.messaging.util.Assert;
35 import com.android.messaging.util.BugleGservices;
36 import com.android.messaging.util.BuglePrefs;
37 import com.android.messaging.util.FakeBugleGservices;
38 import com.android.messaging.util.FakeBuglePrefs;
39 import com.android.messaging.util.MediaUtil;
40 import com.android.messaging.util.OsUtil;
41 import com.android.messaging.util.PhoneUtils;
42 
43 import org.mockito.Matchers;
44 import org.mockito.Mock;
45 import org.mockito.Mockito;
46 import org.mockito.invocation.InvocationOnMock;
47 import org.mockito.stubbing.Answer;
48 
49 import java.util.ArrayList;
50 import java.util.List;
51 
52 public class FakeFactory extends Factory {
53     private Context mContext;
54     private FakeContext mFakeContext;
55     private BugleGservices mBugleGservices;
56     private BuglePrefs mBuglePrefs;
57     private DataModel mDataModel;
58     private UIIntents mUIIntents;
59     private MemoryCacheManager mMemoryCacheManager;
60     private MediaResourceManager mMediaResourceManager;
61     private MediaCacheManager mMediaCacheManager;
62     @Mock protected PhoneUtils mPhoneUtils;
63     private MediaUtil mMediaUtil;
64     private BugleCarrierConfigValuesLoader mCarrierConfigValuesLoader;
65 
FakeFactory()66     private FakeFactory() {
67     }
68 
registerWithFakeContext(final Context context, final FakeContext fake)69     public static FakeFactory registerWithFakeContext(final Context context,
70             final FakeContext fake) {
71         // In tests we currently NEVER run the application/factory initialization
72         Assert.isTrue(!sRegistered);
73         Assert.isTrue(!sInitialized);
74 
75         final FakeFactory factory = new FakeFactory();
76         Factory.setInstance(factory);
77 
78         // At this point Factory is published. Services can now get initialized and depend on
79         // Factory.get().
80         factory.mContext = context;
81         factory.mFakeContext = fake;
82         factory.mMediaResourceManager = Mockito.mock(MediaResourceManager.class);
83         factory.mBugleGservices = new FakeBugleGservices();
84         factory.mBuglePrefs = new FakeBuglePrefs();
85         factory.mPhoneUtils = Mockito.mock(PhoneUtils.class);
86 
87         ApnDatabase.initializeAppContext(context);
88 
89         Mockito.when(factory.mPhoneUtils.getCanonicalBySystemLocale(Matchers.anyString()))
90                 .thenAnswer(new Answer<String>() {
91                         @Override
92                         public String answer(final InvocationOnMock invocation) throws Throwable {
93                             final Object[] args = invocation.getArguments();
94                             return (String) args[0];
95                         }
96                     }
97                 );
98         Mockito.when(factory.mPhoneUtils.getCanonicalBySimLocale(Matchers.anyString())).thenAnswer(
99                 new Answer<String>() {
100                     @Override
101                     public String answer(final InvocationOnMock invocation) throws Throwable {
102                         final Object[] args = invocation.getArguments();
103                         return (String) args[0];
104                     }
105                 }
106         );
107         Mockito.when(factory.mPhoneUtils.formatForDisplay(Matchers.anyString())).thenAnswer(
108                 new Answer<String>() {
109                     @Override
110                     public String answer(final InvocationOnMock invocation) throws Throwable {
111                         return (String) invocation.getArguments()[0];
112                     }
113                 }
114         );
115         if (OsUtil.isAtLeastL_MR1()) {
116             Mockito.when(factory.mPhoneUtils.toLMr1()).thenReturn(
117                     new PhoneUtils.LMr1() {
118                         @Override
119                         public SubscriptionInfo getActiveSubscriptionInfo() {
120                             return null;
121                         }
122 
123                         @Override
124                         public List<SubscriptionInfo> getActiveSubscriptionInfoList() {
125                             // Return empty list
126                             return new ArrayList<>();
127                         }
128 
129                         @Override
130                         public void registerOnSubscriptionsChangedListener(
131                                 final SubscriptionManager.OnSubscriptionsChangedListener listener) {
132                         }
133                     }
134             );
135         }
136         // By default only allow reading of system settings (that we provide) - can delegate
137         // to real provider if required.
138         final FakeContentProvider settings = new FakeContentProvider(context,
139                 Settings.System.CONTENT_URI, false);
140         settings.addOverrideData(Settings.System.CONTENT_URI, "name=?", "time_12_24",
141                 new String[] { "value" }, new Object[][] { { "12" } });
142         settings.addOverrideData(Settings.System.CONTENT_URI, "name=?", "sound_effects_enabled",
143                 new String[] { "value" }, new Object[][] { { 1 } });
144 
145         factory.withProvider(Settings.System.CONTENT_URI, settings);
146 
147         return factory;
148     }
149 
register(final Context applicationContext)150     public static FakeFactory register(final Context applicationContext) {
151         final FakeContext context = new FakeContext(applicationContext);
152         return registerWithFakeContext(applicationContext, context);
153     }
154 
registerWithoutFakeContext(final Context applicationContext)155     public static FakeFactory registerWithoutFakeContext(final Context applicationContext) {
156         return registerWithFakeContext(applicationContext, null);
157     }
158 
159     @Override
onRequiredPermissionsAcquired()160     public void onRequiredPermissionsAcquired() {
161     }
162 
163     @Override
getApplicationContext()164     public Context getApplicationContext() {
165         return ((mFakeContext != null) ? mFakeContext : mContext );
166     }
167 
168     @Override
getDataModel()169     public DataModel getDataModel() {
170         return mDataModel;
171     }
172 
173     @Override
getBugleGservices()174     public BugleGservices getBugleGservices() {
175         return mBugleGservices;
176     }
177 
178     @Override
getApplicationPrefs()179     public BuglePrefs getApplicationPrefs() {
180         return mBuglePrefs;
181     }
182 
183     @Override
getWidgetPrefs()184     public BuglePrefs getWidgetPrefs() {
185         return mBuglePrefs;
186     }
187 
188     @Override
getSubscriptionPrefs(final int subId)189     public BuglePrefs getSubscriptionPrefs(final int subId) {
190         return mBuglePrefs;
191     }
192 
193     @Override
getUIIntents()194     public UIIntents getUIIntents() {
195         return mUIIntents;
196     }
197 
198     @Override
getMemoryCacheManager()199     public MemoryCacheManager getMemoryCacheManager() {
200         return mMemoryCacheManager;
201     }
202 
203     @Override
getMediaResourceManager()204     public MediaResourceManager getMediaResourceManager() {
205         return mMediaResourceManager;
206     }
207 
208     @Override
getMediaCacheManager()209     public MediaCacheManager getMediaCacheManager() {
210         return mMediaCacheManager;
211     }
212 
213     @Override
getPhoneUtils(final int subId)214     public PhoneUtils getPhoneUtils(final int subId) {
215         return mPhoneUtils;
216     }
217 
218     @Override
getMediaUtil()219     public MediaUtil getMediaUtil() {
220         return mMediaUtil;
221     }
222 
223     @Override
getCarrierConfigValuesLoader()224     public BugleCarrierConfigValuesLoader getCarrierConfigValuesLoader() {
225         return mCarrierConfigValuesLoader;
226     }
227 
228     @Override
getContactContentObserver()229     public ContactContentObserver getContactContentObserver() {
230         return null;
231     }
232 
233     @Override
reclaimMemory()234     public void reclaimMemory() {
235     }
236 
237     @Override
onActivityResume()238     public void onActivityResume() {
239     }
240 
withDataModel(final DataModel dataModel)241     public FakeFactory withDataModel(final DataModel dataModel) {
242         this.mDataModel = dataModel;
243         return this;
244     }
245 
withUIIntents(final UIIntents uiIntents)246     public FakeFactory withUIIntents(final UIIntents uiIntents) {
247         this.mUIIntents = uiIntents;
248         return this;
249     }
250 
withMemoryCacheManager(final MemoryCacheManager memoryCacheManager)251     public FakeFactory withMemoryCacheManager(final MemoryCacheManager memoryCacheManager) {
252         this.mMemoryCacheManager = memoryCacheManager;
253         return this;
254     }
255 
withBugleGservices(final BugleGservices bugleGservices)256     public FakeFactory withBugleGservices(final BugleGservices bugleGservices) {
257         this.mBugleGservices = bugleGservices;
258         return this;
259     }
260 
withMediaCacheManager(final MediaCacheManager mediaCacheManager)261     public FakeFactory withMediaCacheManager(final MediaCacheManager mediaCacheManager) {
262         this.mMediaCacheManager = mediaCacheManager;
263         return this;
264     }
265 
withProvider(final Uri uri, final ContentProvider provider)266     public FakeFactory withProvider(final Uri uri, final ContentProvider provider) {
267         if (mFakeContext != null) {
268             mFakeContext.addContentProvider(uri.getAuthority(), provider);
269         }
270         return this;
271     }
272 
withDefaultProvider(final Uri uri)273     public FakeFactory withDefaultProvider(final Uri uri) {
274         if (mFakeContext != null) {
275             mFakeContext.addDefaultProvider(this.mContext, uri);
276         }
277         return this;
278     }
279 
withMediaUtil(final MediaUtil mediaUtil)280     public FakeFactory withMediaUtil(final MediaUtil mediaUtil) {
281         this.mMediaUtil = mediaUtil;
282         return this;
283     }
284 
withCarrierConfigValuesLoader( final BugleCarrierConfigValuesLoader carrierConfigValuesLoader)285     public FakeFactory withCarrierConfigValuesLoader(
286             final BugleCarrierConfigValuesLoader carrierConfigValuesLoader) {
287         this.mCarrierConfigValuesLoader = carrierConfigValuesLoader;
288         return this;
289     }
290 }
291