[Share] Fix chrome share sheet preview when sharing multiple images

When multiple images are shared via Chrome share sheet, take the first
image as preview.

Bug: 1456559
Change-Id: I33fa1cfaa40276b97aecf19681981af8994d604c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4633909
Reviewed-by: Theresa Sullivan <twellington@chromium.org>
Reviewed-by: Sophey Dong <sophey@chromium.org>
Code-Coverage: Findit <findit-for-me@appspot.gserviceaccount.com>
Commit-Queue: Wenyu Fu <wenyufu@chromium.org>
Auto-Submit: Wenyu Fu <wenyufu@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1161018}
diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/share/ShareHelperUnitTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/share/ShareHelperUnitTest.java
index f28e5904..51d3c4e 100644
--- a/chrome/android/junit/src/org/chromium/chrome/browser/share/ShareHelperUnitTest.java
+++ b/chrome/android/junit/src/org/chromium/chrome/browser/share/ShareHelperUnitTest.java
@@ -49,6 +49,7 @@
 import org.chromium.ui.base.WindowAndroid;
 import org.chromium.url.JUnitTestGURLs;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -278,6 +279,28 @@
                 sharingIntent.getClipData().getItemAt(0).getUri());
     }
 
+    @Test
+    public void shareMultipleImage() {
+        ShareParams params = new ShareParams.Builder(mWindow, "", "")
+                                     .setFileUris(new ArrayList<>(List.of(mImageUri, mImageUri)))
+                                     .setFileContentType("image/png")
+                                     .setBypassFixingDomDistillerUrl(true)
+                                     .build();
+        ShareHelper.shareWithSystemShareSheetUi(params, null, true);
+
+        Intent nextIntent = Shadows.shadowOf(mActivity).peekNextStartedActivity();
+        assertNotNull("Shared intent is null.", nextIntent);
+        assertEquals(
+                "Intent is not a chooser intent.", Intent.ACTION_CHOOSER, nextIntent.getAction());
+
+        // Verify sharing intent has the right image.
+        Intent sharingIntent = nextIntent.getParcelableExtra(Intent.EXTRA_INTENT);
+        assertEquals("Intent is not a SEND_MULTIPLE intent.", Intent.ACTION_SEND_MULTIPLE,
+                sharingIntent.getAction());
+        assertNotNull("Images should be shared as file list.",
+                sharingIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM));
+    }
+
     private void selectComponentFromChooserIntent(Intent chooserIntent, ComponentName componentName)
             throws SendIntentException {
         Intent sendBackIntent = new Intent().putExtra(Intent.EXTRA_CHOSEN_COMPONENT, componentName);
diff --git a/chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/share_sheet/ShareSheetBottomSheetContentTest.java b/chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/share_sheet/ShareSheetBottomSheetContentTest.java
index 03c3f8ff..0f028ff 100644
--- a/chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/share_sheet/ShareSheetBottomSheetContentTest.java
+++ b/chrome/browser/share/android/javatests/src/org/chromium/chrome/browser/share/share_sheet/ShareSheetBottomSheetContentTest.java
@@ -57,6 +57,7 @@
 import org.chromium.url.GURL;
 
 import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Unit tests {@link ShareSheetBottomSheetContent}.
@@ -150,6 +151,30 @@
 
     @Test
     @MediumTest
+    public void createRecyclerViews_multipleImageShare() {
+        String fileContentType = "image/jpeg";
+        ShareSheetBottomSheetContent shareSheetBottomSheetContent =
+                new ShareSheetBottomSheetContent(mActivity, new MockLargeIconBridge(), null,
+                        new ShareParams.Builder(/*window=*/null, /*title=*/"", /*url=*/"")
+                                .setFileUris(new ArrayList<>(List.of(sImageUri, sImageUri)))
+                                .setFileContentType(fileContentType)
+                                .build(),
+                        mFeatureEngagementTracker);
+
+        shareSheetBottomSheetContent.createRecyclerViews(ImmutableList.of(), ImmutableList.of(),
+                ImmutableSet.of(ContentType.IMAGE), fileContentType, DetailedContentType.IMAGE,
+                mShareSheetLinkToggleCoordinator);
+
+        TextView titleView =
+                shareSheetBottomSheetContent.getContentView().findViewById(R.id.title_preview);
+        TextView subtitleView =
+                shareSheetBottomSheetContent.getContentView().findViewById(R.id.subtitle_preview);
+        assertEquals("", titleView.getText());
+        assertEquals("image", subtitleView.getText());
+    }
+
+    @Test
+    @MediumTest
     public void createRecyclerViews_fileShare() {
         String fileContentType = "video/mp4";
         ShareSheetBottomSheetContent shareSheetBottomSheetContent =
diff --git a/components/browser_ui/share/android/java/src/org/chromium/components/browser_ui/share/ShareHelper.java b/components/browser_ui/share/android/java/src/org/chromium/components/browser_ui/share/ShareHelper.java
index 4f019e8..2e7edfc7 100644
--- a/components/browser_ui/share/android/java/src/org/chromium/components/browser_ui/share/ShareHelper.java
+++ b/components/browser_ui/share/android/java/src/org/chromium/components/browser_ui/share/ShareHelper.java
@@ -293,7 +293,7 @@
         intent.putExtra(EXTRA_TASK_ID, params.getWindow().getActivity().get().getTaskId());
 
         Uri imageUri = params.getImageUriToShare();
-        if (imageUri != null) {
+        if (imageUri != null && !isMultipleFileShare) {
             intent.putExtra(Intent.EXTRA_STREAM, imageUri);
             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 
diff --git a/components/browser_ui/share/android/java/src/org/chromium/components/browser_ui/share/ShareParams.java b/components/browser_ui/share/android/java/src/org/chromium/components/browser_ui/share/ShareParams.java
index 5504fd7b..207bec7f 100644
--- a/components/browser_ui/share/android/java/src/org/chromium/components/browser_ui/share/ShareParams.java
+++ b/components/browser_ui/share/android/java/src/org/chromium/components/browser_ui/share/ShareParams.java
@@ -248,7 +248,9 @@
         if (getSingleImageUri() != null) {
             return getSingleImageUri();
         }
-        if (getFileUris() != null && getFileUris().size() == 1 && getFileContentType() != null
+
+        // For cases where multiple images are shared, return the first image.
+        if (getFileUris() != null && getFileUris().size() > 0 && getFileContentType() != null
                 && getFileContentType().startsWith("image")) {
             return getFileUris().get(0);
         }