1package eu.siacs.conversations.entities;
2
3import static org.mockito.Mockito.mock;
4import static org.mockito.Mockito.verify;
5import static org.mockito.Mockito.when;
6
7import java.util.HashSet;
8
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12import org.robolectric.RobolectricTestRunner;
13import org.robolectric.annotation.Config;
14import org.robolectric.annotation.ConscryptMode;
15
16import android.os.Build;
17import eu.siacs.conversations.Conversations;
18import eu.siacs.conversations.xmpp.Jid;
19import junit.framework.Assert;
20
21@RunWith(RobolectricTestRunner.class)
22@Config(sdk = Build.VERSION_CODES.TIRAMISU, application = Conversations.class)
23@ConscryptMode(ConscryptMode.Mode.OFF)
24public class MucOptionsTest {
25 private static final String OCCUPANT_ID = "occ-1";
26 private static final MucOptions.User.OccupantId CACHE_KEY =
27 new MucOptions.User.OccupantId(OCCUPANT_ID);
28
29 private Conversation conversation;
30 private MucOptions mucOptions;
31 private MucOptions.User user;
32
33 @Before
34 public void setUp() throws Exception {
35 final var bookmark = mock(Bookmark.class);
36 when(bookmark.getNick()).thenReturn("testBookmarkNick");
37
38 final var roster = mock(Roster.class);
39 final var account = mock(Account.class);
40 when(account.getJid()).thenReturn(Jid.ofLocalAndDomain("testAccount", "example.org"));
41 when(account.getRoster()).thenReturn(roster);
42
43 conversation = mock(Conversation.class);
44 when(conversation.getAccount()).thenReturn(account);
45 when(conversation.getAttribute("mucNick")).thenReturn("testMucNick");
46 when(conversation.getAttribute("affiliation")).thenReturn(null);
47 when(conversation.getAttribute("role")).thenReturn(null);
48 when(conversation.getJid()).thenReturn(Jid.ofLocalAndDomain("testMuc", "example.org"));
49 when(conversation.getBookmark()).thenReturn(bookmark);
50
51 mucOptions = new MucOptions(conversation);
52
53 when(conversation.setCachedOccupantNick(CACHE_KEY, "testNick")).thenReturn(true);
54 user = new MucOptions.User(
55 mucOptions,
56 Jid.ofLocalAndDomain("testUser", "example.org"),
57 OCCUPANT_ID,
58 "testNick",
59 new HashSet<>()
60 );
61 }
62
63 @Test
64 public void testCtorCachesOccupantNick() throws Exception {
65 verify(conversation).setCachedOccupantNick(CACHE_KEY, "testNick");
66 }
67
68 @Test
69 public void testCtorReadsCachedNickAndAvatar() throws Exception {
70 final var key = new MucOptions.User.OccupantId("occ-2");
71 when(conversation.getCachedOccupantNick(key)).thenReturn("cachedNick");
72 when(conversation.getCachedOccupantAvatar(key)).thenReturn("cachedAvatar");
73 final var cachedUser = new MucOptions.User(
74 mucOptions,
75 Jid.ofLocalAndDomain("testUser2", "example.org"),
76 "occ-2",
77 null,
78 new HashSet<>()
79 );
80 Assert.assertEquals("cachedNick", cachedUser.getNick());
81 Assert.assertEquals("cachedAvatar", cachedUser.getAvatar());
82 }
83
84 @Test
85 public void testSetAvatarCachesAvatar() throws Exception {
86 when(conversation.setCachedOccupantAvatar(CACHE_KEY, "newAvatar")).thenReturn(true);
87 user.setAvatar("newAvatar");
88 verify(conversation).setCachedOccupantAvatar(CACHE_KEY, "newAvatar");
89 }
90
91 @Test
92 public void testSetAvatarCachesAvatarForRosterContacts() throws Exception {
93 final var roster = conversation.getAccount().getRoster();
94 final var realJid = Jid.ofLocalAndDomain("realUser", "example.org");
95 final var contact = mock(Contact.class);
96 when(roster.getContactFromContactList(realJid)).thenReturn(contact);
97 user.setRealJid(realJid);
98
99 when(conversation.setCachedOccupantAvatar(CACHE_KEY, "avatarHash")).thenReturn(true);
100 user.setAvatar("avatarHash");
101 verify(conversation).setCachedOccupantAvatar(CACHE_KEY, "avatarHash");
102 }
103}