Commit 9b5990e4 authored by Haiyang Pan's avatar Haiyang Pan Committed by Commit Bot

android: adding helpers in CloseableOnMainThread for setting strictmode on main thread

Also fixing the remaining copy related testing that are failing due to
strict mode

Bug: 1046062
Change-Id: I5a07db2b9ee16fb50094a968eab80f34d9b5addd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2028928Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarJohn Budorick <jbudorick@chromium.org>
Commit-Queue: Haiyang Pan <hypan@google.com>
Cr-Commit-Position: refs/heads/master@{#738663}
parent d591a05c
......@@ -6,6 +6,8 @@ package org.chromium.base.test.util;
import android.support.test.InstrumentationRegistry;
import org.chromium.base.StrictModeContext;
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.Callable;
......@@ -57,4 +59,40 @@ public final class CloseableOnMainThread implements Closeable {
throw new IOException(mException.getCause());
}
}
/**
* Enables try-with-resources compatible StrictMode violation whitelisting on android's main
* thread.
*
* Prefer "ignored" as the variable name to appease Android Studio's "Unused symbol" inspection.
*
* Example:
* <pre>
* try (CloseableOnMainThread ignored =
* CloseableOnMainThread.StrictMode.allowDiskWrites()) {
* return Example.doThingThatRequiresDiskWrites();
* }
* </pre>
*
*/
public static class StrictMode {
private StrictMode() {}
/**
* Convenience method for disabling all thread-level StrictMode checks with
* try-with-resources. Includes everything listed here:
* https://developer.android.com/reference/android/os/StrictMode.ThreadPolicy.Builder.html
*/
public static CloseableOnMainThread allowAllThreadPolicies() throws Exception {
return new CloseableOnMainThread(
() -> { return StrictModeContext.allowAllThreadPolicies(); });
}
/**
* Convenience method for disabling StrictMode for disk-writes with try-with-resources.
*/
public static CloseableOnMainThread allowDiskWrites() throws Exception {
return new CloseableOnMainThread(() -> { return StrictModeContext.allowDiskWrites(); });
}
}
}
......@@ -22,7 +22,6 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.StrictModeContext;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CloseableOnMainThread;
import org.chromium.base.test.util.CommandLineFlags;
......@@ -121,8 +120,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
// Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment.
try (CloseableOnMainThread c = new CloseableOnMainThread(
() -> { return StrictModeContext.allowDiskWrites(); })) {
try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testLink",
R.id.contextmenu_copy_link_address);
......@@ -138,8 +136,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
// Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment.
try (CloseableOnMainThread c = new CloseableOnMainThread(
() -> { return StrictModeContext.allowDiskWrites(); })) {
try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testImageLink",
R.id.contextmenu_copy_link_address);
......@@ -307,8 +304,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
// Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment.
try (CloseableOnMainThread c = new CloseableOnMainThread(
() -> { return StrictModeContext.allowDiskWrites(); })) {
try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testEmail", R.id.contextmenu_copy);
}
......@@ -326,8 +322,8 @@ public class ContextMenuTest implements CustomMainActivityStart {
// Allow all thread policies temporarily in main thread to avoid
// DiskWrite and UnBufferedIo violations during copying under
// emulator environment.
try (CloseableOnMainThread c = new CloseableOnMainThread(
() -> { return StrictModeContext.allowAllThreadPolicies(); })) {
try (CloseableOnMainThread ignored =
CloseableOnMainThread.StrictMode.allowAllThreadPolicies()) {
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testTel", R.id.contextmenu_copy);
}
......
......@@ -20,6 +20,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CloseableOnMainThread;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeSwitches;
......@@ -95,9 +96,13 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv
@MediumTest
public void testCopyLinkURL() throws Throwable {
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
RevampedContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testLink",
R.id.contextmenu_copy_link_address);
// Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment.
try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
RevampedContextMenuUtils.selectContextMenuItem(
InstrumentationRegistry.getInstrumentation(), mDownloadTestRule.getActivity(),
tab, "testLink", R.id.contextmenu_copy_link_address);
}
assertStringContains("test_link.html", getClipboardText());
}
......@@ -193,8 +198,15 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv
@MediumTest
public void testCopyEmailAddress() throws Throwable {
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
RevampedContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testEmail", R.id.contextmenu_copy);
// Allow all thread policies temporarily in main thread to avoid
// DiskWrite and UnBufferedIo violations during copying under
// emulator environment.
try (CloseableOnMainThread ignored =
CloseableOnMainThread.StrictMode.allowAllThreadPolicies()) {
RevampedContextMenuUtils.selectContextMenuItem(
InstrumentationRegistry.getInstrumentation(), mDownloadTestRule.getActivity(),
tab, "testEmail", R.id.contextmenu_copy);
}
Assert.assertEquals("Copied email address is not correct",
"someone1@example.com,someone2@example.com", getClipboardText());
......@@ -204,8 +216,13 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv
@MediumTest
public void testCopyTelNumber() throws Throwable {
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
RevampedContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testTel", R.id.contextmenu_copy);
// Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment.
try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
RevampedContextMenuUtils.selectContextMenuItem(
InstrumentationRegistry.getInstrumentation(), mDownloadTestRule.getActivity(),
tab, "testTel", R.id.contextmenu_copy);
}
Assert.assertEquals("Copied tel number is not correct", "10000000000", getClipboardText());
}
......
......@@ -34,6 +34,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.task.PostTask;
import org.chromium.base.test.util.CloseableOnMainThread;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Restriction;
import org.chromium.base.test.util.RetryOnFailure;
......@@ -484,39 +485,45 @@ public class HistoryActivityTest {
@Test
@SmallTest
public void testCopyLink() throws Exception {
final ClipboardManager clipboardManager = TestThreadUtils.runOnUiThreadBlocking(() -> {
ClipboardManager manager =
(ClipboardManager) mActivityTestRule.getActivity().getSystemService(
Context.CLIPBOARD_SERVICE);
Assert.assertNotNull(manager);
manager.setPrimaryClip(ClipData.newPlainText(null, ""));
return manager;
});
// Clear the clipboard to make sure we start with a clean state.
final HistoryManagerToolbar toolbar = mHistoryManager.getToolbarForTests();
// Check that the copy link item is visible when one item is selected.
toggleItemSelection(2);
Assert.assertTrue(toolbar.getItemById(R.id.selection_mode_copy_link).isVisible());
// Check that link is copied to the clipboard.
TestThreadUtils.runOnUiThreadBlocking(
() -> Assert.assertTrue(mHistoryManager.getToolbarForTests()
.getMenu()
.performIdentifierAction(
R.id.selection_mode_copy_link, 0)));
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
return TextUtils.equals(mItem1.getUrl(), clipboardManager.getText());
}
});
// Check that the copy link item is not visible when more than one item is selected.
toggleItemSelection(2);
toggleItemSelection(3);
Assert.assertFalse(toolbar.getItemById(R.id.selection_mode_copy_link).isVisible());
// Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment.
try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
final ClipboardManager clipboardManager = TestThreadUtils.runOnUiThreadBlocking(() -> {
ClipboardManager manager =
(ClipboardManager) mActivityTestRule.getActivity().getSystemService(
Context.CLIPBOARD_SERVICE);
Assert.assertNotNull(manager);
manager.setPrimaryClip(ClipData.newPlainText(null, ""));
return manager;
});
// Clear the clipboard to make sure we start with a clean state.
final HistoryManagerToolbar toolbar = mHistoryManager.getToolbarForTests();
// Check that the copy link item is visible when one item is selected.
toggleItemSelection(2);
Assert.assertTrue(toolbar.getItemById(R.id.selection_mode_copy_link).isVisible());
// Check that link is copied to the clipboard.
TestThreadUtils.runOnUiThreadBlocking(
()
-> Assert.assertTrue(
mHistoryManager.getToolbarForTests()
.getMenu()
.performIdentifierAction(
R.id.selection_mode_copy_link, 0)));
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
return TextUtils.equals(mItem1.getUrl(), clipboardManager.getText());
}
});
// Check that the copy link item is not visible when more than one item is selected.
toggleItemSelection(2);
toggleItemSelection(3);
Assert.assertFalse(toolbar.getItemById(R.id.selection_mode_copy_link).isVisible());
}
}
// TODO(yolandyan): rewrite this with espresso
......
......@@ -20,6 +20,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.params.ParameterizedCommandLineFlags;
import org.chromium.base.test.params.ParameterizedCommandLineFlags.Switches;
import org.chromium.base.test.util.CloseableOnMainThread;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure;
......@@ -113,10 +114,14 @@ public class UrlBarIntegrationTest {
@SmallTest
@Feature({"Omnibox"})
@RetryOnFailure
public void testCopyHuge() {
public void testCopyHuge() throws Throwable {
mActivityTestRule.startMainActivityWithURL(HUGE_URL);
OmniboxTestUtils.toggleUrlBarFocus(getUrlBar(), true);
Assert.assertEquals(HUGE_URL, copyUrlToClipboard(android.R.id.copy));
// Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment.
try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
Assert.assertEquals(HUGE_URL, copyUrlToClipboard(android.R.id.copy));
}
}
@Test
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment