| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.contextmenu; | 5 package org.chromium.chrome.browser.contextmenu; |
| 6 | 6 |
| 7 import android.support.design.widget.TabLayout; | 7 import android.support.design.widget.TabLayout; |
| 8 import android.support.test.filters.SmallTest; | 8 import android.support.test.filters.SmallTest; |
| 9 import android.util.Pair; | 9 import android.util.Pair; |
| 10 import android.view.View; | 10 import android.view.View; |
| 11 import android.widget.TextView; | 11 import android.widget.TextView; |
| 12 | 12 |
| 13 import org.junit.Assert; | |
| 14 import org.junit.Before; | |
| 15 import org.junit.Rule; | |
| 16 import org.junit.Test; | |
| 17 import org.junit.runner.RunWith; | |
| 18 | |
| 19 import org.chromium.base.CollectionUtil; | 13 import org.chromium.base.CollectionUtil; |
| 20 import org.chromium.base.ThreadUtils; | 14 import org.chromium.base.ThreadUtils; |
| 21 import org.chromium.base.test.util.CommandLineFlags; | |
| 22 import org.chromium.base.test.util.Feature; | 15 import org.chromium.base.test.util.Feature; |
| 23 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
| 24 import org.chromium.chrome.browser.ChromeActivity; | 17 import org.chromium.chrome.browser.ChromeActivity; |
| 25 import org.chromium.chrome.browser.ChromeSwitches; | 18 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 26 import org.chromium.chrome.test.ChromeActivityTestRule; | |
| 27 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | |
| 28 import org.chromium.content_public.common.Referrer; | 19 import org.chromium.content_public.common.Referrer; |
| 29 | 20 |
| 30 import java.util.ArrayList; | 21 import java.util.ArrayList; |
| 31 import java.util.List; | 22 import java.util.List; |
| 32 import java.util.concurrent.Callable; | 23 import java.util.concurrent.Callable; |
| 33 import java.util.concurrent.ExecutionException; | 24 import java.util.concurrent.ExecutionException; |
| 34 | 25 |
| 35 /** | 26 /** |
| 36 * A class to checkout the TabularContextMenuUi. This confirms the the UI repres
ents items and | 27 * A class to checkout the TabularContextMenuUi. This confirms the the UI repres
ents items and |
| 37 * groups. | 28 * groups. |
| 38 */ | 29 */ |
| 39 @RunWith(ChromeJUnit4ClassRunner.class) | 30 public class TabularContextMenuUiTest extends ChromeActivityTestCaseBase<ChromeA
ctivity> { |
| 40 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, | 31 public TabularContextMenuUiTest() { |
| 41 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG}) | 32 super(ChromeActivity.class); |
| 42 public class TabularContextMenuUiTest { | 33 } |
| 43 @Rule | |
| 44 public ChromeActivityTestRule<ChromeActivity> mActivityTestRule = | |
| 45 new ChromeActivityTestRule<>(ChromeActivity.class); | |
| 46 | 34 |
| 47 private static class MockMenuParams extends ContextMenuParams { | 35 private static class MockMenuParams extends ContextMenuParams { |
| 48 private String mUrl = ""; | 36 private String mUrl = ""; |
| 49 | 37 |
| 50 private MockMenuParams(int mediaType, String pageUrl, String linkUrl, St
ring linkText, | 38 private MockMenuParams(int mediaType, String pageUrl, String linkUrl, St
ring linkText, |
| 51 String unfilteredLinkUrl, String srcUrl, String titleText, | 39 String unfilteredLinkUrl, String srcUrl, String titleText, |
| 52 boolean imageWasFetchedLoFi, Referrer referrer, boolean canSavem
edia) { | 40 boolean imageWasFetchedLoFi, Referrer referrer, boolean canSavem
edia) { |
| 53 super(mediaType, pageUrl, linkUrl, linkText, unfilteredLinkUrl, srcU
rl, titleText, | 41 super(mediaType, pageUrl, linkUrl, linkText, unfilteredLinkUrl, srcU
rl, titleText, |
| 54 imageWasFetchedLoFi, referrer, canSavemedia); | 42 imageWasFetchedLoFi, referrer, canSavemedia); |
| 55 } | 43 } |
| 56 | 44 |
| 57 private MockMenuParams(String url) { | 45 private MockMenuParams(String url) { |
| 58 this(0, "", "", "", "", "", "", false, null, true); | 46 this(0, "", "", "", "", "", "", false, null, true); |
| 59 mUrl = url; | 47 mUrl = url; |
| 60 } | 48 } |
| 61 | 49 |
| 62 @Override | 50 @Override |
| 63 public String getLinkUrl() { | 51 public String getLinkUrl() { |
| 64 return mUrl; | 52 return mUrl; |
| 65 } | 53 } |
| 66 } | 54 } |
| 67 | 55 |
| 68 @Before | 56 @Override |
| 69 public void setUp() throws InterruptedException { | 57 public void startMainActivity() throws InterruptedException { |
| 70 mActivityTestRule.startMainActivityOnBlankPage(); | 58 startMainActivityOnBlankPage(); |
| 71 } | 59 } |
| 72 | 60 |
| 73 @Test | |
| 74 @SmallTest | 61 @SmallTest |
| 75 @Feature({"CustomContextMenu"}) | 62 @Feature({"CustomContextMenu"}) |
| 76 public void testViewDisplaysSingleItemProperly() throws ExecutionException { | 63 public void testViewDisplaysSingleItemProperly() throws ExecutionException { |
| 77 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); | 64 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); |
| 78 | 65 |
| 79 final List<Pair<Integer, List<ContextMenuItem>>> itemGroups = new ArrayL
ist<>(); | 66 final List<Pair<Integer, List<ContextMenuItem>>> itemGroups = new ArrayL
ist<>(); |
| 80 List<ContextMenuItem> item = CollectionUtil.newArrayList(ContextMenuItem
.ADD_TO_CONTACTS, | 67 List<ContextMenuItem> item = CollectionUtil.newArrayList(ContextMenuItem
.ADD_TO_CONTACTS, |
| 81 ContextMenuItem.CALL, ContextMenuItem.COPY_LINK_ADDRESS); | 68 ContextMenuItem.CALL, ContextMenuItem.COPY_LINK_ADDRESS); |
| 82 itemGroups.add(new Pair<>(R.string.contextmenu_link_title, item)); | 69 itemGroups.add(new Pair<>(R.string.contextmenu_link_title, item)); |
| 83 final String url = "http://google.com"; | 70 final String url = "http://google.com"; |
| 84 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { | 71 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { |
| 85 @Override | 72 @Override |
| 86 public View call() { | 73 public View call() { |
| 87 return dialog.createPagerView( | 74 return dialog.createPagerView(getActivity(), new MockMenuParams(
url), itemGroups); |
| 88 mActivityTestRule.getActivity(), new MockMenuParams(url)
, itemGroups); | |
| 89 } | 75 } |
| 90 }); | 76 }); |
| 91 | 77 |
| 92 TabLayout layout = (TabLayout) view.findViewById(R.id.tab_layout); | 78 TabLayout layout = (TabLayout) view.findViewById(R.id.tab_layout); |
| 93 Assert.assertEquals(layout.getVisibility(), View.GONE); | 79 assertEquals(layout.getVisibility(), View.GONE); |
| 94 } | 80 } |
| 95 | 81 |
| 96 @Test | |
| 97 @SmallTest | 82 @SmallTest |
| 98 @Feature({"CustomContextMenu"}) | 83 @Feature({"CustomContextMenu"}) |
| 99 public void testViewDisplaysViewPagerForMultipleItems() throws ExecutionExce
ption { | 84 public void testViewDisplaysViewPagerForMultipleItems() throws ExecutionExce
ption { |
| 100 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); | 85 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); |
| 101 | 86 |
| 102 final List<Pair<Integer, List<ContextMenuItem>>> itemGroups = new ArrayL
ist<>(); | 87 final List<Pair<Integer, List<ContextMenuItem>>> itemGroups = new ArrayL
ist<>(); |
| 103 List<ContextMenuItem> item = CollectionUtil.newArrayList(ContextMenuItem
.ADD_TO_CONTACTS, | 88 List<ContextMenuItem> item = CollectionUtil.newArrayList(ContextMenuItem
.ADD_TO_CONTACTS, |
| 104 ContextMenuItem.CALL, ContextMenuItem.COPY_LINK_ADDRESS); | 89 ContextMenuItem.CALL, ContextMenuItem.COPY_LINK_ADDRESS); |
| 105 itemGroups.add(new Pair<>(R.string.contextmenu_link_title, item)); | 90 itemGroups.add(new Pair<>(R.string.contextmenu_link_title, item)); |
| 106 itemGroups.add(new Pair<>(R.string.contextmenu_link_title, item)); | 91 itemGroups.add(new Pair<>(R.string.contextmenu_link_title, item)); |
| 107 final String url = "http://google.com"; | 92 final String url = "http://google.com"; |
| 108 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { | 93 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { |
| 109 @Override | 94 @Override |
| 110 public View call() { | 95 public View call() { |
| 111 return dialog.createPagerView( | 96 return dialog.createPagerView(getActivity(), new MockMenuParams(
url), itemGroups); |
| 112 mActivityTestRule.getActivity(), new MockMenuParams(url)
, itemGroups); | |
| 113 } | 97 } |
| 114 }); | 98 }); |
| 115 | 99 |
| 116 TabLayout layout = (TabLayout) view.findViewById(R.id.tab_layout); | 100 TabLayout layout = (TabLayout) view.findViewById(R.id.tab_layout); |
| 117 Assert.assertEquals(layout.getVisibility(), View.VISIBLE); | 101 assertEquals(layout.getVisibility(), View.VISIBLE); |
| 118 } | 102 } |
| 119 | 103 |
| 120 @Test | |
| 121 @SmallTest | 104 @SmallTest |
| 122 @Feature({"CustomContextMenu"}) | 105 @Feature({"CustomContextMenu"}) |
| 123 public void testURLIsShownOnContextMenu() throws ExecutionException { | 106 public void testURLIsShownOnContextMenu() throws ExecutionException { |
| 124 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); | 107 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); |
| 125 final List<ContextMenuItem> item = | 108 final List<ContextMenuItem> item = |
| 126 CollectionUtil.newArrayList(ContextMenuItem.ADD_TO_CONTACTS, Con
textMenuItem.CALL, | 109 CollectionUtil.newArrayList(ContextMenuItem.ADD_TO_CONTACTS, Con
textMenuItem.CALL, |
| 127 ContextMenuItem.COPY_LINK_ADDRESS); | 110 ContextMenuItem.COPY_LINK_ADDRESS); |
| 128 final String expectedUrl = "http://google.com"; | 111 final String expectedUrl = "http://google.com"; |
| 129 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { | 112 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { |
| 130 @Override | 113 @Override |
| 131 public View call() { | 114 public View call() { |
| 132 return dialog.createContextMenuPageUi(mActivityTestRule.getActiv
ity(), | 115 return dialog.createContextMenuPageUi( |
| 133 new MockMenuParams(expectedUrl), item, false, item.size(
)); | 116 getActivity(), new MockMenuParams(expectedUrl), item, fa
lse, item.size()); |
| 134 } | 117 } |
| 135 }); | 118 }); |
| 136 | 119 |
| 137 TextView textView = (TextView) view.findViewById(R.id.context_header_tex
t); | 120 TextView textView = (TextView) view.findViewById(R.id.context_header_tex
t); |
| 138 Assert.assertEquals(expectedUrl, String.valueOf(textView.getText())); | 121 assertEquals(expectedUrl, String.valueOf(textView.getText())); |
| 139 } | 122 } |
| 140 | 123 |
| 141 @Test | |
| 142 @SmallTest | 124 @SmallTest |
| 143 @Feature({"CustomContextMenu"}) | 125 @Feature({"CustomContextMenu"}) |
| 144 public void testHeaderIsNotShownWhenThereIsNoParams() throws ExecutionExcept
ion { | 126 public void testHeaderIsNotShownWhenThereIsNoParams() throws ExecutionExcept
ion { |
| 145 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); | 127 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); |
| 146 final List<ContextMenuItem> item = | 128 final List<ContextMenuItem> item = |
| 147 CollectionUtil.newArrayList(ContextMenuItem.ADD_TO_CONTACTS, Con
textMenuItem.CALL, | 129 CollectionUtil.newArrayList(ContextMenuItem.ADD_TO_CONTACTS, Con
textMenuItem.CALL, |
| 148 ContextMenuItem.COPY_LINK_ADDRESS); | 130 ContextMenuItem.COPY_LINK_ADDRESS); |
| 149 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { | 131 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { |
| 150 @Override | 132 @Override |
| 151 public View call() { | 133 public View call() { |
| 152 return dialog.createContextMenuPageUi(mActivityTestRule.getActiv
ity(), | 134 return dialog.createContextMenuPageUi( |
| 153 new MockMenuParams(""), item, false, item.size()); | 135 getActivity(), new MockMenuParams(""), item, false, item
.size()); |
| 154 } | 136 } |
| 155 }); | 137 }); |
| 156 | 138 |
| 157 Assert.assertEquals(view.findViewById(R.id.context_header_text).getVisib
ility(), View.GONE); | 139 assertEquals(view.findViewById(R.id.context_header_text).getVisibility()
, View.GONE); |
| 158 Assert.assertEquals(view.findViewById(R.id.context_divider).getVisibilit
y(), View.GONE); | 140 assertEquals(view.findViewById(R.id.context_divider).getVisibility(), Vi
ew.GONE); |
| 159 } | 141 } |
| 160 | 142 |
| 161 @Test | |
| 162 @SmallTest | 143 @SmallTest |
| 163 @Feature({"CustomContextMenu"}) | 144 @Feature({"CustomContextMenu"}) |
| 164 public void testLinkShowsMultipleLinesWhenClicked() throws ExecutionExceptio
n { | 145 public void testLinkShowsMultipleLinesWhenClicked() throws ExecutionExceptio
n { |
| 165 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); | 146 final TabularContextMenuUi dialog = new TabularContextMenuUi(null); |
| 166 final List<ContextMenuItem> item = | 147 final List<ContextMenuItem> item = |
| 167 CollectionUtil.newArrayList(ContextMenuItem.ADD_TO_CONTACTS, Con
textMenuItem.CALL, | 148 CollectionUtil.newArrayList(ContextMenuItem.ADD_TO_CONTACTS, Con
textMenuItem.CALL, |
| 168 ContextMenuItem.COPY_LINK_ADDRESS); | 149 ContextMenuItem.COPY_LINK_ADDRESS); |
| 169 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { | 150 View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { |
| 170 @Override | 151 @Override |
| 171 public View call() { | 152 public View call() { |
| 172 return dialog.createContextMenuPageUi(mActivityTestRule.getActiv
ity(), | 153 return dialog.createContextMenuPageUi(getActivity(), |
| 173 new MockMenuParams("http://google.com"), item, false, it
em.size()); | 154 new MockMenuParams("http://google.com"), item, false, it
em.size()); |
| 174 } | 155 } |
| 175 }); | 156 }); |
| 176 | 157 |
| 177 final TextView headerTextView = (TextView) view.findViewById(R.id.contex
t_header_text); | 158 final TextView headerTextView = (TextView) view.findViewById(R.id.contex
t_header_text); |
| 178 int expectedMaxLines = 1; | 159 int expectedMaxLines = 1; |
| 179 int actualMaxLines = headerTextView.getMaxLines(); | 160 int actualMaxLines = headerTextView.getMaxLines(); |
| 180 Assert.assertEquals("Expected a different number of default maximum line
s.", | 161 assertEquals("Expected a different number of default maximum lines.", ex
pectedMaxLines, |
| 181 expectedMaxLines, actualMaxLines); | 162 actualMaxLines); |
| 182 | 163 |
| 183 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 164 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 184 @Override | 165 @Override |
| 185 public void run() { | 166 public void run() { |
| 186 headerTextView.callOnClick(); | 167 headerTextView.callOnClick(); |
| 187 } | 168 } |
| 188 }); | 169 }); |
| 189 | 170 |
| 190 expectedMaxLines = Integer.MAX_VALUE; | 171 expectedMaxLines = Integer.MAX_VALUE; |
| 191 actualMaxLines = headerTextView.getMaxLines(); | 172 actualMaxLines = headerTextView.getMaxLines(); |
| 192 Assert.assertEquals( | 173 assertEquals("Expected a different number of maximum lines when the head
er is clicked.", |
| 193 "Expected a different number of maximum lines when the header is
clicked.", | |
| 194 expectedMaxLines, actualMaxLines); | 174 expectedMaxLines, actualMaxLines); |
| 195 } | 175 } |
| 196 } | 176 } |
| OLD | NEW |