Skip to content

Commit

Permalink
[M90] Fix chip render bug when closing context menu after query respo…
Browse files Browse the repository at this point in the history
…nse is

sent.

(cherry picked from commit d82c6dc)

Bug: 1099982, 1183382
Bug: b/181598874
Change-Id: I9170770d7e2c9a36378bad06a75b6580f133e58c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2737500
Reviewed-by: Yusuf Ozuysal <yusufo@chromium.org>
Reviewed-by: Ben Goldberger <benwgold@google.com>
Commit-Queue: Juan Mojica <juanmojica@google.com>
Cr-Original-Commit-Position: refs/heads/master@{#860028}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2743014
Reviewed-by: Krishna Govind <govind@chromium.org>
Commit-Queue: Krishna Govind <govind@chromium.org>
Cr-Commit-Position: refs/branch-heads/4430@{#245}
Cr-Branched-From: e5ce7dc-refs/heads/master@{#857950}
  • Loading branch information
Juan Mojica authored and Chromium LUCI CQ committed Mar 8, 2021
1 parent 39468b2 commit f12070a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void displayMenuWithChip(final WindowAndroid window, WebContents webContents,
View chipAnchorView = layout.findViewById(R.id.context_menu_chip_anchor_point);
mChipController = new RevampedContextMenuChipController(activity, chipAnchorView);
chipDelegate.getChipRenderParams((chipRenderParams) -> {
if (chipDelegate.isValidChipRenderParams(chipRenderParams)) {
if (chipDelegate.isValidChipRenderParams(chipRenderParams) && mDialog.isShowing()) {
mChipController.showChip(chipRenderParams);
}
});
Expand Down Expand Up @@ -287,6 +287,15 @@ private void dismissDialog() {
mDialog.dismiss();
}

@VisibleForTesting
Callback<ChipRenderParams> getChipRenderParamsCallbackForTesting(ChipDelegate chipDelegate) {
return (chipRenderParams) -> {
if (chipDelegate.isValidChipRenderParams(chipRenderParams) && mDialog.isShowing()) {
mChipController.showChip(chipRenderParams);
}
};
}

@VisibleForTesting
void initializeHeaderCoordinatorForTesting(Activity activity, ContextMenuParams params,
Profile profile, ContextMenuNativeDelegate nativeDelegate) {
Expand Down Expand Up @@ -318,6 +327,15 @@ void simulateTranslateImageClassificationForTesting() {
mChipController.showChip(chipRenderParamsForTesting);
}

@VisibleForTesting
ChipRenderParams simulateImageClassificationForTesting() {
// Don't need to initialize controller because that should be triggered by
// forcing feature flags.
mChipController.setFakeLensQueryResultForTesting(); // IN-TEST
ChipRenderParams chipRenderParamsForTesting = new ChipRenderParams();
return chipRenderParamsForTesting;
}

// Public only to allow references from RevampedContextMenuUtils.java
public void clickChipForTesting() {
mChipController.clickChipForTesting(); // IN-TEST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import org.chromium.base.Callback;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CloseableOnMainThread;
Expand Down Expand Up @@ -90,6 +91,24 @@ public class RevampedContextMenuTest implements DownloadTestRule.CustomMainActiv
private static final String TEST_GIF_IMAGE_FILE_EXTENSION = ".gif";
private static final String TEST_JPG_IMAGE_FILE_EXTENSION = ".jpg";

// Test chip delegate that always returns valid chip render params.
private static final ChipDelegate FAKE_CHIP_DELEGATE = new ChipDelegate() {
@Override
public void getChipRenderParams(Callback<ChipRenderParams> callback) {
// Do nothing.
}

@Override
public void onMenuClosed() {
// Do nothing.
}

@Override
public boolean isValidChipRenderParams(ChipRenderParams chipRenderParams) {
return true;
}
};

private static final String[] TEST_FILES =
new String[] {FILENAME_GIF, FILENAME_PNG, FILENAME_WEBM};

Expand Down Expand Up @@ -454,6 +473,35 @@ public void testSelectLensTranslateChip() throws Throwable {
menuCoordinator.getCurrentPopupWindowForTesting().isShowing());
}

@Test
@MediumTest
@Feature({"Browser"})
@Features.EnableFeatures({ChromeFeatureList.CONTEXT_MENU_TRANSLATE_WITH_GOOGLE_LENS})
public void testLensChipNotShowingAfterMenuDismissed() 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");
// Dismiss context menu.
TestTouchUtils.singleClickView(InstrumentationRegistry.getInstrumentation(), tab.getView(),
tab.getView().getWidth() - 5, tab.getView().getHeight() - 5);
// Needs to run on UI thread so creation happens on same thread as dismissal.
TestThreadUtils.runOnUiThreadBlocking(() -> {
ChipRenderParams chipRenderParams =
menuCoordinator.simulateImageClassificationForTesting();
menuCoordinator.getChipRenderParamsCallbackForTesting(FAKE_CHIP_DELEGATE)
.bind(chipRenderParams)
.run();
Assert.assertNull("Chip popoup was initialized.",
menuCoordinator.getCurrentPopupWindowForTesting());
});
}

// Assert that focus is unchanged and that the chip popup does not block the dismissal of the
// context menu.
@Test
Expand Down

0 comments on commit f12070a

Please sign in to comment.