mobile/android/tests/background/junit3/src/fxa/authenticator/TestAccountPickler.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/tests/background/junit3/src/fxa/authenticator/TestAccountPickler.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +package org.mozilla.gecko.background.fxa.authenticator;
     1.8 +
     1.9 +import org.mozilla.gecko.background.helpers.AndroidSyncTestCase;
    1.10 +import org.mozilla.gecko.background.sync.TestSyncAccounts;
    1.11 +import org.mozilla.gecko.fxa.FxAccountConstants;
    1.12 +import org.mozilla.gecko.fxa.authenticator.AccountPickler;
    1.13 +import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
    1.14 +import org.mozilla.gecko.fxa.login.Separated;
    1.15 +import org.mozilla.gecko.fxa.login.State;
    1.16 +import org.mozilla.gecko.sync.Utils;
    1.17 +
    1.18 +import android.accounts.Account;
    1.19 +import android.accounts.AccountManager;
    1.20 +import android.test.InstrumentationTestCase;
    1.21 +import android.test.RenamingDelegatingContext;
    1.22 +
    1.23 +public class TestAccountPickler extends AndroidSyncTestCase {
    1.24 +  private final static String FILENAME_PREFIX = "TestAccountPickler-";
    1.25 +  private final static String PICKLE_FILENAME = "pickle";
    1.26 +
    1.27 +  public Account account;
    1.28 +  public RenamingDelegatingContext context;
    1.29 +  public AccountManager accountManager;
    1.30 +
    1.31 +  @Override
    1.32 +  public void setUp() {
    1.33 +    this.account = null;
    1.34 +    // Randomize the filename prefix in case we don't clean up correctly.
    1.35 +    this.context = new RenamingDelegatingContext(getApplicationContext(), FILENAME_PREFIX +
    1.36 +        Math.random() * 1000001 + "-");
    1.37 +    this.accountManager = AccountManager.get(context);
    1.38 +  }
    1.39 +
    1.40 +  @Override
    1.41 +  public void tearDown() {
    1.42 +    if (this.account != null) {
    1.43 +      deleteAccount(this, this.accountManager, this.account);
    1.44 +      this.account = null;
    1.45 +    }
    1.46 +    this.context.deleteFile(PICKLE_FILENAME);
    1.47 +  }
    1.48 +
    1.49 +  public static void deleteAccount(final InstrumentationTestCase test,
    1.50 +      final AccountManager accountManager, final Account account) {
    1.51 +    TestSyncAccounts.deleteAccount(test, accountManager, account);
    1.52 +  }
    1.53 +
    1.54 +  private boolean accountsExist() {
    1.55 +    // Note that we don't use FirefoxAccounts.firefoxAccountsExist because it unpickles.
    1.56 +    return AccountManager.get(context).getAccountsByType(FxAccountConstants.ACCOUNT_TYPE).length > 0;
    1.57 +  }
    1.58 +
    1.59 +  public AndroidFxAccount addDummyAccount() throws Exception {
    1.60 +    final String email = "iu@fakedomain.io";
    1.61 +    final State state = new Separated(email, "uid", false); // State choice is arbitrary.
    1.62 +    final AndroidFxAccount account = AndroidFxAccount.addAndroidAccount(context, email,
    1.63 +        "profile", "serverURI", "tokenServerURI", state);
    1.64 +    assertNotNull(account);
    1.65 +    assertTrue(accountsExist()); // Sanity check.
    1.66 +    this.account = account.getAndroidAccount(); // To remove in tearDown() if we throw.
    1.67 +    return account;
    1.68 +  }
    1.69 +
    1.70 +  public void testPickleAndUnpickle() throws Exception {
    1.71 +    final AndroidFxAccount inputAccount = addDummyAccount();
    1.72 +    // Sync is enabled by default so we do a more thorough test by disabling it.
    1.73 +    inputAccount.disableSyncing();
    1.74 +
    1.75 +    AccountPickler.pickle(inputAccount, PICKLE_FILENAME);
    1.76 +
    1.77 +    // unpickle adds an account to the AccountManager so delete it first.
    1.78 +    deleteAccount(this, this.accountManager, inputAccount.getAndroidAccount());
    1.79 +    assertFalse(accountsExist());
    1.80 +
    1.81 +    final AndroidFxAccount unpickledAccount =
    1.82 +        AccountPickler.unpickle(context, PICKLE_FILENAME);
    1.83 +    assertNotNull(unpickledAccount);
    1.84 +    this.account = unpickledAccount.getAndroidAccount(); // To remove in tearDown().
    1.85 +    assertAccountsEquals(inputAccount, unpickledAccount);
    1.86 +  }
    1.87 +
    1.88 +  public void testDeletePickle() throws Exception {
    1.89 +    final AndroidFxAccount account = addDummyAccount();
    1.90 +    AccountPickler.pickle(account, PICKLE_FILENAME);
    1.91 +
    1.92 +    final String s = Utils.readFile(context, PICKLE_FILENAME);
    1.93 +    assertNotNull(s);
    1.94 +    assertTrue(s.length() > 0);
    1.95 +
    1.96 +    AccountPickler.deletePickle(context, PICKLE_FILENAME);
    1.97 +    org.mozilla.gecko.background.sync.TestAccountPickler.assertFileNotPresent(
    1.98 +        context, PICKLE_FILENAME);
    1.99 +  }
   1.100 +
   1.101 +  private void assertAccountsEquals(final AndroidFxAccount expected,
   1.102 +      final AndroidFxAccount actual) throws Exception {
   1.103 +    // TODO: Write and use AndroidFxAccount.equals
   1.104 +    // TODO: protected.
   1.105 +    //assertEquals(expected.getAccountVersion(), actual.getAccountVersion());
   1.106 +    assertEquals(expected.getProfile(), actual.getProfile());
   1.107 +    assertEquals(expected.getAccountServerURI(), actual.getAccountServerURI());
   1.108 +    assertEquals(expected.getAudience(), actual.getAudience());
   1.109 +    assertEquals(expected.getTokenServerURI(), actual.getTokenServerURI());
   1.110 +    assertEquals(expected.getSyncPrefsPath(), actual.getSyncPrefsPath());
   1.111 +    assertEquals(expected.isSyncing(), actual.isSyncing());
   1.112 +    assertEquals(expected.getEmail(), actual.getEmail());
   1.113 +    assertStateEquals(expected.getState(), actual.getState());
   1.114 +  }
   1.115 +
   1.116 +  private void assertStateEquals(final State expected, final State actual) throws Exception {
   1.117 +    // TODO: Write and use State.equals. Thus, this is only thorough for the State base class.
   1.118 +    assertEquals(expected.getStateLabel(), actual.getStateLabel());
   1.119 +    assertEquals(expected.email, actual.email);
   1.120 +    assertEquals(expected.uid, actual.uid);
   1.121 +    assertEquals(expected.verified, actual.verified);
   1.122 +  }
   1.123 +}

mercurial