Commit ea45d4f3 authored by Yu Su's avatar Yu Su Committed by Commit Bot

Add supports to Lens Shopping experiments for google.com

- enable lens shopping experiments variants for google.com
- add unit tests to LensUtilsTest for shopping intent

Bug: 158099013
Change-Id: Ib605afcfe069320a0b2a52d4ea045759a39e3592
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2261093
Commit-Queue: Yu Su <yusuyoutube@google.com>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#782024}
parent 4b3ea7a9
......@@ -1019,7 +1019,7 @@ public class ChromeContextMenuPopulator implements ContextMenuPopulator {
// In Lens Shopping Menu Item experiment, fallback to search image with google lens
// When the url is not in domain allowlist.
if (LensUtils.enableGoogleLensShoppingFeature()) {
if (LensUtils.isInDomainAllowlist(pageUrl)) {
if (LensUtils.isInShoppingAllowlist(pageUrl)) {
// Hide Search With Google Lens menu item when experiment only with Lens Shopping
// menu items.
if (!LensUtils.showBothSearchAndShopImageWithLens()) {
......
......@@ -45,7 +45,9 @@ public class LensUtils {
private static final String USE_SEARCH_BY_IMAGE_TEXT_FEATURE_PARAM_NAME =
"useSearchByImageText";
private static final String LENS_SHOPPING_ALLOWLIST_ENTRIES_FEATURE_PARAM_NAME =
"AllowlistEntries";
"allowlistEntries";
private static final String LENS_SHOPPING_URL_PATTERNS_FEATURE_PARAM_NAME =
"shoppingUrlPatterns";
private static final String LOG_UKM_PARAM_NAME = "logUkm";
private static final String SEND_SRC_PARAM_NAME = "sendSrc";
private static final String SEND_ALT_PARAM_NAME = "sendAlt";
......@@ -54,12 +56,14 @@ public class LensUtils {
private static final String MIN_AGSA_VERSION_NAME_FOR_LENS_CHROME_SHOPPING_INTENT = "11.16";
private static final String LENS_INTENT_TYPE_LENS_CHROME_SHOPPING = "18";
private static final String LENS_SHOPPING_FEATURE_FLAG_VARIANT_NAME = "lensShopVariation";
private static final String LENS_DEFAULT_SHOPPING_URL_PATTERNS =
"^https://www.google.com/shopping/.*|^https://www.google.com/.*tbm=shop.*";
/**
* See function for details.
*/
private static boolean sFakePassableLensEnvironmentForTesting;
private static boolean sFakeImageSrcUrlInAllowlist;
private static boolean sFakeImageUrlInShoppingAllowlistForTesting;
private static String sFakeVariationsForTesting;
/** Supported Lens intent types. */
@IntDef({
......@@ -81,8 +85,9 @@ public class LensUtils {
sFakePassableLensEnvironmentForTesting = shouldFake;
}
public static void setFakeImageSrlUrlInAllowlist(final boolean shouldFake) {
sFakeImageSrcUrlInAllowlist = shouldFake;
@VisibleForTesting
public static void setFakeImageUrlInShoppingAllowlistForTesting(final boolean shouldFake) {
sFakeImageUrlInShoppingAllowlistForTesting = shouldFake;
}
/*
......@@ -362,17 +367,27 @@ public class LensUtils {
* list of Entry names (as strings).
*/
public static String getAllowlistEntries() {
// TODO(yusuyoutube): Create a default allowlist to support QA testing.
return ChromeFeatureList.getFieldTrialParamByFeature(
ChromeFeatureList.CONTEXT_MENU_SHOP_WITH_GOOGLE_LENS,
LENS_SHOPPING_ALLOWLIST_ENTRIES_FEATURE_PARAM_NAME);
}
/**
* Check if the uri domain is in the Lens shopping domain Allowlist.
* Gets the list of shopping url patterns(regex) as String. Format is a "||" separated
* list of regex strings.
*/
public static String getShoppingUrlPatterns() {
return ChromeFeatureList.getFieldTrialParamByFeature(
ChromeFeatureList.CONTEXT_MENU_SHOP_WITH_GOOGLE_LENS,
LENS_SHOPPING_URL_PATTERNS_FEATURE_PARAM_NAME);
}
/**
* Check if the page uri to determine whether the image is shoppable.
* @return true if the image is shoppable.
*/
public static boolean isInDomainAllowlist(final String url) {
if (sFakeImageSrcUrlInAllowlist) {
public static boolean isInShoppingAllowlist(final String url) {
if (sFakeImageUrlInShoppingAllowlistForTesting) {
return true;
}
......@@ -380,19 +395,7 @@ public class LensUtils {
return false;
}
final String allowlistEntries = getAllowlistEntries();
if (allowlistEntries == null || allowlistEntries.isEmpty()) {
return false;
}
final String[] allowlist = allowlistEntries.split(",");
for (final String allowlistEntry : allowlist) {
if (url.contains(allowlistEntry)) {
return true;
}
}
return false;
return hasShoppingUrlPattern(url) || isInDomainAllowList(url);
}
/*
......@@ -414,4 +417,33 @@ public class LensUtils {
return false;
}
/**
* Check if the uri matches any shopping url patterns.
*/
private static boolean hasShoppingUrlPattern(final String url) {
String shoppingUrlPatterns = getShoppingUrlPatterns();
if (shoppingUrlPatterns == null || shoppingUrlPatterns.isEmpty()) {
// Fallback to default shopping url patterns.
shoppingUrlPatterns = LENS_DEFAULT_SHOPPING_URL_PATTERNS;
}
return url.matches(shoppingUrlPatterns);
}
/**
* Check if the uri domain is in the Lens shopping domain Allowlist.
*/
private static boolean isInDomainAllowList(final String url) {
final String allowlistEntries = getAllowlistEntries();
final String[] allowlist = allowlistEntries.split(",");
for (final String allowlistEntry : allowlist) {
if (allowlistEntry.length() > 0 && url.contains(allowlistEntry)) {
return true;
}
}
return false;
}
}
......@@ -176,7 +176,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void
testShopSimilarProductsFiresIntent() throws Throwable {
LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true);
LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true);
hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION);
......@@ -187,6 +187,8 @@ public class ContextMenuTest implements CustomMainActivityStart {
"com.google.android.googlequicksearchbox");
}
@Test
@MediumTest
@Feature({"Browser"})
......@@ -199,7 +201,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void
testShopImageWithGoogleLensFiresIntent() throws Throwable {
LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true);
LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true);
hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION);
......@@ -222,7 +224,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void
testSearchSimilarProductsFiresIntent() throws Throwable {
LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true);
LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true);
hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION);
......@@ -543,7 +545,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void
testContextMenuLensEnabledShopSimilarProducts() throws TimeoutException {
LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true);
LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage");
......@@ -568,7 +570,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void
testContextMenuLensEnabledShopImageWithGoogleLens() throws TimeoutException {
LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true);
LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage");
......@@ -593,7 +595,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void
testContextMenuLensEnabledSeachSimilarProducts() throws TimeoutException {
LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true);
LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage");
......@@ -618,7 +620,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void
testContextMenuLensEnabledSearchAndShopSimilarProducts() throws TimeoutException {
LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true);
LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage");
......
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