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 package com.android.providers.contacts; 17 18 import android.content.Context; 19 import android.database.DatabaseUtils; 20 import android.database.sqlite.SQLiteDatabase; 21 import android.test.AndroidTestCase; 22 import android.test.suitebuilder.annotation.LargeTest; 23 24 import java.io.File; 25 import java.io.FileOutputStream; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.io.OutputStream; 29 30 @LargeTest 31 public class CallLogMigrationTest extends AndroidTestCase { 32 33 private void writeAssetFileToDisk(String assetName, File diskPath) throws IOException { 34 final Context context = getTestContext(); 35 final byte[] BUF = new byte[1024 * 32]; 36 37 try (final InputStream input = context.getAssets().open(assetName)) { 38 try (final OutputStream output = new FileOutputStream(diskPath)) { 39 for (;;) { 40 final int len = input.read(BUF); 41 if (len == -1) { 42 break; 43 } 44 output.write(BUF, 0, len); 45 } 46 } 47 } 48 } 49 50 public void testMigration() throws IOException { 51 final File sourceDbFile = new File(getTestContext().getCacheDir(), "contacts2src.db"); 52 writeAssetFileToDisk("calllogmigration/contacts2.db", sourceDbFile); 53 54 try (final SQLiteDatabase sourceDb = SQLiteDatabase.openDatabase( 55 sourceDbFile.getAbsolutePath(), /* cursorFactory=*/ null, 56 SQLiteDatabase.OPEN_READWRITE)) { 57 58 // Make sure the source tables exist initially. 59 assertTrue(CallLogDatabaseHelper.tableExists(sourceDb, "calls")); 60 assertTrue(CallLogDatabaseHelper.tableExists(sourceDb, "voicemail_status")); 61 62 // Create the calllog DB to perform the migration. 63 final CallLogDatabaseHelperTestable dbh = 64 new CallLogDatabaseHelperTestable(getTestContext(), sourceDb); 65 66 final SQLiteDatabase db = dbh.getReadableDatabase(); 67 68 // Check the content: 69 // Note what we worry here is basically insertion error due to additional constraints, 70 // renames, etc. So here, we just check the number of rows and don't check the content. 71 assertEquals(3, DatabaseUtils.longForQuery(db, "select count(*) from " + 72 CallLogDatabaseHelper.Tables.CALLS, null)); 73 74 assertEquals(2, DatabaseUtils.longForQuery(db, "select count(*) from " + 75 CallLogDatabaseHelper.Tables.VOICEMAIL_STATUS, null)); 76 77 assertEquals("123456", 78 dbh.getProperty(CallLogDatabaseHelper.DbProperties.CALL_LOG_LAST_SYNCED, "")); 79 80 // Also, the source table should have been removed. 81 assertFalse(CallLogDatabaseHelper.tableExists(sourceDb, "calls")); 82 assertFalse(CallLogDatabaseHelper.tableExists(sourceDb, "voicemail_status")); 83 84 assertEquals("1", 85 dbh.getProperty(CallLogDatabaseHelper.DbProperties.DATA_MIGRATED, "")); 86 } 87 } 88 } 89