@@ -136,7 +136,12 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
accountListView = binding.accountList;
this.mAccountAdapter = new AccountAdapter(this, accountList);
accountListView.setAdapter(this.mAccountAdapter);
- accountListView.setOnItemClickListener((arg0, view, position, arg3) -> switchToAccount(accountList.get(position)));
+ accountListView.setOnItemClickListener((arg0, view, position, arg3) -> {
+ final Object item = arg0.getItemAtPosition(position);
+ if (item != null) {
+ switchToAccount((Account) item);
+ }
+ });
LayoutInflater inflater = getLayoutInflater();
@@ -164,7 +169,9 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
ManageAccountActivity.this.getMenuInflater().inflate(
R.menu.manageaccounts_context, menu);
AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
- this.selectedAccount = accountList.get(acmi.position);
+ final Object item = accountListView.getItemAtPosition(acmi.position);
+ if (item == null) return;
+ this.selectedAccount = (Account) item;
if (this.selectedAccount.isEnabled()) {
menu.findItem(R.id.mgmt_account_enable).setVisible(false);
menu.findItem(R.id.mgmt_account_announce_pgp).setVisible(Config.supportOpenPgp());
@@ -0,0 +1,66 @@
+package eu.siacs.conversations.ui;
+
+import android.content.Intent;
+import android.os.Build;
+import android.widget.AdapterView;
+import android.widget.ListView;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.Shadows;
+import org.robolectric.annotation.Config;
+import org.robolectric.annotation.ConscryptMode;
+import org.robolectric.shadows.ShadowActivity;
+
+import eu.siacs.conversations.Conversations;
+import eu.siacs.conversations.entities.Account;
+import eu.siacs.conversations.xmpp.Jid;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(sdk = Build.VERSION_CODES.TIRAMISU, application = Conversations.class)
+@ConscryptMode(ConscryptMode.Mode.OFF)
+public class ManageAccountActivityTest {
+
+ @Test
+ public void onItemClickNavigatesToCorrectAccountAndIgnoresFooter() {
+ ManageAccountActivity activity =
+ Robolectric.buildActivity(ManageAccountActivity.class).create().get();
+
+ Account[] accounts = new Account[] {
+ new Account(Jid.ofLocalAndDomain("alice", "example.org"), "password"),
+ new Account(Jid.ofLocalAndDomain("bob", "example.org"), "password"),
+ new Account(Jid.ofLocalAndDomain("carol", "example.org"), "password"),
+ };
+ for (Account account : accounts) {
+ activity.accountList.add(account);
+ }
+ activity.mAccountAdapter.notifyDataSetChanged();
+
+ ListView listView = activity.accountListView;
+ AdapterView.OnItemClickListener listener = listView.getOnItemClickListener();
+ ShadowActivity shadow = Shadows.shadowOf(activity);
+
+ for (int position = 0; position < accounts.length; position++) {
+ listener.onItemClick(listView, null, position, 0);
+ Intent started = shadow.getNextStartedActivity();
+ Assert.assertNotNull("position " + position + ": should start activity", started);
+ Assert.assertEquals(
+ "position " + position + ": should navigate to correct account",
+ accounts[position].getJid().asBareJid().toString(),
+ started.getStringExtra("jid"));
+ }
+
+ int footerPosition = accounts.length;
+ Assert.assertEquals(
+ "position " + footerPosition + ": should be a header/footer view type",
+ AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER,
+ listView.getAdapter().getItemViewType(footerPosition));
+ listener.onItemClick(listView, null, footerPosition, 0);
+ Assert.assertNull(
+ "position " + footerPosition + " (footer): should not start activity",
+ shadow.getNextStartedActivity());
+ }
+}