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