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 { ...@@ -1019,7 +1019,7 @@ public class ChromeContextMenuPopulator implements ContextMenuPopulator {
// In Lens Shopping Menu Item experiment, fallback to search image with google lens // In Lens Shopping Menu Item experiment, fallback to search image with google lens
// When the url is not in domain allowlist. // When the url is not in domain allowlist.
if (LensUtils.enableGoogleLensShoppingFeature()) { if (LensUtils.enableGoogleLensShoppingFeature()) {
if (LensUtils.isInDomainAllowlist(pageUrl)) { if (LensUtils.isInShoppingAllowlist(pageUrl)) {
// Hide Search With Google Lens menu item when experiment only with Lens Shopping // Hide Search With Google Lens menu item when experiment only with Lens Shopping
// menu items. // menu items.
if (!LensUtils.showBothSearchAndShopImageWithLens()) { if (!LensUtils.showBothSearchAndShopImageWithLens()) {
......
...@@ -45,7 +45,9 @@ public class LensUtils { ...@@ -45,7 +45,9 @@ public class LensUtils {
private static final String USE_SEARCH_BY_IMAGE_TEXT_FEATURE_PARAM_NAME = private static final String USE_SEARCH_BY_IMAGE_TEXT_FEATURE_PARAM_NAME =
"useSearchByImageText"; "useSearchByImageText";
private static final String LENS_SHOPPING_ALLOWLIST_ENTRIES_FEATURE_PARAM_NAME = 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 LOG_UKM_PARAM_NAME = "logUkm";
private static final String SEND_SRC_PARAM_NAME = "sendSrc"; private static final String SEND_SRC_PARAM_NAME = "sendSrc";
private static final String SEND_ALT_PARAM_NAME = "sendAlt"; private static final String SEND_ALT_PARAM_NAME = "sendAlt";
...@@ -54,12 +56,14 @@ public class LensUtils { ...@@ -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 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_INTENT_TYPE_LENS_CHROME_SHOPPING = "18";
private static final String LENS_SHOPPING_FEATURE_FLAG_VARIANT_NAME = "lensShopVariation"; 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. * See function for details.
*/ */
private static boolean sFakePassableLensEnvironmentForTesting; private static boolean sFakePassableLensEnvironmentForTesting;
private static boolean sFakeImageSrcUrlInAllowlist; private static boolean sFakeImageUrlInShoppingAllowlistForTesting;
private static String sFakeVariationsForTesting; private static String sFakeVariationsForTesting;
/** Supported Lens intent types. */ /** Supported Lens intent types. */
@IntDef({ @IntDef({
...@@ -81,8 +85,9 @@ public class LensUtils { ...@@ -81,8 +85,9 @@ public class LensUtils {
sFakePassableLensEnvironmentForTesting = shouldFake; sFakePassableLensEnvironmentForTesting = shouldFake;
} }
public static void setFakeImageSrlUrlInAllowlist(final boolean shouldFake) { @VisibleForTesting
sFakeImageSrcUrlInAllowlist = shouldFake; public static void setFakeImageUrlInShoppingAllowlistForTesting(final boolean shouldFake) {
sFakeImageUrlInShoppingAllowlistForTesting = shouldFake;
} }
/* /*
...@@ -362,17 +367,27 @@ public class LensUtils { ...@@ -362,17 +367,27 @@ public class LensUtils {
* list of Entry names (as strings). * list of Entry names (as strings).
*/ */
public static String getAllowlistEntries() { public static String getAllowlistEntries() {
// TODO(yusuyoutube): Create a default allowlist to support QA testing.
return ChromeFeatureList.getFieldTrialParamByFeature( return ChromeFeatureList.getFieldTrialParamByFeature(
ChromeFeatureList.CONTEXT_MENU_SHOP_WITH_GOOGLE_LENS, ChromeFeatureList.CONTEXT_MENU_SHOP_WITH_GOOGLE_LENS,
LENS_SHOPPING_ALLOWLIST_ENTRIES_FEATURE_PARAM_NAME); 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) { public static boolean isInShoppingAllowlist(final String url) {
if (sFakeImageSrcUrlInAllowlist) { if (sFakeImageUrlInShoppingAllowlistForTesting) {
return true; return true;
} }
...@@ -380,19 +395,7 @@ public class LensUtils { ...@@ -380,19 +395,7 @@ public class LensUtils {
return false; return false;
} }
final String allowlistEntries = getAllowlistEntries(); return hasShoppingUrlPattern(url) || isInDomainAllowList(url);
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;
} }
/* /*
...@@ -414,4 +417,33 @@ public class LensUtils { ...@@ -414,4 +417,33 @@ public class LensUtils {
return false; 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 { ...@@ -176,7 +176,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void public void
testShopSimilarProductsFiresIntent() throws Throwable { testShopSimilarProductsFiresIntent() throws Throwable {
LensUtils.setFakePassableLensEnvironmentForTesting(true); LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true); LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true); ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true);
hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION); hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION);
...@@ -187,6 +187,8 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -187,6 +187,8 @@ public class ContextMenuTest implements CustomMainActivityStart {
"com.google.android.googlequicksearchbox"); "com.google.android.googlequicksearchbox");
} }
@Test @Test
@MediumTest @MediumTest
@Feature({"Browser"}) @Feature({"Browser"})
...@@ -199,7 +201,7 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -199,7 +201,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void public void
testShopImageWithGoogleLensFiresIntent() throws Throwable { testShopImageWithGoogleLensFiresIntent() throws Throwable {
LensUtils.setFakePassableLensEnvironmentForTesting(true); LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true); LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true); ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true);
hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION); hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION);
...@@ -222,7 +224,7 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -222,7 +224,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void public void
testSearchSimilarProductsFiresIntent() throws Throwable { testSearchSimilarProductsFiresIntent() throws Throwable {
LensUtils.setFakePassableLensEnvironmentForTesting(true); LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true); LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true); ShareHelper.setIgnoreActivityNotFoundExceptionForTesting(true);
hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION); hardcodeTestImageForSharing(TEST_JPG_IMAGE_FILE_EXTENSION);
...@@ -543,7 +545,7 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -543,7 +545,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void public void
testContextMenuLensEnabledShopSimilarProducts() throws TimeoutException { testContextMenuLensEnabledShopSimilarProducts() throws TimeoutException {
LensUtils.setFakePassableLensEnvironmentForTesting(true); LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true); LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage"); ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage");
...@@ -568,7 +570,7 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -568,7 +570,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void public void
testContextMenuLensEnabledShopImageWithGoogleLens() throws TimeoutException { testContextMenuLensEnabledShopImageWithGoogleLens() throws TimeoutException {
LensUtils.setFakePassableLensEnvironmentForTesting(true); LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true); LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage"); ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage");
...@@ -593,7 +595,7 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -593,7 +595,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void public void
testContextMenuLensEnabledSeachSimilarProducts() throws TimeoutException { testContextMenuLensEnabledSeachSimilarProducts() throws TimeoutException {
LensUtils.setFakePassableLensEnvironmentForTesting(true); LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true); LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage"); ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage");
...@@ -618,7 +620,7 @@ public class ContextMenuTest implements CustomMainActivityStart { ...@@ -618,7 +620,7 @@ public class ContextMenuTest implements CustomMainActivityStart {
public void public void
testContextMenuLensEnabledSearchAndShopSimilarProducts() throws TimeoutException { testContextMenuLensEnabledSearchAndShopSimilarProducts() throws TimeoutException {
LensUtils.setFakePassableLensEnvironmentForTesting(true); LensUtils.setFakePassableLensEnvironmentForTesting(true);
LensUtils.setFakeImageSrlUrlInAllowlist(true); LensUtils.setFakeImageUrlInShoppingAllowlistForTesting(true);
Tab tab = mDownloadTestRule.getActivity().getActivityTab(); Tab tab = mDownloadTestRule.getActivity().getActivityTab();
ContextMenu menu = ContextMenuUtils.openContextMenu(tab, "testImage"); 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