private void addAccount(String username, String password) { Account account = new Account(username, BuildConfig.APPLICATION_ID); mAccountManager.addAccountExplicitly(account, password, null); mAccountManager.setPassword(account, password); // for re-login with updated password Bundle bundle = new Bundle(); bundle.putString(AccountManager.KEY_ACCOUNT_NAME, username); bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, BuildConfig.APPLICATION_ID); setAccountAuthenticatorResult(bundle); Preferences.setUsername(this, username); finish(); }
@Test public void testAddAccountExplicitly_withExtras() { Account account = new Account("name", "type"); Bundle extras = new Bundle(); extras.putString("key123", "value123"); boolean accountAdded = am.addAccountExplicitly(account, null, extras); assertThat(accountAdded).isTrue(); assertThat(am.getUserData(account, "key123")).isEqualTo("value123"); assertThat(am.getUserData(account, "key456")).isNull(); }
private void endAuth(final String accessToken, final String scope) { progressDialog.setContent(getString(R.string.loading_user)); TokenStore.getInstance(this).saveToken(accessToken); userService .getUser() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .as(AutoDisposeUtils.bindToLifecycle(this)) .subscribe(response -> { User user = response.body(); Account account = new Account(user.login(), getString(R.string.account_type)); Bundle userData = AccountsHelper.buildBundle(user.name(), user.email(), user.avatarUrl(), scope); userData.putString(AccountManager.KEY_AUTHTOKEN, accessToken); accountManager.addAccountExplicitly(account, null, userData); accountManager.setAuthToken(account, getString(R.string.account_type), accessToken); Bundle result = new Bundle(); result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); result.putString(AccountManager.KEY_AUTHTOKEN, accessToken); setAccountAuthenticatorResult(result); finish(); }, Throwable::printStackTrace); }
@Test public void testAddAccountExplicitly_withPassword() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, "passwd", null); assertThat(accountAdded).isTrue(); assertThat(am.getPassword(account)).isEqualTo("passwd"); }
@Test public void testAddAccountExplicitly_doesNotNotifyListenersIfUnsuccessful() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, "passwd", null); assertThat(accountAdded).isTrue(); TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); // This account is added already, so it'll fail boolean accountAdded2 = am.addAccountExplicitly(account, "passwd", null); assertThat(accountAdded2).isFalse(); assertThat(listener.getInvocationCount()).isEqualTo(0); }
if (accounts.length == 0) { syncAccount = new Account(SYNC_ACCOUNT_NAME, BuildConfig.APPLICATION_ID); accountManager.addAccountExplicitly(syncAccount, null, null); } else { syncAccount = accounts[0];
@Before public void setUp() { MockitoAnnotations.initMocks(this); call = mock(Call.class); Call.Factory callFactory = mock(Call.Factory.class); when(callFactory.newCall(any(Request.class))).thenReturn(call); userServices = new UserServicesClient(callFactory, Schedulers.immediate()); Preferences.setUsername(RuntimeEnvironment.application, "username"); account = new Account("username", BuildConfig.APPLICATION_ID); ShadowAccountManager.get(RuntimeEnvironment.application) .addAccountExplicitly(account, "password", null); }
@Test public void testGetSetUserData_addToInitiallyEmptyExtras() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, null, null); assertThat(accountAdded).isTrue(); am.setUserData(account, "key123", "value123"); assertThat(am.getUserData(account, "key123")).isEqualTo("value123"); }
@Test public void testGetSetPassword_setInAccountInitiallyWithNoPassword() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, null, null); assertThat(accountAdded).isTrue(); assertThat(am.getPassword(account)).isNull(); am.setPassword(account, "passwd"); assertThat(am.getPassword(account)).isEqualTo("passwd"); }
@Test public void testGetSetPassword_remove() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, "passwd1", null); assertThat(accountAdded).isTrue(); assertThat(am.getPassword(account)).isEqualTo("passwd1"); am.setPassword(account, null); assertThat(am.getPassword(account)).isNull(); }
@Test public void testGetSetPassword_overwrite() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, "passwd1", null); assertThat(accountAdded).isTrue(); assertThat(am.getPassword(account)).isEqualTo("passwd1"); am.setPassword(account, "passwd2"); assertThat(am.getPassword(account)).isEqualTo("passwd2"); }
@Test public void testAddAccountExplicitly_noPasswordNoExtras() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, null, null); assertThat(accountAdded).isTrue(); assertThat(am.getAccountsByType("type").length).isEqualTo(1); assertThat(am.getAccountsByType("type")[0].name).isEqualTo("name"); boolean accountAddedTwice = am.addAccountExplicitly(account, null, null); assertThat(accountAddedTwice).isFalse(); account = new Account("another_name", "type"); accountAdded = am.addAccountExplicitly(account, null, null); assertThat(accountAdded).isTrue(); assertThat(am.getAccountsByType("type").length).isEqualTo(2); assertThat(am.getAccountsByType("type")[0].name).isEqualTo("name"); assertThat(am.getAccountsByType("type")[1].name).isEqualTo("another_name"); assertThat(am.getPassword(account)).isNull(); try { am.addAccountExplicitly(null, null, null); fail("An illegal argument exception should have been thrown when trying to add a null account"); } catch (IllegalArgumentException iae) { // NOP } }
@Test public void testAddAccountExplicitly_notifiesListenersIfSuccessful() { TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, "passwd", null); assertThat(accountAdded).isTrue(); assertThat(listener.getInvocationCount()).isEqualTo(1); }
@Test public void testGetSetUserData_overwrite() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, null, null); assertThat(accountAdded).isTrue(); am.setUserData(account, "key123", "value123"); assertThat(am.getUserData(account, "key123")).isEqualTo("value123"); am.setUserData(account, "key123", "value456"); assertThat(am.getUserData(account, "key123")).isEqualTo("value456"); }
@Test public void testGetSetUserData_remove() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, null, null); assertThat(accountAdded).isTrue(); am.setUserData(account, "key123", "value123"); assertThat(am.getUserData(account, "key123")).isEqualTo("value123"); am.setUserData(account, "key123", null); assertThat(am.getUserData(account, "key123")).isNull(); }
@Test public void removeAccount_notifiesListenersIfSuccessful() { Account account = new Account("name", "type"); am.addAccountExplicitly(account, "passwd", null); TestOnAccountsUpdateListener listener = new TestOnAccountsUpdateListener(); am.addOnAccountsUpdatedListener(listener, null, false); assertThat(listener.getInvocationCount()).isEqualTo(0); am.removeAccount(account, null, null); assertThat(listener.getInvocationCount()).isEqualTo(1); }
@Config(sdk = 21) @Test public void testRemoveAccount() { AccountManager.get(activity).addAccountExplicitly(new Account("existing", BuildConfig.APPLICATION_ID), "password", null); Preferences.setUsername(activity, "existing"); drawerAccount.performClick(); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); assertThat(alertDialog.getListView().getAdapter()).hasCount(1); alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL).performClick(); assertThat(alertDialog).isNotShowing(); assertThat(AccountManager.get(activity).getAccounts()).isEmpty(); }
@Test public void testAddAccount() { AccountManager.get(activity).addAccountExplicitly(new Account("existing", BuildConfig.APPLICATION_ID), "password", null); drawerAccount.performClick(); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); assertThat(alertDialog.getListView().getAdapter()).hasCount(1); alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); assertThat(alertDialog).isNotShowing(); ((ShadowSupportDrawerLayout) Shadow.extract(activity.findViewById(R.id.drawer_layout))) .getDrawerListeners().get(0) .onDrawerClosed(activity.findViewById(R.id.drawer)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, LoginActivity.class); }
@Test public void testExistingAccount() { AccountManager.get(activity).addAccountExplicitly(new Account("existing", BuildConfig.APPLICATION_ID), "password", null); drawerAccount.performClick(); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); assertThat(alertDialog.getListView().getAdapter()).hasCount(1); shadowOf(alertDialog).clickOnItem(0); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertThat(alertDialog).isNotShowing(); assertThat(drawerAccount).hasText("existing"); assertThat(drawerLogout).isVisible(); drawerAccount.performClick(); alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertThat(alertDialog.getListView().getAdapter()).hasCount(1); }
private Account createGuestUser(String siteUrl) { String accountName = getAccountName(siteUrl, GUEST_ACCOUNT_NAME); Account account = new Account(accountName, resources.getString(R.string.account_type)); Bundle userdata = new Bundle(); userdata.putString(KEY_IS_GUEST, Boolean.toString(true)); userdata.putString(KEY_SITE_URL, siteUrl); userdata.putString(KEY_USERNAME, GUEST_ACCOUNT_NAME); accountManager.addAccountExplicitly(account, null, userdata); return account; }