Commit a0dac7eb authored by Ben Goldberger's avatar Ben Goldberger Committed by Commit Bot

Add integration tests for context menu chip

Bug: 1099982
Change-Id: I0b8e35b04c9b995bac6c49e8c831520ab350c040
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2416928
Commit-Queue: Ben Goldberger <benwgold@google.com>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarSinan Sahin <sinansahin@google.com>
Cr-Commit-Position: refs/heads/master@{#809713}
parent ed43369c
......@@ -127,8 +127,17 @@ class RevampedContextMenuChipController implements View.OnClickListener {
mPopupWindow.show();
}
@VisibleForTesting
// This method should only be used in test files. It is not marked
// @VisibleForTesting to allow the Coordinator to reference it in its
// own testing methods.
AnchoredPopupWindow getCurrentPopupWindowForTesting() {
return mPopupWindow;
}
// This method should only be used in test files. It is not marked
// @VisibleForTesting to allow the Coordinator to reference it in its
// own testing methods.
void clickChipForTesting() {
onClick(mChipView);
}
}
......@@ -36,6 +36,7 @@ import org.chromium.ui.modelutil.MVCListAdapter.ListItem;
import org.chromium.ui.modelutil.MVCListAdapter.ModelList;
import org.chromium.ui.modelutil.ModelListAdapter;
import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.widget.AnchoredPopupWindow;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
......@@ -299,6 +300,25 @@ public class RevampedContextMenuCoordinator implements ContextMenuUi {
activity, PerformanceClass.PERFORMANCE_UNKNOWN, params, profile);
}
@VisibleForTesting
void simulateShoppyImageClassificationForTesting() {
// Don't need to initialize controller because that should be triggered by
// forcing feature flags.
mChipController.handleImageClassification(true);
}
// Public only to allow references from RevampedContextMenuUtils.java
public void clickChipForTesting() {
mChipController.clickChipForTesting(); // IN-TEST
}
// Public only to allow references from RevampedContextMenuUtils.java
public AnchoredPopupWindow getCurrentPopupWindowForTesting() {
// Don't need to initialize controller because that should be triggered by
// forcing feature flags.
return mChipController.getCurrentPopupWindowForTesting(); // IN-TEST
}
public void clickListItemForTesting(int id) {
mListView.performItemClick(null, -1, id);
}
......
......@@ -9,6 +9,7 @@ import static org.chromium.chrome.browser.contextmenu.RevampedContextMenuCoordin
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Looper;
import android.support.test.InstrumentationRegistry;
import android.view.KeyEvent;
......@@ -406,6 +407,66 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv
}, "Activity did not regain focus.");
}
@Test
@MediumTest
@Feature({"Browser"})
@Features.EnableFeatures({ChromeFeatureList.CONTEXT_MENU_GOOGLE_LENS_CHIP})
public void testSelectLensChip() throws Throwable {
// Required to avoid runtime error.
Looper.prepare();
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true);
hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION);
RevampedContextMenuCoordinator menuCoordinator =
RevampedContextMenuUtils.openContextMenu(tab, "testImage");
// Needs to run on UI thread so creation happens on same thread as dismissal.
TestThreadUtils.runOnUiThreadBlocking(
() -> menuCoordinator.simulateShoppyImageClassificationForTesting());
Assert.assertTrue("Chip popoup not showing.",
menuCoordinator.getCurrentPopupWindowForTesting().isShowing());
RevampedContextMenuUtils.selectAlreadyOpenedContextMenuChipWithExpectedIntent(
InstrumentationRegistry.getInstrumentation(), mDownloadTestRule.getActivity(),
menuCoordinator, "testImage", R.id.contextmenu_shop_image_with_google_lens,
"com.google.android.googlequicksearchbox");
Assert.assertEquals("Selection histogram pings not equal to one", 1,
RecordHistogram.getHistogramTotalCountForTesting(
"ContextMenu.SelectedOptionAndroid.Image"));
Assert.assertFalse("Chip popoup still showing.",
menuCoordinator.getCurrentPopupWindowForTesting().isShowing());
}
// Assert that focus is unchanged and that the chip popup does not block the dismissal of the
// context menu.
@Test
@MediumTest
@Features.EnableFeatures({ChromeFeatureList.CONTEXT_MENU_GOOGLE_LENS_CHIP})
public void testDismissContextMenuOnClickLensChipEnabled() throws TimeoutException {
// Required to avoid runtime error.
Looper.prepare();
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
RevampedContextMenuCoordinator menuCoordinator =
RevampedContextMenuUtils.openContextMenu(tab, "testImage");
// Needs to run on UI thread so creation happens on same thread as dismissal.
TestThreadUtils.runOnUiThreadBlocking(
() -> menuCoordinator.simulateShoppyImageClassificationForTesting());
Assert.assertNotNull("Context menu was not properly created", menuCoordinator);
CriteriaHelper.pollUiThread(() -> {
return !mDownloadTestRule.getActivity().hasWindowFocus();
}, "Context menu did not have window focus");
TestTouchUtils.singleClickView(InstrumentationRegistry.getInstrumentation(), tab.getView(),
tab.getView().getWidth() - 5, tab.getView().getHeight() - 5);
CriteriaHelper.pollUiThread(() -> {
return mDownloadTestRule.getActivity().hasWindowFocus();
}, "Activity did not regain focus.");
}
@Test
@MediumTest
public void testDismissContextMenuOnClick() throws TimeoutException {
......
......@@ -115,6 +115,28 @@ public class RevampedContextMenuUtils {
instrumentation, expectedActivity, tab, jsCode, itemId, expectedIntentPackage);
}
/**
* Selects the context menu chip asserting that an intent will be sent with a
* specific package name. Note that this method does not open the context menu.
* @param instrumentation Instrumentation module used for executing test behavior.
* @param expectedActivity The activity to assert for gaining focus after click or null.
* @param menuCoordinator The menu coordinator which manages the context menu.
* @param openerDOMNodeId The DOM node to long press to open the context menu for.
* @param itemId The context menu item ID to select.
* @param expectedIntentPackage If firing an external intent the expected package name of the
* target.
* @throws TimeoutException
*/
public static void selectAlreadyOpenedContextMenuChipWithExpectedIntent(
Instrumentation instrumentation, Activity expectedActivity,
RevampedContextMenuCoordinator menuCoordinator, String openerDOMNodeId,
final int itemId, String expectedIntentPackage) throws TimeoutException {
Assert.assertNotNull("Menu coordinator was not provided.", menuCoordinator);
selectAlreadyOpenedContextMenuChip(
instrumentation, expectedActivity, menuCoordinator, itemId, expectedIntentPackage);
}
/**
* Long presses to open and selects an item from a context menu.
* @param instrumentation Instrumentation module used for executing test behavior.
......@@ -183,4 +205,25 @@ public class RevampedContextMenuUtils {
Intents.release();
}
}
private static void selectAlreadyOpenedContextMenuChip(Instrumentation instrumentation,
final Activity expectedActivity, final RevampedContextMenuCoordinator menuCoordinator,
final int itemId, final String expectedIntentPackage) {
if (expectedIntentPackage != null) {
Intents.init();
}
instrumentation.runOnMainSync(() -> menuCoordinator.clickChipForTesting());
if (expectedActivity != null) {
CriteriaHelper.pollInstrumentationThread(expectedActivity::hasWindowFocus);
}
if (expectedIntentPackage != null) {
// This line must only execute after all test behavior has completed
// or it will intefere with the expected behavior.
intended(IntentMatchers.hasPackage(expectedIntentPackage));
Intents.release();
}
}
}
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