AccountBackupRestoreTests.java revision 7037a0bd3d8e925a9115f475f5c0d05ddae2eeee
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 android.content.ContentResolver;
20import android.content.Context;
21import android.database.Cursor;
22import android.test.ProviderTestCase2;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.test.suitebuilder.annotation.Suppress;
25
26import com.android.emailcommon.provider.Account;
27import com.android.emailcommon.provider.EmailContent;
28import 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
38public 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(" 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.save(mMockContext);
92        Account saved2 =
93            ProviderTestUtils.setupAccount("testBackup2", false, mMockContext);
94        saved2.mHostAuthRecv =
95            ProviderTestUtils.setupHostAuth("legacy-recv", 0, false, mMockContext);
96        saved2.mHostAuthSend =
97            ProviderTestUtils.setupHostAuth("legacy-send", 0, false, mMockContext);
98        saved2.save(mMockContext);
99        // Make sure they're in the database
100        assertEquals(2, EmailContent.count(mMockContext, Account.CONTENT_URI));
101        assertEquals(4, EmailContent.count(mMockContext, HostAuth.CONTENT_URI));
102
103        // Backup the accounts
104        AccountBackupRestore.backup(mMockContext);
105
106        // Delete the accounts
107        ContentResolver cr = mMockContext.getContentResolver();
108        cr.delete(Account.CONTENT_URI, null, null);
109        cr.delete(HostAuth.CONTENT_URI, null, null);
110
111        // Make sure they're no longer in the database
112        assertEquals(0, EmailContent.count(mMockContext, Account.CONTENT_URI));
113        assertEquals(0, EmailContent.count(mMockContext, HostAuth.CONTENT_URI));
114
115        // Because we restore accounts at the db open time, we first need to close the db
116        // explicitly here.
117        // Accounts will be restored next time we touch the db.
118        getProvider().shutdown();
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