summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/email/provider/AccountBackupRestoreTests.java
blob: 797e9885f57824f12fb31c0f2252faa11cc02db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.email.provider;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.test.ProviderTestCase2;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.Suppress;

import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.HostAuth;

/**
 * This is a series of unit tests for backup/restore of the Account class.
 *
 * You can run this entire test case with:
 *   runtest -c com.android.email.provider.AccountBackupRestoreTests email
 */
@Suppress
@MediumTest
public class AccountBackupRestoreTests extends ProviderTestCase2<EmailProvider> {

    private Context mMockContext;

    public AccountBackupRestoreTests() {
        super(EmailProvider.class, EmailContent.AUTHORITY);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mMockContext = getMockContext();
    }

    /**
     * Delete any dummy accounts we set up for this test
     */
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public static void assertRestoredAccountEqual(Account expect, Account actual) {
        assertEquals(" mDisplayName", expect.mDisplayName, actual.mDisplayName);
        assertEquals(" mEmailAddress", expect.mEmailAddress, actual.mEmailAddress);

        assertEquals(" mSyncLookback", expect.mSyncLookback, actual.mSyncLookback);
        assertEquals(" mSyncInterval", expect.getSyncInterval(), actual.getSyncInterval());
        assertEquals(" mFlags", expect.mFlags, actual.mFlags);
        assertEquals(" mSenderName", expect.mSenderName, actual.mSenderName);
        assertEquals(" mProtocolVersion", expect.mProtocolVersion,
                actual.mProtocolVersion);
        assertEquals(" mSignature", expect.mSignature, actual.mSignature);

        // Nulled out by backup
        assertEquals(0, actual.mPolicyKey);
        assertNull(actual.mSyncKey);
        assertNull(actual.mSecuritySyncKey);
    }

    /**
     * Test backup with accounts
     */
    public void testBackupAndRestore() {
        // Create real accounts in need of backup
        Account saved1 =
            ProviderTestUtils.setupAccount("testBackup1", false, mMockContext);
        saved1.mHostAuthRecv =
            ProviderTestUtils.setupHostAuth("legacy-recv", 0, false, mMockContext);
        saved1.mHostAuthSend =
            ProviderTestUtils.setupHostAuth("legacy-send", 0, false, mMockContext);
        saved1.save(mMockContext);
        Account saved2 =
            ProviderTestUtils.setupAccount("testBackup2", false, mMockContext);
        saved2.mHostAuthRecv =
            ProviderTestUtils.setupHostAuth("legacy-recv", 0, false, mMockContext);
        saved2.mHostAuthSend =
            ProviderTestUtils.setupHostAuth("legacy-send", 0, false, mMockContext);
        saved2.save(mMockContext);
        // Make sure they're in the database
        assertEquals(2, EmailContent.count(mMockContext, Account.CONTENT_URI));
        assertEquals(4, EmailContent.count(mMockContext, HostAuth.CONTENT_URI));

        // Backup the accounts
        AccountBackupRestore.backup(mMockContext);

        // Delete the accounts
        ContentResolver cr = mMockContext.getContentResolver();
        cr.delete(Account.CONTENT_URI, null, null);
        cr.delete(HostAuth.CONTENT_URI, null, null);

        // Make sure they're no longer in the database
        assertEquals(0, EmailContent.count(mMockContext, Account.CONTENT_URI));
        assertEquals(0, EmailContent.count(mMockContext, HostAuth.CONTENT_URI));

        // Because we restore accounts at the db open time, we first need to close the db
        // explicitly here.
        // Accounts will be restored next time we touch the db.
        getProvider().shutdown();

        // Make sure there are two accounts and four host auths
        assertEquals(2, EmailContent.count(mMockContext, Account.CONTENT_URI));
        assertEquals(4, EmailContent.count(mMockContext, HostAuth.CONTENT_URI));

        // Get a cursor to our accounts, from earliest to latest (same order as saved1/saved2)
        Cursor c = cr.query(Account.CONTENT_URI, Account.CONTENT_PROJECTION, null, null, "_id ASC");
        assertNotNull(c);
        assertTrue(c.moveToNext());
        // Restore the account
        Account restored = new Account();
        restored.restore(c);
        // And the host auth's
        HostAuth recv = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeyRecv);
        assertNotNull(recv);
        HostAuth send = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeySend);
        assertNotNull(send);
        // The host auth's should be equal (except id)
        ProviderTestUtils.assertHostAuthEqual("backup", saved1.mHostAuthRecv, recv, false);
        ProviderTestUtils.assertHostAuthEqual("backup", saved1.mHostAuthSend, send, false);
        assertRestoredAccountEqual(saved1, restored);

        assertTrue(c.moveToNext());
        // Restore the account
        restored = new Account();
        restored.restore(c);
        // And the host auth's
        recv = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeyRecv);
        assertNotNull(recv);
        send = HostAuth.restoreHostAuthWithId(mMockContext, restored.mHostAuthKeySend);
        assertNotNull(send);
        // The host auth's should be equal (except id)
        ProviderTestUtils.assertHostAuthEqual("backup", saved2.mHostAuthRecv, recv, false);
        ProviderTestUtils.assertHostAuthEqual("backup", saved2.mHostAuthSend, send, false);
        assertRestoredAccountEqual(saved2, restored);
    }
}