| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.content.ClipData; | 7 import android.content.ClipData; |
| 8 import android.content.ClipboardManager; | 8 import android.content.ClipboardManager; |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.os.Environment; | 10 import android.os.Environment; |
| 11 import android.test.suitebuilder.annotation.LargeTest; | 11 import android.test.suitebuilder.annotation.LargeTest; |
| 12 import android.test.suitebuilder.annotation.MediumTest; | 12 import android.test.suitebuilder.annotation.MediumTest; |
| 13 import android.view.ContextMenu; | 13 import android.view.ContextMenu; |
| 14 import android.view.KeyEvent; | 14 import android.view.KeyEvent; |
| 15 | 15 |
| 16 import org.chromium.base.ThreadUtils; |
| 16 import org.chromium.base.test.util.CommandLineFlags; | 17 import org.chromium.base.test.util.CommandLineFlags; |
| 17 import org.chromium.base.test.util.DisableIf; | 18 import org.chromium.base.test.util.DisableIf; |
| 18 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
| 19 import org.chromium.chrome.R; | 20 import org.chromium.chrome.R; |
| 20 import org.chromium.chrome.browser.ChromeSwitches; | 21 import org.chromium.chrome.browser.ChromeSwitches; |
| 21 import org.chromium.chrome.browser.compositor.layouts.LayoutManager; | 22 import org.chromium.chrome.browser.compositor.layouts.LayoutManager; |
| 22 import org.chromium.chrome.browser.download.DownloadTestBase; | 23 import org.chromium.chrome.browser.download.DownloadTestBase; |
| 23 import org.chromium.chrome.browser.tab.Tab; | 24 import org.chromium.chrome.browser.tab.Tab; |
| 24 import org.chromium.chrome.browser.tabmodel.EmptyTabModelSelectorObserver; | |
| 25 import org.chromium.chrome.browser.tabmodel.TabModel; | 25 import org.chromium.chrome.browser.tabmodel.TabModel; |
| 26 import org.chromium.chrome.test.util.ChromeTabUtils; | |
| 27 import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; | 26 import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; |
| 28 import org.chromium.content.browser.test.util.CallbackHelper; | |
| 29 import org.chromium.content.browser.test.util.Criteria; | 27 import org.chromium.content.browser.test.util.Criteria; |
| 30 import org.chromium.content.browser.test.util.CriteriaHelper; | 28 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 31 import org.chromium.content.browser.test.util.DOMUtils; | 29 import org.chromium.content.browser.test.util.DOMUtils; |
| 30 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| 31 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; |
| 32 import org.chromium.content.browser.test.util.TestTouchUtils; | 32 import org.chromium.content.browser.test.util.TestTouchUtils; |
| 33 import org.chromium.net.test.EmbeddedTestServer; | 33 import org.chromium.net.test.EmbeddedTestServer; |
| 34 | 34 |
| 35 import java.io.IOException; | 35 import java.io.IOException; |
| 36 import java.util.concurrent.Callable; |
| 36 import java.util.concurrent.TimeoutException; | 37 import java.util.concurrent.TimeoutException; |
| 37 import java.util.concurrent.atomic.AtomicReference; | |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * Context menu related tests | 40 * Context menu related tests |
| 41 */ | 41 */ |
| 42 @CommandLineFlags.Add(ChromeSwitches.GOOGLE_BASE_URL + "=http://example.com/") | 42 @CommandLineFlags.Add(ChromeSwitches.GOOGLE_BASE_URL + "=http://example.com/") |
| 43 public class ContextMenuTest extends DownloadTestBase { | 43 public class ContextMenuTest extends DownloadTestBase { |
| 44 private static final String TEST_PATH = | 44 private static final String TEST_PATH = |
| 45 "/chrome/test/data/android/contextmenu/context_menu_test.html"; | 45 "/chrome/test/data/android/contextmenu/context_menu_test.html"; |
| 46 | 46 |
| 47 private EmbeddedTestServer mTestServer; | 47 private EmbeddedTestServer mTestServer; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 Tab tab = getActivity().getActivityTab(); | 104 Tab tab = getActivity().getActivityTab(); |
| 105 ContextMenuUtils.selectContextMenuItem(this, tab, "copyLinkTextComplex", | 105 ContextMenuUtils.selectContextMenuItem(this, tab, "copyLinkTextComplex", |
| 106 R.id.contextmenu_copy_link_text); | 106 R.id.contextmenu_copy_link_text); |
| 107 | 107 |
| 108 assertEquals("Clipboard text was not what was expected", | 108 assertEquals("Clipboard text was not what was expected", |
| 109 "This is pretty extreme \n(newline). ", getClipboardText()); | 109 "This is pretty extreme \n(newline). ", getClipboardText()); |
| 110 } | 110 } |
| 111 | 111 |
| 112 @MediumTest | 112 @MediumTest |
| 113 @Feature({"Browser"}) | 113 @Feature({"Browser"}) |
| 114 @CommandLineFlags.Add(ChromeSwitches.DISABLE_DOCUMENT_MODE) | |
| 115 public void testLongPressOnImage() throws InterruptedException, TimeoutExcep
tion { | 114 public void testLongPressOnImage() throws InterruptedException, TimeoutExcep
tion { |
| 116 checkOpenImageInNewTab( | 115 final Tab tab = getActivity().getActivityTab(); |
| 117 "testImage", "/chrome/test/data/android/contextmenu/test_image.p
ng"); | 116 |
| 117 TestCallbackHelperContainer helper = |
| 118 new TestCallbackHelperContainer(tab.getContentViewCore()); |
| 119 |
| 120 OnPageFinishedHelper callback = helper.getOnPageFinishedHelper(); |
| 121 int callbackCount = callback.getCallCount(); |
| 122 |
| 123 ContextMenuUtils.selectContextMenuItem(this, tab, "testImage", |
| 124 R.id.contextmenu_open_image); |
| 125 |
| 126 callback.waitForCallback(callbackCount); |
| 127 |
| 128 String expectedUrl = mTestServer.getURL( |
| 129 "/chrome/test/data/android/contextmenu/test_image.png"); |
| 130 |
| 131 String actualUrl = ThreadUtils.runOnUiThreadBlockingNoException(new Call
able<String>() { |
| 132 @Override |
| 133 public String call() throws Exception { |
| 134 return tab.getUrl(); |
| 135 } |
| 136 }); |
| 137 |
| 138 assertEquals("Failed to navigate to the image", expectedUrl, actualUrl); |
| 118 } | 139 } |
| 119 | 140 |
| 120 @MediumTest | 141 @MediumTest |
| 121 @Feature({"Browser"}) | 142 @Feature({"Browser"}) |
| 122 @CommandLineFlags.Add(ChromeSwitches.DISABLE_DOCUMENT_MODE) | |
| 123 public void testLongPressOnImageLink() throws InterruptedException, TimeoutE
xception { | 143 public void testLongPressOnImageLink() throws InterruptedException, TimeoutE
xception { |
| 124 checkOpenImageInNewTab( | 144 final Tab tab = getActivity().getActivityTab(); |
| 125 "testImageLink", "/chrome/test/data/android/contextmenu/test_ima
ge.png"); | |
| 126 } | |
| 127 | 145 |
| 128 private void checkOpenImageInNewTab(String domId, String expectedUrl) | 146 TestCallbackHelperContainer helper = |
| 129 throws InterruptedException, TimeoutException { | 147 new TestCallbackHelperContainer(tab.getContentViewCore()); |
| 130 final Tab activityTab = getActivity().getActivityTab(); | |
| 131 | 148 |
| 132 final CallbackHelper newTabCallback = new CallbackHelper(); | 149 OnPageFinishedHelper callback = helper.getOnPageFinishedHelper(); |
| 133 final AtomicReference<Tab> newTab = new AtomicReference<>(); | 150 int callbackCount = callback.getCallCount(); |
| 134 getActivity().getTabModelSelector().addObserver(new EmptyTabModelSelecto
rObserver() { | 151 |
| 152 ContextMenuUtils.selectContextMenuItem(this, tab, "testImage", |
| 153 R.id.contextmenu_open_image); |
| 154 |
| 155 callback.waitForCallback(callbackCount); |
| 156 |
| 157 String actualTitle = ThreadUtils.runOnUiThreadBlockingNoException(new Ca
llable<String>() { |
| 135 @Override | 158 @Override |
| 136 public void onNewTabCreated(Tab tab) { | 159 public String call() throws Exception { |
| 137 super.onNewTabCreated(tab); | 160 return tab.getTitle(); |
| 138 | |
| 139 if (tab.getParentId() != activityTab.getId()) return; | |
| 140 newTab.set(tab); | |
| 141 newTabCallback.notifyCalled(); | |
| 142 | |
| 143 getActivity().getTabModelSelector().removeObserver(this); | |
| 144 } | 161 } |
| 145 }); | 162 }); |
| 146 | 163 |
| 147 int callbackCount = newTabCallback.getCallCount(); | 164 assertTrue("Navigated to the wrong page.", actualTitle.startsWith("test_
image.png")); |
| 148 | |
| 149 ContextMenuUtils.selectContextMenuItem(this, activityTab, domId, | |
| 150 R.id.contextmenu_open_image_in_new_tab); | |
| 151 | |
| 152 try { | |
| 153 newTabCallback.waitForCallback(callbackCount); | |
| 154 } catch (TimeoutException ex) { | |
| 155 fail("New tab never created from context menu press"); | |
| 156 } | |
| 157 | |
| 158 ChromeTabUtils.waitForTabPageLoaded(newTab.get(), mTestServer.getURL(exp
ectedUrl)); | |
| 159 } | 165 } |
| 160 | 166 |
| 161 @MediumTest | 167 @MediumTest |
| 162 @Feature({"Browser"}) | 168 @Feature({"Browser"}) |
| 163 public void testDismissContextMenuOnBack() throws InterruptedException, Time
outException { | 169 public void testDismissContextMenuOnBack() throws InterruptedException, Time
outException { |
| 164 Tab tab = getActivity().getActivityTab(); | 170 Tab tab = getActivity().getActivityTab(); |
| 165 ContextMenu menu = ContextMenuUtils.openContextMenu(this, tab, "testImag
e"); | 171 ContextMenu menu = ContextMenuUtils.openContextMenu(this, tab, "testImag
e"); |
| 166 assertNotNull("Context menu was not properly created", menu); | 172 assertNotNull("Context menu was not properly created", menu); |
| 167 assertFalse("Context menu did not have window focus", getActivity().hasW
indowFocus()); | 173 assertFalse("Context menu did not have window focus", getActivity().hasW
indowFocus()); |
| 168 | 174 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 assertNotNull("Primary clip is null", clipData); | 309 assertNotNull("Primary clip is null", clipData); |
| 304 assertTrue("Primary clip contains no items.", clipData.getItemCount() >
0); | 310 assertTrue("Primary clip contains no items.", clipData.getItemCount() >
0); |
| 305 return clipData.getItemAt(0).getText().toString(); | 311 return clipData.getItemAt(0).getText().toString(); |
| 306 } | 312 } |
| 307 | 313 |
| 308 private void assertStringContains(String subString, String superString) { | 314 private void assertStringContains(String subString, String superString) { |
| 309 assertTrue("String '" + superString + "' does not contain '" + subString
+ "'", | 315 assertTrue("String '" + superString + "' does not contain '" + subString
+ "'", |
| 310 superString.contains(subString)); | 316 superString.contains(subString)); |
| 311 } | 317 } |
| 312 } | 318 } |
| OLD | NEW |