• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.datamodel;
18 
19 import android.content.Context;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.net.Uri;
22 import android.test.RenamingDelegatingContext;
23 
24 import com.android.messaging.datamodel.action.ActionService;
25 import com.android.messaging.datamodel.action.BackgroundWorker;
26 import com.android.messaging.datamodel.data.BlockedParticipantsData;
27 import com.android.messaging.datamodel.data.BlockedParticipantsData.BlockedParticipantsDataListener;
28 import com.android.messaging.datamodel.data.ContactListItemData;
29 import com.android.messaging.datamodel.data.ContactPickerData;
30 import com.android.messaging.datamodel.data.ContactPickerData.ContactPickerDataListener;
31 import com.android.messaging.datamodel.data.ConversationData;
32 import com.android.messaging.datamodel.data.ConversationData.ConversationDataListener;
33 import com.android.messaging.datamodel.data.ConversationListData;
34 import com.android.messaging.datamodel.data.ConversationListData.ConversationListDataListener;
35 import com.android.messaging.datamodel.data.DraftMessageData;
36 import com.android.messaging.datamodel.data.GalleryGridItemData;
37 import com.android.messaging.datamodel.data.LaunchConversationData;
38 import com.android.messaging.datamodel.data.LaunchConversationData.LaunchConversationDataListener;
39 import com.android.messaging.datamodel.data.MediaPickerData;
40 import com.android.messaging.datamodel.data.MessagePartData;
41 import com.android.messaging.datamodel.data.ParticipantData;
42 import com.android.messaging.datamodel.data.ParticipantListItemData;
43 import com.android.messaging.datamodel.data.PeopleAndOptionsData;
44 import com.android.messaging.datamodel.data.PeopleAndOptionsData.PeopleAndOptionsDataListener;
45 import com.android.messaging.datamodel.data.PeopleOptionsItemData;
46 import com.android.messaging.datamodel.data.SettingsData;
47 import com.android.messaging.datamodel.data.SettingsData.SettingsDataListener;
48 import com.android.messaging.datamodel.data.SubscriptionListData;
49 import com.android.messaging.datamodel.data.TestDataFactory;
50 import com.android.messaging.datamodel.data.VCardContactItemData;
51 
52 public class FakeDataModel extends DataModel {
53     private BackgroundWorker mWorker;
54     private ActionService mActionService;
55     private final DatabaseHelper mDatabaseHelper;
56     private ConversationListData mConversationListData;
57     private ContactPickerData mContactPickerData;
58     private MediaPickerData mMediaPickerData;
59     private PeopleAndOptionsData mPeopleAndOptionsData;
60     private SyncManager mSyncManager;
61     private SettingsData mSettingsData;
62     private DraftMessageData mDraftMessageData;
63 
FakeDataModel(final Context context)64     public FakeDataModel(final Context context) {
65         super();
66         if (context instanceof RenamingDelegatingContext) {
67             mDatabaseHelper = DatabaseHelper.getNewInstanceForTest(context);
68         } else {
69             mDatabaseHelper = null;
70         }
71     }
72 
73     @Override
getBackgroundWorkerForActionService()74     public BackgroundWorker getBackgroundWorkerForActionService() {
75         return mWorker;
76     }
77 
withBackgroundWorkerForActionService(final BackgroundWorker worker)78     public FakeDataModel withBackgroundWorkerForActionService(final BackgroundWorker worker) {
79         mWorker = worker;
80         return this;
81     }
82 
withActionService(final ActionService ActionService)83     public FakeDataModel withActionService(final ActionService ActionService) {
84         mActionService = ActionService;
85         return this;
86     }
87 
withConversationListData(final ConversationListData conversationListData)88     public FakeDataModel withConversationListData(final ConversationListData conversationListData) {
89         mConversationListData = conversationListData;
90         return this;
91     }
92 
withContactPickerData(final ContactPickerData contactPickerData)93     public FakeDataModel withContactPickerData(final ContactPickerData contactPickerData) {
94         mContactPickerData = contactPickerData;
95         return this;
96     }
97 
withMediaPickerData(final MediaPickerData mediaPickerData)98     public FakeDataModel withMediaPickerData(final MediaPickerData mediaPickerData) {
99         mMediaPickerData = mediaPickerData;
100         return this;
101     }
102 
withSyncManager(final SyncManager syncManager)103     public FakeDataModel withSyncManager(final SyncManager syncManager) {
104         mSyncManager = syncManager;
105         return this;
106     }
107 
withPeopleAndOptionsData(final PeopleAndOptionsData peopleAndOptionsData)108     public FakeDataModel withPeopleAndOptionsData(final PeopleAndOptionsData peopleAndOptionsData) {
109         mPeopleAndOptionsData = peopleAndOptionsData;
110         return this;
111     }
112 
withSettingsData(final SettingsData settingsData)113     public FakeDataModel withSettingsData(final SettingsData settingsData) {
114         mSettingsData = settingsData;
115         return this;
116     }
117 
withDraftMessageData(final DraftMessageData draftMessageData)118     public FakeDataModel withDraftMessageData(final DraftMessageData draftMessageData) {
119         mDraftMessageData = draftMessageData;
120         return this;
121     }
122 
123     @Override
createConversationListData(final Context context, final ConversationListDataListener listener, final boolean archivedMode)124     public ConversationListData createConversationListData(final Context context,
125             final ConversationListDataListener listener, final boolean archivedMode) {
126         return mConversationListData;
127     }
128 
129     @Override
createConversationData(final Context context, final ConversationDataListener listener, final String conversationId)130     public ConversationData createConversationData(final Context context,
131             final ConversationDataListener listener, final String conversationId) {
132         throw new IllegalStateException("Add withXXX or mock this method");
133     }
134 
135     @Override
createContactListItemData()136     public ContactListItemData createContactListItemData() {
137         // This is a lightweight data holder object for each individual list item for which
138         // we don't perform any data request, so we can directly return a new instance.
139         return new ContactListItemData();
140     }
141 
142     @Override
createContactPickerData(final Context context, final ContactPickerDataListener listener)143     public ContactPickerData createContactPickerData(final Context context,
144             final ContactPickerDataListener listener) {
145         return mContactPickerData;
146     }
147 
148     @Override
createMediaPickerData(final Context context)149     public MediaPickerData createMediaPickerData(final Context context) {
150         return mMediaPickerData;
151     }
152 
153     @Override
createGalleryGridItemData()154     public GalleryGridItemData createGalleryGridItemData() {
155         // This is a lightweight data holder object for each individual grid item for which
156         // we don't perform any data request, so we can directly return a new instance.
157         return new GalleryGridItemData();
158     }
159 
160     @Override
createLaunchConversationData( final LaunchConversationDataListener listener)161     public LaunchConversationData createLaunchConversationData(
162             final LaunchConversationDataListener listener) {
163        return new LaunchConversationData(listener);
164     }
165 
166     @Override
createPeopleOptionsItemData(final Context context)167     public PeopleOptionsItemData createPeopleOptionsItemData(final Context context) {
168         return new PeopleOptionsItemData(context);
169     }
170 
171     @Override
createPeopleAndOptionsData(final String conversationId, final Context context, final PeopleAndOptionsDataListener listener)172     public PeopleAndOptionsData createPeopleAndOptionsData(final String conversationId,
173             final Context context, final PeopleAndOptionsDataListener listener) {
174         return mPeopleAndOptionsData;
175     }
176 
177     @Override
createVCardContactItemData(final Context context, final MessagePartData data)178     public VCardContactItemData createVCardContactItemData(final Context context,
179             final MessagePartData data) {
180         return new VCardContactItemData(context, data);
181     }
182 
183     @Override
createVCardContactItemData(final Context context, final Uri vCardUri)184     public VCardContactItemData createVCardContactItemData(final Context context,
185             final Uri vCardUri) {
186         return new VCardContactItemData(context, vCardUri);
187     }
188 
189     @Override
createParticipantListItemData( final ParticipantData participant)190     public ParticipantListItemData createParticipantListItemData(
191             final ParticipantData participant) {
192         return new ParticipantListItemData(participant);
193     }
194 
195     @Override
createSubscriptonListData(Context context)196     public SubscriptionListData createSubscriptonListData(Context context) {
197         return new SubscriptionListData(context);
198     }
199 
200     @Override
createSettingsData(Context context, SettingsDataListener listener)201     public SettingsData createSettingsData(Context context, SettingsDataListener listener) {
202         return mSettingsData;
203     }
204 
205     @Override
createDraftMessageData(String conversationId)206     public DraftMessageData createDraftMessageData(String conversationId) {
207         return mDraftMessageData;
208     }
209 
210     @Override
getActionService()211     public ActionService getActionService() {
212         return mActionService;
213     }
214 
215     @Override
getSyncManager()216     public SyncManager getSyncManager() {
217         return mSyncManager;
218     }
219 
220     @Override
getDatabase()221     public DatabaseWrapper getDatabase() {
222         // Note this will crash unless the application context is redirected...
223         // This is by design so that tests do not inadvertently use the real database
224         return mDatabaseHelper.getDatabase();
225     }
226 
227     @Override
onCreateTables(final SQLiteDatabase db)228     void onCreateTables(final SQLiteDatabase db) {
229         TestDataFactory.createTestData(db);
230     }
231 
232     @Override
onActivityResume()233     public void onActivityResume() {
234     }
235 
236     @Override
onApplicationCreated()237     public void onApplicationCreated() {
238     }
239 
240     @Override
createBlockedParticipantsData(Context context, BlockedParticipantsDataListener listener)241     public BlockedParticipantsData createBlockedParticipantsData(Context context,
242             BlockedParticipantsDataListener listener) {
243         return new BlockedParticipantsData(context, listener);
244     }
245 }
246