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