@SuppressLint("MissingPermission") public static void registerAccountsUpdatedListener(final Context context) { AccountManager.get(context).addOnAccountsUpdatedListener(accounts -> { String username = Preferences.getUsername(context); if (TextUtils.isEmpty(username)) { return; } for (Account account : accounts) { if (TextUtils.equals(account.name, username)) { return; } } Preferences.setUsername(context, null); }, null, true); }
switch (which) { case DialogInterface.BUTTON_POSITIVE: Preferences.setUsername(context, items[selection]); Toast.makeText(context, context.getString(R.string.welcome, items[selection]),
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 testVoteNoMatchingAccount() { Preferences.setUsername(RuntimeEnvironment.application, "another"); UserServices.Callback callback = mock(UserServices.Callback.class); assertFalse(userServices.voteUp(RuntimeEnvironment.application, "1", callback)); verify(call, never()).enqueue(any(Callback.class)); }
@Test public void testCommentNotLoggedIn() { Preferences.setUsername(RuntimeEnvironment.application, null); UserServices.Callback callback = mock(UserServices.Callback.class); userServices.reply(RuntimeEnvironment.application, "1", "reply", callback); verify(call, never()).enqueue(any(Callback.class)); verify(callback).onDone(eq(false)); }
private void confirmLogout() { mAlertDialogBuilder.init(this) .setMessage(R.string.logout_confirm) .setNegativeButton(android.R.string.cancel, null) .setPositiveButton(android.R.string.ok, (dialog, which) -> Preferences.setUsername(this, null)) .show(); }
@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 testRemoveAccount() { Preferences.setUsername(context, "olduser"); AppUtils.registerAccountsUpdatedListener(context); shadowOf(AccountManager.get(context)).addAccount( new Account("newuser", BuildConfig.APPLICATION_ID)); assertNull(Preferences.getUsername(context)); Preferences.setUsername(context, "newuser"); shadowOf(AccountManager.get(context)).addAccount( new Account("olduser", BuildConfig.APPLICATION_ID)); assertEquals("newuser", Preferences.getUsername(context)); }
@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 testOpenUserProfile() { Preferences.setUsername(activity, "username"); drawerUser.performClick(); ((ShadowSupportDrawerLayout) Shadow.extract(activity.findViewById(R.id.drawer_layout))) .getDrawerListeners().get(0) .onDrawerClosed(activity.findViewById(R.id.drawer)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, UserActivity.class) .hasExtra(UserActivity.EXTRA_USERNAME, "username"); }
@Test public void testLoginStaleAccount() { Preferences.setUsername(context, "username"); shadowOf(ShadowAccountManager.get(context)) .addAccount(new Account("username", BuildConfig.APPLICATION_ID)); AppUtils.showLogin(context, null); assertThat(shadowOf(context).getNextStartedActivity()) .hasComponent(context, LoginActivity.class); }
@Test public void testNoExistingAccount() { assertThat(drawerAccount).hasText(R.string.login); assertThat(drawerLogout).isNotVisible(); assertThat(drawerUser).isNotVisible(); Preferences.setUsername(activity, "username"); assertThat(drawerAccount).hasText("username"); assertThat(drawerLogout).isVisible(); assertThat(drawerUser).isVisible(); drawerLogout.performClick(); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertThat(drawerAccount).hasText(R.string.login); assertThat(drawerLogout).isNotVisible(); }