FileDocCategorySizeDatePackage
AccountUnitTests.javaAPI DocAndroid 1.5 API5437Wed May 06 22:42:46 BST 2009com.android.email

AccountUnitTests

public class AccountUnitTests extends android.test.AndroidTestCase
This is a series of unit tests for the Account class. Technically these are functional because they use the underlying preferences framework.

Fields Summary
private Preferences
mPreferences
private String
mUuid
private Account
mAccount
Constructors Summary
Methods Summary
private voidcreateTestAccount()
Create a dummy account with minimal fields

        mAccount = new Account(getContext());
        mAccount.save(mPreferences);
        
        mUuid = mAccount.getUuid();
    
protected voidsetUp()

        super.setUp();
        
        mPreferences = Preferences.getPreferences(getContext());
    
protected voidtearDown()
Delete any dummy accounts we set up for this test

        super.tearDown();
        
        if (mAccount != null && mPreferences != null) {
            mAccount.delete(mPreferences);
        }
    
public voidtestImapDeletePolicyUpdate()
Test the update path for old IMAP accounts that didn't have DELETE_POLICY_ON_DELETE properly preset.


        final String STORE_URI_IMAP = "imap://user:pass@imap-server.com";
        final String STORE_URI_POP3 = "pop3://user:pass@pop3-server.com";
        
        // Test 1:  try it with a POP3 account - no update should occur
        
        // create a dummy account
        createTestAccount();
        
        // set up a minimal POP3 account with default value
        SharedPreferences.Editor editor = mPreferences.mSharedPreferences.edit();
        editor.putString(mUuid + ".storeUri", Utility.base64Encode(STORE_URI_POP3));
        editor.putInt(mUuid + ".deletePolicy", Account.DELETE_POLICY_NEVER);
        editor.commit();
        
        // read it in and confirm that we get the default value
        mAccount.refresh(mPreferences);
        assertEquals(Account.DELETE_POLICY_NEVER, mAccount.getDeletePolicy());
        
        // flush it and confirm that we don't change the database
        mAccount.save(mPreferences);
        int storedPolicy = mPreferences.mSharedPreferences.getInt(mUuid + ".deletePolicy", -1);
        assertEquals(Account.DELETE_POLICY_NEVER, storedPolicy);

        // Test 2:  try it with an IMAP account - this time we should see an auto-update

        // create a dummy account
        mAccount.delete(mPreferences);
        createTestAccount();
        
        // tweak it to have the wrong settings - this is what IMAP accounts look like
        // with manual setup, in earlier versions
        editor = mPreferences.mSharedPreferences.edit();
        editor.putString(mUuid + ".storeUri", Utility.base64Encode(STORE_URI_IMAP));
        editor.putInt(mUuid + ".deletePolicy", Account.DELETE_POLICY_NEVER);
        editor.commit();
        
        // Now read it in and confirm that we get the properly updated value
        mAccount.refresh(mPreferences);
        assertEquals(Account.DELETE_POLICY_ON_DELETE, mAccount.getDeletePolicy());
        
        // Now flush it and confirm that we fixed the database
        mAccount.save(mPreferences);
        storedPolicy = mPreferences.mSharedPreferences.getInt(mUuid + ".deletePolicy", -1);
        assertEquals(Account.DELETE_POLICY_ON_DELETE, storedPolicy);
    
public voidtestTransportToSenderUpdate()
Test the update path from .transportUri to .senderUri

        
        final String TEST_VALUE = "This Is The Sender Uri";
        
        // Create a dummy account
        createTestAccount();
        
        // Tweak it to look like an old account (with ".transportUri")
        SharedPreferences.Editor editor = mPreferences.mSharedPreferences.edit();
        editor.remove(mUuid + ".senderUri");
        editor.putString(mUuid + ".transportUri", Utility.base64Encode(TEST_VALUE));
        editor.commit();
        
        // Read it, see if we get back the string as a sender string
        mAccount.refresh(mPreferences);
        assertEquals(TEST_VALUE, mAccount.getSenderUri());
        
        // Update it - this will automatically convert it to the newer name
        mAccount.save(mPreferences);
        
        // Confirm that the field was replaced with the new form
        String newString = mPreferences.mSharedPreferences.getString(mUuid + ".senderUri", null);
        assertEquals(TEST_VALUE, Utility.base64Decode(newString));
        String oldString = mPreferences.mSharedPreferences.getString(mUuid + ".transportUri", null);
        assertNull(oldString);