am 3980675f: Save/restore account preference when running tests

* commit '3980675f180cb88dcd2f3bb55cb9231d3bba2c66':
  Save/restore account preference when running tests
main
Sandeep Siddhartha 2014-09-25 19:06:05 +00:00 committed by Android Git Automerger
commit 347343a443
1 changed files with 10 additions and 5 deletions

View File

@ -33,18 +33,21 @@ public class AccountsChangedReceiverTests extends AndroidTestCase {
private static final String ACCOUNT_2 = "account2@example.com"; private static final String ACCOUNT_2 = "account2@example.com";
private SharedPreferences mPrefs; private SharedPreferences mPrefs;
private String mLastKnownAccount = null;
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
// Keep track of the current account so that we restore it when the test finishes.
mLastKnownAccount = mPrefs.getString(Settings.PREF_ACCOUNT_NAME, null);
} }
@Override @Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
super.tearDown(); super.tearDown();
// Remove all preferences before the next test. // Restore the account that was present before running the test.
mPrefs.edit().clear(); updateAccountName(mLastKnownAccount);
} }
public void testUnknownIntent() { public void testUnknownIntent() {
@ -95,9 +98,11 @@ public class AccountsChangedReceiverTests extends AndroidTestCase {
} }
private void updateAccountName(String accountName) { private void updateAccountName(String accountName) {
mPrefs.edit() if (accountName == null) {
.putString(Settings.PREF_ACCOUNT_NAME, accountName) mPrefs.edit().remove(Settings.PREF_ACCOUNT_NAME).apply();
.commit(); } else {
mPrefs.edit().putString(Settings.PREF_ACCOUNT_NAME, accountName).apply();
}
} }
private void assertAccountName(String expectedAccountName) { private void assertAccountName(String expectedAccountName) {