2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.providers.contacts;
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.database.DatabaseUtils;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.net.Uri;
24 import android.provider.BaseColumns;
25 import android.provider.CallLog;
26 import android.provider.CallLog.Calls;
27 import android.provider.ContactsContract.Contacts;
28 import android.provider.ContactsContract.Data;
29 import android.provider.ContactsContract.Groups;
30 import android.provider.ContactsContract.RawContacts;
31 import android.test.mock.MockContext;
32 import android.test.suitebuilder.annotation.LargeTest;
34 import java.io.ByteArrayOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
40 * Unit tests for {@link LegacyContactImporter}.
42 * Run the test like this:
44 * adb shell am instrument -e class com.android.providers.contacts.LegacyContactImporterTest -w \
45 * com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
49 public class LegacyContactImporterTest extends BaseContactsProvider2Test {
51 private static class LegacyMockContext extends MockContext {
53 private String mFileName;
55 public LegacyMockContext(String fileName) {
60 public SQLiteDatabase openOrCreateDatabase(String file, int mode,
61 SQLiteDatabase.CursorFactory factory) {
62 return SQLiteDatabase.openDatabase(mFileName, factory, SQLiteDatabase.OPEN_READONLY);
66 public File getDatabasePath(String name) {
67 return new File(mFileName);
71 private LegacyMockContext createLegacyMockContext(String folder) throws IOException {
72 Context context = getTestContext();
73 File tempDb = new File(context.getFilesDir(), "legacy_contacts.db");
74 if (tempDb.exists()) {
77 createSQLiteDatabaseFromDumpFile(tempDb.getPath(),
78 new File(folder, "legacy_contacts.sql").getPath());
79 return new LegacyMockContext(tempDb.getPath());
82 private void createSQLiteDatabaseFromDumpFile(String tempDbPath, String dumpFileAssetPath)
85 final String[] ignoredTables = new String[] {"android_metadata", "sqlite_sequence"};
87 Context context = getTestContext();
88 SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(tempDbPath, null);
90 String data = readAssetAsString(dumpFileAssetPath);
91 String[] commands = data.split(";\r|;\n|;\r\n");
92 for (String command : commands) {
93 boolean ignore = false;
94 for (String ignoredTable : ignoredTables) {
95 if (command.contains(ignoredTable)) {
101 database.execSQL(command);
106 "Database Version not set. Be sure to add " +
107 "'PRAGMA user_version = <number>;' to the SQL Script",
108 database.getVersion() != 0);
115 protected void setUp() throws Exception {
116 SynchronousContactsProvider2.resetOpenHelper();
118 addProvider(TestCallLogProvider.class, CallLog.AUTHORITY);
122 protected void tearDown() throws Exception {
124 SynchronousContactsProvider2.resetOpenHelper();
127 public void testContactUpgrade1() throws Exception {
128 testAssetSet("test1");
131 public void testSyncedContactsUpgrade() throws Exception {
132 testAssetSet("testSynced");
135 public void testUnsyncedContactsUpgrade() throws Exception {
136 testAssetSet("testUnsynced");
139 private void testAssetSet(String folder) throws Exception {
140 ContactsProvider2 provider = (ContactsProvider2)getProvider();
141 LegacyContactImporter importer =
142 new LegacyContactImporter(createLegacyMockContext(folder), provider);
143 provider.importLegacyContacts(importer);
145 assertQueryResults(folder + "/expected_groups.txt", Groups.CONTENT_URI, new String[]{
150 Groups.GROUP_VISIBLE,
163 assertQueryResults(folder + "/expected_contacts.txt", Contacts.CONTENT_URI, new String[]{
165 Contacts.DISPLAY_NAME_PRIMARY,
166 Contacts.SORT_KEY_PRIMARY,
168 Contacts.TIMES_CONTACTED,
169 Contacts.LAST_TIME_CONTACTED,
170 Contacts.CUSTOM_RINGTONE,
171 Contacts.SEND_TO_VOICEMAIL,
173 Contacts.IN_VISIBLE_GROUP,
174 Contacts.HAS_PHONE_NUMBER,
178 assertQueryResults(folder + "/expected_raw_contacts.txt", RawContacts.CONTENT_URI,
181 RawContacts.ACCOUNT_NAME,
182 RawContacts.ACCOUNT_TYPE,
185 RawContacts.SOURCE_ID,
191 RawContacts.DISPLAY_NAME_SOURCE,
192 RawContacts.DISPLAY_NAME_PRIMARY,
193 RawContacts.DISPLAY_NAME_ALTERNATIVE,
194 RawContacts.SORT_KEY_PRIMARY,
195 RawContacts.SORT_KEY_ALTERNATIVE,
198 assertQueryResults(folder + "/expected_data.txt", Data.CONTENT_URI, new String[]{
218 Data.IS_SUPER_PRIMARY,
226 assertQueryResults(folder + "/expected_calls.txt", Calls.CONTENT_URI, new String[]{
234 Calls.CACHED_NUMBER_LABEL,
235 Calls.CACHED_NUMBER_TYPE,
238 provider.getDatabaseHelper().close();
241 private void assertQueryResults(String fileName, Uri uri, String[] projection)
243 String expected = readAssetAsString(fileName).trim();
244 String actual = dumpCursorToString(uri, projection).trim();
245 assertEquals("Checking golden file " + fileName, expected, actual);
248 private String readAssetAsString(String fileName) throws IOException {
249 Context context = getTestContext();
250 InputStream input = context.getAssets().open(fileName);
251 ByteArrayOutputStream contents = new ByteArrayOutputStream();
253 byte[] data = new byte[1024];
255 len = input.read(data);
256 if (len > 0) contents.write(data, 0, len);
257 } while (len == data.length);
258 return contents.toString();
261 private String dumpCursorToString(Uri uri, String[] projection) {
262 Cursor c = mResolver.query(uri, projection, null, null, BaseColumns._ID);
264 return "Null cursor";
267 String cursorDump = DatabaseUtils.dumpCursorToString(c);
269 return insertLineNumbers(cursorDump);
272 private String insertLineNumbers(String multiline) {
273 String[] lines = multiline.split("\n");
274 StringBuilder sb = new StringBuilder();
276 // Ignore the first line that is a volatile header and the last line which is "<<<<<"
277 for (int i = 1; i < lines.length - 1; i++) {
278 sb.append(i).append(" ").append(lines[i]).append('\n');
280 return sb.toString();
284 public static class TestCallLogProvider extends CallLogProvider {
285 private static ContactsDatabaseHelper mDbHelper;
288 protected ContactsDatabaseHelper getDatabaseHelper(final Context context) {
289 if (mDbHelper == null) {
290 mDbHelper = new ContactsDatabaseHelper(context);