michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.fxa.authenticator; michael@0: michael@0: import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; michael@0: import org.mozilla.gecko.background.sync.TestSyncAccounts; michael@0: import org.mozilla.gecko.fxa.FxAccountConstants; michael@0: import org.mozilla.gecko.fxa.authenticator.AccountPickler; michael@0: import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; michael@0: import org.mozilla.gecko.fxa.login.Separated; michael@0: import org.mozilla.gecko.fxa.login.State; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: michael@0: import android.accounts.Account; michael@0: import android.accounts.AccountManager; michael@0: import android.test.InstrumentationTestCase; michael@0: import android.test.RenamingDelegatingContext; michael@0: michael@0: public class TestAccountPickler extends AndroidSyncTestCase { michael@0: private final static String FILENAME_PREFIX = "TestAccountPickler-"; michael@0: private final static String PICKLE_FILENAME = "pickle"; michael@0: michael@0: public Account account; michael@0: public RenamingDelegatingContext context; michael@0: public AccountManager accountManager; michael@0: michael@0: @Override michael@0: public void setUp() { michael@0: this.account = null; michael@0: // Randomize the filename prefix in case we don't clean up correctly. michael@0: this.context = new RenamingDelegatingContext(getApplicationContext(), FILENAME_PREFIX + michael@0: Math.random() * 1000001 + "-"); michael@0: this.accountManager = AccountManager.get(context); michael@0: } michael@0: michael@0: @Override michael@0: public void tearDown() { michael@0: if (this.account != null) { michael@0: deleteAccount(this, this.accountManager, this.account); michael@0: this.account = null; michael@0: } michael@0: this.context.deleteFile(PICKLE_FILENAME); michael@0: } michael@0: michael@0: public static void deleteAccount(final InstrumentationTestCase test, michael@0: final AccountManager accountManager, final Account account) { michael@0: TestSyncAccounts.deleteAccount(test, accountManager, account); michael@0: } michael@0: michael@0: private boolean accountsExist() { michael@0: // Note that we don't use FirefoxAccounts.firefoxAccountsExist because it unpickles. michael@0: return AccountManager.get(context).getAccountsByType(FxAccountConstants.ACCOUNT_TYPE).length > 0; michael@0: } michael@0: michael@0: public AndroidFxAccount addDummyAccount() throws Exception { michael@0: final String email = "iu@fakedomain.io"; michael@0: final State state = new Separated(email, "uid", false); // State choice is arbitrary. michael@0: final AndroidFxAccount account = AndroidFxAccount.addAndroidAccount(context, email, michael@0: "profile", "serverURI", "tokenServerURI", state); michael@0: assertNotNull(account); michael@0: assertTrue(accountsExist()); // Sanity check. michael@0: this.account = account.getAndroidAccount(); // To remove in tearDown() if we throw. michael@0: return account; michael@0: } michael@0: michael@0: public void testPickleAndUnpickle() throws Exception { michael@0: final AndroidFxAccount inputAccount = addDummyAccount(); michael@0: // Sync is enabled by default so we do a more thorough test by disabling it. michael@0: inputAccount.disableSyncing(); michael@0: michael@0: AccountPickler.pickle(inputAccount, PICKLE_FILENAME); michael@0: michael@0: // unpickle adds an account to the AccountManager so delete it first. michael@0: deleteAccount(this, this.accountManager, inputAccount.getAndroidAccount()); michael@0: assertFalse(accountsExist()); michael@0: michael@0: final AndroidFxAccount unpickledAccount = michael@0: AccountPickler.unpickle(context, PICKLE_FILENAME); michael@0: assertNotNull(unpickledAccount); michael@0: this.account = unpickledAccount.getAndroidAccount(); // To remove in tearDown(). michael@0: assertAccountsEquals(inputAccount, unpickledAccount); michael@0: } michael@0: michael@0: public void testDeletePickle() throws Exception { michael@0: final AndroidFxAccount account = addDummyAccount(); michael@0: AccountPickler.pickle(account, PICKLE_FILENAME); michael@0: michael@0: final String s = Utils.readFile(context, PICKLE_FILENAME); michael@0: assertNotNull(s); michael@0: assertTrue(s.length() > 0); michael@0: michael@0: AccountPickler.deletePickle(context, PICKLE_FILENAME); michael@0: org.mozilla.gecko.background.sync.TestAccountPickler.assertFileNotPresent( michael@0: context, PICKLE_FILENAME); michael@0: } michael@0: michael@0: private void assertAccountsEquals(final AndroidFxAccount expected, michael@0: final AndroidFxAccount actual) throws Exception { michael@0: // TODO: Write and use AndroidFxAccount.equals michael@0: // TODO: protected. michael@0: //assertEquals(expected.getAccountVersion(), actual.getAccountVersion()); michael@0: assertEquals(expected.getProfile(), actual.getProfile()); michael@0: assertEquals(expected.getAccountServerURI(), actual.getAccountServerURI()); michael@0: assertEquals(expected.getAudience(), actual.getAudience()); michael@0: assertEquals(expected.getTokenServerURI(), actual.getTokenServerURI()); michael@0: assertEquals(expected.getSyncPrefsPath(), actual.getSyncPrefsPath()); michael@0: assertEquals(expected.isSyncing(), actual.isSyncing()); michael@0: assertEquals(expected.getEmail(), actual.getEmail()); michael@0: assertStateEquals(expected.getState(), actual.getState()); michael@0: } michael@0: michael@0: private void assertStateEquals(final State expected, final State actual) throws Exception { michael@0: // TODO: Write and use State.equals. Thus, this is only thorough for the State base class. michael@0: assertEquals(expected.getStateLabel(), actual.getStateLabel()); michael@0: assertEquals(expected.email, actual.email); michael@0: assertEquals(expected.uid, actual.uid); michael@0: assertEquals(expected.verified, actual.verified); michael@0: } michael@0: }