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