1 /* 2 * Copyright (C) 2011 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 17 package com.android.email.provider; 18 19 import com.android.emailcommon.provider.Account; 20 import com.android.emailcommon.provider.EmailContent; 21 import com.android.emailcommon.provider.HostAuth; 22 23 import android.content.ContentResolver; 24 import android.content.Context; 25 import android.database.Cursor; 26 import android.test.ProviderTestCase2; 27 import android.test.suitebuilder.annotation.MediumTest; 28 29 /** 30 * This is a series of unit tests for backup/restore of the Account class. 31 * 32 * You can run this entire test case with: 33 * runtest -c com.android.email.provider.AccountBackupRestoreTests email 34 */ 35 @MediumTest 36 public class AccountBackupRestoreTests extends ProviderTestCase2<EmailProvider> { 37 38 private Context mMockContext; 39 40 public AccountBackupRestoreTests() { 41 super(EmailProvider.class, EmailContent.AUTHORITY); 42 } 43 44 @Override 45 protected void setUp() throws Exception { 46 super.setUp(); 47 mMockContext = getMockContext(); 48 } 49 50 /** 51 * Delete any dummy accounts we set up for this test 52 */ 53 @Override 54 protected void tearDown() throws Exception { 55 super.tearDown(); 56 } 57 58 public static void assertRestoredAccountEqual(Account expect, Account actual) { 59 assertEquals(" mDisplayName", expect.mDisplayName, actual.mDisplayName); 60 assertEquals(" mEmailAddress", expect.mEmailAddress, actual.mEmailAddress); 61 62 assertEquals(" mSyncLookback", expect.mSyncLookback, actual.mSyncLookback); 63 assertEquals(" mSyncInterval", expect.mSyncInterval, actual.mSyncInterval); 64 assertEquals(" mFlags", expect.mFlags, actual.mFlags); 65 assertEquals(" mIsDefault", expect.mIsDefault, actual.mIsDefault); 66 assertEquals(" mSenderName", expect.mSenderName, actual.mSenderName); 67 assertEquals(" mRingtoneUri", expect.mRingtoneUri, actual.mRingtoneUri); 68 assertEquals(" mProtocolVersion", expect.mProtocolVersion, 69 actual.mProtocolVersion); 70 assertEquals(" mNewMessageCount", expect.mNewMessageCount, 71 actual.mNewMessageCount); 72 assertEquals(" mSignature", expect.mSignature, actual.mSignature); 73 74 // Nulled out by backup 75 assertEquals(0, actual.mPolicyKey); 76 assertNull(actual.mSyncKey); 77 assertNull(actual.mSecuritySyncKey); 78 } 79 80 /** 81 * Test backup with accounts 82 */ 83 public void testBackupAndRestore() { 84 // Create real accounts in need of backup 85 Account saved1 = 86 ProviderTestUtils.setupAccount("testBackup1", false, mMockContext); 87 saved1.mHostAuthRecv = 88 ProviderTestUtils.setupHostAuth("legacy-recv", 0, false, mMockContext); 89 saved1.mHostAuthSend = 90 ProviderTestUtils.setupHostAuth("legacy-send", 0, false, mMockContext); 91 saved1.setDefaultAccount(true); 92 saved1.save(mMockContext); 93 Account saved2 = 94 ProviderTestUtils.setupAccount("testBackup2", false, mMockContext); 95 saved2.mHostAuthRecv = 96 ProviderTestUtils.setupHostAuth("legacy-recv", 0, false, mMockContext); 97 saved2.mHostAuthSend = 98 ProviderTestUtils.setupHostAuth("legacy-send", 0, false, mMockContext); 99 saved2.setDefaultAccount(false); 100 saved2.save(mMockContext); 101 // Make sure they're in the database 102 assertEquals(2, EmailContent.count(mMockContext, Account.CONTENT_URI)); 103 assertEquals(4, EmailContent.count(mMockContext, HostAuth.CONTENT_URI)); 104 105 // Backup the accounts 106 AccountBackupRestore.backup(mMockContext); 107 108 // Delete the accounts 109 ContentResolver cr = mMockContext.getContentResolver(); 110 cr.delete(Account.CONTENT_URI, null, null); 111 cr.delete(HostAuth.CONTENT_URI, null, null); 112 113 // Make sure they're no longer in the database 114 assertEquals(0, EmailContent.count(mMockContext, Account.CONTENT_URI)); 115 assertEquals(0, EmailContent.count(mMockContext, HostAuth.CONTENT_URI)); 116 117 // Because we restore accounts at the db open time, we first need to close the db 118 // explicitly here. 119 // Accounts will be restored next time we touch the db. 120 getProvider().shutdown(); 121 122 // Make sure there are two accounts and four host auths 123 assertEquals(2, EmailContent.count(mMockContext, Account.CONTENT_URI)); 124 assertEquals(4, EmailContent.count(mMockContext, HostAuth.CONTENT_URI)); 125 126 // Get a cursor to our accounts, from earliest to latest (same order as saved1/saved2) 127 Cursor c = cr.query(Account.CONTENT_URI, Account.CONTENT_PROJECTION, null, null, "_id ASC"); 128 assertNotNull(c); 129 assertTrue(c.moveToNext()); 130 // Restore the account 131 Account restored = new Account(); 132 restored.restore(c); 133 // And the host auth's 134 HostAuth recv = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeyRecv); 135 assertNotNull(recv); 136 HostAuth send = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeySend); 137 assertNotNull(send); 138 // The host auth's should be equal (except id) 139 ProviderTestUtils.assertHostAuthEqual("backup", saved1.mHostAuthRecv, recv, false); 140 ProviderTestUtils.assertHostAuthEqual("backup", saved1.mHostAuthSend, send, false); 141 assertRestoredAccountEqual(saved1, restored); 142 143 assertTrue(c.moveToNext()); 144 // Restore the account 145 restored = new Account(); 146 restored.restore(c); 147 // And the host auth's 148 recv = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeyRecv); 149 assertNotNull(recv); 150 send = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeySend); 151 assertNotNull(send); 152 // The host auth's should be equal (except id) 153 ProviderTestUtils.assertHostAuthEqual("backup", saved2.mHostAuthRecv, recv, false); 154 ProviderTestUtils.assertHostAuthEqual("backup", saved2.mHostAuthSend, send, false); 155 assertRestoredAccountEqual(saved2, restored); 156 } 157 } 158