summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/messaging/datamodel/FakeDataModel.java
blob: 5e80eab4bf9a5672c50131402e52872a54b4afdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.datamodel;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.test.RenamingDelegatingContext;

import com.android.messaging.datamodel.action.ActionService;
import com.android.messaging.datamodel.action.BackgroundWorker;
import com.android.messaging.datamodel.data.BlockedParticipantsData;
import com.android.messaging.datamodel.data.BlockedParticipantsData.BlockedParticipantsDataListener;
import com.android.messaging.datamodel.data.ContactListItemData;
import com.android.messaging.datamodel.data.ContactPickerData;
import com.android.messaging.datamodel.data.ContactPickerData.ContactPickerDataListener;
import com.android.messaging.datamodel.data.ConversationData;
import com.android.messaging.datamodel.data.ConversationData.ConversationDataListener;
import com.android.messaging.datamodel.data.ConversationListData;
import com.android.messaging.datamodel.data.ConversationListData.ConversationListDataListener;
import com.android.messaging.datamodel.data.DraftMessageData;
import com.android.messaging.datamodel.data.GalleryGridItemData;
import com.android.messaging.datamodel.data.LaunchConversationData;
import com.android.messaging.datamodel.data.LaunchConversationData.LaunchConversationDataListener;
import com.android.messaging.datamodel.data.MediaPickerData;
import com.android.messaging.datamodel.data.MessagePartData;
import com.android.messaging.datamodel.data.ParticipantData;
import com.android.messaging.datamodel.data.ParticipantListItemData;
import com.android.messaging.datamodel.data.PeopleAndOptionsData;
import com.android.messaging.datamodel.data.PeopleAndOptionsData.PeopleAndOptionsDataListener;
import com.android.messaging.datamodel.data.PeopleOptionsItemData;
import com.android.messaging.datamodel.data.SettingsData;
import com.android.messaging.datamodel.data.SettingsData.SettingsDataListener;
import com.android.messaging.datamodel.data.SubscriptionListData;
import com.android.messaging.datamodel.data.TestDataFactory;
import com.android.messaging.datamodel.data.VCardContactItemData;
import com.android.messaging.util.ConnectivityUtil;

public class FakeDataModel extends DataModel {
    private BackgroundWorker mWorker;
    private ActionService mActionService;
    private final DatabaseHelper mDatabaseHelper;
    private ConversationListData mConversationListData;
    private ContactPickerData mContactPickerData;
    private MediaPickerData mMediaPickerData;
    private PeopleAndOptionsData mPeopleAndOptionsData;
    private ConnectivityUtil mConnectivityUtil;
    private SyncManager mSyncManager;
    private SettingsData mSettingsData;
    private DraftMessageData mDraftMessageData;

    public FakeDataModel(final Context context) {
        super();
        if (context instanceof RenamingDelegatingContext) {
            mDatabaseHelper = DatabaseHelper.getNewInstanceForTest(context);
        } else {
            mDatabaseHelper = null;
        }
    }

    @Override
    public BackgroundWorker getBackgroundWorkerForActionService() {
        return mWorker;
    }

    public FakeDataModel withBackgroundWorkerForActionService(final BackgroundWorker worker) {
        mWorker = worker;
        return this;
    }

    public FakeDataModel withActionService(final ActionService ActionService) {
        mActionService = ActionService;
        return this;
    }

    public FakeDataModel withConversationListData(final ConversationListData conversationListData) {
        mConversationListData = conversationListData;
        return this;
    }

    public FakeDataModel withContactPickerData(final ContactPickerData contactPickerData) {
        mContactPickerData = contactPickerData;
        return this;
    }

    public FakeDataModel withMediaPickerData(final MediaPickerData mediaPickerData) {
        mMediaPickerData = mediaPickerData;
        return this;
    }

    public FakeDataModel withConnectivityUtil(final ConnectivityUtil connectivityUtil) {
        mConnectivityUtil = connectivityUtil;
        return this;
    }

    public FakeDataModel withSyncManager(final SyncManager syncManager) {
        mSyncManager = syncManager;
        return this;
    }

    public FakeDataModel withPeopleAndOptionsData(final PeopleAndOptionsData peopleAndOptionsData) {
        mPeopleAndOptionsData = peopleAndOptionsData;
        return this;
    }

    public FakeDataModel withSettingsData(final SettingsData settingsData) {
        mSettingsData = settingsData;
        return this;
    }

    public FakeDataModel withDraftMessageData(final DraftMessageData draftMessageData) {
        mDraftMessageData = draftMessageData;
        return this;
    }

    @Override
    public ConversationListData createConversationListData(final Context context,
            final ConversationListDataListener listener, final boolean archivedMode) {
        return mConversationListData;
    }

    @Override
    public ConversationData createConversationData(final Context context,
            final ConversationDataListener listener, final String conversationId) {
        throw new IllegalStateException("Add withXXX or mock this method");
    }

    @Override
    public ContactListItemData createContactListItemData() {
        // This is a lightweight data holder object for each individual list item for which
        // we don't perform any data request, so we can directly return a new instance.
        return new ContactListItemData();
    }

    @Override
    public ContactPickerData createContactPickerData(final Context context,
            final ContactPickerDataListener listener) {
        return mContactPickerData;
    }

    @Override
    public MediaPickerData createMediaPickerData(final Context context) {
        return mMediaPickerData;
    }

    @Override
    public GalleryGridItemData createGalleryGridItemData() {
        // This is a lightweight data holder object for each individual grid item for which
        // we don't perform any data request, so we can directly return a new instance.
        return new GalleryGridItemData();
    }

    @Override
    public LaunchConversationData createLaunchConversationData(
            final LaunchConversationDataListener listener) {
       return new LaunchConversationData(listener);
    }

    @Override
    public PeopleOptionsItemData createPeopleOptionsItemData(final Context context) {
        return new PeopleOptionsItemData(context);
    }

    @Override
    public PeopleAndOptionsData createPeopleAndOptionsData(final String conversationId,
            final Context context, final PeopleAndOptionsDataListener listener) {
        return mPeopleAndOptionsData;
    }

    @Override
    public VCardContactItemData createVCardContactItemData(final Context context,
            final MessagePartData data) {
        return new VCardContactItemData(context, data);
    }

    @Override
    public VCardContactItemData createVCardContactItemData(final Context context,
            final Uri vCardUri) {
        return new VCardContactItemData(context, vCardUri);
    }

    @Override
    public ParticipantListItemData createParticipantListItemData(
            final ParticipantData participant) {
        return new ParticipantListItemData(participant);
    }

    @Override
    public SubscriptionListData createSubscriptonListData(Context context) {
        return new SubscriptionListData(context);
    }

    @Override
    public SettingsData createSettingsData(Context context, SettingsDataListener listener) {
        return mSettingsData;
    }

    @Override
    public DraftMessageData createDraftMessageData(String conversationId) {
        return mDraftMessageData;
    }

    @Override
    public ActionService getActionService() {
        return mActionService;
    }

    @Override
    public ConnectivityUtil getConnectivityUtil() {
        return mConnectivityUtil;
    }

    @Override
    public SyncManager getSyncManager() {
        return mSyncManager;
    }

    @Override
    public DatabaseWrapper getDatabase() {
        // Note this will crash unless the application context is redirected...
        // This is by design so that tests do not inadvertently use the real database
        return mDatabaseHelper.getDatabase();
    }

    @Override
    void onCreateTables(final SQLiteDatabase db) {
        TestDataFactory.createTestData(db);
    }

    @Override
    public void onActivityResume() {
    }

    @Override
    public void onApplicationCreated() {
    }

    @Override
    public BlockedParticipantsData createBlockedParticipantsData(Context context,
            BlockedParticipantsDataListener listener) {
        return new BlockedParticipantsData(context, listener);
    }
}