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 
setSharedPreferences(SharedPreferences sharedPreferences)69         public void setSharedPreferences(SharedPreferences sharedPreferences) {
70             this.sharedPreferences = sharedPreferences;
71         }
72 
73         @Override
getSharedPreferences(String name, int mode)74         public SharedPreferences getSharedPreferences(String name, int mode) {
75             if (sharedPreferences != null) {
76                 return sharedPreferences;
77             } else {
78                 return super.getSharedPreferences(name, mode);
79             }
80         }
81     }
82 
83     // TODO: finish fake implementation.
84     static class FakeCursor extends MockCursor {
85         private List<String> queryResult;
86         int mCurrentPosition = -1;
87 
FakeCursor(List<String> queryResult)88         FakeCursor(List<String> queryResult) {
89             this.queryResult = queryResult;
90         }
91 
92         @Override
getCount()93         public int getCount() {
94             return queryResult.size();
95         }
96 
97         @Override
moveToFirst()98         public boolean moveToFirst() {
99             mCurrentPosition = 0;
100             return true;
101         }
102 
103         @Override
moveToNext()104         public boolean moveToNext() {
105             if (queryResult.size() > 0 && mCurrentPosition < queryResult.size()) {
106                 mCurrentPosition++;
107                 return true;
108             } else {
109                 return false;
110             }
111         }
112 
113         @Override
isBeforeFirst()114         public boolean isBeforeFirst() {
115             return mCurrentPosition < 0;
116         }
117 
118         @Override
getString(int columnIndex)119         public String getString(int columnIndex) {
120             return queryResult.get(columnIndex);
121         }
122 
123         @Override
close()124         public void close() {
125         }
126     }
127 
128     // TODO: finish implementation, perhaps using an in-memory table
129     static class FakeContentProvider extends MockContentProvider {
130         private ArrayList<String> queryResult = null;
131 
FakeContentProvider(Context context)132         public FakeContentProvider(Context context) {
133             super(context);
134         }
135 
136         @Override
call(String method, String request, Bundle args)137         public Bundle call(String method, String request, Bundle args) {
138             return null;
139         }
140 
141         @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)142         public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
143             // TODO: not currently implemented
144             return 1;
145         }
146 
147         /**
148          * Set the mocked results to return from a query call.
149          */
setQueryResult(ArrayList<String> result)150         public void setQueryResult(ArrayList<String> result) {
151             this.queryResult = result;
152         }
153 
154         @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy)155         public final Cursor query(Uri uri, String[] projection, String selection,
156                 String[] selectionArgs, String orderBy) {
157             ArrayList<String> result = (queryResult == null) ?
158                     new ArrayList<String>() : queryResult;
159             return new FakeCursor(result);
160         }
161 
162         @Override
getType(Uri uri)163         public String getType(Uri uri) {
164             return null;
165         }
166 
167         @Override
onCreate()168         public boolean onCreate() {
169             return false;
170         }
171     }
172 
DbTestUtils(Resources resources)173     public DbTestUtils(Resources resources) {
174         this.contentResolver = new MockContentResolver();
175         this.context = new FakeContext(contentResolver, resources);
176         this.sharedPreferences = new FakeSharedPreferences();
177         this.contentProvider = new FakeContentProvider(context);
178         context.setSharedPreferences(sharedPreferences);
179     }
180 
getContentResolver()181     public MockContentResolver getContentResolver() {
182         return contentResolver;
183     }
184 
getContext()185     public FakeContext getContext() {
186         return context;
187     }
188 
getContentProvider()189     public FakeContentProvider getContentProvider() {
190         return contentProvider;
191     }
192 
getMockSharedPreferences()193     public FakeSharedPreferences getMockSharedPreferences() {
194         return sharedPreferences;
195     }
196 }
197