Commit 9db3eef5 authored by Sophey Dong's avatar Sophey Dong Committed by Commit Bot

[SharingHub] Add ability to filter chrome options by content type.

Still haven't added the part to get the content types yet, so it is not hooked up yet. Instead, another method is added to provide all the options.

Bug: 1079467
Change-Id: Ia6326db8d7aa01dab2690485f6891418b3f4d11a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2197524
Commit-Queue: Sophey Dong <sophey@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarKyle Milka <kmilka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#770804}
parent ccad5cf1
......@@ -444,6 +444,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/settings/SettingsActivityTest.java",
"javatests/src/org/chromium/chrome/browser/settings/language/LanguageSettingsTest.java",
"javatests/src/org/chromium/chrome/browser/shape_detection/ShapeDetectionTest.java",
"javatests/src/org/chromium/chrome/browser/share/ChromeProvidedSharingOptionsProviderTest.java",
"javatests/src/org/chromium/chrome/browser/share/LensUtilsTest.java",
"javatests/src/org/chromium/chrome/browser/share/ShareButtonControllerTest.java",
"javatests/src/org/chromium/chrome/browser/share/ShareDelegateImplIntegrationTest.java",
......
......@@ -9,14 +9,19 @@ import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import androidx.annotation.IntDef;
import androidx.appcompat.content.res.AppCompatResources;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ActivityTabProvider;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.preferences.Pref;
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.printing.TabPrinter;
import org.chromium.chrome.browser.send_tab_to_self.SendTabToSelfShareActivity;
import org.chromium.chrome.browser.share.ShareSheetCoordinator.ContentType;
import org.chromium.chrome.browser.share.qrcode.QrCodeCoordinator;
import org.chromium.chrome.browser.share.screenshot.ScreenshotCoordinator;
import org.chromium.chrome.browser.tab.Tab;
......@@ -31,6 +36,17 @@ import org.chromium.printing.PrintingControllerImpl;
import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.widget.Toast;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Provides {@code PropertyModel}s of Chrome-provided sharing options.
*/
......@@ -39,8 +55,10 @@ class ChromeProvidedSharingOptionsProvider {
private final ActivityTabProvider mActivityTabProvider;
private final BottomSheetController mBottomSheetController;
private final ShareSheetBottomSheetContent mBottomSheetContent;
private final PrefServiceBridge mPrefServiceBridge;
private final long mShareStartTime;
private ScreenshotCoordinator mScreenshotCoordinator;
private Map<Integer, Set<Integer>> mSharingOptionToContentTypes;
/**
* Constructs a new {@link ChromeProvidedSharingOptionsProvider}.
......@@ -50,16 +68,77 @@ class ChromeProvidedSharingOptionsProvider {
* @param bottomSheetController The {@link BottomSheetController} for the current activity.
* @param bottomSheetContent The {@link ShareSheetBottomSheetContent} for the current
* activity.
* @param prefServiceBridge The {@link PrefServiceBridge} singleton. This provides printing
* preferences.
* @param shareStartTime The start time of the current share.
*/
ChromeProvidedSharingOptionsProvider(Activity activity, ActivityTabProvider activityTabProvider,
BottomSheetController bottomSheetController,
ShareSheetBottomSheetContent bottomSheetContent, long shareStartTime) {
ShareSheetBottomSheetContent bottomSheetContent, PrefServiceBridge prefServiceBridge,
long shareStartTime) {
mActivity = activity;
mActivityTabProvider = activityTabProvider;
mBottomSheetController = bottomSheetController;
mBottomSheetContent = bottomSheetContent;
mPrefServiceBridge = prefServiceBridge;
mShareStartTime = shareStartTime;
mSharingOptionToContentTypes = createSharingOptionToContentTypesMap();
}
/**
* Returns a {@link Map} from {@link SharingOption}s to a set of {@link ContentType}s that it
* should be shown for.
*/
private static Map<Integer, Set<Integer>> createSharingOptionToContentTypesMap() {
Map<Integer, Set<Integer>> sharingOptionToContentTypes = new HashMap<>();
sharingOptionToContentTypes.put(SharingOption.SCREENSHOT,
new HashSet<>(Arrays.asList(ContentType.LINK_PAGE_VISIBLE, ContentType.IMAGE)));
sharingOptionToContentTypes.put(SharingOption.COPY_LINK,
new HashSet<>(Arrays.asList(
ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE)));
sharingOptionToContentTypes.put(SharingOption.SEND_TAB_TO_SELF,
new HashSet<>(Arrays.asList(
ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE)));
sharingOptionToContentTypes.put(SharingOption.QR_CODE,
new HashSet<>(Arrays.asList(
ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE)));
sharingOptionToContentTypes.put(
SharingOption.PRINT, new HashSet<>(Arrays.asList(ContentType.LINK_PAGE_VISIBLE)));
return sharingOptionToContentTypes;
}
/**
* Returns a list of {@link PropertyModel}s that should be shown given the {@code contentTypes}
* being shared.
*
* @param contentTypes a {@link Set} of {@link ContentType}.
* @return a list of {@link PropertyModel}s.
*/
List<PropertyModel> createPropertyModels(Set<Integer> contentTypes) {
ArrayList<PropertyModel> propertyModels = new ArrayList<>();
if (!Collections.disjoint(
mSharingOptionToContentTypes.get(SharingOption.SCREENSHOT), contentTypes)
&& ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARE_SCREENSHOT)) {
propertyModels.add(createScreenshotPropertyModel());
}
if (!Collections.disjoint(
mSharingOptionToContentTypes.get(SharingOption.COPY_LINK), contentTypes)) {
propertyModels.add(createCopyLinkPropertyModel());
}
if (!Collections.disjoint(mSharingOptionToContentTypes.get(SharingOption.SEND_TAB_TO_SELF),
contentTypes)) {
propertyModels.add(createSendTabToSelfPropertyModel());
}
if (!Collections.disjoint(
mSharingOptionToContentTypes.get(SharingOption.QR_CODE), contentTypes)) {
propertyModels.add(createQrCodePropertyModel());
}
if (!Collections.disjoint(
mSharingOptionToContentTypes.get(SharingOption.PRINT), contentTypes)
&& mPrefServiceBridge.getBoolean(Pref.PRINTING_ENABLED)) {
propertyModels.add(createPrintingPropertyModel());
}
return propertyModels;
}
/**
......@@ -78,7 +157,7 @@ class ChromeProvidedSharingOptionsProvider {
}
};
PropertyModel createScreenshotPropertyModel() {
private PropertyModel createScreenshotPropertyModel() {
return ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(mActivity, R.drawable.screenshot),
mActivity.getResources().getString(R.string.sharing_screenshot),
......@@ -98,7 +177,7 @@ class ChromeProvidedSharingOptionsProvider {
/*isFirstParty=*/true);
}
PropertyModel createCopyLinkPropertyModel() {
private PropertyModel createCopyLinkPropertyModel() {
return ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(mActivity, R.drawable.ic_content_copy_black),
mActivity.getResources().getString(R.string.sharing_copy_url), (shareParams) -> {
......@@ -120,7 +199,7 @@ class ChromeProvidedSharingOptionsProvider {
}, /*isFirstParty=*/true);
}
PropertyModel createSendTabToSelfPropertyModel() {
private PropertyModel createSendTabToSelfPropertyModel() {
return ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(mActivity, R.drawable.send_tab),
mActivity.getResources().getString(R.string.send_tab_to_self_share_activity_title),
......@@ -141,7 +220,7 @@ class ChromeProvidedSharingOptionsProvider {
/*isFirstParty=*/true);
}
PropertyModel createQrCodePropertyModel() {
private PropertyModel createQrCodePropertyModel() {
return ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(mActivity, R.drawable.qr_code),
mActivity.getResources().getString(R.string.qr_code_share_icon_label),
......@@ -158,7 +237,7 @@ class ChromeProvidedSharingOptionsProvider {
/*isFirstParty=*/true);
}
PropertyModel createPrintingPropertyModel() {
private PropertyModel createPrintingPropertyModel() {
return ShareSheetPropertyModelBuilder.createPropertyModel(
AppCompatResources.getDrawable(mActivity, R.drawable.sharing_print),
mActivity.getResources().getString(R.string.print_share_activity_title),
......@@ -177,4 +256,15 @@ class ChromeProvidedSharingOptionsProvider {
},
/*isFirstParty=*/true);
}
@IntDef({SharingOption.SCREENSHOT, SharingOption.COPY_LINK, SharingOption.SEND_TAB_TO_SELF,
SharingOption.QR_CODE, SharingOption.PRINT})
@Retention(RetentionPolicy.SOURCE)
@interface SharingOption {
int SCREENSHOT = 0;
int COPY_LINK = 1;
int SEND_TAB_TO_SELF = 2;
int QR_CODE = 3;
int PRINT = 4;
}
}
\ No newline at end of file
......@@ -24,7 +24,6 @@ import org.chromium.chrome.browser.send_tab_to_self.SendTabToSelfShareActivity;
import org.chromium.chrome.browser.share.qrcode.QrCodeShareActivity;
import org.chromium.chrome.browser.tab.SadTab;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabModel;
import org.chromium.chrome.browser.util.ChromeFileProvider;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController;
import org.chromium.components.browser_ui.share.ShareImageFileUtils;
......@@ -52,11 +51,11 @@ public class ShareDelegateImpl implements ShareDelegate {
private static boolean sScreenshotCaptureSkippedForTesting;
/**
* Construct a new {@link ShareDelegateImpl}.
* @param controller The BottomSheetController for the current activity.
* Constructs a new {@link ShareDelegateImpl}.
*
* @param controller The BottomSheetController for the current activity.
* @param tabProvider The ActivityTabProvider for the current visible tab.
* @param delegate The ShareSheetDelegate for the current activity.
* @param tabCreator The TabCreator for the current selected {@link TabModel}.
* @param delegate The ShareSheetDelegate for the current activity.
*/
public ShareDelegateImpl(BottomSheetController controller, ActivityTabProvider tabProvider,
ShareSheetDelegate delegate) {
......@@ -87,9 +86,10 @@ public class ShareDelegateImpl implements ShareDelegate {
/**
* Triggered when the share menu item is selected.
* This creates and shows a share intent picker dialog or starts a share intent directly.
*
* @param shareDirectly Whether it should share directly with the activity that was most
* recently used to share.
* @param isIncognito Whether currentTab is incognito.
* @param isIncognito Whether currentTab is incognito.
*/
private void onShareSelected(
Activity activity, Tab currentTab, boolean shareDirectly, boolean isIncognito) {
......
......@@ -26,7 +26,7 @@ import org.chromium.ui.modelutil.PropertyKey;
import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.modelutil.SimpleRecyclerViewAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* Bottom sheet content to display a 2-row custom share sheet.
......@@ -42,7 +42,7 @@ public class ShareSheetBottomSheetContent implements BottomSheetContent, OnItemC
*
* @param context The context the share sheet was launched from.
*/
public ShareSheetBottomSheetContent(Context context) {
ShareSheetBottomSheetContent(Context context) {
mContext = context;
createContentView();
......@@ -60,8 +60,8 @@ public class ShareSheetBottomSheetContent implements BottomSheetContent, OnItemC
* @param topRowModels The PropertyModels used to build the top row.
* @param bottomRowModels The PropertyModels used to build the bottom row.
*/
public void createRecyclerViews(
ArrayList<PropertyModel> topRowModels, ArrayList<PropertyModel> bottomRowModels) {
void createRecyclerViews(
List<PropertyModel> topRowModels, List<PropertyModel> bottomRowModels) {
RecyclerView topRow = this.getContentView().findViewById(R.id.share_sheet_chrome_apps);
if (topRowModels != null && topRowModels.size() > 0) {
View divider = this.getContentView().findViewById(R.id.share_sheet_divider);
......@@ -78,7 +78,7 @@ public class ShareSheetBottomSheetContent implements BottomSheetContent, OnItemC
new ScrollEventReporter("SharingHubAndroid.BottomRowScrolled"));
}
private void populateView(ArrayList<PropertyModel> models, RecyclerView view) {
private void populateView(List<PropertyModel> models, RecyclerView view) {
ModelList modelList = new ModelList();
for (PropertyModel model : models) {
modelList.add(new ListItem(SHARE_SHEET_ITEM, model));
......
......@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.share;
import android.app.Activity;
import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.content.res.AppCompatResources;
......@@ -13,14 +14,17 @@ import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ActivityTabProvider;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.preferences.Pref;
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetController;
import org.chromium.components.browser_ui.share.ShareParams;
import org.chromium.ui.modelutil.PropertyModel;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* Coordinator for displaying the share sheet.
......@@ -36,16 +40,19 @@ class ShareSheetCoordinator {
/**
* Constructs a new ShareSheetCoordinator.
*
* @param controller The BottomSheetController for the current activity.
* @param provider The ActivityTabProvider for the current visible tab.
* @param controller The {@link BottomSheetController} for the current activity.
* @param provider The {@link ActivityTabProvider} for the current visible tab.
* @param modelBuilder The {@link ShareSheetPropertyModelBuilder} for the share sheet.
* @param prefServiceBridge The {@link PrefServiceBridge} singleton. This provides preferences
* for the Chrome-provided property models.
*/
ShareSheetCoordinator(BottomSheetController controller, ActivityTabProvider provider,
ShareSheetPropertyModelBuilder modelBuilder, PrefServiceBridge prefServiceBridge) {
mBottomSheetController = controller;
mActivityTabProvider = provider;
mPropertyModelBuilder = modelBuilder;
mPrefServiceBridge = prefServiceBridge;
mExcludeFirstParty = false;
mPrefServiceBridge = prefServiceBridge;
}
void showShareSheet(ShareParams params, boolean saveLastUsed, long shareStartTime) {
......@@ -57,8 +64,8 @@ class ShareSheetCoordinator {
ShareSheetBottomSheetContent bottomSheet = new ShareSheetBottomSheetContent(activity);
mShareStartTime = shareStartTime;
ArrayList<PropertyModel> chromeFeatures = createTopRowPropertyModels(bottomSheet, activity);
ArrayList<PropertyModel> thirdPartyApps =
List<PropertyModel> chromeFeatures = createTopRowPropertyModels(bottomSheet, activity);
List<PropertyModel> thirdPartyApps =
createBottomRowPropertyModels(bottomSheet, activity, params, saveLastUsed);
bottomSheet.createRecyclerViews(chromeFeatures, thirdPartyApps);
......@@ -72,38 +79,29 @@ class ShareSheetCoordinator {
}
// Used by first party features to share with only non-chrome apps.
protected void showThirdPartyShareSheet(ShareParams params, boolean saveLastUsed, long shareStartTime) {
protected void showThirdPartyShareSheet(
ShareParams params, boolean saveLastUsed, long shareStartTime) {
mExcludeFirstParty = true;
showShareSheet(params, saveLastUsed, shareStartTime);
}
@VisibleForTesting
ArrayList<PropertyModel> createTopRowPropertyModels(
List<PropertyModel> createTopRowPropertyModels(
ShareSheetBottomSheetContent bottomSheet, Activity activity) {
ChromeProvidedSharingOptionsProvider chromeProvidedSharingOptionsProvider =
new ChromeProvidedSharingOptionsProvider(activity, mActivityTabProvider,
mBottomSheetController, bottomSheet, mShareStartTime);
ArrayList<PropertyModel> models = new ArrayList<>();
if (mExcludeFirstParty) {
return models;
}
if (ChromeFeatureList.isEnabled(ChromeFeatureList.CHROME_SHARE_SCREENSHOT)) {
models.add(chromeProvidedSharingOptionsProvider.createScreenshotPropertyModel());
}
models.add(chromeProvidedSharingOptionsProvider.createCopyLinkPropertyModel());
models.add(chromeProvidedSharingOptionsProvider.createSendTabToSelfPropertyModel());
models.add(chromeProvidedSharingOptionsProvider.createQrCodePropertyModel());
if (mPrefServiceBridge.getBoolean(Pref.PRINTING_ENABLED)) {
models.add(chromeProvidedSharingOptionsProvider.createPrintingPropertyModel());
return new ArrayList<>();
}
return models;
ChromeProvidedSharingOptionsProvider chromeProvidedSharingOptionsProvider =
new ChromeProvidedSharingOptionsProvider(activity, mActivityTabProvider,
mBottomSheetController, bottomSheet, mPrefServiceBridge, mShareStartTime);
return chromeProvidedSharingOptionsProvider.createPropertyModels(new HashSet<>(
Arrays.asList(ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE,
ContentType.TEXT, ContentType.IMAGE, ContentType.OTHER_FILE_TYPE)));
}
@VisibleForTesting
ArrayList<PropertyModel> createBottomRowPropertyModels(ShareSheetBottomSheetContent bottomSheet,
List<PropertyModel> createBottomRowPropertyModels(ShareSheetBottomSheetContent bottomSheet,
Activity activity, ShareParams params, boolean saveLastUsed) {
ArrayList<PropertyModel> models = mPropertyModelBuilder.selectThirdPartyApps(
List<PropertyModel> models = mPropertyModelBuilder.selectThirdPartyApps(
bottomSheet, params, saveLastUsed, mShareStartTime);
// More...
PropertyModel morePropertyModel = ShareSheetPropertyModelBuilder.createPropertyModel(
......@@ -125,4 +123,15 @@ class ShareSheetCoordinator {
protected void disableFirstPartyFeaturesForTesting() {
mExcludeFirstParty = true;
}
@IntDef({ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE, ContentType.TEXT,
ContentType.IMAGE, ContentType.OTHER_FILE_TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface ContentType {
int LINK_PAGE_VISIBLE = 0;
int LINK_PAGE_NOT_VISIBLE = 1;
int TEXT = 2;
int IMAGE = 3;
int OTHER_FILE_TYPE = 4;
}
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.share;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import android.app.Activity;
import android.support.test.filters.MediumTest;
import android.support.test.rule.ActivityTestRule;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.share.ShareSheetCoordinator.ContentType;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.test.util.DummyUiActivity;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* Tests {@link ChromeProvidedSharingOptionsProvider}.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
public class ChromeProvidedSharingOptionsProviderTest {
@Rule
public ActivityTestRule<DummyUiActivity> mActivityTestRule =
new ActivityTestRule<>(DummyUiActivity.class);
@Rule
public TestRule mFeatureProcessor = new Features.JUnitProcessor();
@Mock
private PrefServiceBridge mPrefServiceBridge;
private Activity mActivity;
private ChromeProvidedSharingOptionsProvider mChromeProvidedSharingOptionsProvider;
private static Set<Integer> sAllContentTypes =
ImmutableSet.of(ContentType.LINK_PAGE_VISIBLE, ContentType.LINK_PAGE_NOT_VISIBLE,
ContentType.TEXT, ContentType.IMAGE, ContentType.OTHER_FILE_TYPE);
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mActivity = mActivityTestRule.getActivity();
mChromeProvidedSharingOptionsProvider = new ChromeProvidedSharingOptionsProvider(mActivity,
/*activityTabProvider=*/null, /*bottomSheetController=*/null,
new ShareSheetBottomSheetContent(mActivity), mPrefServiceBridge,
/*shareStartTime=*/0);
// Return false to indicate printing is disabled.
Mockito.when(mPrefServiceBridge.getBoolean(anyInt())).thenReturn(false);
}
@Test
@MediumTest
@Features.EnableFeatures({ChromeFeatureList.CHROME_SHARE_SCREENSHOT})
public void createPropertyModels_screenshotEnabled_includesScreenshot() {
List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.createPropertyModels(sAllContentTypes);
Assert.assertEquals("Incorrect number of property models.", 4, propertyModels.size());
assertModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_screenshot),
mActivity.getResources().getString(R.string.sharing_copy_url),
mActivity.getResources().getString(
R.string.send_tab_to_self_share_activity_title),
mActivity.getResources().getString(R.string.qr_code_share_icon_label)));
assertModelsAreFirstParty(propertyModels);
}
@Test
@MediumTest
@Features.DisableFeatures({ChromeFeatureList.CHROME_SHARE_SCREENSHOT})
public void createPropertyModels_screenshotDisabled_doesNotIncludeScreenshot() {
List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.createPropertyModels(sAllContentTypes);
Assert.assertEquals("Incorrect number of property models.", 3, propertyModels.size());
assertModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_url),
mActivity.getResources().getString(
R.string.send_tab_to_self_share_activity_title),
mActivity.getResources().getString(R.string.qr_code_share_icon_label)));
assertModelsAreFirstParty(propertyModels);
}
@Test
@MediumTest
@Features.DisableFeatures({ChromeFeatureList.CHROME_SHARE_SCREENSHOT})
public void createPropertyModels_printingEnabled_includesPrinting() {
Mockito.when(mPrefServiceBridge.getBoolean(anyInt())).thenReturn(true);
List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.createPropertyModels(sAllContentTypes);
Assert.assertEquals("Incorrect number of property models.", 4, propertyModels.size());
assertModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_url),
mActivity.getResources().getString(
R.string.send_tab_to_self_share_activity_title),
mActivity.getResources().getString(R.string.qr_code_share_icon_label),
mActivity.getResources().getString(R.string.print_share_activity_title)));
assertModelsAreFirstParty(propertyModels);
}
@Test
@MediumTest
@Features.EnableFeatures({ChromeFeatureList.CHROME_SHARE_SCREENSHOT})
public void createPropertyModels_filtersByContentType() {
Mockito.when(mPrefServiceBridge.getBoolean(anyInt())).thenReturn(true);
List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.createPropertyModels(
ImmutableSet.of(ContentType.LINK_PAGE_NOT_VISIBLE));
Assert.assertEquals("Incorrect number of property models.", 3, propertyModels.size());
assertModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_copy_url),
mActivity.getResources().getString(
R.string.send_tab_to_self_share_activity_title),
mActivity.getResources().getString(R.string.qr_code_share_icon_label)));
assertModelsAreFirstParty(propertyModels);
}
@Test
@MediumTest
@Features.EnableFeatures({ChromeFeatureList.CHROME_SHARE_SCREENSHOT})
public void createPropertyModels_multipleTypes_filtersByContentType() {
Mockito.when(mPrefServiceBridge.getBoolean(anyInt())).thenReturn(true);
List<PropertyModel> propertyModels =
mChromeProvidedSharingOptionsProvider.createPropertyModels(
ImmutableSet.of(ContentType.LINK_PAGE_NOT_VISIBLE, ContentType.IMAGE));
Assert.assertEquals("Incorrect number of property models.", 4, propertyModels.size());
assertModelsAreInTheRightOrder(propertyModels,
ImmutableList.of(mActivity.getResources().getString(R.string.sharing_screenshot),
mActivity.getResources().getString(R.string.sharing_copy_url),
mActivity.getResources().getString(
R.string.send_tab_to_self_share_activity_title),
mActivity.getResources().getString(R.string.qr_code_share_icon_label)));
assertModelsAreFirstParty(propertyModels);
}
private void assertModelsAreInTheRightOrder(
List<PropertyModel> propertyModels, List<String> expectedOrder) {
ImmutableList.Builder<String> actualLabelOrder = ImmutableList.builder();
for (PropertyModel propertyModel : propertyModels) {
actualLabelOrder.add(propertyModel.get(ShareSheetItemViewProperties.LABEL));
}
assertEquals(
"Property models in the wrong order.", expectedOrder, actualLabelOrder.build());
}
private void assertModelsAreFirstParty(Collection<PropertyModel> propertyModels) {
for (PropertyModel propertyModel : propertyModels) {
assertEquals(propertyModel.get(ShareSheetItemViewProperties.LABEL)
+ " isn't marked as first party.",
true, propertyModel.get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
}
}
}
......@@ -7,7 +7,6 @@ package org.chromium.chrome.browser.share;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import android.app.Activity;
import android.support.test.filters.MediumTest;
......@@ -25,9 +24,7 @@ import org.mockito.MockitoAnnotations;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.ui.modelutil.PropertyModel;
......@@ -35,6 +32,7 @@ import org.chromium.ui.test.util.DummyUiActivity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Tests {@link ShareSheetCoordinator}.
......@@ -52,9 +50,6 @@ public final class ShareSheetCoordinatorTest {
@Mock
private ShareSheetPropertyModelBuilder mPropertyModelBuilder;
@Mock
private PrefServiceBridge mPrefServiceBridge;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
......@@ -76,95 +71,19 @@ public final class ShareSheetCoordinatorTest {
Mockito.when(mPropertyModelBuilder.selectThirdPartyApps(
any(), any(), anyBoolean(), anyLong()))
.thenReturn(thirdPartyPropertyModels);
// Return true to indicate printing is enabled.
Mockito.when(mPrefServiceBridge.getBoolean(anyInt())).thenReturn(true);
}
@Test
@MediumTest
@Features.DisableFeatures({ChromeFeatureList.CHROME_SHARE_SCREENSHOT})
public void testCreateTopRowPropertyModelsScreenshotsDisabled() {
ShareSheetCoordinator coordinator =
new ShareSheetCoordinator(null, null, mPropertyModelBuilder, mPrefServiceBridge);
Activity activity = mActivityTestRule.getActivity();
ShareSheetBottomSheetContent bottomSheet = new ShareSheetBottomSheetContent(activity);
ArrayList<PropertyModel> propertyModels =
coordinator.createTopRowPropertyModels(bottomSheet, activity);
Assert.assertEquals("Incorrect number of property models.", 4, propertyModels.size());
Assert.assertEquals("First property model isn't Copy URL.",
activity.getResources().getString(R.string.sharing_copy_url),
propertyModels.get(0).get(ShareSheetItemViewProperties.LABEL));
Assert.assertEquals("First property model isn't marked as first party.", true,
propertyModels.get(0).get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
Assert.assertEquals("Second property model isn't SendTabToSelf.",
activity.getResources().getString(R.string.send_tab_to_self_share_activity_title),
propertyModels.get(1).get(ShareSheetItemViewProperties.LABEL));
Assert.assertEquals("Second property model isn't marked as first party.", true,
propertyModels.get(1).get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
Assert.assertEquals("Third property model isn't QR Code.",
activity.getResources().getString(R.string.qr_code_share_icon_label),
propertyModels.get(2).get(ShareSheetItemViewProperties.LABEL));
Assert.assertEquals("Third property model isn't marked as first party.", true,
propertyModels.get(2).get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
Assert.assertEquals("Fourth property model isn't Print.",
activity.getResources().getString(R.string.print_share_activity_title),
propertyModels.get(3).get(ShareSheetItemViewProperties.LABEL));
Assert.assertEquals("Fourth property model isn't marked as first party.", true,
propertyModels.get(3).get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
}
@Test
@MediumTest
@Features.EnableFeatures({ChromeFeatureList.CHROME_SHARE_SCREENSHOT})
public void testCreateTopRowPropertyModelsScreenshotsEnabled() {
ShareSheetCoordinator coordinator =
new ShareSheetCoordinator(null, null, mPropertyModelBuilder, mPrefServiceBridge);
Activity activity = mActivityTestRule.getActivity();
ShareSheetBottomSheetContent bottomSheet = new ShareSheetBottomSheetContent(activity);
ArrayList<PropertyModel> propertyModels =
coordinator.createTopRowPropertyModels(bottomSheet, activity);
Assert.assertEquals("Incorrect number of property models.", 5, propertyModels.size());
Assert.assertEquals("First property model isn't Screenshotz.",
activity.getResources().getString(R.string.sharing_screenshot),
propertyModels.get(0).get(ShareSheetItemViewProperties.LABEL));
Assert.assertEquals("First property model isn't marked as first party.", true,
propertyModels.get(0).get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
Assert.assertEquals("Second property model isn't Copy URL.",
activity.getResources().getString(R.string.sharing_copy_url),
propertyModels.get(1).get(ShareSheetItemViewProperties.LABEL));
Assert.assertEquals("Second property model isn't marked as first party.", true,
propertyModels.get(1).get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
Assert.assertEquals("Third property model isn't SendTabToSelf.",
activity.getResources().getString(R.string.send_tab_to_self_share_activity_title),
propertyModels.get(2).get(ShareSheetItemViewProperties.LABEL));
Assert.assertEquals("Third property model isn't marked as first party.", true,
propertyModels.get(2).get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
Assert.assertEquals("Fourth property model isn't QR Code.",
activity.getResources().getString(R.string.qr_code_share_icon_label),
propertyModels.get(3).get(ShareSheetItemViewProperties.LABEL));
Assert.assertEquals("Fourth property model isn't marked as first party.", true,
propertyModels.get(3).get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
Assert.assertEquals("Fifth property model isn't Print.",
activity.getResources().getString(R.string.print_share_activity_title),
propertyModels.get(4).get(ShareSheetItemViewProperties.LABEL));
Assert.assertEquals("Fifth property model isn't marked as first party.", true,
propertyModels.get(4).get(ShareSheetItemViewProperties.IS_FIRST_PARTY));
}
@Test
@MediumTest
public void disableFirstPartyFeatures() {
ShareSheetCoordinator coordinator =
new ShareSheetCoordinator(null, null, mPropertyModelBuilder, mPrefServiceBridge);
new ShareSheetCoordinator(null, null, mPropertyModelBuilder, null);
coordinator.disableFirstPartyFeaturesForTesting();
Activity activity = mActivityTestRule.getActivity();
ShareSheetBottomSheetContent bottomSheet = new ShareSheetBottomSheetContent(activity);
ArrayList<PropertyModel> propertyModels =
List<PropertyModel> propertyModels =
coordinator.createTopRowPropertyModels(bottomSheet, activity);
Assert.assertEquals("Property model list should be empty.", 0, propertyModels.size());
}
......@@ -173,11 +92,11 @@ public final class ShareSheetCoordinatorTest {
@MediumTest
public void testCreateBottomRowPropertyModels() {
ShareSheetCoordinator coordinator =
new ShareSheetCoordinator(null, null, mPropertyModelBuilder, mPrefServiceBridge);
new ShareSheetCoordinator(null, null, mPropertyModelBuilder, null);
Activity activity = mActivityTestRule.getActivity();
ShareSheetBottomSheetContent bottomSheet = new ShareSheetBottomSheetContent(activity);
ArrayList<PropertyModel> propertyModels = coordinator.createBottomRowPropertyModels(
List<PropertyModel> propertyModels = coordinator.createBottomRowPropertyModels(
bottomSheet, activity, /*shareParams=*/null, /*saveLastUsed=*/false);
Assert.assertEquals("Incorrect number of property models.", 3, propertyModels.size());
Assert.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