Commit 9593ee49 authored by Tanya Gupta's avatar Tanya Gupta Committed by Commit Bot

[SharingHub] Changed the way firstPartyOptions are created.

They will not be created on demand. This simplifies the logic to decide
which options are relevant given the current state of the Chrome app.

Change-Id: I9b3ebf3030cfaa48088d7c1d0082d659b47f28cd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2485838
Commit-Queue: Tanya Gupta <tgupta@chromium.org>
Reviewed-by: default avatarKyle Milka <kmilka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821914}
parent fae7adc6
...@@ -44,132 +44,142 @@ import java.util.Arrays; ...@@ -44,132 +44,142 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* Provides {@code PropertyModel}s of Chrome-provided sharing options. * Provides {@code PropertyModel}s of Chrome-provided sharing options.
*/ */
class ChromeProvidedSharingOptionsProvider { class ChromeProvidedSharingOptionsProvider {
private final Activity mActivity; private Activity mActivity;
private final Supplier<Tab> mTabProvider; private final Supplier<Tab> mTabProvider;
private final BottomSheetController mBottomSheetController; private final BottomSheetController mBottomSheetController;
private final ShareSheetBottomSheetContent mBottomSheetContent; private final ShareSheetBottomSheetContent mBottomSheetContent;
private final ShareParams mShareParams; private ShareParams mShareParams;
private final Callback<Tab> mPrintTabCallback; private ChromeShareExtras mChromeShareExtras;
private final SettingsLauncher mSettingsLauncher; private Callback<Tab> mPrintTabCallback;
private final boolean mIsSyncEnabled; private SettingsLauncher mSettingsLauncher;
private final long mShareStartTime; private boolean mIsSyncEnabled;
private final List<FirstPartyOption> mOrderedFirstPartyOptions; private long mShareStartTime;
private final ChromeOptionShareCallback mChromeOptionShareCallback; private ChromeOptionShareCallback mChromeOptionShareCallback;
private ScreenshotCoordinator mScreenshotCoordinator; private ScreenshotCoordinator mScreenshotCoordinator;
private final String mUrl; private String mUrl;
private boolean mIsMultiWindow;
private Collection<Integer> mContentTypes;
/** /**
* Constructs a new {@link ChromeProvidedSharingOptionsProvider}. * Constructs a new {@link ChromeProvidedSharingOptionsProvider}.
* *
* @param activity The current {@link Activity}.
* @param tabProvider Supplier for the current activity tab. * @param tabProvider Supplier for the current activity tab.
* @param bottomSheetController The {@link BottomSheetController} for the current activity. * @param bottomSheetController The {@link BottomSheetController} for the current activity.
* @param bottomSheetContent The {@link ShareSheetBottomSheetContent} for the current * @param bottomSheetContent The {@link ShareSheetBottomSheetContent} for the current activity.
* activity.
*/
ChromeProvidedSharingOptionsProvider(Supplier<Tab> tabProvider,
BottomSheetController bottomSheetController,
ShareSheetBottomSheetContent bottomSheetContent) {
mTabProvider = tabProvider;
mBottomSheetController = bottomSheetController;
mBottomSheetContent = bottomSheetContent;
}
/**
* Sets params related to the share itself.
*
* @param shareParams The {@link ShareParams} for the current share. * @param shareParams The {@link ShareParams} for the current share.
* @param chromeShareExtras The {@link ChromeShareExtras} for the current share. * @param chromeShareExtras The {@link ChromeShareExtras} for the current share.
* @param printTab A {@link Callback} that will print a given Tab.
* @param shareStartTime The start time of the current share. * @param shareStartTime The start time of the current share.
* @param chromeOptionShareCallback A ChromeOptionShareCallback that can be used by * @param chromeOptionShareCallback A ChromeOptionShareCallback that can be used by
* Chrome-provided sharing options. * Chrome-provided sharing options.
* @param contentTypes The contentTypes the share is comprised of.
*/ */
ChromeProvidedSharingOptionsProvider(Activity activity, Supplier<Tab> tabProvider, void setShareRelatedParams(ShareParams shareParams, ChromeShareExtras chromeShareExtras,
BottomSheetController bottomSheetController, long shareStartTime, ChromeOptionShareCallback chromeOptionShareCallback,
ShareSheetBottomSheetContent bottomSheetContent, ShareParams shareParams, Collection<Integer> contentTypes) {
ChromeShareExtras chromeShareExtras, Callback<Tab> printTab,
SettingsLauncher settingsLauncher, boolean isSyncEnabled, long shareStartTime,
ChromeOptionShareCallback chromeOptionShareCallback) {
mActivity = activity;
mTabProvider = tabProvider;
mBottomSheetController = bottomSheetController;
mBottomSheetContent = bottomSheetContent;
mShareParams = shareParams; mShareParams = shareParams;
mShareStartTime = shareStartTime;
mChromeShareExtras = chromeShareExtras;
mActivity = mShareParams.getWindow().getActivity().get();
mChromeOptionShareCallback = chromeOptionShareCallback;
mContentTypes = contentTypes;
}
/**
* Set params needed by the individual sharing features.
*
* @param printTab A {@link Callback} that will print a given Tab.
* @param settingsLauncher Launches Chrome settings.
* @param isSyncEnabled Whether the user has enabled sync.
*/
void setFeatureSpecificParams(
Callback<Tab> printTab, SettingsLauncher settingsLauncher, boolean isSyncEnabled) {
mPrintTabCallback = printTab; mPrintTabCallback = printTab;
mSettingsLauncher = settingsLauncher; mSettingsLauncher = settingsLauncher;
mIsSyncEnabled = isSyncEnabled; mIsSyncEnabled = isSyncEnabled;
mShareStartTime = shareStartTime;
mOrderedFirstPartyOptions = new ArrayList<>();
initializeFirstPartyOptionsInOrder();
mChromeOptionShareCallback = chromeOptionShareCallback;
mUrl = getUrlToShare(shareParams, chromeShareExtras,
mTabProvider.get().isInitialized() ? mTabProvider.get().getUrl().getSpec() : "");
} }
/** /**
* Encapsulates a {@link PropertyModel} and the {@link ContentType}s it should be shown for. * Set whether Chrome is in multiWindow mode.
*/ */
private static class FirstPartyOption { void setIsMultiWindow(boolean isMultiWindow) {
final Collection<Integer> mContentTypes; mIsMultiWindow = isMultiWindow;
final Collection<Integer> mContentTypesToDisableFor; }
final PropertyModel mPropertyModel;
final boolean mDisableForMultiWindow;
/** /**
* Should only be used when the default property model constructed in the builder does not * Calculates the property models to display for the Share.
* fit the feature's needs. This should be rare. *
* <p>New share features should add a call to this function. The order of the functions
* determine the order in which they appear in the share sheet. Each feature is gated on the
* supported {@link ContentType}(s).
* *
* @param model Property model for the first party option. * @return the set of ChromeProvidedSharingOptions for the share.
* @param contentTypes Content types to trigger for.
* @param contentTypesToDisableFor Content types to disable for.
* @param disableForMultiWindow If the feature should be disabled if in multi-window mode.
*/ */
FirstPartyOption(PropertyModel model, Collection<Integer> contentTypes, List<PropertyModel> calculatePropertyModels() {
Collection<Integer> contentTypesToDisableFor, boolean disableForMultiWindow) { mUrl = getUrlToShare(mShareParams, mChromeShareExtras,
mPropertyModel = model; mTabProvider.get().isInitialized() ? mTabProvider.get().getUrl().getSpec() : "");
mContentTypes = contentTypes;
mContentTypesToDisableFor = contentTypesToDisableFor; List<PropertyModel> propertyModels = new ArrayList<>();
mDisableForMultiWindow = disableForMultiWindow;
if (mContentTypes == null || mContentTypes.isEmpty()) {
return propertyModels;
} }
maybeAddScreenshotOption(propertyModels);
maybeAddCopyLinkOption(propertyModels);
maybeCopyImageOption(propertyModels);
maybeAddCopyTextAndLinkOption(propertyModels);
maybeAddCopyTextOption(propertyModels);
maybeAddSendTabToSelfOption(propertyModels);
maybeAddHighlightsOption(propertyModels);
maybeAddQrCodeOption(propertyModels);
maybeAddPrintingOption(propertyModels);
return propertyModels;
} }
private class FirstPartyOptionBuilder { private class PropertyModelBuilder {
private int mIcon; private int mIcon;
private int mIconLabel; private int mIconLabel;
private String mFeatureNameForMetrics; private String mFeatureNameForMetrics;
private Callback<View> mOnClickCallback; private Callback<View> mOnClickCallback;
private boolean mDisableForMultiWindow;
private Integer[] mContentTypesToDisableFor;
private final Integer[] mContentTypesInBuilder;
FirstPartyOptionBuilder(Integer... contentTypes) {
mContentTypesInBuilder = contentTypes;
mContentTypesToDisableFor = new Integer[] {};
}
FirstPartyOptionBuilder setIcon(int icon, int iconLabel) { PropertyModelBuilder setIcon(int icon, int iconLabel) {
mIcon = icon; mIcon = icon;
mIconLabel = iconLabel; mIconLabel = iconLabel;
return this; return this;
} }
FirstPartyOptionBuilder setFeatureNameForMetrics(String featureName) { PropertyModelBuilder setFeatureNameForMetrics(String featureName) {
mFeatureNameForMetrics = featureName; mFeatureNameForMetrics = featureName;
return this; return this;
} }
FirstPartyOptionBuilder setOnClickCallback(Callback<View> onClickCallback) { PropertyModelBuilder setOnClickCallback(Callback<View> onClickCallback) {
mOnClickCallback = onClickCallback; mOnClickCallback = onClickCallback;
return this; return this;
} }
FirstPartyOptionBuilder setContentTypesToDisableFor(Integer... contentTypesToDisableFor) { PropertyModel build() {
mContentTypesToDisableFor = contentTypesToDisableFor; return ShareSheetPropertyModelBuilder.createPropertyModel(
return this;
}
FirstPartyOptionBuilder setDisableForMultiWindow(boolean disableForMultiWindow) {
mDisableForMultiWindow = disableForMultiWindow;
return this;
}
FirstPartyOption build() {
PropertyModel model = ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(mActivity, mIcon), AppCompatResources.getDrawable(mActivity, mIcon),
mActivity.getResources().getString(mIconLabel), (view) -> { mActivity.getResources().getString(mIconLabel), (view) -> {
RecordUserAction.record(mFeatureNameForMetrics); RecordUserAction.record(mFeatureNameForMetrics);
...@@ -177,57 +187,6 @@ class ChromeProvidedSharingOptionsProvider { ...@@ -177,57 +187,6 @@ class ChromeProvidedSharingOptionsProvider {
mBottomSheetController.hideContent(mBottomSheetContent, true); mBottomSheetController.hideContent(mBottomSheetContent, true);
mOnClickCallback.onResult(view); mOnClickCallback.onResult(view);
}); });
return new FirstPartyOption(model, Arrays.asList(mContentTypesInBuilder),
Arrays.asList(mContentTypesToDisableFor), mDisableForMultiWindow);
}
}
/**
* Returns an ordered list of {@link PropertyModel}s that should be shown given the {@code
* contentTypes} being shared.
*
* @param contentTypes a {@link Set} of {@link ContentType}.
* @param isMultiWindow if in multi-window mode.
* @return a list of {@link PropertyModel}s.
*/
List<PropertyModel> getPropertyModels(Set<Integer> contentTypes, boolean isMultiWindow) {
List<PropertyModel> propertyModels = new ArrayList<>();
for (FirstPartyOption firstPartyOption : mOrderedFirstPartyOptions) {
if (!Collections.disjoint(contentTypes, firstPartyOption.mContentTypes)
&& Collections.disjoint(
contentTypes, firstPartyOption.mContentTypesToDisableFor)
&& !(isMultiWindow && firstPartyOption.mDisableForMultiWindow)) {
propertyModels.add(firstPartyOption.mPropertyModel);
}
}
return propertyModels;
}
/**
* Creates all enabled {@link FirstPartyOption}s and adds them to {@code
* mOrderedFirstPartyOptions} in the order they should appear.
*/
private void initializeFirstPartyOptionsInOrder() {
if (ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARE_SCREENSHOT)) {
mOrderedFirstPartyOptions.add(createScreenshotFirstPartyOption());
}
mOrderedFirstPartyOptions.add(createCopyLinkFirstPartyOption());
if (ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARING_HUB_V15)) {
mOrderedFirstPartyOptions.add(createCopyImageFirstPartyOption());
mOrderedFirstPartyOptions.add(createCopyFirstPartyOption());
mOrderedFirstPartyOptions.add(createCopyTextFirstPartyOption());
}
mOrderedFirstPartyOptions.add(createSendTabToSelfFirstPartyOption());
if (ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARING_HUB_V15)
&& ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID)) {
mOrderedFirstPartyOptions.add(createHighlightsFirstPartyOption());
}
if (ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARE_QRCODE)
&& !mTabProvider.get().getWebContents().isIncognito()) {
mOrderedFirstPartyOptions.add(createQrCodeFirstPartyOption());
}
if (UserPrefs.get(Profile.getLastUsedRegularProfile()).getBoolean(Pref.PRINTING_ENABLED)) {
mOrderedFirstPartyOptions.add(createPrintingFirstPartyOption());
} }
} }
...@@ -247,8 +206,18 @@ class ChromeProvidedSharingOptionsProvider { ...@@ -247,8 +206,18 @@ class ChromeProvidedSharingOptionsProvider {
} }
}; };
private FirstPartyOption createScreenshotFirstPartyOption() { private void maybeAddScreenshotOption(List<PropertyModel> propertyModels) {
PropertyModel propertyModel = ShareSheetPropertyModelBuilder.createPropertyModel( if (!ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARE_SCREENSHOT)
|| mIsMultiWindow) {
return;
}
if (Collections.disjoint(mContentTypes,
Arrays.asList(ContentType.LINK_PAGE_VISIBLE, ContentType.TEXT,
ContentType.HIGHLIGHTED_TEXT, ContentType.IMAGE))) {
return;
}
propertyModels.add(ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(mActivity, R.drawable.screenshot), AppCompatResources.getDrawable(mActivity, R.drawable.screenshot),
mActivity.getResources().getString(R.string.sharing_screenshot), (view) -> { mActivity.getResources().getString(R.string.sharing_screenshot), (view) -> {
RecordUserAction.record("SharingHubAndroid.ScreenshotSelected"); RecordUserAction.record("SharingHubAndroid.ScreenshotSelected");
...@@ -259,80 +228,113 @@ class ChromeProvidedSharingOptionsProvider { ...@@ -259,80 +228,113 @@ class ChromeProvidedSharingOptionsProvider {
// observer will then remove itself. // observer will then remove itself.
mBottomSheetController.addObserver(mSheetObserver); mBottomSheetController.addObserver(mSheetObserver);
mBottomSheetController.hideContent(mBottomSheetContent, true); mBottomSheetController.hideContent(mBottomSheetContent, true);
}); }));
return new FirstPartyOption(propertyModel, }
Arrays.asList(ContentType.LINK_PAGE_VISIBLE, ContentType.TEXT,
ContentType.HIGHLIGHTED_TEXT, ContentType.IMAGE), private void maybeAddCopyLinkOption(List<PropertyModel> propertyModels) {
/*contentTypesToDisableFor=*/Collections.emptySet(), if (mContentTypes.contains(ContentType.LINK_AND_TEXT)
/*disableForMultiWindow=*/true); || Collections.disjoint(mContentTypes,
Arrays.asList(ContentType.LINK_PAGE_VISIBLE,
ContentType.LINK_PAGE_NOT_VISIBLE))) {
return;
} }
private FirstPartyOption createCopyLinkFirstPartyOption() { propertyModels.add(
return new FirstPartyOptionBuilder( new PropertyModelBuilder()
ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE)
.setContentTypesToDisableFor(ContentType.LINK_AND_TEXT)
.setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy_url) .setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy_url)
.setFeatureNameForMetrics("SharingHubAndroid.CopyURLSelected") .setFeatureNameForMetrics("SharingHubAndroid.CopyURLSelected")
.setOnClickCallback((view) -> { .setOnClickCallback((view) -> {
ClipboardManager clipboard = (ClipboardManager) mActivity.getSystemService( ClipboardManager clipboard =
(ClipboardManager) mActivity.getSystemService(
Context.CLIPBOARD_SERVICE); Context.CLIPBOARD_SERVICE);
clipboard.setPrimaryClip( clipboard.setPrimaryClip(ClipData.newPlainText(
ClipData.newPlainText(mShareParams.getTitle(), mShareParams.getUrl())); mShareParams.getTitle(), mShareParams.getUrl()));
Toast.makeText(mActivity, R.string.link_copied, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.link_copied, Toast.LENGTH_SHORT)
.show();
}) })
.build(); .build());
} }
private FirstPartyOption createCopyImageFirstPartyOption() { private void maybeCopyImageOption(List<PropertyModel> propertyModels) {
return new FirstPartyOptionBuilder(ContentType.IMAGE) if (!ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARING_HUB_V15)
|| !mContentTypes.contains(ContentType.IMAGE)) {
return;
}
propertyModels.add(
new PropertyModelBuilder()
.setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy_image) .setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy_image)
.setFeatureNameForMetrics("SharingHubAndroid.CopyImageSelected") .setFeatureNameForMetrics("SharingHubAndroid.CopyImageSelected")
.setOnClickCallback((view) -> { .setOnClickCallback((view) -> {
if (!mShareParams.getFileUris().isEmpty()) { if (!mShareParams.getFileUris().isEmpty()) {
Clipboard.getInstance().setImageUri(mShareParams.getFileUris().get(0)); Clipboard.getInstance().setImageUri(
Toast.makeText(mActivity, R.string.image_copied, Toast.LENGTH_SHORT).show(); mShareParams.getFileUris().get(0));
Toast.makeText(mActivity, R.string.image_copied, Toast.LENGTH_SHORT)
.show();
} }
}) })
.build(); .build());
} }
private FirstPartyOption createCopyFirstPartyOption() { private void maybeAddCopyTextAndLinkOption(List<PropertyModel> propertyModels) {
return new FirstPartyOptionBuilder(ContentType.LINK_AND_TEXT) if (!ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARING_HUB_V15)
|| !mContentTypes.contains(ContentType.LINK_AND_TEXT)) {
return;
}
propertyModels.add(
new PropertyModelBuilder()
.setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy) .setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy)
.setFeatureNameForMetrics("SharingHubAndroid.CopySelected") .setFeatureNameForMetrics("SharingHubAndroid.CopySelected")
.setOnClickCallback((view) -> { .setOnClickCallback((view) -> {
ClipboardManager clipboard = (ClipboardManager) mActivity.getSystemService( ClipboardManager clipboard =
(ClipboardManager) mActivity.getSystemService(
Context.CLIPBOARD_SERVICE); Context.CLIPBOARD_SERVICE);
clipboard.setPrimaryClip(ClipData.newPlainText( clipboard.setPrimaryClip(ClipData.newPlainText(
mShareParams.getTitle(), mShareParams.getTextAndUrl())); mShareParams.getTitle(), mShareParams.getTextAndUrl()));
Toast.makeText(mActivity, R.string.sharing_copied, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.sharing_copied, Toast.LENGTH_SHORT)
.show();
}) })
.build(); .build());
} }
private FirstPartyOption createCopyTextFirstPartyOption() { private void maybeAddCopyTextOption(List<PropertyModel> propertyModels) {
return new FirstPartyOptionBuilder(ContentType.TEXT, ContentType.HIGHLIGHTED_TEXT) if (!ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARING_HUB_V15)
.setContentTypesToDisableFor(ContentType.LINK_AND_TEXT) || mContentTypes.contains(ContentType.LINK_AND_TEXT)) {
return;
}
if (Collections.disjoint(
mContentTypes, Arrays.asList(ContentType.TEXT, ContentType.HIGHLIGHTED_TEXT))) {
return;
}
propertyModels.add(
new PropertyModelBuilder()
.setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy_text) .setIcon(R.drawable.ic_content_copy_black, R.string.sharing_copy_text)
.setFeatureNameForMetrics("SharingHubAndroid.CopyTextSelected") .setFeatureNameForMetrics("SharingHubAndroid.CopyTextSelected")
.setOnClickCallback((view) -> { .setOnClickCallback((view) -> {
ClipboardManager clipboard = (ClipboardManager) mActivity.getSystemService( ClipboardManager clipboard =
(ClipboardManager) mActivity.getSystemService(
Context.CLIPBOARD_SERVICE); Context.CLIPBOARD_SERVICE);
clipboard.setPrimaryClip( clipboard.setPrimaryClip(ClipData.newPlainText(
ClipData.newPlainText(mShareParams.getTitle(), mShareParams.getText())); mShareParams.getTitle(), mShareParams.getText()));
Toast.makeText(mActivity, R.string.text_copied, Toast.LENGTH_SHORT).show(); Toast.makeText(mActivity, R.string.text_copied, Toast.LENGTH_SHORT)
.show();
}) })
.build(); .build());
} }
private FirstPartyOption createSendTabToSelfFirstPartyOption() { private void maybeAddSendTabToSelfOption(List<PropertyModel> propertyModels) {
return new FirstPartyOptionBuilder( if (Collections.disjoint(mContentTypes,
ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE, ContentType.IMAGE) Arrays.asList(ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE,
.setIcon(R.drawable.send_tab, R.string.send_tab_to_self_share_activity_title) ContentType.IMAGE))) {
return;
}
propertyModels.add(
new PropertyModelBuilder()
.setIcon(
R.drawable.send_tab, R.string.send_tab_to_self_share_activity_title)
.setFeatureNameForMetrics("SharingHubAndroid.SendTabToSelfSelected") .setFeatureNameForMetrics("SharingHubAndroid.SendTabToSelfSelected")
.setOnClickCallback((view) -> { .setOnClickCallback((view) -> {
SendTabToSelfCoordinator sttsCoordinator = SendTabToSelfCoordinator sttsCoordinator = new SendTabToSelfCoordinator(
new SendTabToSelfCoordinator(mActivity, mUrl, mShareParams.getTitle(), mActivity, mUrl, mShareParams.getTitle(),
mBottomSheetController, mSettingsLauncher, mIsSyncEnabled, mBottomSheetController, mSettingsLauncher, mIsSyncEnabled,
mTabProvider.get() mTabProvider.get()
.getWebContents() .getWebContents()
...@@ -341,45 +343,66 @@ class ChromeProvidedSharingOptionsProvider { ...@@ -341,45 +343,66 @@ class ChromeProvidedSharingOptionsProvider {
.getTimestamp()); .getTimestamp());
sttsCoordinator.show(); sttsCoordinator.show();
}) })
.build(); .build());
} }
private FirstPartyOption createQrCodeFirstPartyOption() { private void maybeAddQrCodeOption(List<PropertyModel> propertyModels) {
return new FirstPartyOptionBuilder( if (!ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARE_QRCODE)
ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE, ContentType.IMAGE) || mTabProvider.get().getWebContents().isIncognito()) {
return;
}
if (Collections.disjoint(mContentTypes,
Arrays.asList(ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE,
ContentType.IMAGE))) {
return;
}
propertyModels.add(new PropertyModelBuilder()
.setIcon(R.drawable.qr_code, R.string.qr_code_share_icon_label) .setIcon(R.drawable.qr_code, R.string.qr_code_share_icon_label)
.setFeatureNameForMetrics("SharingHubAndroid.QRCodeSelected") .setFeatureNameForMetrics("SharingHubAndroid.QRCodeSelected")
.setOnClickCallback((view) -> { .setOnClickCallback((view) -> {
QrCodeCoordinator qrCodeCoordinator = new QrCodeCoordinator(mActivity, mUrl); QrCodeCoordinator qrCodeCoordinator =
new QrCodeCoordinator(mActivity, mUrl);
qrCodeCoordinator.show(); qrCodeCoordinator.show();
}) })
.build(); .build());
} }
private FirstPartyOption createPrintingFirstPartyOption() { private void maybeAddPrintingOption(List<PropertyModel> propertyModels) {
return new FirstPartyOptionBuilder(ContentType.LINK_PAGE_VISIBLE) if (!UserPrefs.get(Profile.getLastUsedRegularProfile()).getBoolean(Pref.PRINTING_ENABLED)
|| !mContentTypes.contains(ContentType.LINK_PAGE_VISIBLE)) {
return;
}
propertyModels.add(
new PropertyModelBuilder()
.setIcon(R.drawable.sharing_print, R.string.print_share_activity_title) .setIcon(R.drawable.sharing_print, R.string.print_share_activity_title)
.setFeatureNameForMetrics("SharingHubAndroid.PrintSelected") .setFeatureNameForMetrics("SharingHubAndroid.PrintSelected")
.setOnClickCallback((view) -> { mPrintTabCallback.onResult(mTabProvider.get()); }) .setOnClickCallback(
.build(); (view) -> { mPrintTabCallback.onResult(mTabProvider.get()); })
.build());
} }
private FirstPartyOption createHighlightsFirstPartyOption() { private void maybeAddHighlightsOption(List<PropertyModel> propertyModels) {
return new FirstPartyOptionBuilder(ContentType.HIGHLIGHTED_TEXT) if (!ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARING_HUB_V15)
|| !ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID)
|| !mContentTypes.contains(ContentType.HIGHLIGHTED_TEXT)) {
return;
}
propertyModels.add(
new PropertyModelBuilder()
.setIcon(R.drawable.link, R.string.sharing_highlights) .setIcon(R.drawable.link, R.string.sharing_highlights)
.setFeatureNameForMetrics("SharingHubAndroid.LinkToTextSelected") .setFeatureNameForMetrics("SharingHubAndroid.LinkToTextSelected")
.setOnClickCallback((view) -> { .setOnClickCallback((view) -> {
LinkToTextCoordinator linkToTextCoordinator = LinkToTextCoordinator linkToTextCoordinator = new LinkToTextCoordinator(
new LinkToTextCoordinator(mActivity, mTabProvider.get(), mActivity, mTabProvider.get(), mChromeOptionShareCallback, mUrl,
mChromeOptionShareCallback, mUrl, mShareParams.getText()); mShareParams.getText());
}) })
.build(); .build());
} }
/** /**
* Returns the url to share. * Returns the url to share.
* *
* <p>This prioritizes the URL in {@link ShareParams}, but if it does not exist, we look for an * <p> This prioritizes the URL in {@link ShareParams}, but if it does not exist, we look for an
* image source URL from {@link ChromeShareExtras}. The image source URL is not contained in * image source URL from {@link ChromeShareExtras}. The image source URL is not contained in
* {@link ShareParams#getUrl()} because we do not want to share the image URL with the image * {@link ShareParams#getUrl()} because we do not want to share the image URL with the image
* file in third-party app shares. If both are empty then current tab URL is used. This is * file in third-party app shares. If both are empty then current tab URL is used. This is
......
...@@ -47,10 +47,6 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio ...@@ -47,10 +47,6 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio
private final BottomSheetController mBottomSheetController; private final BottomSheetController mBottomSheetController;
private final Supplier<Tab> mTabProvider; private final Supplier<Tab> mTabProvider;
private final ShareSheetPropertyModelBuilder mPropertyModelBuilder; private final ShareSheetPropertyModelBuilder mPropertyModelBuilder;
private final Callback<Tab> mPrintTabCallback;
private final SettingsLauncher mSettingsLauncher;
private final boolean mIsSyncEnabled;
private long mShareStartTime;
private boolean mExcludeFirstParty; private boolean mExcludeFirstParty;
private boolean mIsMultiWindow; private boolean mIsMultiWindow;
private Set<Integer> mContentTypes; private Set<Integer> mContentTypes;
...@@ -82,9 +78,6 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio ...@@ -82,9 +78,6 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio
mLifecycleDispatcher.register(this); mLifecycleDispatcher.register(this);
mTabProvider = tabProvider; mTabProvider = tabProvider;
mPropertyModelBuilder = modelBuilder; mPropertyModelBuilder = modelBuilder;
mPrintTabCallback = printTab;
mSettingsLauncher = settingsLauncher;
mIsSyncEnabled = isSyncEnabled;
mBottomSheetObserver = new EmptyBottomSheetObserver() { mBottomSheetObserver = new EmptyBottomSheetObserver() {
@Override @Override
public void onSheetContentChanged(BottomSheetContent bottomSheet) { public void onSheetContentChanged(BottomSheetContent bottomSheet) {
...@@ -100,6 +93,11 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio ...@@ -100,6 +93,11 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio
}; };
mBottomSheetController.addObserver(mBottomSheetObserver); mBottomSheetController.addObserver(mBottomSheetObserver);
mIconBridge = iconBridge; mIconBridge = iconBridge;
mChromeProvidedSharingOptionsProvider =
new ChromeProvidedSharingOptionsProvider(tabProvider, controller, mBottomSheet);
mChromeProvidedSharingOptionsProvider.setFeatureSpecificParams(
printTab, settingsLauncher, isSyncEnabled);
} }
protected void destroy() { protected void destroy() {
...@@ -117,6 +115,7 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio ...@@ -117,6 +115,7 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio
mLifecycleDispatcher.unregister(this); mLifecycleDispatcher.unregister(this);
mLifecycleDispatcher = null; mLifecycleDispatcher = null;
} }
mChromeProvidedSharingOptionsProvider = null;
} }
// TODO(crbug/1022172): Should be package-protected once modularization is complete. // TODO(crbug/1022172): Should be package-protected once modularization is complete.
...@@ -135,12 +134,11 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio ...@@ -135,12 +134,11 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio
mBottomSheet = new ShareSheetBottomSheetContent(mActivity, mIconBridge, this, params); mBottomSheet = new ShareSheetBottomSheetContent(mActivity, mIconBridge, this, params);
mShareStartTime = shareStartTime;
mContentTypes = ShareSheetPropertyModelBuilder.getContentTypes(params, chromeShareExtras); mContentTypes = ShareSheetPropertyModelBuilder.getContentTypes(params, chromeShareExtras);
List<PropertyModel> firstPartyApps = List<PropertyModel> firstPartyApps = createFirstPartyPropertyModels(
createFirstPartyPropertyModels(mActivity, params, chromeShareExtras, mContentTypes); mActivity, params, chromeShareExtras, mContentTypes, shareStartTime);
List<PropertyModel> thirdPartyApps = createThirdPartyPropertyModels( List<PropertyModel> thirdPartyApps = createThirdPartyPropertyModels(
mActivity, params, mContentTypes, chromeShareExtras.saveLastUsed()); mActivity, params, mContentTypes, chromeShareExtras.saveLastUsed(), shareStartTime);
mBottomSheet.createRecyclerViews( mBottomSheet.createRecyclerViews(
firstPartyApps, thirdPartyApps, mContentTypes, params.getFileContentType()); firstPartyApps, thirdPartyApps, mContentTypes, params.getFileContentType());
...@@ -162,25 +160,24 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio ...@@ -162,25 +160,24 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio
} }
List<PropertyModel> createFirstPartyPropertyModels(Activity activity, ShareParams shareParams, List<PropertyModel> createFirstPartyPropertyModels(Activity activity, ShareParams shareParams,
ChromeShareExtras chromeShareExtras, Set<Integer> contentTypes) { ChromeShareExtras chromeShareExtras, Set<Integer> contentTypes, long shareStartTime) {
if (mExcludeFirstParty) { if (mExcludeFirstParty) {
return new ArrayList<>(); return new ArrayList<>();
} }
mChromeProvidedSharingOptionsProvider = new ChromeProvidedSharingOptionsProvider(activity,
mTabProvider, mBottomSheetController, mBottomSheet, shareParams, chromeShareExtras,
mPrintTabCallback, mSettingsLauncher, mIsSyncEnabled, mShareStartTime, this);
mIsMultiWindow = ApiCompatibilityUtils.isInMultiWindowMode(activity); mIsMultiWindow = ApiCompatibilityUtils.isInMultiWindowMode(activity);
return mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.setShareRelatedParams(
contentTypes, mIsMultiWindow); shareParams, chromeShareExtras, shareStartTime, this, contentTypes);
mChromeProvidedSharingOptionsProvider.setIsMultiWindow(mIsMultiWindow);
return mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
} }
@VisibleForTesting @VisibleForTesting
List<PropertyModel> createThirdPartyPropertyModels(Activity activity, ShareParams params, List<PropertyModel> createThirdPartyPropertyModels(Activity activity, ShareParams params,
Set<Integer> contentTypes, boolean saveLastUsed) { Set<Integer> contentTypes, boolean saveLastUsed, long shareStartTime) {
if (params == null) return null; if (params == null) return null;
List<PropertyModel> models = mPropertyModelBuilder.selectThirdPartyApps(mBottomSheet, List<PropertyModel> models = mPropertyModelBuilder.selectThirdPartyApps(mBottomSheet,
contentTypes, params, saveLastUsed, params.getWindow(), mShareStartTime); contentTypes, params, saveLastUsed, params.getWindow(), shareStartTime);
// More... // More...
PropertyModel morePropertyModel = ShareSheetPropertyModelBuilder.createPropertyModel( PropertyModel morePropertyModel = ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(activity, R.drawable.sharing_more), AppCompatResources.getDrawable(activity, R.drawable.sharing_more),
...@@ -218,16 +215,22 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio ...@@ -218,16 +215,22 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio
return; return;
} }
boolean isMultiWindow = ApiCompatibilityUtils.isInMultiWindowMode(mActivity); boolean isMultiWindow = ApiCompatibilityUtils.isInMultiWindowMode(mActivity);
// mContentTypes is null if Chrome features should not be shown. if (mIsMultiWindow == isMultiWindow) {
if (mIsMultiWindow == isMultiWindow || mContentTypes == null) {
return; return;
} }
mIsMultiWindow = isMultiWindow; mIsMultiWindow = isMultiWindow;
mBottomSheet.createFirstPartyRecyclerViews( mChromeProvidedSharingOptionsProvider.setIsMultiWindow(mIsMultiWindow);
mChromeProvidedSharingOptionsProvider.getPropertyModels( List<PropertyModel> firstPartyOptions =
mContentTypes, mIsMultiWindow)); mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
mBottomSheetController.requestShowContent(mBottomSheet, /*animate=*/false);
// firstPartyOptions is empty if Chrome features should not be shown.
if (firstPartyOptions == null || firstPartyOptions.isEmpty()) {
return;
}
mBottomSheet.createFirstPartyRecyclerViews(firstPartyOptions);
mBottomSheetController.requestShowContent(mBottomSheet, /* animate= */ false);
} }
// View.OnLayoutChangeListener // View.OnLayoutChangeListener
...@@ -242,5 +245,4 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio ...@@ -242,5 +245,4 @@ public class ShareSheetCoordinator implements ActivityStateObserver, ChromeOptio
mBottomSheet.getThirdPartyView().invalidate(); mBottomSheet.getThirdPartyView().invalidate();
mBottomSheet.getThirdPartyView().requestLayout(); mBottomSheet.getThirdPartyView().requestLayout();
} }
} }
...@@ -40,10 +40,13 @@ import org.chromium.components.prefs.PrefService; ...@@ -40,10 +40,13 @@ import org.chromium.components.prefs.PrefService;
import org.chromium.components.user_prefs.UserPrefs; import org.chromium.components.user_prefs.UserPrefs;
import org.chromium.components.user_prefs.UserPrefsJni; import org.chromium.components.user_prefs.UserPrefsJni;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.ImmutableWeakReference;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.ui.modelutil.PropertyModel; import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.test.util.DummyUiActivity; import org.chromium.ui.test.util.DummyUiActivity;
import org.chromium.url.GURL; import org.chromium.url.GURL;
import java.util.Collection;
import java.util.List; import java.util.List;
/** /**
...@@ -77,6 +80,9 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -77,6 +80,9 @@ public class ChromeProvidedSharingOptionsProviderTest {
@Mock @Mock
private ShareSheetCoordinator mShareSheetCoordinator; private ShareSheetCoordinator mShareSheetCoordinator;
@Mock
private WindowAndroid mWindow;
private Activity mActivity; private Activity mActivity;
private ChromeProvidedSharingOptionsProvider mChromeProvidedSharingOptionsProvider; private ChromeProvidedSharingOptionsProvider mChromeProvidedSharingOptionsProvider;
...@@ -100,6 +106,7 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -100,6 +106,7 @@ public class ChromeProvidedSharingOptionsProviderTest {
Mockito.when(mTab.getUrl()).thenReturn(new GURL(URL)); Mockito.when(mTab.getUrl()).thenReturn(new GURL(URL));
Mockito.when(mWebContents.isIncognito()).thenReturn(false); Mockito.when(mWebContents.isIncognito()).thenReturn(false);
mActivity = mActivityTestRule.getActivity(); mActivity = mActivityTestRule.getActivity();
Mockito.when(mWindow.getActivity()).thenReturn(new ImmutableWeakReference<>(mActivity));
} }
@Test @Test
...@@ -109,10 +116,11 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -109,10 +116,11 @@ public class ChromeProvidedSharingOptionsProviderTest {
@Features.DisableFeatures({ChromeFeatureList.CHROME_SHARING_HUB_V15}) @Features.DisableFeatures({ChromeFeatureList.CHROME_SHARING_HUB_V15})
public void public void
getPropertyModels_screenshotQrCodeEnabled_includesBoth() { getPropertyModels_screenshotQrCodeEnabled_includesBoth() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/false); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ false,
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES,
/* isMultiWindow= */ false);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES, /*isMultiWindow=*/false);
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_screenshot), ImmutableList.of(mActivity.getResources().getString(R.string.sharing_screenshot),
...@@ -127,10 +135,11 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -127,10 +135,11 @@ public class ChromeProvidedSharingOptionsProviderTest {
ChromeFeatureList.CHROME_SHARE_QRCODE, ChromeFeatureList.CHROME_SHARING_HUB_V15}) ChromeFeatureList.CHROME_SHARE_QRCODE, ChromeFeatureList.CHROME_SHARING_HUB_V15})
public void public void
getPropertyModels_screenshotQrCodeDisabled_doesNotIncludeEither() { getPropertyModels_screenshotQrCodeDisabled_doesNotIncludeEither() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/false); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ false,
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES,
/* isMultiWindow= */ false);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES, /*isMultiWindow=*/false);
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString( ImmutableList.of(mActivity.getResources().getString(
...@@ -143,10 +152,11 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -143,10 +152,11 @@ public class ChromeProvidedSharingOptionsProviderTest {
ChromeFeatureList.CHROME_SHARE_QRCODE, ChromeFeatureList.CHROME_SHARING_HUB_V15}) ChromeFeatureList.CHROME_SHARE_QRCODE, ChromeFeatureList.CHROME_SHARING_HUB_V15})
public void public void
getPropertyModels_printingEnabled_includesPrinting() { getPropertyModels_printingEnabled_includesPrinting() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/true); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ true,
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES,
/* isMultiWindow= */ false);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES, /*isMultiWindow=*/false);
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString( ImmutableList.of(mActivity.getResources().getString(
...@@ -162,10 +172,10 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -162,10 +172,10 @@ public class ChromeProvidedSharingOptionsProviderTest {
ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID}) ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID})
public void public void
getPropertyModels_sharingHub15Enabled_includesCopyText() { getPropertyModels_sharingHub15Enabled_includesCopyText() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/false); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ false,
ImmutableSet.of(ContentType.TEXT), /* isMultiWindow= */ false);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ImmutableSet.of(ContentType.TEXT), /*isMultiWindow=*/false);
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_text))); ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_text)));
...@@ -179,13 +189,13 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -179,13 +189,13 @@ public class ChromeProvidedSharingOptionsProviderTest {
ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID}) ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID})
public void public void
getPropertyModels_linkAndTextShare() { getPropertyModels_linkAndTextShare() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/false); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ false,
ImmutableSet.of(ContentType.LINK_AND_TEXT, ContentType.LINK_PAGE_NOT_VISIBLE,
ContentType.TEXT),
/* isMultiWindow= */ true);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ImmutableSet.of(ContentType.LINK_AND_TEXT,
ContentType.LINK_PAGE_NOT_VISIBLE, ContentType.TEXT),
/*isMultiWindow=*/true);
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy), ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy),
...@@ -201,12 +211,12 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -201,12 +211,12 @@ public class ChromeProvidedSharingOptionsProviderTest {
ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID}) ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID})
public void public void
getPropertyModels_linkShare() { getPropertyModels_linkShare() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/false); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ false,
ImmutableSet.of(ContentType.LINK_PAGE_NOT_VISIBLE),
/* isMultiWindow= */ true);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ImmutableSet.of(ContentType.LINK_PAGE_NOT_VISIBLE),
/*isMultiWindow=*/true);
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_url), ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_url),
...@@ -222,12 +232,12 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -222,12 +232,12 @@ public class ChromeProvidedSharingOptionsProviderTest {
ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID}) ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID})
public void public void
getPropertyModels_textShare() { getPropertyModels_textShare() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/false); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ false,
ImmutableSet.of(ContentType.TEXT),
/* isMultiWindow= */ true);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ImmutableSet.of(ContentType.TEXT),
/*isMultiWindow=*/true);
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_text))); ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_text)));
...@@ -241,11 +251,11 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -241,11 +251,11 @@ public class ChromeProvidedSharingOptionsProviderTest {
ChromeFeatureList.CHROME_SHARING_HUB_V15}) ChromeFeatureList.CHROME_SHARING_HUB_V15})
public void public void
getPropertyModels_multiWindow_doesNotIncludeScreenshot() { getPropertyModels_multiWindow_doesNotIncludeScreenshot() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/false); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ false,
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES, /* isMultiWindow= */ true);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES, /*isMultiWindow=*/true);
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString( ImmutableList.of(mActivity.getResources().getString(
...@@ -259,11 +269,11 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -259,11 +269,11 @@ public class ChromeProvidedSharingOptionsProviderTest {
@Features.DisableFeatures({ChromeFeatureList.CHROME_SHARING_HUB_V15}) @Features.DisableFeatures({ChromeFeatureList.CHROME_SHARING_HUB_V15})
public void public void
getPropertyModels_filtersByContentType() { getPropertyModels_filtersByContentType() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/true); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ true,
List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels(
ImmutableSet.of(ContentType.LINK_PAGE_NOT_VISIBLE), ImmutableSet.of(ContentType.LINK_PAGE_NOT_VISIBLE),
/*isMultiWindow=*/false); /* isMultiWindow= */ false);
List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_url), ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_url),
...@@ -279,11 +289,11 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -279,11 +289,11 @@ public class ChromeProvidedSharingOptionsProviderTest {
@Features.DisableFeatures({ChromeFeatureList.CHROME_SHARING_HUB_V15}) @Features.DisableFeatures({ChromeFeatureList.CHROME_SHARING_HUB_V15})
public void public void
getPropertyModels_multipleTypes_filtersByContentType() { getPropertyModels_multipleTypes_filtersByContentType() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/true); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ true,
List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels(
ImmutableSet.of(ContentType.LINK_PAGE_NOT_VISIBLE, ContentType.IMAGE), ImmutableSet.of(ContentType.LINK_PAGE_NOT_VISIBLE, ContentType.IMAGE),
/*isMultiWindow=*/false); /* isMultiWindow= */ false);
List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_screenshot), ImmutableList.of(mActivity.getResources().getString(R.string.sharing_screenshot),
...@@ -300,10 +310,10 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -300,10 +310,10 @@ public class ChromeProvidedSharingOptionsProviderTest {
@Features.EnableFeatures({ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID}) @Features.EnableFeatures({ChromeFeatureList.CHROME_SHARE_HIGHLIGHTS_ANDROID})
public void public void
getPropertyModels_sharingHub15Disabled_noHighlights() { getPropertyModels_sharingHub15Disabled_noHighlights() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/false); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ false,
ImmutableSet.of(ContentType.TEXT), /* isMultiWindow= */ false);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ImmutableSet.of(ContentType.TEXT), /*isMultiWindow=*/false);
assertEquals("Incorrect number of property models.", 0, propertyModels.size()); assertEquals("Incorrect number of property models.", 0, propertyModels.size());
} }
...@@ -315,10 +325,10 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -315,10 +325,10 @@ public class ChromeProvidedSharingOptionsProviderTest {
@Features.DisableFeatures({ChromeFeatureList.CHROME_SHARE_SCREENSHOT}) @Features.DisableFeatures({ChromeFeatureList.CHROME_SHARE_SCREENSHOT})
public void public void
getPropertyModels_sharingHub15HighlightsEnabled() { getPropertyModels_sharingHub15HighlightsEnabled() {
setUpChromeProvidedSharingOptionsProviderTest(/*printingEnabled=*/false); setUpChromeProvidedSharingOptionsProviderTest(/* printingEnabled= */ false,
ImmutableSet.of(ContentType.HIGHLIGHTED_TEXT), /* isMultiWindow= */ false);
List<PropertyModel> propertyModels = List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.getPropertyModels( mChromeProvidedSharingOptionsProvider.calculatePropertyModels();
ImmutableSet.of(ContentType.HIGHLIGHTED_TEXT), /*isMultiWindow=*/false);
assertCorrectModelsAreInTheRightOrder(propertyModels, assertCorrectModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_text), ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_text),
...@@ -328,7 +338,8 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -328,7 +338,8 @@ public class ChromeProvidedSharingOptionsProviderTest {
@Test @Test
@MediumTest @MediumTest
public void getUrlToShare_noShareParamsUrl_returnsImageUrl() { public void getUrlToShare_noShareParamsUrl_returnsImageUrl() {
ShareParams shareParams = new ShareParams.Builder(null, /*title=*/"", /*url=*/"").build(); ShareParams shareParams =
new ShareParams.Builder(null, /* title= */ "", /* url= */ "").build();
ChromeShareExtras chromeShareExtras = ChromeShareExtras chromeShareExtras =
new ChromeShareExtras.Builder().setImageSrcUrl(URL).build(); new ChromeShareExtras.Builder().setImageSrcUrl(URL).build();
...@@ -341,7 +352,7 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -341,7 +352,7 @@ public class ChromeProvidedSharingOptionsProviderTest {
@Test @Test
@MediumTest @MediumTest
public void getUrlToShare_shareParamsUrlExists_returnsShareParamsUrl() { public void getUrlToShare_shareParamsUrlExists_returnsShareParamsUrl() {
ShareParams shareParams = new ShareParams.Builder(null, /*title=*/"", URL).build(); ShareParams shareParams = new ShareParams.Builder(null, /* title= */ "", URL).build();
ChromeShareExtras chromeShareExtras = ChromeShareExtras chromeShareExtras =
new ChromeShareExtras.Builder().setImageSrcUrl("").build(); new ChromeShareExtras.Builder().setImageSrcUrl("").build();
...@@ -354,7 +365,8 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -354,7 +365,8 @@ public class ChromeProvidedSharingOptionsProviderTest {
@Test @Test
@MediumTest @MediumTest
public void getUrlToShare_noShareParamsUrl_noImageUrl() { public void getUrlToShare_noShareParamsUrl_noImageUrl() {
ShareParams shareParams = new ShareParams.Builder(null, /*title=*/"", /*url=*/"").build(); ShareParams shareParams =
new ShareParams.Builder(null, /* title= */ "", /* url= */ "").build();
ChromeShareExtras chromeShareExtras = ChromeShareExtras chromeShareExtras =
new ChromeShareExtras.Builder().setImageSrcUrl("").build(); new ChromeShareExtras.Builder().setImageSrcUrl("").build();
...@@ -364,21 +376,24 @@ public class ChromeProvidedSharingOptionsProviderTest { ...@@ -364,21 +376,24 @@ public class ChromeProvidedSharingOptionsProviderTest {
URL); URL);
} }
private void setUpChromeProvidedSharingOptionsProviderTest(boolean printingEnabled) { private void setUpChromeProvidedSharingOptionsProviderTest(
boolean printingEnabled, Collection<Integer> contentTypes, boolean isMultiWindow) {
Mockito.when(mPrefService.getBoolean(anyString())).thenReturn(printingEnabled); Mockito.when(mPrefService.getBoolean(anyString())).thenReturn(printingEnabled);
ShareParams shareParams = new ShareParams.Builder(null, /*title=*/"", /*url=*/"").build(); ShareParams shareParams =
new ShareParams.Builder(mWindow, /* title= */ "", /* url= */ "").build();
mChromeProvidedSharingOptionsProvider = mChromeProvidedSharingOptionsProvider =
new ChromeProvidedSharingOptionsProvider(mActivity, mTabProvider, new ChromeProvidedSharingOptionsProvider(mTabProvider,
/*bottomSheetController=*/null, /* bottomSheetController= */ null,
new ShareSheetBottomSheetContent( new ShareSheetBottomSheetContent(
mActivity, null, mShareSheetCoordinator, shareParams), mActivity, null, mShareSheetCoordinator, shareParams));
new ShareParams.Builder(null, "", "").build(), mChromeProvidedSharingOptionsProvider.setShareRelatedParams(shareParams,
new ChromeShareExtras.Builder().build(), new ChromeShareExtras.Builder().build(), /* shareStartTime= */ 0,
/*TabPrinterDelegate=*/null, mShareSheetCoordinator, contentTypes);
/*settingsLauncher=*/null, mChromeProvidedSharingOptionsProvider.setFeatureSpecificParams(
/*syncState=*/false, /* TabPrinterDelegate= */ null, /* settingsLauncher= */ null,
/*shareStartTime=*/0, mShareSheetCoordinator); /* syncState= */ false);
mChromeProvidedSharingOptionsProvider.setIsMultiWindow(isMultiWindow);
} }
private void assertCorrectModelsAreInTheRightOrder( private void assertCorrectModelsAreInTheRightOrder(
......
...@@ -22,6 +22,7 @@ import org.junit.Test; ...@@ -22,6 +22,7 @@ import org.junit.Test;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
...@@ -33,6 +34,8 @@ import org.chromium.chrome.test.ChromeJUnit4ClassRunner; ...@@ -33,6 +34,8 @@ import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features; import org.chromium.chrome.test.util.browser.Features;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetController; import org.chromium.components.browser_ui.bottomsheet.BottomSheetController;
import org.chromium.components.browser_ui.share.ShareParams; import org.chromium.components.browser_ui.share.ShareParams;
import org.chromium.ui.base.ImmutableWeakReference;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.ui.modelutil.PropertyModel; import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.test.util.DummyUiActivity; import org.chromium.ui.test.util.DummyUiActivity;
...@@ -68,6 +71,9 @@ public final class ShareSheetCoordinatorTest { ...@@ -68,6 +71,9 @@ public final class ShareSheetCoordinatorTest {
@Mock @Mock
private ShareParams mParams; private ShareParams mParams;
@Mock
private WindowAndroid mWindow;
private Activity mActivity; private Activity mActivity;
private ShareSheetCoordinator mShareSheetCoordinator; private ShareSheetCoordinator mShareSheetCoordinator;
...@@ -95,6 +101,9 @@ public final class ShareSheetCoordinatorTest { ...@@ -95,6 +101,9 @@ public final class ShareSheetCoordinatorTest {
mShareSheetCoordinator = new ShareSheetCoordinator(mController, mLifecycleDispatcher, null, mShareSheetCoordinator = new ShareSheetCoordinator(mController, mLifecycleDispatcher, null,
mPropertyModelBuilder, null, null, null, false); mPropertyModelBuilder, null, null, null, false);
Mockito.when(mParams.getWindow()).thenReturn(mWindow);
Mockito.when(mWindow.getActivity()).thenReturn(new ImmutableWeakReference<>(mActivity));
} }
@Test @Test
...@@ -104,7 +113,7 @@ public final class ShareSheetCoordinatorTest { ...@@ -104,7 +113,7 @@ public final class ShareSheetCoordinatorTest {
List<PropertyModel> propertyModels = mShareSheetCoordinator.createFirstPartyPropertyModels( List<PropertyModel> propertyModels = mShareSheetCoordinator.createFirstPartyPropertyModels(
mActivity, mParams, /*chromeShareExtras=*/null, mActivity, mParams, /*chromeShareExtras=*/null,
ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES); ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES, /*shareStartTime=*/0);
assertEquals("Property model list should be empty.", 0, propertyModels.size()); assertEquals("Property model list should be empty.", 0, propertyModels.size());
} }
...@@ -113,7 +122,7 @@ public final class ShareSheetCoordinatorTest { ...@@ -113,7 +122,7 @@ public final class ShareSheetCoordinatorTest {
public void testCreateThirdPartyPropertyModels() { public void testCreateThirdPartyPropertyModels() {
List<PropertyModel> propertyModels = mShareSheetCoordinator.createThirdPartyPropertyModels( List<PropertyModel> propertyModels = mShareSheetCoordinator.createThirdPartyPropertyModels(
mActivity, mParams, ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES, mActivity, mParams, ShareSheetPropertyModelBuilder.ALL_CONTENT_TYPES,
/*saveLastUsed=*/false); /*saveLastUsed=*/false, /*shareStartTime=*/0);
assertEquals("Incorrect number of property models.", 3, propertyModels.size()); assertEquals("Incorrect number of property models.", 3, propertyModels.size());
assertEquals("First property model isn't testModel1.", "testModel1", assertEquals("First property model isn't testModel1.", "testModel1",
......
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