AccountBackupRestoreTests.java revision f5418f1f93b02e7fab9f15eb201800b65510998e
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
17package com.android.email.provider;
18
19import com.android.emailcommon.provider.Account;
20import com.android.emailcommon.provider.EmailContent;
21import com.android.emailcommon.provider.HostAuth;
22
23import android.content.ContentResolver;
24import android.content.Context;
25import android.database.Cursor;
26import android.test.ProviderTestCase2;
27import 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
36public 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        // Restore the accounts
118        AccountBackupRestore.restoreIfNeeded(mMockContext);
119
120        // Make sure there are two accounts and four host auths
121        assertEquals(2, EmailContent.count(mMockContext, Account.CONTENT_URI));
122        assertEquals(4, EmailContent.count(mMockContext, HostAuth.CONTENT_URI));
123
124        // Get a cursor to our accounts, from earliest to latest (same order as saved1/saved2)
125        Cursor c = cr.query(Account.CONTENT_URI, Account.CONTENT_PROJECTION, null, null, "_id ASC");
126        assertNotNull(c);
127        assertTrue(c.moveToNext());
128        // Restore the account
129        Account restored = new Account();
130        restored.restore(c);
131        // And the host auth's
132        HostAuth recv = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeyRecv);
133        assertNotNull(recv);
134        HostAuth send = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeySend);
135        assertNotNull(send);
136        // The host auth's should be equal (except id)
137        ProviderTestUtils.assertHostAuthEqual("backup", saved1.mHostAuthRecv, recv, false);
138        ProviderTestUtils.assertHostAuthEqual("backup", saved1.mHostAuthSend, send, false);
139        assertRestoredAccountEqual(saved1, restored);
140
141        assertTrue(c.moveToNext());
142        // Restore the account
143        restored = new Account();
144        restored.restore(c);
145        // And the host auth's
146        recv = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeyRecv);
147        assertNotNull(recv);
148        send = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeySend);
149        assertNotNull(send);
150        // The host auth's should be equal (except id)
151        ProviderTestUtils.assertHostAuthEqual("backup", saved2.mHostAuthRecv, recv, false);
152        ProviderTestUtils.assertHostAuthEqual("backup", saved2.mHostAuthSend, send, false);
153        assertRestoredAccountEqual(saved2, restored);
154    }
155}
156