ManageAccountActivityTest.java

 1package eu.siacs.conversations.ui;
 2
 3import android.content.Intent;
 4import android.os.Build;
 5import android.widget.AdapterView;
 6import android.widget.ListView;
 7
 8import org.junit.Assert;
 9import org.junit.Test;
10import org.junit.runner.RunWith;
11import org.robolectric.Robolectric;
12import org.robolectric.RobolectricTestRunner;
13import org.robolectric.Shadows;
14import org.robolectric.annotation.Config;
15import org.robolectric.annotation.ConscryptMode;
16import org.robolectric.shadows.ShadowActivity;
17
18import eu.siacs.conversations.Conversations;
19import eu.siacs.conversations.entities.Account;
20import eu.siacs.conversations.xmpp.Jid;
21
22@RunWith(RobolectricTestRunner.class)
23@Config(sdk = Build.VERSION_CODES.TIRAMISU, application = Conversations.class)
24@ConscryptMode(ConscryptMode.Mode.OFF)
25public class ManageAccountActivityTest {
26
27    @Test
28    public void onItemClickNavigatesToCorrectAccountAndIgnoresFooter() {
29        ManageAccountActivity activity =
30                Robolectric.buildActivity(ManageAccountActivity.class).create().get();
31
32        Account[] accounts = new Account[] {
33            new Account(Jid.ofLocalAndDomain("alice", "example.org"), "password"),
34            new Account(Jid.ofLocalAndDomain("bob", "example.org"), "password"),
35            new Account(Jid.ofLocalAndDomain("carol", "example.org"), "password"),
36        };
37        for (Account account : accounts) {
38            activity.accountList.add(account);
39        }
40        activity.mAccountAdapter.notifyDataSetChanged();
41
42        ListView listView = activity.accountListView;
43        AdapterView.OnItemClickListener listener = listView.getOnItemClickListener();
44        ShadowActivity shadow = Shadows.shadowOf(activity);
45
46        for (int position = 0; position < accounts.length; position++) {
47            listener.onItemClick(listView, null, position, 0);
48            Intent started = shadow.getNextStartedActivity();
49            Assert.assertNotNull("position " + position + ": should start activity", started);
50            Assert.assertEquals(
51                    "position " + position + ": should navigate to correct account",
52                    accounts[position].getJid().asBareJid().toString(),
53                    started.getStringExtra("jid"));
54        }
55
56        int footerPosition = accounts.length;
57        Assert.assertEquals(
58                "position " + footerPosition + ": should be a header/footer view type",
59                AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER,
60                listView.getAdapter().getItemViewType(footerPosition));
61        listener.onItemClick(listView, null, footerPosition, 0);
62        Assert.assertNull(
63                "position " + footerPosition + " (footer): should not start activity",
64                shadow.getNextStartedActivity());
65    }
66}