Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 package org.mozilla.gecko.background.fxa.authenticator;
6 import org.mozilla.gecko.background.helpers.AndroidSyncTestCase;
7 import org.mozilla.gecko.background.sync.TestSyncAccounts;
8 import org.mozilla.gecko.fxa.FxAccountConstants;
9 import org.mozilla.gecko.fxa.authenticator.AccountPickler;
10 import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
11 import org.mozilla.gecko.fxa.login.Separated;
12 import org.mozilla.gecko.fxa.login.State;
13 import org.mozilla.gecko.sync.Utils;
15 import android.accounts.Account;
16 import android.accounts.AccountManager;
17 import android.test.InstrumentationTestCase;
18 import android.test.RenamingDelegatingContext;
20 public class TestAccountPickler extends AndroidSyncTestCase {
21 private final static String FILENAME_PREFIX = "TestAccountPickler-";
22 private final static String PICKLE_FILENAME = "pickle";
24 public Account account;
25 public RenamingDelegatingContext context;
26 public AccountManager accountManager;
28 @Override
29 public void setUp() {
30 this.account = null;
31 // Randomize the filename prefix in case we don't clean up correctly.
32 this.context = new RenamingDelegatingContext(getApplicationContext(), FILENAME_PREFIX +
33 Math.random() * 1000001 + "-");
34 this.accountManager = AccountManager.get(context);
35 }
37 @Override
38 public void tearDown() {
39 if (this.account != null) {
40 deleteAccount(this, this.accountManager, this.account);
41 this.account = null;
42 }
43 this.context.deleteFile(PICKLE_FILENAME);
44 }
46 public static void deleteAccount(final InstrumentationTestCase test,
47 final AccountManager accountManager, final Account account) {
48 TestSyncAccounts.deleteAccount(test, accountManager, account);
49 }
51 private boolean accountsExist() {
52 // Note that we don't use FirefoxAccounts.firefoxAccountsExist because it unpickles.
53 return AccountManager.get(context).getAccountsByType(FxAccountConstants.ACCOUNT_TYPE).length > 0;
54 }
56 public AndroidFxAccount addDummyAccount() throws Exception {
57 final String email = "iu@fakedomain.io";
58 final State state = new Separated(email, "uid", false); // State choice is arbitrary.
59 final AndroidFxAccount account = AndroidFxAccount.addAndroidAccount(context, email,
60 "profile", "serverURI", "tokenServerURI", state);
61 assertNotNull(account);
62 assertTrue(accountsExist()); // Sanity check.
63 this.account = account.getAndroidAccount(); // To remove in tearDown() if we throw.
64 return account;
65 }
67 public void testPickleAndUnpickle() throws Exception {
68 final AndroidFxAccount inputAccount = addDummyAccount();
69 // Sync is enabled by default so we do a more thorough test by disabling it.
70 inputAccount.disableSyncing();
72 AccountPickler.pickle(inputAccount, PICKLE_FILENAME);
74 // unpickle adds an account to the AccountManager so delete it first.
75 deleteAccount(this, this.accountManager, inputAccount.getAndroidAccount());
76 assertFalse(accountsExist());
78 final AndroidFxAccount unpickledAccount =
79 AccountPickler.unpickle(context, PICKLE_FILENAME);
80 assertNotNull(unpickledAccount);
81 this.account = unpickledAccount.getAndroidAccount(); // To remove in tearDown().
82 assertAccountsEquals(inputAccount, unpickledAccount);
83 }
85 public void testDeletePickle() throws Exception {
86 final AndroidFxAccount account = addDummyAccount();
87 AccountPickler.pickle(account, PICKLE_FILENAME);
89 final String s = Utils.readFile(context, PICKLE_FILENAME);
90 assertNotNull(s);
91 assertTrue(s.length() > 0);
93 AccountPickler.deletePickle(context, PICKLE_FILENAME);
94 org.mozilla.gecko.background.sync.TestAccountPickler.assertFileNotPresent(
95 context, PICKLE_FILENAME);
96 }
98 private void assertAccountsEquals(final AndroidFxAccount expected,
99 final AndroidFxAccount actual) throws Exception {
100 // TODO: Write and use AndroidFxAccount.equals
101 // TODO: protected.
102 //assertEquals(expected.getAccountVersion(), actual.getAccountVersion());
103 assertEquals(expected.getProfile(), actual.getProfile());
104 assertEquals(expected.getAccountServerURI(), actual.getAccountServerURI());
105 assertEquals(expected.getAudience(), actual.getAudience());
106 assertEquals(expected.getTokenServerURI(), actual.getTokenServerURI());
107 assertEquals(expected.getSyncPrefsPath(), actual.getSyncPrefsPath());
108 assertEquals(expected.isSyncing(), actual.isSyncing());
109 assertEquals(expected.getEmail(), actual.getEmail());
110 assertStateEquals(expected.getState(), actual.getState());
111 }
113 private void assertStateEquals(final State expected, final State actual) throws Exception {
114 // TODO: Write and use State.equals. Thus, this is only thorough for the State base class.
115 assertEquals(expected.getStateLabel(), actual.getStateLabel());
116 assertEquals(expected.email, actual.email);
117 assertEquals(expected.uid, actual.uid);
118 assertEquals(expected.verified, actual.verified);
119 }
120 }