OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.contextmenu; |
| 6 |
| 7 import static org.mockito.Mockito.doReturn; |
| 8 |
| 9 import android.util.Pair; |
| 10 |
| 11 import org.hamcrest.Matchers; |
| 12 import org.junit.Assert; |
| 13 import org.junit.Before; |
| 14 import org.junit.Rule; |
| 15 import org.junit.Test; |
| 16 import org.junit.runner.RunWith; |
| 17 import org.mockito.Mock; |
| 18 import org.mockito.Mockito; |
| 19 import org.mockito.MockitoAnnotations; |
| 20 import org.robolectric.RuntimeEnvironment; |
| 21 import org.robolectric.annotation.Config; |
| 22 |
| 23 import org.chromium.base.ContextUtils; |
| 24 import org.chromium.chrome.browser.ChromeFeatureList; |
| 25 import org.chromium.chrome.browser.contextmenu.ChromeContextMenuPopulator.Contex
tMenuMode; |
| 26 import org.chromium.chrome.browser.firstrun.FirstRunStatus; |
| 27 import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
| 28 import org.chromium.chrome.test.util.browser.Features; |
| 29 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 30 |
| 31 import java.util.ArrayList; |
| 32 import java.util.List; |
| 33 |
| 34 /** |
| 35 * Unit tests for the context menu logic of Chrome. |
| 36 */ |
| 37 @RunWith(LocalRobolectricTestRunner.class) |
| 38 @Config(manifest = Config.NONE) |
| 39 @Features(@Features.Register(value = ChromeFeatureList.CUSTOM_CONTEXT_MENU, enab
led = false)) |
| 40 public class ChromeContextMenuPopulatorTest { |
| 41 private static final String PAGE_URL = "http://www.blah.com"; |
| 42 private static final String LINK_URL = "http://www.blah.com/other_blah"; |
| 43 private static final String LINK_TEXT = "BLAH!"; |
| 44 private static final String IMAGE_SRC_URL = "http://www.blah.com/image.jpg"; |
| 45 private static final String IMAGE_TITLE_TEXT = "IMAGE!"; |
| 46 |
| 47 @Rule |
| 48 public Features.Processor mFeaturesProcessor = new Features.Processor(); |
| 49 |
| 50 @Mock |
| 51 private ContextMenuItemDelegate mItemDelegate; |
| 52 @Mock |
| 53 private TemplateUrlService mTemplateUrlService; |
| 54 |
| 55 private ChromeContextMenuPopulator mPopulator; |
| 56 |
| 57 @Before |
| 58 public void setUp() throws Exception { |
| 59 MockitoAnnotations.initMocks(this); |
| 60 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati
on); |
| 61 |
| 62 doReturn(PAGE_URL).when(mItemDelegate).getPageUrl(); |
| 63 |
| 64 initializePopulator(ChromeContextMenuPopulator.NORMAL_MODE); |
| 65 } |
| 66 |
| 67 private void initializePopulator(@ContextMenuMode int mode) { |
| 68 mPopulator = Mockito.spy(new ChromeContextMenuPopulator(mItemDelegate, m
ode)); |
| 69 doReturn(true).when(mPopulator).isAcceptedScheme(Mockito.anyString()); |
| 70 doReturn(true).when(mPopulator).isDownloadableScheme(Mockito.anyString()
); |
| 71 doReturn(mTemplateUrlService).when(mPopulator).getTemplateUrlService(); |
| 72 } |
| 73 |
| 74 @Test |
| 75 public void testBeforeFRE_Link() { |
| 76 FirstRunStatus.setFirstRunFlowComplete(false); |
| 77 |
| 78 final ContextMenuParams contextMenuParams = createLinkContextParams(); |
| 79 |
| 80 List<ContextMenuItem> enabledItems = getEnabledItems(contextMenuParams); |
| 81 Assert.assertThat(enabledItems, |
| 82 Matchers.containsInAnyOrder( |
| 83 ContextMenuItem.COPY_LINK_ADDRESS, ContextMenuItem.COPY_
LINK_TEXT)); |
| 84 |
| 85 initializePopulator(ChromeContextMenuPopulator.CUSTOM_TAB_MODE); |
| 86 enabledItems = getEnabledItems(contextMenuParams); |
| 87 Assert.assertThat(enabledItems, |
| 88 Matchers.containsInAnyOrder( |
| 89 ContextMenuItem.COPY_LINK_ADDRESS, ContextMenuItem.COPY_
LINK_TEXT)); |
| 90 |
| 91 initializePopulator(ChromeContextMenuPopulator.FULLSCREEN_TAB_MODE); |
| 92 enabledItems = getEnabledItems(contextMenuParams); |
| 93 Assert.assertThat(enabledItems, |
| 94 Matchers.containsInAnyOrder( |
| 95 ContextMenuItem.COPY_LINK_ADDRESS, ContextMenuItem.COPY_
LINK_TEXT)); |
| 96 } |
| 97 |
| 98 @Test |
| 99 public void testBeforeFRE_Image() { |
| 100 FirstRunStatus.setFirstRunFlowComplete(false); |
| 101 |
| 102 final ContextMenuParams contextMenuParams = createImageContextParams(); |
| 103 |
| 104 List<ContextMenuItem> enabledItems = getEnabledItems(contextMenuParams); |
| 105 Assert.assertThat(enabledItems, Matchers.empty()); |
| 106 |
| 107 initializePopulator(ChromeContextMenuPopulator.CUSTOM_TAB_MODE); |
| 108 enabledItems = getEnabledItems(contextMenuParams); |
| 109 Assert.assertThat(enabledItems, Matchers.empty()); |
| 110 |
| 111 initializePopulator(ChromeContextMenuPopulator.FULLSCREEN_TAB_MODE); |
| 112 enabledItems = getEnabledItems(contextMenuParams); |
| 113 Assert.assertThat(enabledItems, Matchers.empty()); |
| 114 } |
| 115 |
| 116 @Test |
| 117 public void testBeforeFRE_ImageLink() { |
| 118 FirstRunStatus.setFirstRunFlowComplete(false); |
| 119 |
| 120 final ContextMenuParams contextMenuParams = createImageLinkContextParams
(); |
| 121 |
| 122 List<ContextMenuItem> enabledItems = getEnabledItems(contextMenuParams); |
| 123 Assert.assertThat( |
| 124 enabledItems, Matchers.containsInAnyOrder(ContextMenuItem.COPY_L
INK_ADDRESS)); |
| 125 |
| 126 initializePopulator(ChromeContextMenuPopulator.CUSTOM_TAB_MODE); |
| 127 enabledItems = getEnabledItems(contextMenuParams); |
| 128 Assert.assertThat( |
| 129 enabledItems, Matchers.containsInAnyOrder(ContextMenuItem.COPY_L
INK_ADDRESS)); |
| 130 |
| 131 initializePopulator(ChromeContextMenuPopulator.FULLSCREEN_TAB_MODE); |
| 132 enabledItems = getEnabledItems(contextMenuParams); |
| 133 Assert.assertThat( |
| 134 enabledItems, Matchers.containsInAnyOrder(ContextMenuItem.COPY_L
INK_ADDRESS)); |
| 135 } |
| 136 |
| 137 private List<ContextMenuItem> getEnabledItems(ContextMenuParams params) { |
| 138 List<Pair<Integer, List<ContextMenuItem>>> contextMenuState = |
| 139 mPopulator.buildContextMenu(null, RuntimeEnvironment.application
, params); |
| 140 |
| 141 List<ContextMenuItem> enabledItems = new ArrayList<>(); |
| 142 for (int i = 0; i < contextMenuState.size(); i++) { |
| 143 enabledItems.addAll(contextMenuState.get(i).second); |
| 144 } |
| 145 return enabledItems; |
| 146 } |
| 147 |
| 148 private static ContextMenuParams createLinkContextParams() { |
| 149 return new ContextMenuParams( |
| 150 0, PAGE_URL, LINK_URL, LINK_TEXT, "", "", "", false, null, false
); |
| 151 } |
| 152 |
| 153 private static ContextMenuParams createImageContextParams() { |
| 154 return new ContextMenuParams(ContextMenuParams.MediaType.MEDIA_TYPE_IMAG
E, PAGE_URL, "", "", |
| 155 IMAGE_SRC_URL, IMAGE_TITLE_TEXT, "", false, null, true); |
| 156 } |
| 157 |
| 158 private static ContextMenuParams createImageLinkContextParams() { |
| 159 return new ContextMenuParams(ContextMenuParams.MediaType.MEDIA_TYPE_IMAG
E, PAGE_URL, |
| 160 PAGE_URL, LINK_URL, IMAGE_SRC_URL, IMAGE_TITLE_TEXT, "", false,
null, true); |
| 161 } |
| 162 } |
OLD | NEW |