1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.calendar;
18 
19 import android.content.ContentResolver;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.content.res.Resources;
24 import android.database.Cursor;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.test.mock.MockContentProvider;
28 import android.test.mock.MockContentResolver;
29 import android.test.mock.MockContext;
30 import android.test.mock.MockCursor;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /**
36  * A helper class for creating and wiring fake implementations of db classes, like
37  * Context, ContentResolver, ContentProvider, etc.  Typical setup will look something like:
38  *      DbUtils dbUtils = new DbUtils(mockResources);
39  *      dbUtils.getContentResolver().addProvider("settings", dbUtils.getContentProvider());
40  *      dbUtils.getContentResolver().addProvider(CalendarCache.URI.getAuthority(),
41  *            dbUtils.getContentProvider());
42  */
43 class DbTestUtils {
44     private final MockContentResolver contentResolver;
45     private final FakeContext context;
46     private final FakeSharedPreferences sharedPreferences;
47     private final FakeContentProvider contentProvider;
48 
49     class FakeContext extends MockContext {
50         private ContentResolver contentResolver;
51         private Resources resources;
52         private SharedPreferences sharedPreferences;
53 
FakeContext(ContentResolver contentResolver, Resources resources)54         FakeContext(ContentResolver contentResolver, Resources resources) {
55             this.contentResolver = contentResolver;
56             this.resources = resources;
57         }
58 
59         @Override
getContentResolver()60         public ContentResolver getContentResolver() {
61             return contentResolver;
62         }
63 
64         @Override
getResources()65         public Resources getResources() {
66             return resources;
67         }
68 
getUserId()69         public int getUserId() {
70             return 0;
71         }
72 
setSharedPreferences(SharedPreferences sharedPreferences)73         public void setSharedPreferences(SharedPreferences sharedPreferences) {
74             this.sharedPreferences = sharedPreferences;
75         }
76 
77         @Override
getSharedPreferences(String name, int mode)78         public SharedPreferences getSharedPreferences(String name, int mode) {
79             if (sharedPreferences != null) {
80                 return sharedPreferences;
81             } else {
82                 return super.getSharedPreferences(name, mode);
83             }
84         }
85     }
86 
87     // TODO: finish fake implementation.
88     static class FakeCursor extends MockCursor {
89         private List<String> queryResult;
90         int mCurrentPosition = -1;
91 
FakeCursor(List<String> queryResult)92         FakeCursor(List<String> queryResult) {
93             this.queryResult = queryResult;
94         }
95 
96         @Override
getCount()97         public int getCount() {
98             return queryResult.size();
99         }
100 
101         @Override
moveToFirst()102         public boolean moveToFirst() {
103             mCurrentPosition = 0;
104             return true;
105         }
106 
107         @Override
moveToNext()108         public boolean moveToNext() {
109             if (queryResult.size() > 0 && mCurrentPosition < queryResult.size()) {
110                 mCurrentPosition++;
111                 return true;
112             } else {
113                 return false;
114             }
115         }
116 
117         @Override
isBeforeFirst()118         public boolean isBeforeFirst() {
119             return mCurrentPosition < 0;
120         }
121 
122         @Override
getString(int columnIndex)123         public String getString(int columnIndex) {
124             return queryResult.get(columnIndex);
125         }
126 
127         @Override
close()128         public void close() {
129         }
130     }
131 
132     // TODO: finish implementation, perhaps using an in-memory table
133     static class FakeContentProvider extends MockContentProvider {
134         private ArrayList<String> queryResult = null;
135 
FakeContentProvider(Context context)136         public FakeContentProvider(Context context) {
137             super(context);
138         }
139 
140         @Override
call(String method, String request, Bundle args)141         public Bundle call(String method, String request, Bundle args) {
142             return null;
143         }
144 
145         @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)146         public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
147             // TODO: not currently implemented
148             return 1;
149         }
150 
151         /**
152          * Set the mocked results to return from a query call.
153          */
setQueryResult(ArrayList<String> result)154         public void setQueryResult(ArrayList<String> result) {
155             this.queryResult = result;
156         }
157 
158         @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy)159         public final Cursor query(Uri uri, String[] projection, String selection,
160                 String[] selectionArgs, String orderBy) {
161             ArrayList<String> result = (queryResult == null) ?
162                     new ArrayList<String>() : queryResult;
163             return new FakeCursor(result);
164         }
165 
166         @Override
getType(Uri uri)167         public String getType(Uri uri) {
168             return null;
169         }
170 
171         @Override
onCreate()172         public boolean onCreate() {
173             return false;
174         }
175     }
176 
DbTestUtils(Resources resources)177     public DbTestUtils(Resources resources) {
178         this.contentResolver = new MockContentResolver();
179         this.context = new FakeContext(contentResolver, resources);
180         this.sharedPreferences = new FakeSharedPreferences();
181         this.contentProvider = new FakeContentProvider(context);
182         context.setSharedPreferences(sharedPreferences);
183     }
184 
getContentResolver()185     public MockContentResolver getContentResolver() {
186         return contentResolver;
187     }
188 
getContext()189     public FakeContext getContext() {
190         return context;
191     }
192 
getContentProvider()193     public FakeContentProvider getContentProvider() {
194         return contentProvider;
195     }
196 
getMockSharedPreferences()197     public FakeSharedPreferences getMockSharedPreferences() {
198         return sharedPreferences;
199     }
200 }
201