ProviderTests.java revision f5418f1f93b02e7fab9f15eb201800b65510998e
17143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler/*
27143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * Copyright (C) 2009 The Android Open Source Project
37143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler *
47143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * Licensed under the Apache License, Version 2.0 (the "License");
57143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * you may not use this file except in compliance with the License.
67143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * You may obtain a copy of the License at
77143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler *
87143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler *      http://www.apache.org/licenses/LICENSE-2.0
97143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler *
107143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * Unless required by applicable law or agreed to in writing, software
117143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * distributed under the License is distributed on an "AS IS" BASIS,
127143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * See the License for the specific language governing permissions and
147143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * limitations under the License.
157143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler */
167143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
177143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadlerpackage com.android.email.provider;
187143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
19f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blankimport com.android.emailcommon.AccountManagerTypes;
20f5418f1f93b02e7fab9f15eb201800b65510998eMarc Blankimport com.android.emailcommon.provider.Account;
21a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent;
22a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent.AccountColumns;
23a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent.Attachment;
24a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent.AttachmentColumns;
25a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent.Body;
26a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent.BodyColumns;
27a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent.MailboxColumns;
28a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent.Message;
29a7bc0319a75184ad706bb35c049af107ac3688e6Marc Blankimport com.android.emailcommon.provider.EmailContent.MessageColumns;
3012b82d9374947c9268217f45befe8a74bd9b60d7Ben Komaloimport com.android.emailcommon.provider.HostAuth;
3153ea83ebf91f820692e8fa8e781f5cc982dd94dbBen Komaloimport com.android.emailcommon.provider.Mailbox;
32f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blankimport com.android.emailcommon.utility.AccountReconciler;
3376f614720db6a282c3e890969a600585122cdcc5Marc Blankimport com.android.emailcommon.utility.TextUtilities;
3431d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blankimport com.android.emailcommon.utility.Utility;
357143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
36f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blankimport android.accounts.AccountManager;
37f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blankimport android.accounts.AuthenticatorException;
38f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blankimport android.accounts.OperationCanceledException;
39ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadlerimport android.content.ContentResolver;
407143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadlerimport android.content.ContentUris;
41ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadlerimport android.content.ContentValues;
427143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadlerimport android.content.Context;
437143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadlerimport android.database.Cursor;
44574854b528163f3bf1a7cb974aa80082d1768edfMakoto Onukiimport android.database.sqlite.SQLiteDatabase;
457143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadlerimport android.net.Uri;
464a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadlerimport android.os.Bundle;
47976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blankimport android.os.Environment;
484a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadlerimport android.os.Parcel;
49e37881aac562c066e6e666349749e40b7c116a21Makoto Onukiimport android.test.MoreAsserts;
507143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadlerimport android.test.ProviderTestCase2;
517143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
52758a532fce2f672673d38b2daa5f67eb757b118bMarc Blankimport java.io.File;
53758a532fce2f672673d38b2daa5f67eb757b118bMarc Blankimport java.io.IOException;
54758a532fce2f672673d38b2daa5f67eb757b118bMarc Blankimport java.util.ArrayList;
55758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
567143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler/**
577143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * Tests of the Email provider.
58758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank *
597143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler * You can run this entire test case with:
607143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler *   runtest -c com.android.email.provider.ProviderTests email
61261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki *
62261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki * TODO: Add tests for cursor notification mechanism.  (setNotificationUri and notifyChange)
63261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki * We can't test the entire notification mechanism with a mock content resolver, because which URI
64261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki * to notify when notifyChange() is called is in the actual content resolver.
65261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki * Implementing the same mechanism in a mock one is pointless.  Instead what we could do is check
66261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki * what notification URI each cursor has, and with which URI is notified when
67261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki * inserting/updating/deleting.  (The former require a new method from AbstractCursor)
687143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler */
697143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadlerpublic class ProviderTests extends ProviderTestCase2<EmailProvider> {
70758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
719d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    private EmailProvider mProvider;
729d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    private Context mMockContext;
737143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
747143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    public ProviderTests() {
7531d9acbf0623872f9d4a2b3210b5970854b654c7Marc Blank        super(EmailProvider.class, EmailContent.AUTHORITY);
767143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    }
77758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
787143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    @Override
797143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    public void setUp() throws Exception {
807143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        super.setUp();
817143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        mMockContext = getMockContext();
829d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        mProvider = getProvider();
83349055aad47184b72cd86de1f37ac1b7557d2e70Marc Blank        // Invalidate all caches, since we reset the database for each test
84349055aad47184b72cd86de1f37ac1b7557d2e70Marc Blank        ContentCache.invalidateAllCachesForTest();
857143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    }
867143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
877143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    @Override
887143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    public void tearDown() throws Exception {
897143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        super.tearDown();
907143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    }
917143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
927143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
930d00889f83fc8c1d69039a2c849b630fb4dd28ddAndrew Stadler     * TODO: Database upgrade tests
940d00889f83fc8c1d69039a2c849b630fb4dd28ddAndrew Stadler     */
950d00889f83fc8c1d69039a2c849b630fb4dd28ddAndrew Stadler
9622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    //////////////////////////////////////////////////////////
9722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    ////// Utility methods
9822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    //////////////////////////////////////////////////////////
9922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
10022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    /** Sets the message count of all mailboxes to {@code -1}. */
10122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    private void setMinusOneToMessageCounts() {
10222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        ContentValues values = new ContentValues();
10322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        values.put(MailboxColumns.MESSAGE_COUNT, -1);
10422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
10522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // EmailProvider.update() doesn't allow updating messageCount, so directly use the DB.
10622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        SQLiteDatabase db = getProvider().getDatabase(mMockContext);
10722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        db.update(Mailbox.TABLE_NAME, values, null, null);
10822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    }
10922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
11022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    /** Returns the number of messages in a mailbox. */
11122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    private int getMessageCount(long mailboxId) {
11222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        return Utility.getFirstRowInt(mMockContext,
11322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId),
11422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                new String[] {MailboxColumns.MESSAGE_COUNT}, null, null, null, 0);
11522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    }
11622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
11722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    /** Creates a new message. */
11822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    private static Message createMessage(Context c, Mailbox b, boolean starred, boolean read,
11922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy            int flagLoaded) {
12022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message message = ProviderTestUtils.setupMessage(
12122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                "1", b.mAccountKey, b.mId, true, false, c, starred, read);
12222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        message.mFlagLoaded = flagLoaded;
12322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        message.save(c);
12422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        return message;
12522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    }
12622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
12722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    //////////////////////////////////////////////////////////
12822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    ////// The tests
12922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    //////////////////////////////////////////////////////////
13022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
1310d00889f83fc8c1d69039a2c849b630fb4dd28ddAndrew Stadler    /**
1327143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * Test simple account save/retrieve
1337143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
1347143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    public void testAccountSave() {
13528448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Account account1 = ProviderTestUtils.setupAccount("account-save", true, mMockContext);
1367143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long account1Id = account1.mId;
137758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
138f5418f1f93b02e7fab9f15eb201800b65510998eMarc Blank        Account account2 = Account.restoreAccountWithId(mMockContext, account1Id);
139758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
14028448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        ProviderTestUtils.assertAccountEqual("testAccountSave", account1, account2);
1417143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    }
14241f775bbc731b25854da9f5a80c66a4aa1eb0af9satok
1434a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler    /**
144da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler     * Test simple account save/retrieve with predefined hostauth records
145da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler     */
146da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler    public void testAccountSaveHostAuth() {
147da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        Account account1 = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext);
148da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        // add hostauth data, which should be saved the first time
149da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        account1.mHostAuthRecv = ProviderTestUtils.setupHostAuth("account-hostauth-recv", -1, false,
150da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler                mMockContext);
151da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        account1.mHostAuthSend = ProviderTestUtils.setupHostAuth("account-hostauth-send", -1, false,
152da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler                mMockContext);
153da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        account1.save(mMockContext);
154da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        long account1Id = account1.mId;
155da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler
156da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        // Confirm account reads back correctly
157f5418f1f93b02e7fab9f15eb201800b65510998eMarc Blank        Account account1get = Account.restoreAccountWithId(mMockContext, account1Id);
158da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        ProviderTestUtils.assertAccountEqual("testAccountSave", account1, account1get);
159da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler
160da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        // Confirm hostauth fields can be accessed & read back correctly
16112b82d9374947c9268217f45befe8a74bd9b60d7Ben Komalo        HostAuth hostAuth1get = HostAuth.restoreHostAuthWithId(mMockContext,
162da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler                account1get.mHostAuthKeyRecv);
163da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        ProviderTestUtils.assertHostAuthEqual("testAccountSaveHostAuth-recv",
164da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler                account1.mHostAuthRecv, hostAuth1get);
16512b82d9374947c9268217f45befe8a74bd9b60d7Ben Komalo        HostAuth hostAuth2get = HostAuth.restoreHostAuthWithId(mMockContext,
166da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler                account1get.mHostAuthKeySend);
167da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler        ProviderTestUtils.assertHostAuthEqual("testAccountSaveHostAuth-send",
168da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler                account1.mHostAuthSend, hostAuth2get);
169da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler    }
170da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler
171040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy    public void testAccountGetHostAuthSend() {
172040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        Account account = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext);
173040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.mHostAuthSend = ProviderTestUtils.setupHostAuth("account-hostauth-send", -1, false,
174040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy                mMockContext);
175040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.save(mMockContext);
176040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        HostAuth authGet;
177040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        HostAuth authTest;
178040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
179040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        authTest = account.mHostAuthSend;
180040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(authTest);
181040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertTrue(account.mHostAuthKeySend != 0);
182040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
183040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        // HostAuth is not changed
184040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        authGet = account.getOrCreateHostAuthSend(mMockContext);
185040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertTrue(authGet == authTest); // return the same object
186040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
187040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        // New HostAuth; based upon mHostAuthKeyRecv
18812b82d9374947c9268217f45befe8a74bd9b60d7Ben Komalo        authTest = HostAuth.restoreHostAuthWithId(mMockContext,
189040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy                account.mHostAuthKeySend);
190040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.mHostAuthSend = null;
191040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        authGet = account.getOrCreateHostAuthSend(mMockContext);
192040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(authGet);
193040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(account.mHostAuthSend);
194040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        ProviderTestUtils.assertHostAuthEqual("testAccountGetHostAuthSend-1", authTest, authGet);
195040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
196040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        // New HostAuth; completely empty
19712b82d9374947c9268217f45befe8a74bd9b60d7Ben Komalo        authTest = new HostAuth();
198040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.mHostAuthSend = null;
199040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.mHostAuthKeySend = 0;
200040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        authGet = account.getOrCreateHostAuthSend(mMockContext);
201040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(authGet);
202040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(account.mHostAuthSend);
203040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        ProviderTestUtils.assertHostAuthEqual("testAccountGetHostAuthSendv-2", authTest, authGet);
204040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy    }
205040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
206040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy    public void testAccountGetHostAuthRecv() {
207040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        Account account = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext);
208040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.mHostAuthRecv = ProviderTestUtils.setupHostAuth("account-hostauth-recv", -1, false,
209040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy                mMockContext);
210040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.save(mMockContext);
211040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        HostAuth authGet;
212040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        HostAuth authTest;
213040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
214040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        authTest = account.mHostAuthRecv;
215040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(authTest);
216040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertTrue(account.mHostAuthKeyRecv != 0);
217040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
218040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        // HostAuth is not changed
219040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        authGet = account.getOrCreateHostAuthRecv(mMockContext);
220040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertTrue(authGet == authTest); // return the same object
221040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
222040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        // New HostAuth; based upon mHostAuthKeyRecv
22312b82d9374947c9268217f45befe8a74bd9b60d7Ben Komalo        authTest = HostAuth.restoreHostAuthWithId(mMockContext,
224040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy                account.mHostAuthKeyRecv);
225040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.mHostAuthRecv = null;
226040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        authGet = account.getOrCreateHostAuthRecv(mMockContext);
227040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(authGet);
228040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(account.mHostAuthRecv);
229040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        ProviderTestUtils.assertHostAuthEqual("testAccountGetHostAuthRecv-1", authTest, authGet);
230040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
231040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        // New HostAuth; completely empty
23212b82d9374947c9268217f45befe8a74bd9b60d7Ben Komalo        authTest = new HostAuth();
233040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.mHostAuthRecv = null;
234040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        account.mHostAuthKeyRecv = 0;
235040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        authGet = account.getOrCreateHostAuthRecv(mMockContext);
236040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(authGet);
237040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        assertNotNull(account.mHostAuthRecv);
238040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy        ProviderTestUtils.assertHostAuthEqual("testAccountGetHostAuthRecv-2", authTest, authGet);
239040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy    }
240040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy
241da8836a76cd8a6eaa7e3693eeacc6393870b2066Andrew Stadler    /**
242040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy     * Simple test of account parceling.  The rather torturous path is to ensure that the
2434a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler     * account is really flattened all the way down to a parcel and back.
2444a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler     */
2454a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler    public void testAccountParcel() {
2464a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        Account account1 = ProviderTestUtils.setupAccount("parcel", false, mMockContext);
2474a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        Bundle b = new Bundle();
2484a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        b.putParcelable("account", account1);
2494a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        Parcel p = Parcel.obtain();
2504a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        b.writeToParcel(p, 0);
2514a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        p.setDataPosition(0);       // rewind it for reading
2524a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        Bundle b2 = new Bundle(Account.class.getClassLoader());
2534a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        b2.readFromParcel(p);
2544a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        Account account2 = (Account) b2.getParcelable("account");
2554a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        p.recycle();
2564a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler
2574a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler        ProviderTestUtils.assertAccountEqual("testAccountParcel", account1, account2);
2584a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler    }
2594a8c70c09be3914ded18031b4cca5a6d867de0d3Andrew Stadler
260e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki    /**
261e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki     * Test for {@link Account#getShortcutSafeUri()} and
262040ddf60cfef4aaecf4bfe1f897fce3248d777a4Todd Kennedy     * {@link Account#getAccountIdFromShortcutSafeUri}.
263e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki     */
264e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki    public void testAccountShortcutSafeUri() {
265e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        final Account account1 = ProviderTestUtils.setupAccount("account-1", true, mMockContext);
266e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        final Account account2 = ProviderTestUtils.setupAccount("account-2", true, mMockContext);
267e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        final long account1Id = account1.mId;
268e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        final long account2Id = account2.mId;
269e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki
270e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        final Uri uri1 = account1.getShortcutSafeUri();
271e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        final Uri uri2 = account2.getShortcutSafeUri();
272e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki
273e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        // Check the path part of the URIs.
274e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        MoreAsserts.assertEquals(new String[] {"account", account1.mCompatibilityUuid},
275e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki                uri1.getPathSegments().toArray());
276e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        MoreAsserts.assertEquals(new String[] {"account", account2.mCompatibilityUuid},
277e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki                uri2.getPathSegments().toArray());
278e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki
279e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        assertEquals(account1Id, Account.getAccountIdFromShortcutSafeUri(mMockContext, uri1));
280e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        assertEquals(account2Id, Account.getAccountIdFromShortcutSafeUri(mMockContext, uri2));
281e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki
282e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        // Test for the Eclair(2.0-2.1) style URI.
283e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        assertEquals(account1Id, Account.getAccountIdFromShortcutSafeUri(mMockContext,
284e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki                getEclairStyleShortcutUri(account1)));
285e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        assertEquals(account2Id, Account.getAccountIdFromShortcutSafeUri(mMockContext,
286e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki                getEclairStyleShortcutUri(account2)));
287e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki    }
288e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki
289e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki    private static Uri getEclairStyleShortcutUri(Account account) {
290e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        // We used _id instead of UUID only on Eclair(2.0-2.1).
291e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki        return Account.CONTENT_URI.buildUpon().appendEncodedPath("" + account.mId).build();
292e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki    }
293e37881aac562c066e6e666349749e40b7c116a21Makoto Onuki
2948d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank    public void testGetProtocol() {
2958d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        Account account1 = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext);
2968d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        // add hostauth data, with protocol
297fe68c0e7c2672e09076038b36ad24f095633d313Todd Kennedy        account1.mHostAuthRecv = ProviderTestUtils.setupHostAuth("eas", "account-hostauth-recv",
2988d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank                false, mMockContext);
2998d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        // Note that getProtocol uses the receive host auth, so the protocol here shouldn't matter
3008d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        // to the test result
301fe68c0e7c2672e09076038b36ad24f095633d313Todd Kennedy        account1.mHostAuthSend = ProviderTestUtils.setupHostAuth("foo", "account-hostauth-send",
3028d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank                false, mMockContext);
3038d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        account1.save(mMockContext);
30425144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        assertEquals("eas", Account.getProtocol(mMockContext, account1.mId));
30525144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        assertEquals("eas", account1.getProtocol(mMockContext));
3068d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        Account account2 = ProviderTestUtils.setupAccount("account-nohostauth", false,
3078d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank                mMockContext);
3088d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        account2.save(mMockContext);
3098d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        // Make sure that we return null when there's no host auth
3108d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        assertNull(Account.getProtocol(mMockContext, account2.mId));
3118d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        assertNull(account2.getProtocol(mMockContext));
3128d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        // And when there's no account
3138d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank        assertNull(Account.getProtocol(mMockContext, 0));
3148d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank    }
3158d8f86e899165772a7d91250b98dfc3c0d78b538Marc Blank
316652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki    public void testAccountIsValidId() {
317652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki        final Account account1 = ProviderTestUtils.setupAccount("account-1", true, mMockContext);
318652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki        final Account account2 = ProviderTestUtils.setupAccount("account-2", true, mMockContext);
319652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki
320652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki        assertTrue(Account.isValidId(mMockContext, account1.mId));
321652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki        assertTrue(Account.isValidId(mMockContext, account2.mId));
322652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki
323652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki        assertFalse(Account.isValidId(mMockContext, 1234567)); // Some random ID
324c133e6f1858e33fabaa6ffa173c1402bf9a98e31Makoto Onuki        assertFalse(Account.isValidId(mMockContext, -1));
325c133e6f1858e33fabaa6ffa173c1402bf9a98e31Makoto Onuki        assertFalse(Account.isValidId(mMockContext, -500));
326652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki    }
327652be6fb3d04a4ceba6b765cd3160cdaef9e6107Makoto Onuki
32841f775bbc731b25854da9f5a80c66a4aa1eb0af9satok    private final static String[] MAILBOX_UNREAD_COUNT_PROJECTION = new String [] {
32941f775bbc731b25854da9f5a80c66a4aa1eb0af9satok        MailboxColumns.UNREAD_COUNT
33041f775bbc731b25854da9f5a80c66a4aa1eb0af9satok    };
33141f775bbc731b25854da9f5a80c66a4aa1eb0af9satok    private final static int MAILBOX_UNREAD_COUNT_COLMUN = 0;
33241f775bbc731b25854da9f5a80c66a4aa1eb0af9satok
33341f775bbc731b25854da9f5a80c66a4aa1eb0af9satok    /**
33441f775bbc731b25854da9f5a80c66a4aa1eb0af9satok     * Get the value of the unread count in the mailbox of the account.
33541f775bbc731b25854da9f5a80c66a4aa1eb0af9satok     * This can be different from the actual number of unread messages in that mailbox.
33641f775bbc731b25854da9f5a80c66a4aa1eb0af9satok     */
33741f775bbc731b25854da9f5a80c66a4aa1eb0af9satok    private int getUnreadCount(long mailboxId) {
33841f775bbc731b25854da9f5a80c66a4aa1eb0af9satok        String text = null;
33941f775bbc731b25854da9f5a80c66a4aa1eb0af9satok        Cursor c = null;
34041f775bbc731b25854da9f5a80c66a4aa1eb0af9satok        try {
34141f775bbc731b25854da9f5a80c66a4aa1eb0af9satok            c = mMockContext.getContentResolver().query(
34241f775bbc731b25854da9f5a80c66a4aa1eb0af9satok                    Mailbox.CONTENT_URI,
34341f775bbc731b25854da9f5a80c66a4aa1eb0af9satok                    MAILBOX_UNREAD_COUNT_PROJECTION,
34441f775bbc731b25854da9f5a80c66a4aa1eb0af9satok                    EmailContent.RECORD_ID + "=?",
34541f775bbc731b25854da9f5a80c66a4aa1eb0af9satok                    new String[] { String.valueOf(mailboxId) },
34641f775bbc731b25854da9f5a80c66a4aa1eb0af9satok                    null);
34741f775bbc731b25854da9f5a80c66a4aa1eb0af9satok            c.moveToFirst();
34841f775bbc731b25854da9f5a80c66a4aa1eb0af9satok            text = c.getString(MAILBOX_UNREAD_COUNT_COLMUN);
34941f775bbc731b25854da9f5a80c66a4aa1eb0af9satok        } finally {
35041f775bbc731b25854da9f5a80c66a4aa1eb0af9satok            c.close();
35141f775bbc731b25854da9f5a80c66a4aa1eb0af9satok        }
35241f775bbc731b25854da9f5a80c66a4aa1eb0af9satok        return Integer.valueOf(text);
35341f775bbc731b25854da9f5a80c66a4aa1eb0af9satok    }
35441f775bbc731b25854da9f5a80c66a4aa1eb0af9satok
3554119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler    private static String[] expectedAttachmentNames =
356976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        new String[] {"attachment1.doc", "attachment2.xls", "attachment3"};
357976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank    // The lengths need to be kept in ascending order
3584119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler    private static long[] expectedAttachmentSizes = new long[] {31415L, 97701L, 151213L};
359976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank
360fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda    /*
361fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * Returns null if the message has no body.
362fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     */
363fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda    private Body loadBodyForMessageId(long messageId) {
364fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Cursor c = null;
365fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        try {
366fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda            c = mMockContext.getContentResolver().query(
367fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda                    EmailContent.Body.CONTENT_URI,
368fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda                    EmailContent.Body.CONTENT_PROJECTION,
369fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda                    EmailContent.Body.MESSAGE_KEY + "=?",
370fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda                    new String[] {String.valueOf(messageId)},
371fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda                    null);
372fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda            int numBodies = c.getCount();
373fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda            assertTrue("at most one body", numBodies < 2);
374fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda            return c.moveToFirst() ? EmailContent.getContent(c, Body.class) : null;
375fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        } finally {
376fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda            c.close();
377fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        }
378fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda    }
379fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
3807143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
3817143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * Test simple message save/retrieve
382758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank     *
3837143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: serverId vs. serverIntId
3847143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
3857143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    public void testMessageSave() {
38628448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Account account1 = ProviderTestUtils.setupAccount("message-save", true, mMockContext);
3877143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long account1Id = account1.mId;
38828448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
3897143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long box1Id = box1.mId;
3907143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
3917143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // Test a simple message (saved with no body)
39228448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false,
39328448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                true, mMockContext);
3947143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long message1Id = message1.mId;
3957143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        Message message1get = EmailContent.Message.restoreMessageWithId(mMockContext, message1Id);
39628448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        ProviderTestUtils.assertMessageEqual("testMessageSave", message1, message1get);
3977143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
3987143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // Test a message saved with a body
3997143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // Note that it will read back w/o the text & html so we must extract those
40028448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true,
40128448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                true, mMockContext);
4027143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long message2Id = message2.mId;
4037143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        String text2 = message2.mText;
4047143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        String html2 = message2.mHtml;
405936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        String textReply2 = message2.mTextReply;
406936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        String htmlReply2 = message2.mHtmlReply;
4076c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        long sourceKey2 = message2.mSourceKey;
4085fc57eccefeba517fe5e6f093786f37c6c4d50e6Marc Blank        String introText2 = message2.mIntroText;
4097143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        message2.mText = null;
4107143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        message2.mHtml = null;
411936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        message2.mTextReply = null;
412936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        message2.mHtmlReply = null;
413e25698319325d5e9619bc45ae85d861b0f1c5954Marc Blank        message2.mSourceKey = 0;
4145fc57eccefeba517fe5e6f093786f37c6c4d50e6Marc Blank        message2.mIntroText = null;
4157143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        Message message2get = EmailContent.Message.restoreMessageWithId(mMockContext, message2Id);
41628448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        ProviderTestUtils.assertMessageEqual("testMessageSave", message2, message2get);
417758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
4187143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // Now see if there's a body saved with the right stuff
419fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Body body2 = loadBodyForMessageId(message2Id);
420fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        assertEquals("body text", text2, body2.mTextContent);
421fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        assertEquals("body html", html2, body2.mHtmlContent);
422936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        assertEquals("reply text", textReply2, body2.mTextReply);
423936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        assertEquals("reply html", htmlReply2, body2.mHtmlReply);
4246c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        assertEquals("source key", sourceKey2, body2.mSourceKey);
4255fc57eccefeba517fe5e6f093786f37c6c4d50e6Marc Blank        assertEquals("intro text", introText2, body2.mIntroText);
426976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank
4274119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        // Message with attachments and body
428976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        Message message3 = ProviderTestUtils.setupMessage("message3", account1Id, box1Id, true,
429976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                false, mMockContext);
430976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        ArrayList<Attachment> atts = new ArrayList<Attachment>();
431976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        for (int i = 0; i < 3; i++) {
4324119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            atts.add(ProviderTestUtils.setupAttachment(
4334119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    -1, expectedAttachmentNames[i], expectedAttachmentSizes[i],
4344119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    false, mMockContext));
435976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        }
436976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        message3.mAttachments = atts;
4379e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        message3.save(mMockContext);
438976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        long message3Id = message3.mId;
439976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank
440976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        // Now check the attachments; there should be three and they should match name and size
441fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Cursor c = null;
442976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        try {
443976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            // Note that there is NO guarantee of the order of returned records in the general case,
444976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            // so we specifically ask for ordering by size.  The expectedAttachmentSizes array must
445976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            // be kept sorted by size (ascending) for this test to work properly
446976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            c = mMockContext.getContentResolver().query(
447976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                    Attachment.CONTENT_URI,
448976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                    Attachment.CONTENT_PROJECTION,
449976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                    Attachment.MESSAGE_KEY + "=?",
450976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                    new String[] {
451976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                            String.valueOf(message3Id)
452976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                    },
453976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                    Attachment.SIZE);
454976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            int numAtts = c.getCount();
455976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            assertEquals(3, numAtts);
456976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            int i = 0;
457976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            while (c.moveToNext()) {
4584119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                Attachment actual = EmailContent.getContent(c, Attachment.class);
4594119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                ProviderTestUtils.assertAttachmentEqual("save-message3", atts.get(i), actual);
4604119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                i++;
4614119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            }
4624119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        } finally {
4634119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            c.close();
4644119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        }
4654119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler
4664119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        // Message with attachments but no body
4674119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        Message message4 = ProviderTestUtils.setupMessage("message4", account1Id, box1Id, false,
4684119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                false, mMockContext);
4694119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        atts = new ArrayList<Attachment>();
4704119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        for (int i = 0; i < 3; i++) {
4714119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            atts.add(ProviderTestUtils.setupAttachment(
4724119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    -1, expectedAttachmentNames[i], expectedAttachmentSizes[i],
4734119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    false, mMockContext));
4744119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        }
4754119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        message4.mAttachments = atts;
4769e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        message4.save(mMockContext);
4774119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        long message4Id = message4.mId;
4784119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler
4794119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        // Now check the attachments; there should be three and they should match name and size
4804119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        c = null;
48141f775bbc731b25854da9f5a80c66a4aa1eb0af9satok
4824119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        try {
4834119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            // Note that there is NO guarantee of the order of returned records in the general case,
4844119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            // so we specifically ask for ordering by size.  The expectedAttachmentSizes array must
4854119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            // be kept sorted by size (ascending) for this test to work properly
4864119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            c = mMockContext.getContentResolver().query(
4874119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    Attachment.CONTENT_URI,
4884119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    Attachment.CONTENT_PROJECTION,
4894119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    Attachment.MESSAGE_KEY + "=?",
4904119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    new String[] {
4914119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                            String.valueOf(message4Id)
4924119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    },
4934119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                    Attachment.SIZE);
4944119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            int numAtts = c.getCount();
4954119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            assertEquals(3, numAtts);
4964119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            int i = 0;
4974119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            while (c.moveToNext()) {
4984119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                Attachment actual = EmailContent.getContent(c, Attachment.class);
4994119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                ProviderTestUtils.assertAttachmentEqual("save-message4", atts.get(i), actual);
500976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                i++;
501976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            }
502976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        } finally {
503976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            c.close();
5047143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        }
50544144d0301213536b02f2080237b63d8271b2d0bMihai Preda
50644144d0301213536b02f2080237b63d8271b2d0bMihai Preda        // test EmailContent.restoreAttachmentsWitdMessageId()
50744144d0301213536b02f2080237b63d8271b2d0bMihai Preda        Attachment[] attachments =
50844144d0301213536b02f2080237b63d8271b2d0bMihai Preda            Attachment.restoreAttachmentsWithMessageId(mMockContext, message4Id);
50944144d0301213536b02f2080237b63d8271b2d0bMihai Preda        int size = attachments.length;
51044144d0301213536b02f2080237b63d8271b2d0bMihai Preda        assertEquals(3, size);
51144144d0301213536b02f2080237b63d8271b2d0bMihai Preda        for (int i = 0; i < size; ++i) {
51244144d0301213536b02f2080237b63d8271b2d0bMihai Preda            ProviderTestUtils.assertAttachmentEqual("save-message4", atts.get(i), attachments[i]);
51344144d0301213536b02f2080237b63d8271b2d0bMihai Preda        }
5147143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    }
515758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
5167143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
517e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank     * Test that saving a message creates the proper snippet for that message
518e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank     */
519e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank    public void testMessageSaveAddsSnippet() {
520e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        Account account = ProviderTestUtils.setupAccount("message-snippet", true, mMockContext);
521e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        Mailbox box = ProviderTestUtils.setupMailbox("box1", account.mId, true, mMockContext);
522e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank
523e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        // Create a message without a body, unsaved
524e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        Message message = ProviderTestUtils.setupMessage("message", account.mId, box.mId, false,
525e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank                false, mMockContext);
526e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        message.mText = "This is some text";
527e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        message.mHtml = "<html>This is some text</html>";
528e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        message.save(mMockContext);
529e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        Message restoredMessage = Message.restoreMessageWithId(mMockContext, message.mId);
530e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        // We should have the plain text as the snippet
53176f614720db6a282c3e890969a600585122cdcc5Marc Blank        assertEquals(restoredMessage.mSnippet,
53276f614720db6a282c3e890969a600585122cdcc5Marc Blank                TextUtilities.makeSnippetFromPlainText(message.mText));
533e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank
534e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        // Start again
535e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        message = ProviderTestUtils.setupMessage("message", account.mId, box.mId, false,
536e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank                false, mMockContext);
537e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        message.mText = null;
538e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        message.mHtml = "<html>This is some text</html>";
539e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        message.save(mMockContext);
540e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        restoredMessage = Message.restoreMessageWithId(mMockContext, message.mId);
541e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank        // We should have the plain text as the snippet
54276f614720db6a282c3e890969a600585122cdcc5Marc Blank        assertEquals(restoredMessage.mSnippet,
54376f614720db6a282c3e890969a600585122cdcc5Marc Blank                TextUtilities.makeSnippetFromHtmlText(message.mHtml));
544e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank    }
545e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank
546e7b9e4ab94093127002f4cddd3ffcc3deb1c0b41Marc Blank    /**
5477143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: update account
5487143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
549758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
5507143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
5517143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: update mailbox
5527143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
553758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
5547143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
5557143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: update message
5567143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
557758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
5587143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
5597143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * Test delete account
5607143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: hostauth
5617143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
5627143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    public void testAccountDelete() {
56328448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Account account1 = ProviderTestUtils.setupAccount("account-delete-1", true, mMockContext);
5647143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long account1Id = account1.mId;
56528448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Account account2 = ProviderTestUtils.setupAccount("account-delete-2", true, mMockContext);
5667143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long account2Id = account2.mId;
5677143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
5687143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // make sure there are two accounts
5697143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        int numBoxes = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null);
5707143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(2, numBoxes);
5717143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
5727143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // now delete one of them
5737143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, account1Id);
5747143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
5757143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
5767143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // make sure there's only one account now
5777143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        numBoxes = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null);
5787143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(1, numBoxes);
5797143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
5807143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // now delete the other one
5817143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        uri = ContentUris.withAppendedId(Account.CONTENT_URI, account2Id);
5827143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
5837143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
5847143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // make sure there are no accounts now
5857143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        numBoxes = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null);
5867143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(0, numBoxes);
5877143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    }
588758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
5897143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
5909627d014e16235eadf981b9165807dc72a14a383Mihai Preda     * Test for Body.lookupBodyIdWithMessageId()
5919627d014e16235eadf981b9165807dc72a14a383Mihai Preda     * Verifies that:
5929627d014e16235eadf981b9165807dc72a14a383Mihai Preda     * - for a message without body, -1 is returned.
5939627d014e16235eadf981b9165807dc72a14a383Mihai Preda     * - for a mesage with body, the id matches the one from loadBodyForMessageId.
5949627d014e16235eadf981b9165807dc72a14a383Mihai Preda     */
5959627d014e16235eadf981b9165807dc72a14a383Mihai Preda    public void testLookupBodyIdWithMessageId() {
5969627d014e16235eadf981b9165807dc72a14a383Mihai Preda        final ContentResolver resolver = mMockContext.getContentResolver();
5979627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Account account1 = ProviderTestUtils.setupAccount("orphaned body", true, mMockContext);
5989627d014e16235eadf981b9165807dc72a14a383Mihai Preda        long account1Id = account1.mId;
5999627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
6009627d014e16235eadf981b9165807dc72a14a383Mihai Preda        long box1Id = box1.mId;
6019627d014e16235eadf981b9165807dc72a14a383Mihai Preda
6029627d014e16235eadf981b9165807dc72a14a383Mihai Preda        // 1. create message with no body, check that returned bodyId is -1
6039627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false,
6049627d014e16235eadf981b9165807dc72a14a383Mihai Preda                true, mMockContext);
6059627d014e16235eadf981b9165807dc72a14a383Mihai Preda        long message1Id = message1.mId;
606bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        long bodyId1 = Body.lookupBodyIdWithMessageId(mMockContext, message1Id);
6079627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertEquals(bodyId1, -1);
6089627d014e16235eadf981b9165807dc72a14a383Mihai Preda
6099627d014e16235eadf981b9165807dc72a14a383Mihai Preda        // 2. create message with body, check that returned bodyId is correct
6109627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true,
6119627d014e16235eadf981b9165807dc72a14a383Mihai Preda                true, mMockContext);
6129627d014e16235eadf981b9165807dc72a14a383Mihai Preda        long message2Id = message2.mId;
613bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        long bodyId2 = Body.lookupBodyIdWithMessageId(mMockContext, message2Id);
6149627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Body body = loadBodyForMessageId(message2Id);
6159627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertNotNull(body);
6169627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertEquals(body.mId, bodyId2);
6179627d014e16235eadf981b9165807dc72a14a383Mihai Preda    }
6189627d014e16235eadf981b9165807dc72a14a383Mihai Preda
6199627d014e16235eadf981b9165807dc72a14a383Mihai Preda    /**
6209627d014e16235eadf981b9165807dc72a14a383Mihai Preda     * Test for Body.updateBodyWithMessageId().
6219627d014e16235eadf981b9165807dc72a14a383Mihai Preda     * 1. - create message without body,
6229627d014e16235eadf981b9165807dc72a14a383Mihai Preda     *    - update its body (set TEXT_CONTENT)
6239627d014e16235eadf981b9165807dc72a14a383Mihai Preda     *    - check correct updated body is read back
6249627d014e16235eadf981b9165807dc72a14a383Mihai Preda     *
6259627d014e16235eadf981b9165807dc72a14a383Mihai Preda     * 2. - create message with body,
6269627d014e16235eadf981b9165807dc72a14a383Mihai Preda     *    - update body (set TEXT_CONTENT)
6279627d014e16235eadf981b9165807dc72a14a383Mihai Preda     *    - check correct updated body is read back
6289627d014e16235eadf981b9165807dc72a14a383Mihai Preda     */
6299627d014e16235eadf981b9165807dc72a14a383Mihai Preda    public void testUpdateBodyWithMessageId() {
6309627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Account account1 = ProviderTestUtils.setupAccount("orphaned body", true, mMockContext);
6319627d014e16235eadf981b9165807dc72a14a383Mihai Preda        long account1Id = account1.mId;
6329627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
6339627d014e16235eadf981b9165807dc72a14a383Mihai Preda        long box1Id = box1.mId;
6349627d014e16235eadf981b9165807dc72a14a383Mihai Preda
6359627d014e16235eadf981b9165807dc72a14a383Mihai Preda        final String textContent = "foobar some odd text";
636936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        final String htmlContent = "and some html";
637936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        final String textReply = "plain text reply";
638936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        final String htmlReply = "or the html reply";
6395fc57eccefeba517fe5e6f093786f37c6c4d50e6Marc Blank        final String introText = "fred wrote:";
6409627d014e16235eadf981b9165807dc72a14a383Mihai Preda
6419627d014e16235eadf981b9165807dc72a14a383Mihai Preda        ContentValues values = new ContentValues();
6429627d014e16235eadf981b9165807dc72a14a383Mihai Preda        values.put(BodyColumns.TEXT_CONTENT, textContent);
643936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        values.put(BodyColumns.HTML_CONTENT, htmlContent);
644936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        values.put(BodyColumns.TEXT_REPLY, textReply);
645936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        values.put(BodyColumns.HTML_REPLY, htmlReply);
646e25698319325d5e9619bc45ae85d861b0f1c5954Marc Blank        values.put(BodyColumns.SOURCE_MESSAGE_KEY, 17);
6475fc57eccefeba517fe5e6f093786f37c6c4d50e6Marc Blank        values.put(BodyColumns.INTRO_TEXT, introText);
6489627d014e16235eadf981b9165807dc72a14a383Mihai Preda
6499627d014e16235eadf981b9165807dc72a14a383Mihai Preda        // 1
6509627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false,
6519627d014e16235eadf981b9165807dc72a14a383Mihai Preda                true, mMockContext);
6529627d014e16235eadf981b9165807dc72a14a383Mihai Preda        long message1Id = message1.mId;
6539627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Body body1 = loadBodyForMessageId(message1Id);
6549627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertNull(body1);
6559627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Body.updateBodyWithMessageId(mMockContext, message1Id, values);
6569627d014e16235eadf981b9165807dc72a14a383Mihai Preda        body1 = loadBodyForMessageId(message1Id);
6579627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertNotNull(body1);
6589627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertEquals(body1.mTextContent, textContent);
659936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        assertEquals(body1.mHtmlContent, htmlContent);
660936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        assertEquals(body1.mTextReply, textReply);
661936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        assertEquals(body1.mHtmlReply, htmlReply);
662e25698319325d5e9619bc45ae85d861b0f1c5954Marc Blank        assertEquals(body1.mSourceKey, 17);
6635fc57eccefeba517fe5e6f093786f37c6c4d50e6Marc Blank        assertEquals(body1.mIntroText, introText);
6649627d014e16235eadf981b9165807dc72a14a383Mihai Preda
6659627d014e16235eadf981b9165807dc72a14a383Mihai Preda        // 2
6669627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true,
6679627d014e16235eadf981b9165807dc72a14a383Mihai Preda                true, mMockContext);
6689627d014e16235eadf981b9165807dc72a14a383Mihai Preda        long message2Id = message2.mId;
6699627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Body body2 = loadBodyForMessageId(message2Id);
6709627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertNotNull(body2);
6719627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertTrue(!body2.mTextContent.equals(textContent));
6729627d014e16235eadf981b9165807dc72a14a383Mihai Preda        Body.updateBodyWithMessageId(mMockContext, message2Id, values);
6739627d014e16235eadf981b9165807dc72a14a383Mihai Preda        body2 = loadBodyForMessageId(message1Id);
6749627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertNotNull(body2);
6759627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertEquals(body2.mTextContent, textContent);
676936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        assertEquals(body2.mHtmlContent, htmlContent);
677936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        assertEquals(body2.mTextReply, textReply);
678936babc145e2e6eb2e222f2ce5e3da8f9e4fb9f2Andrew Stadler        assertEquals(body2.mHtmlReply, htmlReply);
679e25698319325d5e9619bc45ae85d861b0f1c5954Marc Blank        assertEquals(body2.mSourceKey, 17);
6805fc57eccefeba517fe5e6f093786f37c6c4d50e6Marc Blank        assertEquals(body2.mIntroText, introText);
6819627d014e16235eadf981b9165807dc72a14a383Mihai Preda    }
6829627d014e16235eadf981b9165807dc72a14a383Mihai Preda
6839627d014e16235eadf981b9165807dc72a14a383Mihai Preda    /**
684c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler     * Test body retrieve methods
685c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler     */
686c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler    public void testBodyRetrieve() {
687c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler        // No account needed
688c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler        // No mailbox needed
689c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler        Message message1 = ProviderTestUtils.setupMessage("bodyretrieve", 1, 1, true,
690c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler                true, mMockContext);
691c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler        long messageId = message1.mId;
692c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler
693c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler        assertEquals(message1.mText,
694c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler                Body.restoreBodyTextWithMessageId(mMockContext, messageId));
695c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler        assertEquals(message1.mHtml,
696c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler                Body.restoreBodyHtmlWithMessageId(mMockContext, messageId));
697c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler        assertEquals(message1.mTextReply,
698c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler                Body.restoreReplyTextWithMessageId(mMockContext, messageId));
699c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler        assertEquals(message1.mHtmlReply,
700c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler                Body.restoreReplyHtmlWithMessageId(mMockContext, messageId));
701c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler        assertEquals(message1.mIntroText,
702c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler                Body.restoreIntroTextWithMessageId(mMockContext, messageId));
7031575e7860d2259f1aed201ab23d526cddf787365Mihai Preda        assertEquals(message1.mSourceKey,
7041575e7860d2259f1aed201ab23d526cddf787365Mihai Preda                Body.restoreBodySourceKey(mMockContext, messageId));
705c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler    }
706c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler
707c41c47fa07a22f8a7612fb0191f152a36d95b7a5Andrew Stadler    /**
708fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * Test delete body.
709fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 1. create message without body (message id 1)
710fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 2. create message with body (message id 2. The body has _id 1 and messageKey 2).
711fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 3. delete first message.
712fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 4. verify that body for message 2 has not been deleted.
713fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 5. delete message 2, verify body is deleted.
714fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     */
715fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda    public void testDeleteBody() {
716fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        final ContentResolver resolver = mMockContext.getContentResolver();
717fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
718fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // Create account and mailboxes
719fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Account account1 = ProviderTestUtils.setupAccount("orphaned body", true, mMockContext);
720fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        long account1Id = account1.mId;
721fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
722fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        long box1Id = box1.mId;
723fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
724fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 1. create message without body
725fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false,
726fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda                true, mMockContext);
727fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        long message1Id = message1.mId;
728fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
729fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 2. create message with body
730fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true,
731fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda                true, mMockContext);
732fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        long message2Id = message2.mId;
733fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // verify body is there
734fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        assertNotNull(loadBodyForMessageId(message2Id));
735fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
736fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 3. delete first message
737fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        resolver.delete(ContentUris.withAppendedId(Message.CONTENT_URI, message1Id), null, null);
738c0c9c33322deecace00a32766e0a1b355aad4b31Marc Blank
739fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 4. verify body for second message wasn't deleted
740fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        assertNotNull(loadBodyForMessageId(message2Id));
741fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
742fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 5. delete second message, check its body is deleted
743fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        resolver.delete(ContentUris.withAppendedId(Message.CONTENT_URI, message2Id), null, null);
744fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        assertNull(loadBodyForMessageId(message2Id));
745fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda    }
746fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
747fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda    /**
748fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * Test delete orphan bodies.
749fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 1. create message without body (message id 1)
750fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 2. create message with body (message id 2. Body has _id 1 and messageKey 2).
751fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 3. delete first message.
752fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 4. delete some other mailbox -- this triggers delete orphan bodies.
753fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     * 5. verify that body for message 2 has not been deleted.
754fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda     */
755fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda    public void testDeleteOrphanBodies() {
756fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        final ContentResolver resolver = mMockContext.getContentResolver();
757fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
758ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Create account and two mailboxes
759fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Account account1 = ProviderTestUtils.setupAccount("orphaned body", true, mMockContext);
760fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        long account1Id = account1.mId;
761fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
762fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        long box1Id = box1.mId;
763fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Mailbox box2 = ProviderTestUtils.setupMailbox("box2", account1Id, true, mMockContext);
764fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        long box2Id = box2.mId;
765fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
766fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 1. create message without body
767fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false,
768fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda                true, mMockContext);
769fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        long message1Id = message1.mId;
770fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
771fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 2. create message with body
772fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true,
773fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda                true, mMockContext);
774fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        long message2Id = message2.mId;
775fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        //verify body is there
776fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        assertNotNull(loadBodyForMessageId(message2Id));
777fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
778fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 3. delete first message
779fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        resolver.delete(ContentUris.withAppendedId(Message.CONTENT_URI, message1Id), null, null);
780fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
781fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 4. delete some mailbox (because it triggers "delete orphan bodies")
782fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        resolver.delete(ContentUris.withAppendedId(Mailbox.CONTENT_URI, box2Id), null, null);
783fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
784fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        // 5. verify body for second message wasn't deleted during "delete orphan bodies"
785fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda        assertNotNull(loadBodyForMessageId(message2Id));
786fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda    }
787fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda
788fb7974f5bfb6275fb856b0f7ff386ef10680c83aMihai Preda    /**
7890efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank     * Note that we can't use EmailContent.count() here because it uses a projection including
7900efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank     * count(*), and count(*) is incompatible with a LIMIT (i.e. the limit would be applied to the
7910efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank     * single column returned with count(*), rather than to the query itself)
7920efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank     */
7930efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank    private int count(Context context, Uri uri, String selection, String[] selectionArgs) {
7940efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        Cursor c = context.getContentResolver().query(uri, EmailContent.ID_PROJECTION, selection,
7950efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank                selectionArgs, null);
7960efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        try {
7970efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank            return c.getCount();
7980efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        } finally {
7990efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank            c.close();
8000efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        }
8010efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank    }
8020efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank
8030efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank    public void testMessageQueryWithLimit() {
8040efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        final Context context = mMockContext;
8050efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank
8060efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        // Create account and two mailboxes
8070efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        Account acct = ProviderTestUtils.setupAccount("orphaned body", true, context);
8080efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", acct.mId, true, context);
8090efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        Mailbox box2 = ProviderTestUtils.setupMailbox("box2", acct.mId, true, context);
8100efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank
8110efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        // Create 4 messages in box1
8120efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        ProviderTestUtils.setupMessage("message1", acct.mId, box1.mId, false, true, context);
8130efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        ProviderTestUtils.setupMessage("message2", acct.mId, box1.mId, false, true, context);
8140efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        ProviderTestUtils.setupMessage("message3", acct.mId, box1.mId, false, true, context);
8150efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        ProviderTestUtils.setupMessage("message4", acct.mId, box1.mId, false, true, context);
8160efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank
8170efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        // Create 4 messages in box2
8180efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        ProviderTestUtils.setupMessage("message1", acct.mId, box2.mId, false, true, context);
8190efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        ProviderTestUtils.setupMessage("message2", acct.mId, box2.mId, false, true, context);
8200efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        ProviderTestUtils.setupMessage("message3", acct.mId, box2.mId, false, true, context);
8210efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        ProviderTestUtils.setupMessage("message4", acct.mId, box2.mId, false, true, context);
8220efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank
8230efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        // Check normal case, special case (limit 1), and arbitrary limits
8240efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        assertEquals(8, count(mMockContext, Message.CONTENT_URI, null, null));
8250efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        assertEquals(1, count(mMockContext, EmailContent.uriWithLimit(Message.CONTENT_URI, 1),
8260efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank                null, null));
8270efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        assertEquals(3, count(mMockContext, EmailContent.uriWithLimit(Message.CONTENT_URI, 3),
8280efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank                null, null));
8290efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        assertEquals(8, count(mMockContext, EmailContent.uriWithLimit(Message.CONTENT_URI, 100),
8300efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank                null, null));
8310efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank
8320efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        // Check that it works with selection/selection args
8330efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        String[] args = new String[] {Long.toString(box1.mId)};
8340efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        assertEquals(4, count(mMockContext, Message.CONTENT_URI,
8350efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank                MessageColumns.MAILBOX_KEY + "=?", args));
8360efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank        assertEquals(1, count(mMockContext,
8370efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank                EmailContent.uriWithLimit(Message.CONTENT_URI, 1),
8380efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank                MessageColumns.MAILBOX_KEY + "=?", args));
8390efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank    }
8400efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank
8410efe738e05a31e0c1ebfba645bd2364a373a3f33Marc Blank    /**
842ef83299b99288c00b9d661260d19715e73e6889cMarc Blank     * Test delete orphan messages
843ef83299b99288c00b9d661260d19715e73e6889cMarc Blank     * 1. create message without body (message id 1)
844ef83299b99288c00b9d661260d19715e73e6889cMarc Blank     * 2. create message with body (message id 2. Body has _id 1 and messageKey 2).
845ef83299b99288c00b9d661260d19715e73e6889cMarc Blank     * 3. delete first message.
846ef83299b99288c00b9d661260d19715e73e6889cMarc Blank     * 4. delete some other mailbox -- this triggers delete orphan bodies.
847ef83299b99288c00b9d661260d19715e73e6889cMarc Blank     * 5. verify that body for message 2 has not been deleted.
848ef83299b99288c00b9d661260d19715e73e6889cMarc Blank     */
849ef83299b99288c00b9d661260d19715e73e6889cMarc Blank     public void testDeleteOrphanMessages() {
850ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        final ContentResolver resolver = mMockContext.getContentResolver();
851ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        final Context context = mMockContext;
852ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
853ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Create account and two mailboxes
854ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Account acct = ProviderTestUtils.setupAccount("orphaned body", true, context);
855ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", acct.mId, true, context);
856ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Mailbox box2 = ProviderTestUtils.setupMailbox("box2", acct.mId, true, context);
857ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
858ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Create 4 messages in box1
859ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msg1_1 =
860ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("message1", acct.mId, box1.mId, false, true, context);
861ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msg1_2 =
862ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("message2", acct.mId, box1.mId, false, true, context);
863ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msg1_3 =
864ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("message3", acct.mId, box1.mId, false, true, context);
865ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msg1_4 =
866ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("message4", acct.mId, box1.mId, false, true, context);
867ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
868ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Create 4 messages in box2
869ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msg2_1 =
870ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("message1", acct.mId, box2.mId, false, true, context);
871ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msg2_2 =
872ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("message2", acct.mId, box2.mId, false, true, context);
873ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msg2_3 =
874ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("message3", acct.mId, box2.mId, false, true, context);
875ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msg2_4 =
876ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("message4", acct.mId, box2.mId, false, true, context);
877ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
878ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Delete 2 from each mailbox
879ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg1_1.mId),
880ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                null, null);
881ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg1_2.mId),
882ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                null, null);
883ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg2_1.mId),
884ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                null, null);
885ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg2_2.mId),
886ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                null, null);
887ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
888ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // There should be 4 items in the deleted item table
889ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(4, EmailContent.count(context, Message.DELETED_CONTENT_URI, null, null));
890ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
891ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Update 2 from each mailbox
892ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        ContentValues v = new ContentValues();
893ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        v.put(MessageColumns.DISPLAY_NAME, "--updated--");
894ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg1_3.mId),
895ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                v, null, null);
896ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg1_4.mId),
897ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                v, null, null);
898ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg2_3.mId),
899ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                v, null, null);
900ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg2_4.mId),
901ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                v, null, null);
902ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
903ef83299b99288c00b9d661260d19715e73e6889cMarc Blank         // There should be 4 items in the updated item table
904ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(4, EmailContent.count(context, Message.UPDATED_CONTENT_URI, null, null));
905ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
906ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Manually add 2 messages from a "deleted" mailbox to deleted and updated tables
907ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Use a value > 2 for the deleted box id
908ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        long delBoxId = 10;
909ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Create 4 messages in the "deleted" mailbox
910ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msgX_A =
911ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("messageA", acct.mId, delBoxId, false, false, context);
912ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msgX_B =
913ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("messageB", acct.mId, delBoxId, false, false, context);
914ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msgX_C =
915ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("messageC", acct.mId, delBoxId, false, false, context);
916ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message msgX_D =
917ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            ProviderTestUtils.setupMessage("messageD", acct.mId, delBoxId, false, false, context);
918ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
919ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        ContentValues cv;
920ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // We have to assign id's manually because there are no autoincrement id's for these tables
921ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Start with an id that won't exist, since id's in these tables must be unique
922ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        long msgId = 10;
923ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // It's illegal to manually insert these, so we need to catch the exception
924ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // NOTE: The insert succeeds, and then throws the exception
925ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        try {
926ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            cv = msgX_A.toContentValues();
927ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            cv.put(EmailContent.RECORD_ID, msgId++);
928ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            resolver.insert(Message.DELETED_CONTENT_URI, cv);
929ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        } catch (IllegalArgumentException e) {
930ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        }
931ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        try {
932ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            cv = msgX_B.toContentValues();
933ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            cv.put(EmailContent.RECORD_ID, msgId++);
934ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            resolver.insert(Message.DELETED_CONTENT_URI, cv);
935ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        } catch (IllegalArgumentException e) {
936ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        }
937ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        try {
938ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            cv = msgX_C.toContentValues();
939ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            cv.put(EmailContent.RECORD_ID, msgId++);
940ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            resolver.insert(Message.UPDATED_CONTENT_URI, cv);
941ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        } catch (IllegalArgumentException e) {
942ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        }
943ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        try {
944ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            cv = msgX_D.toContentValues();
945ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            cv.put(EmailContent.RECORD_ID, msgId++);
946ef83299b99288c00b9d661260d19715e73e6889cMarc Blank            resolver.insert(Message.UPDATED_CONTENT_URI, cv);
947ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        } catch (IllegalArgumentException e) {
948ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        }
949ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
950ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // There should be 6 items in the deleted and updated tables
951ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(6, EmailContent.count(context, Message.UPDATED_CONTENT_URI, null, null));
952ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(6, EmailContent.count(context, Message.DELETED_CONTENT_URI, null, null));
953ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
954ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Delete the orphans
955ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        EmailProvider.deleteOrphans(EmailProvider.getReadableDatabase(context),
956ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                Message.DELETED_TABLE_NAME);
957ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        EmailProvider.deleteOrphans(EmailProvider.getReadableDatabase(context),
958ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                Message.UPDATED_TABLE_NAME);
959ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
960ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // There should now be 4 messages in each of the deleted and updated tables again
961ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(4, EmailContent.count(context, Message.UPDATED_CONTENT_URI, null, null));
962ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(4, EmailContent.count(context, Message.DELETED_CONTENT_URI, null, null));
963ef83299b99288c00b9d661260d19715e73e6889cMarc Blank    }
964ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
965ef83299b99288c00b9d661260d19715e73e6889cMarc Blank    /**
9667143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * Test delete message
9677143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: body
9687143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: attachments
9697143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
9707143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    public void testMessageDelete() {
97128448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Account account1 = ProviderTestUtils.setupAccount("message-delete", true, mMockContext);
9727143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long account1Id = account1.mId;
97328448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
9747143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long box1Id = box1.mId;
97528448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false,
97628448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                true, mMockContext);
9777143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long message1Id = message1.mId;
97828448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, false,
97928448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                true, mMockContext);
9807143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long message2Id = message2.mId;
9817143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
9827143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        String selection = EmailContent.MessageColumns.ACCOUNT_KEY + "=? AND " +
9837143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler                EmailContent.MessageColumns.MAILBOX_KEY + "=?";
9847143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        String[] selArgs = new String[] { String.valueOf(account1Id), String.valueOf(box1Id) };
9857143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
9867143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // make sure there are two messages
9877143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
9887143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(2, numMessages);
9897143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
9907143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // now delete one of them
9917143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        Uri uri = ContentUris.withAppendedId(Message.CONTENT_URI, message1Id);
9927143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
9937143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
9947143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // make sure there's only one message now
9957143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
9967143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(1, numMessages);
9977143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
9987143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // now delete the other one
9997143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        uri = ContentUris.withAppendedId(Message.CONTENT_URI, message2Id);
10007143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
10017143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
10027143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // make sure there are no messages now
10037143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
10047143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(0, numMessages);
10057143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    }
1006758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
10077143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
1008ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler     * Test delete synced message
1009ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler     * TODO: body
1010ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler     * TODO: attachments
1011ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler     */
1012ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler    public void testSyncedMessageDelete() {
101328448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Account account1 = ProviderTestUtils.setupAccount("synced-message-delete", true,
101428448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                mMockContext);
1015ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        long account1Id = account1.mId;
101628448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
1017ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        long box1Id = box1.mId;
101828448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false,
101928448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                true, mMockContext);
1020ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        long message1Id = message1.mId;
102128448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, false,
102228448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                true, mMockContext);
1023ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        long message2Id = message2.mId;
1024ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1025ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        String selection = EmailContent.MessageColumns.ACCOUNT_KEY + "=? AND "
1026ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                + EmailContent.MessageColumns.MAILBOX_KEY + "=?";
1027ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        String[] selArgs = new String[] {
1028ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler            String.valueOf(account1Id), String.valueOf(box1Id)
1029ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        };
1030ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1031ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // make sure there are two messages
1032ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
1033ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(2, numMessages);
1034ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1035ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // make sure we start with no synced deletions
1036ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection,
1037ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                selArgs);
1038ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(0, numMessages);
1039ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1040ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // now delete one of them SYNCED
1041ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        Uri uri = ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message1Id);
1042ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
1043ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1044ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // make sure there's only one message now
1045ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
1046ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(1, numMessages);
1047ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1048ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // make sure there's one synced deletion now
1049ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection,
1050ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                selArgs);
1051ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(1, numMessages);
1052ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1053ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // now delete the other one NOT SYNCED
1054ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        uri = ContentUris.withAppendedId(Message.CONTENT_URI, message2Id);
1055ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
1056ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1057ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // make sure there are no messages now
1058ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
1059ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(0, numMessages);
1060ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1061ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // make sure there's still one deletion now
1062ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection,
1063ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                selArgs);
1064ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(1, numMessages);
1065ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler    }
1066ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1067ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler    /**
1068ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler     * Test message update
1069ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler     * TODO: body
1070ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler     * TODO: attachments
1071ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler     */
1072ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler    public void testMessageUpdate() {
107328448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Account account1 = ProviderTestUtils.setupAccount("message-update", true, mMockContext);
1074ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        long account1Id = account1.mId;
107528448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
1076ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        long box1Id = box1.mId;
107728448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false,
107828448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                true, mMockContext);
1079ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        long message1Id = message1.mId;
108028448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, false,
108128448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                true, mMockContext);
1082ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        long message2Id = message2.mId;
1083ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        ContentResolver cr = mMockContext.getContentResolver();
1084ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1085ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        String selection = EmailContent.MessageColumns.ACCOUNT_KEY + "=? AND "
1086ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                + EmailContent.MessageColumns.MAILBOX_KEY + "=?";
1087ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        String[] selArgs = new String[] {
1088ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler            String.valueOf(account1Id), String.valueOf(box1Id)
1089ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        };
1090ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1091ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // make sure there are two messages
1092ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
1093ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(2, numMessages);
1094ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1095ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // change the first one
1096ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        Uri uri = ContentUris.withAppendedId(Message.CONTENT_URI, message1Id);
1097ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        ContentValues cv = new ContentValues();
1098ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        cv.put(MessageColumns.FROM_LIST, "from-list");
1099ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        cr.update(uri, cv, null, null);
1100ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1101ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // make sure there's no updated message
1102ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection,
1103ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                selArgs);
1104ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(0, numMessages);
1105ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1106ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // get the message back from the provider, make sure the change "stuck"
1107ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        Message restoredMessage = Message.restoreMessageWithId(mMockContext, message1Id);
1108ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals("from-list", restoredMessage.mFrom);
1109ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1110ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // change the second one
1111ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        uri = ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message2Id);
1112ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        cv = new ContentValues();
1113ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        cv.put(MessageColumns.FROM_LIST, "from-list");
1114ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        cr.update(uri, cv, null, null);
1115ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1116ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // make sure there's one updated message
1117ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection,
1118ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                selArgs);
1119ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(1, numMessages);
1120ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1121ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // get the message back from the provider, make sure the change "stuck",
1122ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // as before
1123ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        restoredMessage = Message.restoreMessageWithId(mMockContext, message2Id);
1124ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals("from-list", restoredMessage.mFrom);
1125ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1126ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // get the original message back from the provider
1127ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        Cursor c = cr.query(Message.UPDATED_CONTENT_URI, Message.CONTENT_PROJECTION, null, null,
1128ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                null);
1129ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        try {
1130ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler            assertTrue(c.moveToFirst());
1131ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler            Message originalMessage = EmailContent.getContent(c, Message.class);
1132ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler            // make sure this has the original value
1133ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler            assertEquals("from message2", originalMessage.mFrom);
1134ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler            // Should only be one
1135ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler            assertFalse(c.moveToNext());
1136ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        } finally {
1137ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler            c.close();
1138ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        }
1139ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1140ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // delete the second message
1141ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        cr.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message2Id), null, null);
1142ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1143ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // hey, presto! the change should be gone
1144ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection,
1145ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                selArgs);
1146ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(0, numMessages);
1147ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1148ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        // and there should now be a deleted record
1149ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection,
1150ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler                selArgs);
1151ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler        assertEquals(1, numMessages);
1152ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler    }
1153ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler
1154ddc871d9eddd088c0200a30dbfeb24812a81cf80Andrew Stadler    /**
11557143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: cascaded delete account
11567143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: hostauth
11577143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: body
11587143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: attachments
11597143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: create other account, mailbox & messages and confirm the right objects were deleted
11607143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
11617143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    public void testCascadeDeleteAccount() {
116228448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Account account1 = ProviderTestUtils.setupAccount("account-delete-cascade", true,
116328448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                mMockContext);
11647143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long account1Id = account1.mId;
116528448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
11667143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long box1Id = box1.mId;
116728448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        /* Message message1 = */ ProviderTestUtils.setupMessage("message1", account1Id, box1Id,
116828448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                false, true, mMockContext);
116928448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        /* Message message2 = */ ProviderTestUtils.setupMessage("message2", account1Id, box1Id,
117028448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                false, true, mMockContext);
11717143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
11727143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // make sure there is one account, one mailbox, and two messages
11737143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        int numAccounts = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null);
11747143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(1, numAccounts);
11757143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        int numBoxes = EmailContent.count(mMockContext, Mailbox.CONTENT_URI, null, null);
11767143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(1, numBoxes);
11777143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null);
11787143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(2, numMessages);
11797143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
11807143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // delete the account
11817143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, account1Id);
11827143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
11837143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
11847143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // make sure there are no accounts, mailboxes, or messages
11857143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        numAccounts = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null);
11867143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(0, numAccounts);
11877143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        numBoxes = EmailContent.count(mMockContext, Mailbox.CONTENT_URI, null, null);
11887143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(0, numBoxes);
11897143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null);
11907143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(0, numMessages);
11917143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    }
1192758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
11937143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
11947143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * Test cascaded delete mailbox
11957143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: body
11967143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: attachments
11977143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     * TODO: create other mailbox & messages and confirm the right objects were deleted
11987143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
11997143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    public void testCascadeDeleteMailbox() {
120028448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Account account1 = ProviderTestUtils.setupAccount("mailbox-delete-cascade", true,
120128448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                mMockContext);
12027143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long account1Id = account1.mId;
120328448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
12047143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        long box1Id = box1.mId;
1205ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id,
120628448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                false, true, mMockContext);
1207ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id,
1208ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                false, true, mMockContext);
1209ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message message3 = ProviderTestUtils.setupMessage("message3", account1Id, box1Id,
1210ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                false, true, mMockContext);
1211ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        Message message4 = ProviderTestUtils.setupMessage("message4", account1Id, box1Id,
121228448e782b825d1978c0923003a2cf91efe733dcAndrew Stadler                false, true, mMockContext);
1213ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        ProviderTestUtils.setupMessage("message5", account1Id, box1Id, false, true, mMockContext);
1214ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        ProviderTestUtils.setupMessage("message6", account1Id, box1Id, false, true, mMockContext);
12157143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
12167143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        String selection = EmailContent.MessageColumns.ACCOUNT_KEY + "=? AND " +
12177143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler                EmailContent.MessageColumns.MAILBOX_KEY + "=?";
12187143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        String[] selArgs = new String[] { String.valueOf(account1Id), String.valueOf(box1Id) };
12197143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
12200e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // make sure there are six messages
12217143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
1222ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(6, numMessages);
1223ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
1224ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        ContentValues cv = new ContentValues();
1225ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        cv.put(Message.SERVER_ID, "SERVER_ID");
1226ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        ContentResolver resolver = mMockContext.getContentResolver();
1227ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
1228ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Update two messages
1229ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message1.mId),
1230ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                cv, null, null);
1231ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message2.mId),
1232ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                cv, null, null);
1233ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // Delete two messages
1234ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message3.mId),
1235ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                null, null);
1236ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message4.mId),
1237ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                null, null);
1238ef83299b99288c00b9d661260d19715e73e6889cMarc Blank
1239ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // There should now be two messages in updated/deleted, and 4 in messages
1240ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
1241ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(4, numMessages);
1242ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection,
1243ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                selArgs);
1244ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(2, numMessages);
1245ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection,
1246ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                selArgs);
12477143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(2, numMessages);
1248758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
12497143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        // now delete the mailbox
12507143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        Uri uri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, box1Id);
1251ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        resolver.delete(uri, null, null);
1252758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1253ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        // there should now be zero messages in all three tables
12547143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs);
12557143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler        assertEquals(0, numMessages);
1256ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection,
1257ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                selArgs);
1258ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(0, numMessages);
1259ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection,
1260ef83299b99288c00b9d661260d19715e73e6889cMarc Blank                selArgs);
1261ef83299b99288c00b9d661260d19715e73e6889cMarc Blank        assertEquals(0, numMessages);
12627143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    }
1263758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
12647143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler    /**
12656c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler     * Test cascaded delete message
12666c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler     * Confirms that deleting a message will also delete its body & attachments
12677143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler     */
12686c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler    public void testCascadeMessageDelete() {
12696c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        Account account1 = ProviderTestUtils.setupAccount("message-cascade", true, mMockContext);
12706c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        long account1Id = account1.mId;
12716c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext);
12726c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        long box1Id = box1.mId;
1273bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki
12746c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // Each message has a body, and also give each 2 attachments
12756c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true,
12766c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                false, mMockContext);
12776c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        ArrayList<Attachment> atts = new ArrayList<Attachment>();
12786c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        for (int i = 0; i < 2; i++) {
12796c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler            atts.add(ProviderTestUtils.setupAttachment(
12806c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                    -1, expectedAttachmentNames[i], expectedAttachmentSizes[i],
12816c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                    false, mMockContext));
12826c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        }
12836c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        message1.mAttachments = atts;
12846c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        message1.save(mMockContext);
12856c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        long message1Id = message1.mId;
12866c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
12876c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, true,
12886c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                false, mMockContext);
12896c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        atts = new ArrayList<Attachment>();
12906c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        for (int i = 0; i < 2; i++) {
12916c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler            atts.add(ProviderTestUtils.setupAttachment(
12926c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                    -1, expectedAttachmentNames[i], expectedAttachmentSizes[i],
12936c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                    false, mMockContext));
12946c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        }
12956c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        message2.mAttachments = atts;
12966c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        message2.save(mMockContext);
12976c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        long message2Id = message2.mId;
12986c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
12996c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // Set up to test total counts of bodies & attachments for our test messages
13006c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        String bodySelection = BodyColumns.MESSAGE_KEY + " IN (?,?)";
13016c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        String attachmentSelection = AttachmentColumns.MESSAGE_KEY + " IN (?,?)";
13026c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        String[] selArgs = new String[] { String.valueOf(message1Id), String.valueOf(message2Id) };
1303bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki
13046c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // make sure there are two bodies
13056c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        int numBodies = EmailContent.count(mMockContext, Body.CONTENT_URI, bodySelection, selArgs);
13066c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        assertEquals(2, numBodies);
13076c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
13086c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // make sure there are four attachments
13096c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        int numAttachments = EmailContent.count(mMockContext, Attachment.CONTENT_URI,
13106c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                attachmentSelection, selArgs);
13116c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        assertEquals(4, numAttachments);
13126c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
13136c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // now delete one of the messages
13146c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        Uri uri = ContentUris.withAppendedId(Message.CONTENT_URI, message1Id);
13156c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
13166c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
13176c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // there should be one body and two attachments
13186c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        numBodies = EmailContent.count(mMockContext, Body.CONTENT_URI, bodySelection, selArgs);
13196c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        assertEquals(1, numBodies);
13206c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
13216c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        numAttachments = EmailContent.count(mMockContext, Attachment.CONTENT_URI,
13226c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                attachmentSelection, selArgs);
13236c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        assertEquals(2, numAttachments);
13246c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
13256c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // now delete the other message
13266c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        uri = ContentUris.withAppendedId(Message.CONTENT_URI, message2Id);
13276c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
13286c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
13296c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // make sure there are no bodies or attachments
13306c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        numBodies = EmailContent.count(mMockContext, Body.CONTENT_URI, bodySelection, selArgs);
13316c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        assertEquals(0, numBodies);
13326c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
13336c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        numAttachments = EmailContent.count(mMockContext, Attachment.CONTENT_URI,
13346c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                attachmentSelection, selArgs);
13356c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        assertEquals(0, numAttachments);
13366c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler    }
13377143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler
1338976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank    /**
1339976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank     * Test that our unique file name algorithm works as expected.  Since this test requires an
1340976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank     * SD card, we check the environment first, and return immediately if none is mounted.
1341976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank     * @throws IOException
1342976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank     */
1343976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank    public void testCreateUniqueFile() throws IOException {
1344976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        // Delete existing files, if they exist
1345976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
1346976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            return;
1347976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        }
1348976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        try {
1349976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            String fileName = "A11achm3n1.doc";
1350976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            File uniqueFile = Attachment.createUniqueFile(fileName);
1351976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            assertEquals(fileName, uniqueFile.getName());
1352976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            if (uniqueFile.createNewFile()) {
1353976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                uniqueFile = Attachment.createUniqueFile(fileName);
1354976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                assertEquals("A11achm3n1-2.doc", uniqueFile.getName());
1355976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                if (uniqueFile.createNewFile()) {
1356976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                    uniqueFile = Attachment.createUniqueFile(fileName);
1357976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                    assertEquals("A11achm3n1-3.doc", uniqueFile.getName());
1358976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                }
1359976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank           }
1360976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            fileName = "A11achm3n1";
1361976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            uniqueFile = Attachment.createUniqueFile(fileName);
1362976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            assertEquals(fileName, uniqueFile.getName());
1363976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            if (uniqueFile.createNewFile()) {
1364976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                uniqueFile = Attachment.createUniqueFile(fileName);
1365976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                assertEquals("A11achm3n1-2", uniqueFile.getName());
1366976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            }
1367976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        } finally {
1368976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            File directory = Environment.getExternalStorageDirectory();
1369976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            // These are the files that should be created earlier in the test.  Make sure
1370976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            // they are deleted for the next go-around
1371976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            String[] fileNames = new String[] {"A11achm3n1.doc", "A11achm3n1-2.doc", "A11achm3n1"};
1372976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            int length = fileNames.length;
1373976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            for (int i = 0; i < length; i++) {
1374976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                File file = new File(directory, fileNames[i]);
1375976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                if (file.exists()) {
1376976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                    file.delete();
1377976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank                }
1378976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank            }
1379976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank        }
1380976f92908dd2f69f21f62690632ff24b08d9f5d3Marc Blank    }
13814119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler
13824119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler    /**
13834119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler     * Test retrieving attachments by message ID (using EmailContent.Attachment.MESSAGE_ID_URI)
13844119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler     */
13854119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler    public void testGetAttachmentByMessageIdUri() {
13864119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler
13874119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        // Note, we don't strictly need accounts, mailboxes or messages to run this test.
13884119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        Attachment a1 = ProviderTestUtils.setupAttachment(1, "a1", 100, true, mMockContext);
13894119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        Attachment a2 = ProviderTestUtils.setupAttachment(1, "a2", 200, true, mMockContext);
1390758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        ProviderTestUtils.setupAttachment(2, "a3", 300, true, mMockContext);
1391758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        ProviderTestUtils.setupAttachment(2, "a4", 400, true, mMockContext);
13924119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler
13934119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        // Now ask for the attachments of message id=1
13944119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        // Note: Using the "sort by size" trick to bring them back in expected order
13954119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, 1);
13964119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        Cursor c = mMockContext.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION,
13974119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler                null, null, Attachment.SIZE);
13984119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        assertEquals(2, c.getCount());
13994119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler
14004119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        try {
14014119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            c.moveToFirst();
14024119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            Attachment a1Get = EmailContent.getContent(c, Attachment.class);
14034119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            ProviderTestUtils.assertAttachmentEqual("getAttachByUri-1", a1, a1Get);
14044119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            c.moveToNext();
14054119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            Attachment a2Get = EmailContent.getContent(c, Attachment.class);
14064119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            ProviderTestUtils.assertAttachmentEqual("getAttachByUri-2", a2, a2Get);
14074119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        } finally {
14084119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler            c.close();
14094119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler        }
14104119218e2fd64341ac946fb8f2cbdb796a444cb8Andrew Stadler    }
141154c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
141254c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler    /**
14136c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler     * Test deleting attachments by message ID (using EmailContent.Attachment.MESSAGE_ID_URI)
14146c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler     */
14156c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler    public void testDeleteAttachmentByMessageIdUri() {
14166c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        ContentResolver mockResolver = mMockContext.getContentResolver();
14176c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
14186c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // Note, we don't strictly need accounts, mailboxes or messages to run this test.
14196c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        ProviderTestUtils.setupAttachment(1, "a1", 100, true, mMockContext);
14206c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        ProviderTestUtils.setupAttachment(1, "a2", 200, true, mMockContext);
14216c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        Attachment a3 = ProviderTestUtils.setupAttachment(2, "a3", 300, true, mMockContext);
14226c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        Attachment a4 = ProviderTestUtils.setupAttachment(2, "a4", 400, true, mMockContext);
14236c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
14246c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // Delete all attachments for message id=1
14256c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, 1);
14266c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        mockResolver.delete(uri, null, null);
14276c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
14286c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // Read back all attachments and confirm that we have the expected remaining attachments
14296c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        // (the attachments that are set for message id=2).  Note order-by size to simplify test.
14306c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        Cursor c = mockResolver.query(Attachment.CONTENT_URI, Attachment.CONTENT_PROJECTION,
14316c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler                null, null, Attachment.SIZE);
14326c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        assertEquals(2, c.getCount());
14336c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
14346c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        try {
14356c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler            c.moveToFirst();
14366c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler            Attachment a3Get = EmailContent.getContent(c, Attachment.class);
14376c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler            ProviderTestUtils.assertAttachmentEqual("getAttachByUri-3", a3, a3Get);
14386c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler            c.moveToNext();
14396c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler            Attachment a4Get = EmailContent.getContent(c, Attachment.class);
14406c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler            ProviderTestUtils.assertAttachmentEqual("getAttachByUri-4", a4, a4Get);
14416c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        } finally {
14426c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler            c.close();
14436c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler        }
14446c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler    }
14456c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler
14466c21942ec45f561d711b3d74ecca8e62afb735c4Andrew Stadler    /**
144754c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler     * Tests of default account behavior
1448758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank     *
144954c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler     * 1.  Simple set/get
145054c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler     * 2.  Moving default between 3 accounts
145154c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler     * 3.  Delete default, make sure another becomes default
145254c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler     */
145354c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler    public void testSetGetDefaultAccount() {
145454c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        // There should be no default account if there are no accounts
145554c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        long defaultAccountId = Account.getDefaultAccountId(mMockContext);
145654c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        assertEquals(-1, defaultAccountId);
145754c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
145854c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        Account account1 = ProviderTestUtils.setupAccount("account-default-1", true, mMockContext);
145954c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        long account1Id = account1.mId;
146054c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        Account account2 = ProviderTestUtils.setupAccount("account-default-2", true, mMockContext);
146154c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        long account2Id = account2.mId;
146254c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        Account account3 = ProviderTestUtils.setupAccount("account-default-3", true, mMockContext);
146354c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        long account3Id = account3.mId;
146454c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
14659e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        // With three accounts, but none marked default, confirm that some default account
14669e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        // is returned.  Which one is undefined here.
146754c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        defaultAccountId = Account.getDefaultAccountId(mMockContext);
14689e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        assertTrue(defaultAccountId == account1Id
14699e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler                    || defaultAccountId == account2Id
14709e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler                    || defaultAccountId == account3Id);
147154c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
14729e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        updateIsDefault(account1, true);
147354c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        defaultAccountId = Account.getDefaultAccountId(mMockContext);
14749e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        assertEquals(account1Id, defaultAccountId);
147554c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
1476531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        updateIsDefault(account2, true);
1477531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        defaultAccountId = Account.getDefaultAccountId(mMockContext);
1478531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        assertEquals(account2Id, defaultAccountId);
1479531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank
1480531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        updateIsDefault(account3, true);
1481531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        defaultAccountId = Account.getDefaultAccountId(mMockContext);
1482531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        assertEquals(account3Id, defaultAccountId);
148354c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
148454c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        // Now delete a non-default account and confirm no change
148554c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, account1Id);
148654c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
148754c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
1488531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        defaultAccountId = Account.getDefaultAccountId(mMockContext);
1489531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        assertEquals(account3Id, defaultAccountId);
149054c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
149154c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        // Now confirm deleting the default account and it switches to another one
149254c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        uri = ContentUris.withAppendedId(Account.CONTENT_URI, account3Id);
149354c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
149454c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
1495531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        defaultAccountId = Account.getDefaultAccountId(mMockContext);
1496531ae9d25f4310e4774e136ed74ed9749f940861Marc Blank        assertEquals(account2Id, defaultAccountId);
1497758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
14989e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        // Now delete the final account and confirm there are no default accounts again
14999e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        uri = ContentUris.withAppendedId(Account.CONTENT_URI, account2Id);
15009e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        mMockContext.getContentResolver().delete(uri, null, null);
15019e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler
150254c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler        defaultAccountId = Account.getDefaultAccountId(mMockContext);
15039e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        assertEquals(-1, defaultAccountId);
150454c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler    }
150554c1f2bf9a6574240b7c9af253f83a2b566442abAndrew Stadler
15069e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler    private void updateIsDefault(Account account, boolean newState) {
15079e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        account.setDefaultAccount(newState);
15089e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        ContentValues cv = new ContentValues();
15099e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        cv.put(AccountColumns.IS_DEFAULT, account.mIsDefault);
15109e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler        account.update(mMockContext, cv);
15119e2c6bd5f21f2d19eef7ebfe30e6fdf94ede0857Andrew Stadler    }
1512758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1513758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank    public static Message setupUnreadMessage(String name, long accountId, long mailboxId,
1514758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank            boolean addBody, boolean saveIt, Context context) {
1515758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        Message msg =
1516758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank            ProviderTestUtils.setupMessage(name, accountId, mailboxId, addBody, false, context);
1517758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        msg.mFlagRead = false;
1518758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        if (saveIt) {
1519758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank            msg.save(context);
1520758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        }
1521758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        return msg;
1522758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank    }
1523758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1524758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank    public void testUnreadCountTriggers() {
1525758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        // Start with one account and three mailboxes
1526758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        Account account = ProviderTestUtils.setupAccount("triggers", true, mMockContext);
1527758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        Mailbox boxA = ProviderTestUtils.setupMailbox("boxA", account.mId, true, mMockContext);
1528758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        Mailbox boxB = ProviderTestUtils.setupMailbox("boxB", account.mId, true, mMockContext);
1529758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        Mailbox boxC = ProviderTestUtils.setupMailbox("boxC", account.mId, true, mMockContext);
1530758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1531758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        // Make sure there are no unreads
1532758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(0, getUnreadCount(boxA.mId));
1533758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(0, getUnreadCount(boxB.mId));
1534758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(0, getUnreadCount(boxC.mId));
1535758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1536758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        // Create 4 unread messages (only 3 named) in boxA
1537758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        Message message1 = setupUnreadMessage("message1", account.mId, boxA.mId,
1538758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank                false, true, mMockContext);
1539758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        Message message2= setupUnreadMessage("message2", account.mId, boxA.mId,
1540758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank                false, true, mMockContext);
1541758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        Message message3 =  setupUnreadMessage("message3", account.mId, boxA.mId,
1542758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank                false, true, mMockContext);
1543758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        setupUnreadMessage("message4", account.mId, boxC.mId, false, true, mMockContext);
1544758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1545758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        // Make sure the unreads are where we expect them
1546758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(3, getUnreadCount(boxA.mId));
1547758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(0, getUnreadCount(boxB.mId));
1548758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxC.mId));
1549758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1550758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        // After deleting message 1, the count in box A should be decremented (to 2)
1551758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        ContentResolver cr = mMockContext.getContentResolver();
1552758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        Uri uri = ContentUris.withAppendedId(Message.CONTENT_URI, message1.mId);
1553758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cr.delete(uri, null, null);
1554758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(2, getUnreadCount(boxA.mId));
1555758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(0, getUnreadCount(boxB.mId));
1556758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxC.mId));
1557758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1558758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        // Move message 2 to box B, leaving 1 in box A and 1 in box B
1559758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        message2.mMailboxKey = boxB.mId;
1560758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        ContentValues cv = new ContentValues();
1561758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cv.put(MessageColumns.MAILBOX_KEY, boxB.mId);
1562758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, message2.mId), cv, null, null);
1563758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxA.mId));
1564758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxB.mId));
1565758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxC.mId));
1566758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1567758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        // Mark message 3 (from box A) read, leaving 0 in box A
1568758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cv.clear();
1569758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cv.put(MessageColumns.FLAG_READ, 1);
1570758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, message3.mId), cv, null, null);
1571758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(0, getUnreadCount(boxA.mId));
1572758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxB.mId));
1573758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxC.mId));
1574758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1575758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        // Move message 3 to box C; should be no change (it's read)
1576758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        message3.mMailboxKey = boxC.mId;
1577758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cv.clear();
1578758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cv.put(MessageColumns.MAILBOX_KEY, boxC.mId);
1579758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, message3.mId), cv, null, null);
1580758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(0, getUnreadCount(boxA.mId));
1581758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxB.mId));
1582758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxC.mId));
1583758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank
1584758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        // Mark message 3 unread; it's now in box C, so that box's count should go up to 3
1585758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cv.clear();
1586758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cv.put(MessageColumns.FLAG_READ, 0);
1587758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, message3.mId), cv, null, null);
1588758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(0, getUnreadCount(boxA.mId));
1589758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(1, getUnreadCount(boxB.mId));
1590758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank        assertEquals(2, getUnreadCount(boxC.mId));
1591758a532fce2f672673d38b2daa5f67eb757b118bMarc Blank    }
15929627d014e16235eadf981b9165807dc72a14a383Mihai Preda
15939627d014e16235eadf981b9165807dc72a14a383Mihai Preda    /**
15949627d014e16235eadf981b9165807dc72a14a383Mihai Preda     * Test for EmailProvider.createIndex().
15959627d014e16235eadf981b9165807dc72a14a383Mihai Preda     * Check that it returns exacly the same string as the one used previously for index creation.
15969627d014e16235eadf981b9165807dc72a14a383Mihai Preda     */
15979627d014e16235eadf981b9165807dc72a14a383Mihai Preda    public void testCreateIndex() {
15989627d014e16235eadf981b9165807dc72a14a383Mihai Preda        String oldStr = "create index message_" + MessageColumns.TIMESTAMP
15999627d014e16235eadf981b9165807dc72a14a383Mihai Preda            + " on " + Message.TABLE_NAME + " (" + MessageColumns.TIMESTAMP + ");";
16009627d014e16235eadf981b9165807dc72a14a383Mihai Preda        String newStr = EmailProvider.createIndex(Message.TABLE_NAME, MessageColumns.TIMESTAMP);
16019627d014e16235eadf981b9165807dc72a14a383Mihai Preda        assertEquals(newStr, oldStr);
16029627d014e16235eadf981b9165807dc72a14a383Mihai Preda    }
1603c0c9c33322deecace00a32766e0a1b355aad4b31Marc Blank
16040e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank    public void testDatabaseCorruptionRecovery() {
16050e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        final ContentResolver resolver = mMockContext.getContentResolver();
16060e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        final Context context = mMockContext;
16070e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16080e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Create account and two mailboxes
16090e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        Account acct = ProviderTestUtils.setupAccount("acct1", true, context);
16100e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", acct.mId, true, context);
16110e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16120e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Create 4 messages in box1 with bodies
16130e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        ProviderTestUtils.setupMessage("message1", acct.mId, box1.mId, true, true, context);
16140e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        ProviderTestUtils.setupMessage("message2", acct.mId, box1.mId, true, true, context);
16150e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        ProviderTestUtils.setupMessage("message3", acct.mId, box1.mId, true, true, context);
16160e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        ProviderTestUtils.setupMessage("message4", acct.mId, box1.mId, true, true, context);
16170e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16180e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Confirm there are four messages
16190e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        int count = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null);
16200e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertEquals(4, count);
16210e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Confirm there are four bodies
16220e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        count = EmailContent.count(mMockContext, Body.CONTENT_URI, null, null);
16230e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertEquals(4, count);
16240e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16250e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Find the EmailProvider.db file
16260e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        File dbFile = mMockContext.getDatabasePath(EmailProvider.DATABASE_NAME);
16270e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // The EmailProvider.db database should exist (the provider creates it automatically)
16280e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile != null);
16290e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile.exists());
16300e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Delete it, and confirm it is gone
16310e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile.delete());
16320e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertFalse(dbFile.exists());
16330e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16340e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Find the EmailProviderBody.db file
16350e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        dbFile = mMockContext.getDatabasePath(EmailProvider.BODY_DATABASE_NAME);
16360e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // The EmailProviderBody.db database should still exist
16370e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile != null);
16380e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile.exists());
16390e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16400e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // URI to uncache the databases
16410e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // This simulates the Provider starting up again (otherwise, it will still be pointing to
16420e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // the already opened files)
16430e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Note that we only have access to the EmailProvider via the ContentResolver; therefore,
16440e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // we cannot directly call into the provider and use a URI for this
16450e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        resolver.update(EmailProvider.INTEGRITY_CHECK_URI, null, null, null);
16460e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16470e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // TODO We should check for the deletion of attachment files once this is implemented in
16480e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // the provider
1649bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki
16500e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Explanation for what happens below...
16510e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // The next time the database is created by the provider, it will notice that there's
16520e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // already a EmailProviderBody.db file.  In this case, it will delete that database to
16530e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // ensure that both are in sync (and empty)
16540e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16550e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Confirm there are no bodies
16560e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        count = EmailContent.count(mMockContext, Body.CONTENT_URI, null, null);
16570e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertEquals(0, count);
16580e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16590e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Confirm there are no messages
16600e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        count = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null);
16610e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertEquals(0, count);
16620e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank    }
16630e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16640e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank    public void testBodyDatabaseCorruptionRecovery() {
16650e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        final ContentResolver resolver = mMockContext.getContentResolver();
16660e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        final Context context = mMockContext;
16670e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16680e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Create account and two mailboxes
16690e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        Account acct = ProviderTestUtils.setupAccount("acct1", true, context);
16700e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        Mailbox box1 = ProviderTestUtils.setupMailbox("box1", acct.mId, true, context);
16710e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16720e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Create 4 messages in box1 with bodies
16730e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        ProviderTestUtils.setupMessage("message1", acct.mId, box1.mId, true, true, context);
16740e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        ProviderTestUtils.setupMessage("message2", acct.mId, box1.mId, true, true, context);
16750e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        ProviderTestUtils.setupMessage("message3", acct.mId, box1.mId, true, true, context);
16760e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        ProviderTestUtils.setupMessage("message4", acct.mId, box1.mId, true, true, context);
16770e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16780e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Confirm there are four messages
16790e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        int count = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null);
16800e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertEquals(4, count);
16810e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Confirm there are four bodies
16820e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        count = EmailContent.count(mMockContext, Body.CONTENT_URI, null, null);
16830e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertEquals(4, count);
16840e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16850e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Find the EmailProviderBody.db file
16860e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        File dbFile = mMockContext.getDatabasePath(EmailProvider.BODY_DATABASE_NAME);
16870e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // The EmailProviderBody.db database should exist (the provider creates it automatically)
16880e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile != null);
16890e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile.exists());
16900e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Delete it, and confirm it is gone
16910e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile.delete());
16920e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertFalse(dbFile.exists());
16930e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
16940e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Find the EmailProvider.db file
16950e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        dbFile = mMockContext.getDatabasePath(EmailProvider.DATABASE_NAME);
16960e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // The EmailProviderBody.db database should still exist
16970e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile != null);
16980e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertTrue(dbFile.exists());
16990e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
17000e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // URI to uncache the databases
17010e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // This simulates the Provider starting up again (otherwise, it will still be pointing to
17020e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // the already opened files)
17030e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Note that we only have access to the EmailProvider via the ContentResolver; therefore,
17040e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // we cannot directly call into the provider and use a URI for this
17050e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        resolver.update(EmailProvider.INTEGRITY_CHECK_URI, null, null, null);
17060e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
17070e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // TODO We should check for the deletion of attachment files once this is implemented in
17080e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // the provider
17090e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
17100e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Explanation for what happens below...
17110e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // The next time the body database is created by the provider, it will notice that there's
17120e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // already a populated EmailProvider.db file.  In this case, it will delete that database to
17130e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // ensure that both are in sync (and empty)
17140e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
17150e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Confirm there are no messages
17160e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        count = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null);
17170e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertEquals(0, count);
17180e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank
17190e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        // Confirm there are no bodies
17200e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        count = EmailContent.count(mMockContext, Body.CONTENT_URI, null, null);
17210e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank        assertEquals(0, count);
17220e1595c177e40428b267a8696dfc05d015ce6a2fMarc Blank    }
1723694257cb7e7a191b3314cb209b6c38a8a07583efMarc Blank
1724bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki    public void testAccountIsSecurityHold() {
1725bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        final Context context = mMockContext;
1726bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        Account acct1 = ProviderTestUtils.setupAccount("acct1", true, context);
1727bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki
1728bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        Account acct2 = ProviderTestUtils.setupAccount("acct2", false, context);
1729bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        acct2.mFlags |= Account.FLAGS_SECURITY_HOLD;
1730bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        acct2.save(context);
1731bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki
1732bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        assertFalse(Account.isSecurityHold(context, acct1.mId));
1733bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        assertTrue(Account.isSecurityHold(context, acct2.mId));
1734bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        assertFalse(Account.isSecurityHold(context, 9999999)); // No such account
1735bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki   }
1736bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki
1737bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki    public void testClearAccountHoldFlags() {
1738bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        Account a1 = ProviderTestUtils.setupAccount("holdflag-1", false, mMockContext);
1739bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        a1.mFlags = Account.FLAGS_NOTIFY_NEW_MAIL;
1740bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        a1.save(mMockContext);
1741bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        Account a2 = ProviderTestUtils.setupAccount("holdflag-2", false, mMockContext);
1742bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        a2.mFlags = Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_SECURITY_HOLD;
1743bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        a2.save(mMockContext);
1744bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki
1745bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        // bulk clear
1746bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        Account.clearSecurityHoldOnAllAccounts(mMockContext);
1747bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki
1748bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        // confirm new values as expected - no hold flags; other flags unmolested
1749bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        Account a1a = Account.restoreAccountWithId(mMockContext, a1.mId);
1750bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        assertEquals(Account.FLAGS_NOTIFY_NEW_MAIL, a1a.mFlags);
1751bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        Account a2a = Account.restoreAccountWithId(mMockContext, a2.mId);
1752bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki        assertEquals(Account.FLAGS_VIBRATE_ALWAYS, a2a.mFlags);
1753bcf32320e2600e96c8a9e997a8903bfc3893b35eMakoto Onuki    }
1754574854b528163f3bf1a7cb974aa80082d1768edfMakoto Onuki
1755833fe73b99e62ad9cf6e80c782717c7de1ff12e4Makoto Onuki    private static Message createMessage(Context c, Mailbox b, boolean starred, boolean read) {
1756543953ad87a12d251fcec82c57d00383225b4149Todd Kennedy        return ProviderTestUtils.setupMessage(
1757543953ad87a12d251fcec82c57d00383225b4149Todd Kennedy                "1", b.mAccountKey, b.mId, true, true, c, starred, read);
1758543953ad87a12d251fcec82c57d00383225b4149Todd Kennedy    }
1759543953ad87a12d251fcec82c57d00383225b4149Todd Kennedy
17605247ab8cae802272b1e29e81d3b31a1c4e710da5Makoto Onuki    public void testAccountIsEasAccount() {
17616d8bfa67c438ee18921d13d8bfba876aacaa9ff6Makoto Onuki        Account account = new Account();
176225144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        // No hostauth
17635247ab8cae802272b1e29e81d3b31a1c4e710da5Makoto Onuki        assertFalse(account.isEasAccount(mMockContext));
17646d8bfa67c438ee18921d13d8bfba876aacaa9ff6Makoto Onuki
176525144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        checkAccountIsEasAccount(null, false);
176625144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        checkAccountIsEasAccount("", false);
176725144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        checkAccountIsEasAccount("x", false);
176825144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        checkAccountIsEasAccount("eas", true);
176925144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki    }
17706d8bfa67c438ee18921d13d8bfba876aacaa9ff6Makoto Onuki
177125144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki    private void checkAccountIsEasAccount(String protocol, boolean expected) {
177225144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        Account account = ProviderTestUtils.setupAccount("account", false, mMockContext);
177325144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        account.mHostAuthRecv = ProviderTestUtils.setupHostAuth(protocol, "account-hostauth-recv",
1774fe68c0e7c2672e09076038b36ad24f095633d313Todd Kennedy                false, mMockContext);
177525144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        account.save(mMockContext);
177625144e2b7990c50bb3650faf1aec341837c05348Makoto Onuki        assertEquals(expected, account.isEasAccount(mMockContext));
17776d8bfa67c438ee18921d13d8bfba876aacaa9ff6Makoto Onuki    }
1778bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank
1779bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank    public void testGetKeyColumnLong() {
1780bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        final Context c = mMockContext;
1781bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        Account a = ProviderTestUtils.setupAccount("acct", true, c);
1782bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a.mId, true, c, Mailbox.TYPE_MAIL);
1783bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        Mailbox b2 = ProviderTestUtils.setupMailbox("box2", a.mId, true, c, Mailbox.TYPE_MAIL);
1784bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        Message m1 = createMessage(c, b1, false, false);
1785bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        Message m2 = createMessage(c, b2, false, false);
1786bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        assertEquals(a.mId, Message.getKeyColumnLong(c, m1.mId, MessageColumns.ACCOUNT_KEY));
1787bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        assertEquals(a.mId, Message.getKeyColumnLong(c, m2.mId, MessageColumns.ACCOUNT_KEY));
1788bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        assertEquals(b1.mId, Message.getKeyColumnLong(c, m1.mId, MessageColumns.MAILBOX_KEY));
1789bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank        assertEquals(b2.mId, Message.getKeyColumnLong(c, m2.mId, MessageColumns.MAILBOX_KEY));
1790bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank    }
1791bca4e6e70b53ca7db0ac14522f0d26a7b465cf24Marc Blank
1792aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki    public void testGetAccountIdForMessageId() {
1793aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        final Context c = mMockContext;
1794aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        Account a1 = ProviderTestUtils.setupAccount("acct1", true, c);
1795aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        Account a2 = ProviderTestUtils.setupAccount("acct2", true, c);
1796aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a1.mId, true, c, Mailbox.TYPE_MAIL);
1797aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        Mailbox b2 = ProviderTestUtils.setupMailbox("box2", a2.mId, true, c, Mailbox.TYPE_MAIL);
1798aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        Message m1 = createMessage(c, b1, false, false);
1799aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        Message m2 = createMessage(c, b2, false, false);
1800aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki
1801aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        assertEquals(a1.mId, Account.getAccountIdForMessageId(c, m1.mId));
1802aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        assertEquals(a2.mId, Account.getAccountIdForMessageId(c, m2.mId));
1803aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki
1804aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        // message desn't exist
1805aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki        assertEquals(-1, Account.getAccountIdForMessageId(c, 12345));
1806aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki    }
1807aef9515ee70f1f0b6cc4fa601078597b55831331Makoto Onuki
1808200c6bd9fa19b78acc2c1664f858521aa9885353Todd Kennedy    public void testGetAccountForMessageId() {
1809200c6bd9fa19b78acc2c1664f858521aa9885353Todd Kennedy        final Context c = mMockContext;
1810200c6bd9fa19b78acc2c1664f858521aa9885353Todd Kennedy        Account a = ProviderTestUtils.setupAccount("acct", true, c);
1811200c6bd9fa19b78acc2c1664f858521aa9885353Todd Kennedy        Message m1 = ProviderTestUtils.setupMessage("1", a.mId, 1, true, true, c, false, false);
1812200c6bd9fa19b78acc2c1664f858521aa9885353Todd Kennedy        Message m2 = ProviderTestUtils.setupMessage("1", a.mId, 2, true, true, c, false, false);
1813200c6bd9fa19b78acc2c1664f858521aa9885353Todd Kennedy        ProviderTestUtils.assertAccountEqual("x", a, Account.getAccountForMessageId(c, m1.mId));
1814200c6bd9fa19b78acc2c1664f858521aa9885353Todd Kennedy        ProviderTestUtils.assertAccountEqual("x", a, Account.getAccountForMessageId(c, m2.mId));
1815200c6bd9fa19b78acc2c1664f858521aa9885353Todd Kennedy    }
1816200c6bd9fa19b78acc2c1664f858521aa9885353Todd Kennedy
1817e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki    public void testGetAccountGetInboxIdTest() {
1818e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki        final Context c = mMockContext;
1819e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki
1820e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki        // Prepare some data with red-herrings.
1821e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki        Account a1 = ProviderTestUtils.setupAccount("acct1", true, c);
1822e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki        Account a2 = ProviderTestUtils.setupAccount("acct2", true, c);
1823e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki        Mailbox b1i = ProviderTestUtils.setupMailbox("b1i", a1.mId, true, c, Mailbox.TYPE_INBOX);
1824e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki        Mailbox b2a = ProviderTestUtils.setupMailbox("b2a", a2.mId, true, c, Mailbox.TYPE_MAIL);
1825e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki        Mailbox b2i = ProviderTestUtils.setupMailbox("b2b", a2.mId, true, c, Mailbox.TYPE_INBOX);
1826e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki
1827e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki        assertEquals(b2i.mId, Account.getInboxId(c, a2.mId));
1828d25d87c7ba8bbbbcad771695a7085f227bac8a1bMakoto Onuki
1829d25d87c7ba8bbbbcad771695a7085f227bac8a1bMakoto Onuki        // No account found.
1830d25d87c7ba8bbbbcad771695a7085f227bac8a1bMakoto Onuki        assertEquals(-1, Account.getInboxId(c, 999999));
1831e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki    }
1832e357f5879187124c7af5c2ece5d7d3e4f60f07d2Makoto Onuki
1833261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki    /**
1834261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki     * Check if update to {@link Account#RESET_NEW_MESSAGE_COUNT_URI} resets the new message count.
1835261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki     */
1836261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki    public void testResetNewMessageCount() {
1837261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        final Context c = mMockContext;
1838261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        final ContentResolver cr = c.getContentResolver();
1839261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki
1840261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        // Prepare test data
1841261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        Account a1 = ProviderTestUtils.setupAccount("acct1", false, c);
1842261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a1.mNewMessageCount = 1;
1843261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a1.save(c);
1844261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        Account a2 = ProviderTestUtils.setupAccount("acct2", false, c);
1845261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a2.mNewMessageCount = 2;
1846261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a2.save(c);
1847261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        Account a3 = ProviderTestUtils.setupAccount("acct3", false, c);
1848261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a3.mNewMessageCount = 3;
1849261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a3.save(c);
1850261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        Account a4 = ProviderTestUtils.setupAccount("acct4", false, c);
1851261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a4.mNewMessageCount = 4;
1852261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a4.save(c);
1853261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        Account a5 = ProviderTestUtils.setupAccount("acct5", false, c);
1854261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a5.mNewMessageCount = 5;
1855261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        a5.save(c);
1856261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki
1857261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        // With ID in URI, no selection
1858261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        cr.update(ContentUris.withAppendedId(Account.RESET_NEW_MESSAGE_COUNT_URI, a1.mId),
1859261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki                null, null, null);
1860261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
1861261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(2, Account.restoreAccountWithId(c, a2.mId).mNewMessageCount);
1862261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(3, Account.restoreAccountWithId(c, a3.mId).mNewMessageCount);
1863261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(4, Account.restoreAccountWithId(c, a4.mId).mNewMessageCount);
1864261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(5, Account.restoreAccountWithId(c, a5.mId).mNewMessageCount);
1865261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki
1866261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        // No ID in URI, with selection
1867261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        cr.update(Account.RESET_NEW_MESSAGE_COUNT_URI, null,
1868261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki                EmailContent.ID_SELECTION, new String[] {Long.toString(a2.mId)});
1869261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
1870261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a2.mId).mNewMessageCount);
1871261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(3, Account.restoreAccountWithId(c, a3.mId).mNewMessageCount);
1872261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(4, Account.restoreAccountWithId(c, a4.mId).mNewMessageCount);
1873261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(5, Account.restoreAccountWithId(c, a5.mId).mNewMessageCount);
1874261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki
1875261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        // With ID, with selection
1876261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        cr.update(ContentUris.withAppendedId(Account.RESET_NEW_MESSAGE_COUNT_URI, a3.mId), null,
1877261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki                EmailContent.ID_SELECTION, new String[] {Long.toString(a3.mId)});
1878261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
1879261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a2.mId).mNewMessageCount);
1880261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a3.mId).mNewMessageCount);
1881261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(4, Account.restoreAccountWithId(c, a4.mId).mNewMessageCount);
1882261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(5, Account.restoreAccountWithId(c, a5.mId).mNewMessageCount);
1883261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki
1884261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        // No ID in URI, no selection
1885261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        cr.update(Account.RESET_NEW_MESSAGE_COUNT_URI, null, null, null);
1886261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
1887261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a2.mId).mNewMessageCount);
1888261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a3.mId).mNewMessageCount);
1889261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a4.mId).mNewMessageCount);
1890261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a5.mId).mNewMessageCount);
1891261d6c3f0c97a12256519a2c3b131a56e57ab45fMakoto Onuki    }
1892899c5b866192a4c4a12413446d10e5d98dbf94faMakoto Onuki
18939d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    /**
18949d5aaeacd6b222877f25924818317c9153708261Makoto Onuki     * Check if update on ACCOUNT_ID_ADD_TO_FIELD updates the cache properly.
18959d5aaeacd6b222877f25924818317c9153708261Makoto Onuki     */
18969d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    public void testUpdateCacheAccountIdAddToField() {
18979d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        final Context c = mMockContext;
18989d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
18999d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19009d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        int start = Account.restoreAccountWithId(c, a1.mId).mNewMessageCount;
19019d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19029d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        // +1 to NEW_MESSAGE_COUNT
19039d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        ContentValues cv = new ContentValues();
19049d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        cv.put(EmailContent.FIELD_COLUMN_NAME, AccountColumns.NEW_MESSAGE_COUNT);
19059d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        cv.put(EmailContent.ADD_COLUMN_NAME, 1);
19069d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        mProvider.update(ContentUris.withAppendedId(Account.ADD_TO_FIELD_URI, a1.mId), cv,
19079d5aaeacd6b222877f25924818317c9153708261Makoto Onuki                null, null);
19089d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19099d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        // Check
19109d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        assertEquals(start + 1, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
19119d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    }
19129d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19139d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    /**
19149d5aaeacd6b222877f25924818317c9153708261Makoto Onuki     * Check if update on ACCOUNT_RESET_NEW_COUNT updates the cache properly.
19159d5aaeacd6b222877f25924818317c9153708261Makoto Onuki     */
19169d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    public void testUpdateCacheAccountResetNewCount() {
19179d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        final Context c = mMockContext;
19189d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
19199d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19209d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        // precondition
19219d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        assertTrue(Account.restoreAccountWithId(c, a1.mId).mNewMessageCount > 0);
19229d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19239d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        // Reset
19249d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        mProvider.update(Account.RESET_NEW_MESSAGE_COUNT_URI, null, null, null);
19259d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19269d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        // Check
19279d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
19289d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    }
19299d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19309d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    /**
19319d5aaeacd6b222877f25924818317c9153708261Makoto Onuki     * Check if update on ACCOUNT_RESET_NEW_COUNT_ID updates the cache properly.
19329d5aaeacd6b222877f25924818317c9153708261Makoto Onuki     */
19339d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    public void testUpdateCacheAccountResetNewCountId() {
19349d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        final Context c = mMockContext;
19359d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
19369d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19379d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        // precondition
19389d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        assertTrue(Account.restoreAccountWithId(c, a1.mId).mNewMessageCount > 0);
19399d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19409d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        // Reset
19419d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        mProvider.update(ContentUris.withAppendedId(Account.RESET_NEW_MESSAGE_COUNT_URI, a1.mId),
19429d5aaeacd6b222877f25924818317c9153708261Makoto Onuki                null, null, null);
19439d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19449d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        // Check
19459d5aaeacd6b222877f25924818317c9153708261Makoto Onuki        assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
19469d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    }
19479d5aaeacd6b222877f25924818317c9153708261Makoto Onuki
19489d5aaeacd6b222877f25924818317c9153708261Makoto Onuki    /**
1949d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank     * Check that we're handling illegal uri's properly (by throwing an exception unless it's a
1950d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank     * query for an id of -1, in which case we return a zero-length cursor)
1951d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank     */
1952d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank    public void testIllegalUri() {
1953d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        final ContentResolver cr = mMockContext.getContentResolver();
1954d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank
1955d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        ContentValues cv = new ContentValues();
1956d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        Uri uri = Uri.parse("content://" + EmailContent.AUTHORITY + "/fooble");
1957d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        try {
1958d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            cr.insert(uri, cv);
1959d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            fail("Insert should have thrown exception");
1960d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        } catch (IllegalArgumentException e) {
1961d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        }
1962d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        try {
1963d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            cr.update(uri, cv, null, null);
1964d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            fail("Update should have thrown exception");
1965d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        } catch (IllegalArgumentException e) {
1966d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        }
1967d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        try {
1968d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            cr.delete(uri, null, null);
1969d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            fail("Delete should have thrown exception");
1970d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        } catch (IllegalArgumentException e) {
1971d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        }
1972d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        try {
1973d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            cr.query(uri, EmailContent.ID_PROJECTION, null, null, null);
1974d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            fail("Query should have thrown exception");
1975d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        } catch (IllegalArgumentException e) {
1976d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        }
1977d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        uri = Uri.parse("content://" + EmailContent.AUTHORITY + "/mailbox/fred");
1978d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        try {
1979d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            cr.query(uri, EmailContent.ID_PROJECTION, null, null, null);
1980d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank            fail("Query should have thrown exception");
1981d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        } catch (IllegalArgumentException e) {
1982d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        }
1983d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        uri = Uri.parse("content://" + EmailContent.AUTHORITY + "/mailbox/-1");
1984d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        Cursor c = cr.query(uri, EmailContent.ID_PROJECTION, null, null, null);
1985d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        assertNotNull(c);
1986d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        assertEquals(0, c.getCount());
1987d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank        c.close();
1988d306ba34387f3a7e77a4b8d98c6ac45cc14b95adMarc Blank    }
198922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
199022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    /**
199122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy     * Verify {@link EmailProvider#recalculateMessageCount(android.database.sqlite.SQLiteDatabase)}
199222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy     */
199322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    public void testRecalculateMessageCounts() {
199422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        final Context c = mMockContext;
199522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
199622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Create accounts
199722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Account a1 = ProviderTestUtils.setupAccount("holdflag-1", true, c);
199822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Account a2 = ProviderTestUtils.setupAccount("holdflag-2", true, c);
199922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
200022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Create mailboxes for each account
200122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a1.mId, true, c, Mailbox.TYPE_INBOX);
200222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b2 = ProviderTestUtils.setupMailbox("box2", a1.mId, true, c, Mailbox.TYPE_OUTBOX);
200322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b3 = ProviderTestUtils.setupMailbox("box3", a2.mId, true, c, Mailbox.TYPE_INBOX);
200422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b4 = ProviderTestUtils.setupMailbox("box4", a2.mId, true, c, Mailbox.TYPE_OUTBOX);
200522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox bt = ProviderTestUtils.setupMailbox("boxT", a2.mId, true, c, Mailbox.TYPE_TRASH);
200622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
200722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Create some messages
200822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // b1 (account 1, inbox): 1 message, including 1 starred
200922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message m11 = createMessage(c, b1, true, false, Message.FLAG_LOADED_COMPLETE);
201022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
201122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // b2 (account 1, outbox): 2 message, including 1 starred
201222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message m21 = createMessage(c, b2, false, false, Message.FLAG_LOADED_COMPLETE);
201322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message m22 = createMessage(c, b2, true, true, Message.FLAG_LOADED_COMPLETE);
201422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
201522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // b3 (account 2, inbox): 3 message, including 1 starred
201622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message m31 = createMessage(c, b3, false, false, Message.FLAG_LOADED_COMPLETE);
201722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message m32 = createMessage(c, b3, false, false, Message.FLAG_LOADED_COMPLETE);
201822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message m33 = createMessage(c, b3, true, true, Message.FLAG_LOADED_COMPLETE);
201922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
202022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // b4 (account 2, outbox) has no messages.
202122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
202222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // bt (account 2, trash) has 3 messages, including 2 starred
202322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message mt1 = createMessage(c, bt, true, false, Message.FLAG_LOADED_COMPLETE);
202422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message mt2 = createMessage(c, bt, true, false, Message.FLAG_LOADED_COMPLETE);
202522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Message mt3 = createMessage(c, bt, false, false, Message.FLAG_LOADED_COMPLETE);
202622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
202722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Verifiy initial message counts
202822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(1, getMessageCount(b1.mId));
202922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(2, getMessageCount(b2.mId));
203022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(3, getMessageCount(b3.mId));
203122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(0, getMessageCount(b4.mId));
203222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(3, getMessageCount(bt.mId));
203322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
203422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Whew. The setup is done; now let's actually get to the test
203522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
203622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // First, invalidate the message counts.
203722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        setMinusOneToMessageCounts();
203822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(-1, getMessageCount(b1.mId));
203922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(-1, getMessageCount(b2.mId));
204022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(-1, getMessageCount(b3.mId));
204122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(-1, getMessageCount(b4.mId));
204222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(-1, getMessageCount(bt.mId));
204322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
204422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Batch update.
204522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        SQLiteDatabase db = getProvider().getDatabase(mMockContext);
204622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        EmailProvider.recalculateMessageCount(db);
204722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
204822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Check message counts are valid again
204922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(1, getMessageCount(b1.mId));
205022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(2, getMessageCount(b2.mId));
205122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(3, getMessageCount(b3.mId));
205222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(0, getMessageCount(b4.mId));
205322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(3, getMessageCount(bt.mId));
205422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    }
205522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
205622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    /** Creates an account */
205722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    private Account createAccount(Context c, String name, HostAuth recvAuth, HostAuth sendAuth) {
205822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Account account = ProviderTestUtils.setupAccount(name, false, c);
205922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        if (recvAuth != null) {
206022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy            account.mHostAuthKeyRecv = recvAuth.mId;
206122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy            if (sendAuth == null) {
206222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                account.mHostAuthKeySend = recvAuth.mId;
206322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy            }
206422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        }
206522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        if (sendAuth != null) {
206622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy            account.mHostAuthKeySend = sendAuth.mId;
206722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        }
206822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        account.save(c);
206922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        return account;
207022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    }
207122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
207222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    /** Creates a mailbox; redefine as we need version 17 mailbox values */
207322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    private Mailbox createMailbox(Context c, String displayName, String serverId, long parentKey,
207422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy            long accountId) {
207522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox box = new Mailbox();
207622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
207722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mDisplayName = displayName;
207822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mServerId = serverId;
207922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mParentKey = parentKey;
208022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mAccountKey = accountId;
208122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Don't care about the fields below ... set them for giggles
208222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mType = Mailbox.TYPE_MAIL;
208322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mDelimiter = '/';
208422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mSyncKey = "sync-key";
208522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mSyncLookback = 2;
2086f5418f1f93b02e7fab9f15eb201800b65510998eMarc Blank        box.mSyncInterval = Account.CHECK_INTERVAL_NEVER;
208722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mSyncTime = 3;
208822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mFlagVisible = true;
208922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mFlags = 5;
209022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.mVisibleLimit = 6;
209122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        box.save(c);
209222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        return box;
209322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    }
209422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
209522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    /**
209622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy     * Asserts equality between two mailboxes. We define this as we don't have implementations
209722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy     * for Mailbox#equals().
209822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy     */
209922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    private void assertEquals(Mailbox expected, Mailbox actual) {
210022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        if (expected == null && actual == null) return;
210122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertTrue(expected != null && actual != null);
210222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEqualsExceptServerId(expected, actual, expected.mServerId);
210322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    }
210422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
210522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    /**
210622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy     * Asserts equality between the two mailboxes EXCEPT for the server id. The given server
210722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy     * ID is the expected value.
210822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy     */
210922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    private void assertEqualsExceptServerId(Mailbox expected, Mailbox actual, String serverId) {
211022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        if (expected == null && actual == null) return;
211122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
211222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertTrue(expected != null && actual != null);
211322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(expected.mDisplayName, actual.mDisplayName);
211422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(serverId, actual.mServerId);
211522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(expected.mParentKey, actual.mParentKey);
211622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(expected.mAccountKey, actual.mAccountKey);
211722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    }
211822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
211922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    /** Verifies updating the DB from v17 to v18 works as expected */
212022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    public void testUpgradeFromVersion17ToVersion18() {
212122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        final Context c = mMockContext;
212222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Create accounts
212322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Account a1 =createAccount(c, "exchange",
212422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                ProviderTestUtils.setupHostAuth("eas", "exchange.host.com", true, c),
212522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                null);
212622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Account a2 = createAccount(c, "imap",
212722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                ProviderTestUtils.setupHostAuth("imap", "imap.host.com", true, c),
212822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                ProviderTestUtils.setupHostAuth("smtp", "smtp.host.com", true, c));
212922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Account a3 = createAccount(c, "pop3",
213022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                ProviderTestUtils.setupHostAuth("pop3", "imap.host.com", true, c),
213122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy                ProviderTestUtils.setupHostAuth("smtp", "smtp.host.com", true, c));
213222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
213322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Create mailboxes; some w/ valid parent IDs, others without
213422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b11 = createMailbox(c, "box1", "12", 0L, a1.mId);
213522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b12 = createMailbox(c, "box2", "67", -1L, a1.mId);
213622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b13 = createMailbox(c, "box3", "18", b12.mId, a1.mId);
213722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
213822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b21 = createMailbox(c, "box4", null, 0L, a2.mId);
213922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b22 = createMailbox(c, "box4/foo/bar", "will-be-replaced", 0L, a2.mId);
214022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b23 = createMailbox(c, "box5", null, -1L, a2.mId);
214122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b24 = createMailbox(c, "box6", "box5/box6", b23.mId, a2.mId);
214222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
214322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b31 = createMailbox(c, "box7", "12", 0L, a3.mId);
214422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b32 = createMailbox(c, "box8/foo/bar", "will-be-replaced", 0L, a3.mId);
214522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b33 = createMailbox(c, "box9", "box9", -1L, a3.mId);
214622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox b34 = createMailbox(c, "boxA", "box9/boxA", b33.mId, a3.mId);
214722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
214822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Sanity check the mailboxes that were just added
214922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        Mailbox testMailbox;
215022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b11.mId);
215122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b11, testMailbox);
215222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b12.mId);
215322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b12, testMailbox);
215422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b13.mId);
215522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b13, testMailbox);
215622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b21.mId);
215722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEqualsExceptServerId(b21, testMailbox, null);
215822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b22.mId);
215922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEqualsExceptServerId(b22, testMailbox, "will-be-replaced");
216022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b23.mId);
216122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b23, testMailbox);
216222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b24.mId);
216322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b24, testMailbox);
216422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b31.mId);
216522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEqualsExceptServerId(b31, testMailbox, "12");
216622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b32.mId);
216722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEqualsExceptServerId(b32, testMailbox, "will-be-replaced");
216822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b33.mId);
216922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b33, testMailbox);
217022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b34.mId);
217122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b34, testMailbox);
217222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
217322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        SQLiteDatabase db = getProvider().getDatabase(mMockContext);
217422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        EmailProvider.upgradeFromVersion17ToVersion18(db);
217522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
217622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Verify that only IMAP/POP3 mailboxes w/ a parent key of '0' are changed
217722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // Exchange mailboxes; none should be changed
217822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b11.mId);
217922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b11, testMailbox);
218022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b12.mId);
218122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b12, testMailbox);
218222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b13.mId);
218322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b13, testMailbox);
218422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
218522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // IMAP mailboxes; only mailboxes w/ a parent id of '0' are changed
218622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b21.mId);
218722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEqualsExceptServerId(b21, testMailbox, "box4");
218822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b22.mId);
218922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEqualsExceptServerId(b22, testMailbox, "box4/foo/bar");
219022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b23.mId);
219122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b23, testMailbox);
219222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b24.mId);
219322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b24, testMailbox);
219422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy
219522208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        // POP3 mailboxes; only mailboxes w/ a parent id of '0' are changed
219622208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b31.mId);
219722208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEqualsExceptServerId(b31, testMailbox, "box7");
219822208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b32.mId);
219922208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEqualsExceptServerId(b32, testMailbox, "box8/foo/bar");
220022208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b33.mId);
220122208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b33, testMailbox);
220222208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        testMailbox = Mailbox.restoreMailboxWithId(c, b34.mId);
220322208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy        assertEquals(b34, testMailbox);
220422208771b7b39c5d131372ba6bc45ab23cc22232Todd Kennedy    }
22054c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki
22064c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki    public void testBuildMessageListSelection() {
22074c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        final Context c = mMockContext;
22084c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki
22094c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        assertEquals(Message.ALL_INBOX_SELECTION, Message.buildMessageListSelection(c,
22104c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki                     Mailbox.QUERY_ALL_INBOXES));
22114c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki
22124c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        assertEquals(Message.ALL_DRAFT_SELECTION, Message.buildMessageListSelection(c,
22134c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki                Mailbox.QUERY_ALL_DRAFTS));
22144c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki
22154c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        assertEquals(Message.ALL_OUTBOX_SELECTION, Message.buildMessageListSelection(c,
22164c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki                Mailbox.QUERY_ALL_OUTBOX));
22174c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki
22184c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        assertEquals(Message.ALL_UNREAD_SELECTION, Message.buildMessageListSelection(c,
22194c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki                Mailbox.QUERY_ALL_UNREAD));
22204c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki
22214c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        assertEquals(Message.ALL_FAVORITE_SELECTION, Message.buildMessageListSelection(c,
22224c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki                Mailbox.QUERY_ALL_FAVORITES));
22234c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki
22244c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        final Account account = ProviderTestUtils.setupAccount("1", true, mMockContext);
22254c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        final Mailbox in = ProviderTestUtils.setupMailbox("i", account.mId, true, c,
22264c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki                Mailbox.TYPE_INBOX);
22274c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        final Mailbox out = ProviderTestUtils.setupMailbox("o", account.mId, true, c,
22284c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki                Mailbox.TYPE_OUTBOX);
22294c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki
22304c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        assertEquals(Message.MAILBOX_KEY + "=" + in.mId + " AND " + Message.FLAG_LOADED_SELECTION,
22314c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki                Message.buildMessageListSelection(c, in.mId));
22324c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki
22334c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        // No LOADED check for outboxes.
22344c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki        assertEquals(Message.MAILBOX_KEY + "=" + out.mId,
22354c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki                Message.buildMessageListSelection(c, out.mId));
22364c4e4c3515c3e3300e03f90e02a0c520dc2dff32Makoto Onuki    }
2237f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank
2238f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    /**
2239f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * Determine whether a list of AccountManager accounts includes a given EmailProvider account
2240f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * @param amAccountList a list of AccountManager accounts
2241f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * @param account an EmailProvider account
2242f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * @param context the caller's context (our test provider's context)
2243f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * @return whether or not the EmailProvider account is represented in AccountManager
2244f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     */
2245f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    private boolean amAccountListHasAccount(android.accounts.Account[] amAccountList,
2246f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            Account account, Context context) {
2247f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        HostAuth hostAuth = HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv);
2248f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        if (hostAuth == null) return false;
2249f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        String login = hostAuth.mLogin;
2250f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        for (android.accounts.Account amAccount: amAccountList) {
2251f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            if (amAccount.name.equals(login)) {
2252f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank                return true;
2253f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            }
2254f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        }
2255f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        return false;
2256f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    }
2257f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank
2258f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    /**
2259f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * Remove a single pop/imap account from the AccountManager
2260f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * @param accountManager our AccountManager
2261f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * @param name the name of the test account to remove
2262f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     */
2263f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    private void removeAccountManagerAccount(AccountManager accountManager, String name) {
2264f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        try {
2265f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            accountManager.removeAccount(
2266f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank                    new android.accounts.Account(name, AccountManagerTypes.TYPE_POP_IMAP),
2267f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank                    null, null).getResult();
2268f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        } catch (OperationCanceledException e) {
2269f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        } catch (AuthenticatorException e) {
2270f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        } catch (IOException e) {
2271f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        }
2272f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    }
2273f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank
2274f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    /**
2275f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * Remove all test accounts from the AccountManager
2276f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     * @param accountManager the AccountManager
2277f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank     */
2278f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    private void cleanupTestAccountManagerAccounts(AccountManager accountManager) {
2279f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        android.accounts.Account[] amAccountList =
2280f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            accountManager.getAccountsByType(AccountManagerTypes.TYPE_POP_IMAP);
2281f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        for (android.accounts.Account account: amAccountList) {
2282f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            if (account.name.startsWith(AccountReconciler.ACCOUNT_MANAGER_ACCOUNT_TEST_PREFIX)) {
2283f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank                removeAccountManagerAccount(accountManager, account.name);
2284f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            }
2285f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        }
2286f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    }
2287f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank
2288f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    /** Verifies updating the DB from v21 to v22 works as expected */
2289f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    public void testUpgradeFromVersion21ToVersion22() {
2290f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        String imapTestLogin =
2291f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            AccountReconciler.ACCOUNT_MANAGER_ACCOUNT_TEST_PREFIX + "imap.host.com";
2292f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        String pop3TestLogin =
2293f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            AccountReconciler.ACCOUNT_MANAGER_ACCOUNT_TEST_PREFIX + "pop3.host.com";
2294f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        AccountManager accountManager = AccountManager.get(mContext);
2295f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank
2296f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        // Create provider accounts (one of each type)
2297f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        Account a1 = createAccount(mMockContext, "exchange",
2298f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank                ProviderTestUtils.setupHostAuth("eas", "exchange.host.com", true, mMockContext),
2299f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank                null);
2300f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        HostAuth h2 =
2301f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            ProviderTestUtils.setupHostAuth("imap", "imap.host.com", false, mMockContext);
2302f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        h2.mLogin = imapTestLogin;
2303f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        h2.save(mMockContext);
2304f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        Account a2 = createAccount(mMockContext, "imap", h2,
2305f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank                ProviderTestUtils.setupHostAuth("smtp", "smtp.host.com", true, mMockContext));
2306f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        HostAuth h3 =
2307f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            ProviderTestUtils.setupHostAuth("pop3", "pop3.host.com", false, mMockContext);
2308f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        h3.mLogin = pop3TestLogin;
2309f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        h3.save(mMockContext);
2310f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        Account a3 = createAccount(mMockContext, "pop3", h3,
2311f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank                ProviderTestUtils.setupHostAuth("smtp", "smtp.host.com", true, mMockContext));
2312f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank
2313f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        // Get the current list of AccountManager accounts (we have to use the real context here),
2314f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        // whereas we use the mock context for EmailProvider (this is because the mock context
2315f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        // doesn't implement AccountManager hooks)
2316f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        android.accounts.Account[] amAccountList =
2317f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            accountManager.getAccountsByType(AccountManagerTypes.TYPE_POP_IMAP);
2318f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        // There shouldn't be AccountManager accounts for these
2319f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        assertFalse(amAccountListHasAccount(amAccountList, a1, mMockContext));
2320f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        assertFalse(amAccountListHasAccount(amAccountList, a2, mMockContext));
2321f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        assertFalse(amAccountListHasAccount(amAccountList, a3, mMockContext));
2322f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank
2323f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        amAccountList = null;
2324f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        try {
2325f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            // Upgrade the database
2326f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            SQLiteDatabase db = getProvider().getDatabase(mMockContext);
2327f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            EmailProvider.upgradeFromVersion21ToVersion22(db, getContext());
2328f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank
2329f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            // The pop3 and imap account should now be in account manager
2330f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            amAccountList = accountManager.getAccountsByType(AccountManagerTypes.TYPE_POP_IMAP);
2331f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            assertFalse(amAccountListHasAccount(amAccountList, a1, mMockContext));
2332f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            assertTrue(amAccountListHasAccount(amAccountList, a2, mMockContext));
2333f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            assertTrue(amAccountListHasAccount(amAccountList, a3, mMockContext));
2334f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        } finally {
2335f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank            cleanupTestAccountManagerAccounts(accountManager);
2336f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank        }
2337f3ff0ba91076ef1fb087fc30fe65d9504011c2b3Marc Blank    }
23387143d969dde180b7a44bdc42ade6ca2878d0760dAndrew Stadler}
2339