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; ...@@ -6,6 +6,8 @@ package org.chromium.base.test.util;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import org.chromium.base.StrictModeContext;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
...@@ -57,4 +59,40 @@ public final class CloseableOnMainThread implements Closeable { ...@@ -57,4 +59,40 @@ public final class CloseableOnMainThread implements Closeable {
throw new IOException(mException.getCause()); 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; ...@@ -22,7 +22,6 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.StrictModeContext;
import org.chromium.base.test.util.CallbackHelper; import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CloseableOnMainThread; import org.chromium.base.test.util.CloseableOnMainThread;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
...@@ -121,8 +120,7 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -121,8 +120,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
// Allow DiskWrites temporarily in main thread to avoid // Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment. // violation during copying under emulator environment.
try (CloseableOnMainThread c = new CloseableOnMainThread( try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
() -> { return StrictModeContext.allowDiskWrites(); })) {
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(), ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testLink", mDownloadTestRule.getActivity(), tab, "testLink",
R.id.contextmenu_copy_link_address); R.id.contextmenu_copy_link_address);
...@@ -138,8 +136,7 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -138,8 +136,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
// Allow DiskWrites temporarily in main thread to avoid // Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment. // violation during copying under emulator environment.
try (CloseableOnMainThread c = new CloseableOnMainThread( try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
() -> { return StrictModeContext.allowDiskWrites(); })) {
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(), ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testImageLink", mDownloadTestRule.getActivity(), tab, "testImageLink",
R.id.contextmenu_copy_link_address); R.id.contextmenu_copy_link_address);
...@@ -307,8 +304,7 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -307,8 +304,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
// Allow DiskWrites temporarily in main thread to avoid // Allow DiskWrites temporarily in main thread to avoid
// violation during copying under emulator environment. // violation during copying under emulator environment.
try (CloseableOnMainThread c = new CloseableOnMainThread( try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
() -> { return StrictModeContext.allowDiskWrites(); })) {
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(), ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testEmail", R.id.contextmenu_copy); mDownloadTestRule.getActivity(), tab, "testEmail", R.id.contextmenu_copy);
} }
...@@ -326,8 +322,8 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -326,8 +322,8 @@ public class ContextMenuTest implements CustomMainActivityStart {
// Allow all thread policies temporarily in main thread to avoid // Allow all thread policies temporarily in main thread to avoid
// DiskWrite and UnBufferedIo violations during copying under // DiskWrite and UnBufferedIo violations during copying under
// emulator environment. // emulator environment.
try (CloseableOnMainThread c = new CloseableOnMainThread( try (CloseableOnMainThread ignored =
() -> { return StrictModeContext.allowAllThreadPolicies(); })) { CloseableOnMainThread.StrictMode.allowAllThreadPolicies()) {
ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(), ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(),
mDownloadTestRule.getActivity(), tab, "testTel", R.id.contextmenu_copy); mDownloadTestRule.getActivity(), tab, "testTel", R.id.contextmenu_copy);
} }
......
...@@ -20,6 +20,7 @@ import org.junit.Test; ...@@ -20,6 +20,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.test.util.CallbackHelper; import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CloseableOnMainThread;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeSwitches; import org.chromium.chrome.browser.ChromeSwitches;
...@@ -95,9 +96,13 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv ...@@ -95,9 +96,13 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv
@MediumTest @MediumTest
public void testCopyLinkURL() throws Throwable { public void testCopyLinkURL() throws Throwable {
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
RevampedContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(), // Allow DiskWrites temporarily in main thread to avoid
mDownloadTestRule.getActivity(), tab, "testLink", // violation during copying under emulator environment.
R.id.contextmenu_copy_link_address); 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()); assertStringContains("test_link.html", getClipboardText());
} }
...@@ -193,8 +198,15 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv ...@@ -193,8 +198,15 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv
@MediumTest @MediumTest
public void testCopyEmailAddress() throws Throwable { public void testCopyEmailAddress() throws Throwable {
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
RevampedContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(), // Allow all thread policies temporarily in main thread to avoid
mDownloadTestRule.getActivity(), tab, "testEmail", R.id.contextmenu_copy); // 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", Assert.assertEquals("Copied email address is not correct",
"someone1@example.com,someone2@example.com", getClipboardText()); "someone1@example.com,someone2@example.com", getClipboardText());
...@@ -204,8 +216,13 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv ...@@ -204,8 +216,13 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv
@MediumTest @MediumTest
public void testCopyTelNumber() throws Throwable { public void testCopyTelNumber() throws Throwable {
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
RevampedContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstrumentation(), // Allow DiskWrites temporarily in main thread to avoid
mDownloadTestRule.getActivity(), tab, "testTel", R.id.contextmenu_copy); // 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()); Assert.assertEquals("Copied tel number is not correct", "10000000000", getClipboardText());
} }
......
...@@ -34,6 +34,7 @@ import org.junit.Test; ...@@ -34,6 +34,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.task.PostTask; 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.CommandLineFlags;
import org.chromium.base.test.util.Restriction; import org.chromium.base.test.util.Restriction;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
...@@ -484,39 +485,45 @@ public class HistoryActivityTest { ...@@ -484,39 +485,45 @@ public class HistoryActivityTest {
@Test @Test
@SmallTest @SmallTest
public void testCopyLink() throws Exception { public void testCopyLink() throws Exception {
final ClipboardManager clipboardManager = TestThreadUtils.runOnUiThreadBlocking(() -> { // Allow DiskWrites temporarily in main thread to avoid
ClipboardManager manager = // violation during copying under emulator environment.
(ClipboardManager) mActivityTestRule.getActivity().getSystemService( try (CloseableOnMainThread ignored = CloseableOnMainThread.StrictMode.allowDiskWrites()) {
Context.CLIPBOARD_SERVICE); final ClipboardManager clipboardManager = TestThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertNotNull(manager); ClipboardManager manager =
manager.setPrimaryClip(ClipData.newPlainText(null, "")); (ClipboardManager) mActivityTestRule.getActivity().getSystemService(
return manager; Context.CLIPBOARD_SERVICE);
}); Assert.assertNotNull(manager);
// Clear the clipboard to make sure we start with a clean state. manager.setPrimaryClip(ClipData.newPlainText(null, ""));
return manager;
final HistoryManagerToolbar toolbar = mHistoryManager.getToolbarForTests(); });
// Clear the clipboard to make sure we start with a clean state.
// Check that the copy link item is visible when one item is selected.
toggleItemSelection(2); final HistoryManagerToolbar toolbar = mHistoryManager.getToolbarForTests();
Assert.assertTrue(toolbar.getItemById(R.id.selection_mode_copy_link).isVisible());
// Check that the copy link item is visible when one item is selected.
// Check that link is copied to the clipboard. toggleItemSelection(2);
TestThreadUtils.runOnUiThreadBlocking( Assert.assertTrue(toolbar.getItemById(R.id.selection_mode_copy_link).isVisible());
() -> Assert.assertTrue(mHistoryManager.getToolbarForTests()
.getMenu() // Check that link is copied to the clipboard.
.performIdentifierAction( TestThreadUtils.runOnUiThreadBlocking(
R.id.selection_mode_copy_link, 0))); ()
CriteriaHelper.pollUiThread(new Criteria() { -> Assert.assertTrue(
@Override mHistoryManager.getToolbarForTests()
public boolean isSatisfied() { .getMenu()
return TextUtils.equals(mItem1.getUrl(), clipboardManager.getText()); .performIdentifierAction(
} R.id.selection_mode_copy_link, 0)));
}); CriteriaHelper.pollUiThread(new Criteria() {
@Override
// Check that the copy link item is not visible when more than one item is selected. public boolean isSatisfied() {
toggleItemSelection(2); return TextUtils.equals(mItem1.getUrl(), clipboardManager.getText());
toggleItemSelection(3); }
Assert.assertFalse(toolbar.getItemById(R.id.selection_mode_copy_link).isVisible()); });
// 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 // TODO(yolandyan): rewrite this with espresso
......
...@@ -20,6 +20,7 @@ import org.junit.runner.RunWith; ...@@ -20,6 +20,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.params.ParameterizedCommandLineFlags; import org.chromium.base.test.params.ParameterizedCommandLineFlags;
import org.chromium.base.test.params.ParameterizedCommandLineFlags.Switches; 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.CommandLineFlags;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
...@@ -113,10 +114,14 @@ public class UrlBarIntegrationTest { ...@@ -113,10 +114,14 @@ public class UrlBarIntegrationTest {
@SmallTest @SmallTest
@Feature({"Omnibox"}) @Feature({"Omnibox"})
@RetryOnFailure @RetryOnFailure
public void testCopyHuge() { public void testCopyHuge() throws Throwable {
mActivityTestRule.startMainActivityWithURL(HUGE_URL); mActivityTestRule.startMainActivityWithURL(HUGE_URL);
OmniboxTestUtils.toggleUrlBarFocus(getUrlBar(), true); 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 @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