1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.email; 18 19 import android.content.SharedPreferences; 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.MediumTest; 22 23 /** 24 * This is a series of unit tests for the Account class. 25 * 26 * Technically these are functional because they use the underlying preferences framework. 27 */ 28 @MediumTest 29 public class AccountUnitTests extends AndroidTestCase { 30 31 private Preferences mPreferences; 32 33 private String mUuid; 34 private Account mAccount; 35 36 @Override 37 protected void setUp() throws Exception { 38 super.setUp(); 39 40 mPreferences = Preferences.getPreferences(getContext()); 41 } 42 43 /** 44 * Delete any dummy accounts we set up for this test 45 */ 46 @Override 47 protected void tearDown() throws Exception { 48 super.tearDown(); 49 50 if (mAccount != null && mPreferences != null) { 51 mAccount.delete(mPreferences); 52 } 53 } 54 55 /** 56 * Test the update path from .transportUri to .senderUri 57 */ 58 public void testTransportToSenderUpdate() { 59 60 final String TEST_VALUE = "This Is The Sender Uri"; 61 62 // Create a dummy account 63 createTestAccount(); 64 65 // Tweak it to look like an old account (with ".transportUri") 66 SharedPreferences.Editor editor = mPreferences.mSharedPreferences.edit(); 67 editor.remove(mUuid + ".senderUri"); 68 editor.putString(mUuid + ".transportUri", Utility.base64Encode(TEST_VALUE)); 69 editor.commit(); 70 71 // Read it, see if we get back the string as a sender string 72 mAccount.refresh(mPreferences); 73 assertEquals(TEST_VALUE, mAccount.getSenderUri()); 74 75 // Update it - this will automatically convert it to the newer name 76 mAccount.save(mPreferences); 77 78 // Confirm that the field was replaced with the new form 79 String newString = mPreferences.mSharedPreferences.getString(mUuid + ".senderUri", null); 80 assertEquals(TEST_VALUE, Utility.base64Decode(newString)); 81 String oldString = mPreferences.mSharedPreferences.getString(mUuid + ".transportUri", null); 82 assertNull(oldString); 83 84 } 85 86 /** 87 * Test the update path for old IMAP accounts that didn't have DELETE_POLICY_ON_DELETE 88 * properly preset. 89 */ 90 public void testImapDeletePolicyUpdate() { 91 92 final String STORE_URI_IMAP = "imap://user:pass (at) imap-server.com"; 93 final String STORE_URI_POP3 = "pop3://user:pass (at) pop3-server.com"; 94 95 // Test 1: try it with a POP3 account - no update should occur 96 97 // create a dummy account 98 createTestAccount(); 99 100 // set up a minimal POP3 account with default value 101 SharedPreferences.Editor editor = mPreferences.mSharedPreferences.edit(); 102 editor.putString(mUuid + ".storeUri", Utility.base64Encode(STORE_URI_POP3)); 103 editor.putInt(mUuid + ".deletePolicy", Account.DELETE_POLICY_NEVER); 104 editor.commit(); 105 106 // read it in and confirm that we get the default value 107 mAccount.refresh(mPreferences); 108 assertEquals(Account.DELETE_POLICY_NEVER, mAccount.getDeletePolicy()); 109 110 // flush it and confirm that we don't change the database 111 mAccount.save(mPreferences); 112 int storedPolicy = mPreferences.mSharedPreferences.getInt(mUuid + ".deletePolicy", -1); 113 assertEquals(Account.DELETE_POLICY_NEVER, storedPolicy); 114 115 // Test 2: try it with an IMAP account - this time we should see an auto-update 116 117 // create a dummy account 118 mAccount.delete(mPreferences); 119 createTestAccount(); 120 121 // tweak it to have the wrong settings - this is what IMAP accounts look like 122 // with manual setup, in earlier versions 123 editor = mPreferences.mSharedPreferences.edit(); 124 editor.putString(mUuid + ".storeUri", Utility.base64Encode(STORE_URI_IMAP)); 125 editor.putInt(mUuid + ".deletePolicy", Account.DELETE_POLICY_NEVER); 126 editor.commit(); 127 128 // Now read it in and confirm that we get the properly updated value 129 mAccount.refresh(mPreferences); 130 assertEquals(Account.DELETE_POLICY_ON_DELETE, mAccount.getDeletePolicy()); 131 132 // Now flush it and confirm that we fixed the database 133 mAccount.save(mPreferences); 134 storedPolicy = mPreferences.mSharedPreferences.getInt(mUuid + ".deletePolicy", -1); 135 assertEquals(Account.DELETE_POLICY_ON_DELETE, storedPolicy); 136 } 137 138 /** 139 * Test new flags field (added only for backups - not used by real/legacy accounts) 140 */ 141 public void testFlagsField() { 142 createTestAccount(); 143 assertEquals(0, mAccount.mBackupFlags); 144 mAccount.save(mPreferences); 145 mAccount.mBackupFlags = -1; 146 mAccount.refresh(mPreferences); 147 assertEquals(0, mAccount.mBackupFlags); 148 149 mAccount.mBackupFlags = Account.BACKUP_FLAGS_IS_BACKUP; 150 mAccount.save(mPreferences); 151 mAccount.mBackupFlags = -1; 152 mAccount.refresh(mPreferences); 153 assertEquals(Account.BACKUP_FLAGS_IS_BACKUP, mAccount.mBackupFlags); 154 155 mAccount.mBackupFlags = Account.BACKUP_FLAGS_SYNC_CONTACTS; 156 mAccount.save(mPreferences); 157 mAccount.mBackupFlags = -1; 158 mAccount.refresh(mPreferences); 159 assertEquals(Account.BACKUP_FLAGS_SYNC_CONTACTS, mAccount.mBackupFlags); 160 161 mAccount.mBackupFlags = Account.BACKUP_FLAGS_IS_DEFAULT; 162 mAccount.save(mPreferences); 163 mAccount.mBackupFlags = -1; 164 mAccount.refresh(mPreferences); 165 assertEquals(Account.BACKUP_FLAGS_IS_DEFAULT, mAccount.mBackupFlags); 166 } 167 168 /** 169 * Create a dummy account with minimal fields 170 */ 171 private void createTestAccount() { 172 mAccount = new Account(getContext()); 173 mAccount.save(mPreferences); 174 175 mUuid = mAccount.getUuid(); 176 } 177 178 } 179