replicant-packages_apps_Email/tests/src/com/android/email/AccountUnitTests.java
Andy Stadler cd7e5664f9 AI 146061: Add persistent storage that Store classes can access.
The current design for Store classes (e.g. IMAP) did not provide for
  any persistent storage.  This is the beginning of a mechanism to
  provide that.  It's quite simplisitic - each Store can read/write one
  persistent string - but that's enough for the first simple use case
  (saving some sync data for EAS).
  The core changes here - suggest reviewing first - are in Account.java,
  Store.java, and AccountUnitTests.java.  Everything else is just
  following the API change that was necessary.
  Note that, by definition, this only applies to remote stores (e.g.
  IMAP, POP3).  You'll see everywhere that LocalStore is passed null, and
  this is correct - LocalStore *is* persistent storage and does not need
  access (so far, at least).
  BUG=1786939

Automated import of CL 146061
2009-04-13 20:07:56 -07:00

204 lines
7.5 KiB
Java

/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.email;
import com.android.email.mail.Store;
import android.content.SharedPreferences;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
/**
* This is a series of unit tests for the Account class.
*
* Technically these are functional because they use the underlying preferences framework.
*/
@SmallTest
public class AccountUnitTests extends AndroidTestCase {
private Preferences mPreferences;
private String mUuid;
private Account mAccount;
@Override
protected void setUp() throws Exception {
super.setUp();
mPreferences = Preferences.getPreferences(getContext());
}
/**
* Delete any dummy accounts we set up for this test
*/
@Override
protected void tearDown() throws Exception {
super.tearDown();
if (mAccount != null && mPreferences != null) {
mAccount.delete(mPreferences);
}
}
/**
* Test the update path from .transportUri to .senderUri
*/
public void testTransportToSenderUpdate() {
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);
}
/**
* Test the update path for old IMAP accounts that didn't have DELETE_POLICY_ON_DELETE
* properly preset.
*/
public void testImapDeletePolicyUpdate() {
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);
}
/**
* Test the new store persistent data code.
*
* This test, and the underlying code, reflect the essential error in the Account class. The
* account objects should have been singletons-per-account. As it stands there are lots of
* them floating around, which is very expensive (we waste a lot of effort creating them)
* and forces slow sync hacks for dynamic data like the store's persistent data.
*/
public void testStorePersistentData() {
final String TEST_STRING = "This is the store's persistent data.";
final String TEST_STRING_2 = "Rewrite the store data.";
// create a dummy account
createTestAccount();
// confirm null on new account
assertNull(mAccount.getPersistentString());
// test write/readback
mAccount.setPersistentString(TEST_STRING);
mAccount.save(mPreferences);
mAccount.refresh(mPreferences);
assertEquals(TEST_STRING, mAccount.getPersistentString());
// test that the data is shared across multiple account instantiations
Account account2 = new Account(mPreferences, mUuid);
assertEquals(TEST_STRING, account2.getPersistentString());
mAccount.setPersistentString(TEST_STRING_2);
assertEquals(TEST_STRING_2, account2.getPersistentString());
}
/**
* Test the callbacks for setting & getting persistent data
*/
public void testStorePersistentCallbacks() {
final String TEST_STRING = "This is the store's persistent data.";
final String TEST_STRING_2 = "Rewrite the store data.";
// create a dummy account
createTestAccount();
Store.PersistentDataCallbacks callbacks = mAccount.getStoreCallbacks();
// push some data through the interfaces
assertNull(callbacks.getPersistentString());
callbacks.setPersistentString(TEST_STRING);
assertEquals(TEST_STRING, mAccount.getPersistentString());
mAccount.setPersistentString(TEST_STRING_2);
assertEquals(TEST_STRING_2, callbacks.getPersistentString());
}
/**
* Create a dummy account with minimal fields
*/
private void createTestAccount() {
mAccount = new Account(getContext());
mAccount.save(mPreferences);
mUuid = mAccount.getUuid();
}
}