Bug 1414084 - Part 16 - Add some tests for the handling of PageActions. r=Grisha

MozReview-Commit-ID: EUl19vyK3Ra

--HG--
extra : rebase_source : d1fb530dc5e895173eb0a6f5c5aaad7a26ca7cf9
This commit is contained in:
Jan Henning 2018-03-19 20:27:01 +01:00
Родитель d08db48c78
Коммит 2894a34f2b
1 изменённых файлов: 100 добавлений и 2 удалений

Просмотреть файл

@ -12,6 +12,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mozilla.gecko.util.GeckoBundle;
@ -23,6 +24,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
@ -31,11 +33,17 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mozilla.gecko.toolbar.PageActionLayout.PageAction;
import static org.mozilla.gecko.toolbar.PageActionLayout.PageActionLayoutDelegate;
@RunWith(RobolectricTestRunner.class)
public class TestAddonUICache {
private AddonUICache mAddonUICache;
private @Mock Menu mMockMenu;
private @Mock PageActionLayoutDelegate mMockPalDelegate;
private @Captor ArgumentCaptor<List<PageAction>> palCaptor;
private static final MessageGenerator addonMessage = new AddonMenuMessageGenerator();
private static final MessageGenerator pageActionMessage = new PageActionMessageGenerator();
@Before
public void setUp() {
@ -89,12 +97,87 @@ public class TestAddonUICache {
verify(mMockMenu, never()).add(anyInt(), anyInt(), anyInt(), anyString());
}
@Test
public void testResolvedPageActionListSaving() {
final List<PageAction> pageActionList = new ArrayList<>();
mAddonUICache.removePageActionLayoutDelegate(pageActionList);
mAddonUICache.setPageActionLayoutDelegate(mMockPalDelegate);
verify(mMockPalDelegate).setCachedPageActions(palCaptor.capture());
Assert.assertEquals(pageActionList, palCaptor.getValue());
}
@Test
public void testPageActionMessageDirectForward() {
mAddonUICache.setPageActionLayoutDelegate(mMockPalDelegate);
verify(mMockPalDelegate, never()).addPageAction(any(GeckoBundle.class));
verify(mMockPalDelegate, never()).removePageAction(any(GeckoBundle.class));
Map<String, GeckoBundle> sentMessages = sendAddonMessages("PageActions:Add",
pageActionMessage);
ArgumentCaptor<GeckoBundle> messages = ArgumentCaptor.forClass(GeckoBundle.class);
verify(mMockPalDelegate, times(4)).addPageAction(messages.capture());
List<GeckoBundle> expectedMessages = new ArrayList<>(sentMessages.values());
Assert.assertEquals(expectedMessages, messages.getAllValues());
sendAddonMessages("PageActions:Remove", new ArrayList<>(sentMessages.values()));
messages = ArgumentCaptor.forClass(GeckoBundle.class);
verify(mMockPalDelegate, times(4)).removePageAction(messages.capture());
Assert.assertEquals(expectedMessages, messages.getAllValues());
}
@Test
public void testPageActionMessageStoreForward() {
Map<String, GeckoBundle> sentMessages = sendAddonMessages("PageActions:Add",
pageActionMessage);
mAddonUICache.setPageActionLayoutDelegate(mMockPalDelegate);
ArgumentCaptor<GeckoBundle> messages = ArgumentCaptor.forClass(GeckoBundle.class);
verify(mMockPalDelegate, times(4)).addPageAction(messages.capture());
List<GeckoBundle> expectedMessages = new ArrayList<>(sentMessages.values());
Assert.assertEquals(expectedMessages, messages.getAllValues());
verify(mMockPalDelegate, never()).removePageAction(any(GeckoBundle.class));
}
@Test
public void testPageActionMessageStoreRemoveForward() {
Map<String, GeckoBundle> sentMessages = sendAddonMessages("PageActions:Add",
pageActionMessage);
sendAddonMessages("PageActions:Remove", new ArrayList<>(sentMessages.values()));
mAddonUICache.setPageActionLayoutDelegate(mMockPalDelegate);
verify(mMockPalDelegate, never()).addPageAction(any(GeckoBundle.class));
verify(mMockPalDelegate, never()).removePageAction(any(GeckoBundle.class));
}
@Test
public void testResolvedPageActionListSavingRemoval() {
final List<PageAction> pageActionList = new ArrayList<>();
final GeckoBundle palMessage = pageActionMessage.getMessage("Frob widget");
final PageAction pageAction = new PageAction(palMessage.getString("id"),
palMessage.getString("title"), null, null, false);
pageActionList.add(pageAction);
mAddonUICache.removePageActionLayoutDelegate(pageActionList);
sendAddonMessage("PageActions:Remove", palMessage);
mAddonUICache.setPageActionLayoutDelegate(mMockPalDelegate);
verify(mMockPalDelegate).setCachedPageActions(palCaptor.capture());
Assert.assertEquals(pageActionList, palCaptor.getValue());
Assert.assertTrue(pageActionList.isEmpty());
}
private Map<String, GeckoBundle> sendAddonMessages(String event, MessageGenerator generator) {
Map<String, GeckoBundle> sentMessages = new LinkedHashMap<>();
for (int i = 0; i < 4; i++) {
String label = "Menu " + i;
GeckoBundle message = generator.getMessage(label);
mAddonUICache.handleMessage(event, message, null);
sendAddonMessage(event, message);
sentMessages.put(label, message);
}
return sentMessages;
@ -102,10 +185,14 @@ public class TestAddonUICache {
private void sendAddonMessages(String event, List<GeckoBundle> messages) {
for (GeckoBundle message : messages) {
mAddonUICache.handleMessage(event, message, null);
sendAddonMessage(event, message);
}
}
private void sendAddonMessage(String event, GeckoBundle message) {
mAddonUICache.handleMessage(event, message, null);
}
private interface MessageGenerator {
GeckoBundle getMessage(String label);
}
@ -119,4 +206,15 @@ public class TestAddonUICache {
return message;
}
}
private static class PageActionMessageGenerator implements MessageGenerator {
@Override
public GeckoBundle getMessage(String label) {
GeckoBundle message = new GeckoBundle(2);
message.putString("title", label);
message.putString("id", "{" + UUID.randomUUID().toString() + "}");
return message;
}
}
}