Commit 74292531 authored by Peter Kotwicz's avatar Peter Kotwicz Committed by Commit Bot

[Android Refactor] Cleanup share target related JUnit tests

This CL is in preparation to adding logic in WebApkShareTargetUtil
which deals with androidx.browser.trusted.sharing.ShareTarget
(e.g. computing equality)

This CL:
1) Adds tests in WebApkInfoTest for:
  - invalid JSON in AndroidManifest in
org.chromium.webapk.shell_apk.shareParamNames <meta-data> key
  - building WebApkInfo.ShareData for Intent.EXTRA_STREAM
2) Refactors tests:
  - Introduces builder for WebApkInfo.ShareTarget This will simplify
    adding more tests
  - Replaces explicit references to WebApkShareTargetUtilShadow with
    WebApkShareTargetUtil. Robolectric magic changes references to
    WebApkShareTargetUtil to point to WebApkShareTargetUtilShadow
  - Introduces helper method createMinimalWebApkIntent() in
    WebApkTests to cut down on amount of boiler plate code.

BUG=997771

Change-Id: I47204c2fba3c4884685feb864d3b9da0f3b90664
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1766432
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695892}
parent bbe7ea0f
...@@ -12,8 +12,8 @@ import android.os.Build; ...@@ -12,8 +12,8 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Browser; import android.provider.Browser;
import org.hamcrest.Matchers;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
...@@ -108,8 +108,14 @@ public class WebApkInfoTest { ...@@ -108,8 +108,14 @@ public class WebApkInfoTest {
} }
} }
@Before /**
public void setUp() { * Returns simplest intent which builds valid WebApkInfo via {@link WebApkInfo#create()}.
*/
private static Intent createMinimalWebApkIntent(String webApkPackage, String url) {
Intent intent = new Intent();
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackage);
intent.putExtra(ShortcutHelper.EXTRA_URL, url);
return intent;
} }
@Test @Test
...@@ -130,9 +136,15 @@ public class WebApkInfoTest { ...@@ -130,9 +136,15 @@ public class WebApkInfoTest {
Bundle shareActivityBundle = new Bundle(); Bundle shareActivityBundle = new Bundle();
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "action0"); shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "action0");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TITLE, "title0"); shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TITLE, "title0");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "text0"); shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "text0");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_URL, "url0"); shareActivityBundle.putString(
WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"name1\", \"name2\"]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS,
"[[\"text/plain\"], [\"image/png\", \"image/jpeg\"]]");
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, new Bundle[] {shareActivityBundle}); WEBAPK_PACKAGE_NAME, bundle, new Bundle[] {shareActivityBundle});
...@@ -178,9 +190,13 @@ public class WebApkInfoTest { ...@@ -178,9 +190,13 @@ public class WebApkInfoTest {
WebApkInfo.ShareTarget shareTarget = info.shareTarget(); WebApkInfo.ShareTarget shareTarget = info.shareTarget();
Assert.assertNotNull(shareTarget); Assert.assertNotNull(shareTarget);
Assert.assertEquals("action0", shareTarget.getAction()); Assert.assertEquals("action0", shareTarget.getAction());
Assert.assertTrue(shareTarget.isShareMethodPost());
Assert.assertTrue(shareTarget.isShareEncTypeMultipart());
Assert.assertEquals("title0", shareTarget.getParamTitle()); Assert.assertEquals("title0", shareTarget.getParamTitle());
Assert.assertEquals("text0", shareTarget.getParamText()); Assert.assertEquals("text0", shareTarget.getParamText());
Assert.assertEquals("url0", shareTarget.getParamUrl()); Assert.assertEquals(new String[] {"name1", "name2"}, shareTarget.getFileNames());
Assert.assertEquals(new String[][] {{"text/plain"}, {"image/png", "image/jpeg"}},
shareTarget.getFileAccepts());
} }
/** /**
...@@ -198,9 +214,7 @@ public class WebApkInfoTest { ...@@ -198,9 +214,7 @@ public class WebApkInfoTest {
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, intentStartUrl);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, intentStartUrl);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
Assert.assertEquals(intentStartUrl, info.url()); Assert.assertEquals(intentStartUrl, info.url());
...@@ -230,9 +244,7 @@ public class WebApkInfoTest { ...@@ -230,9 +244,7 @@ public class WebApkInfoTest {
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, intentStartUrl);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, intentStartUrl);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
Assert.assertEquals(scopeFromManifestStartUrl, info.scopeUrl()); Assert.assertEquals(scopeFromManifestStartUrl, info.scopeUrl());
...@@ -255,9 +267,7 @@ public class WebApkInfoTest { ...@@ -255,9 +267,7 @@ public class WebApkInfoTest {
iconUrl1 + " " + murmur2Hash1 + " " + iconUrl2 + " " + murmur2Hash2); iconUrl1 + " " + murmur2Hash1 + " " + iconUrl2 + " " + murmur2Hash2);
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
Map<String, String> iconUrlToMurmur2HashMap = info.iconUrlToMurmur2HashMap(); Map<String, String> iconUrlToMurmur2HashMap = info.iconUrlToMurmur2HashMap();
...@@ -280,9 +290,7 @@ public class WebApkInfoTest { ...@@ -280,9 +290,7 @@ public class WebApkInfoTest {
bundle.putString(WebApkMetaDataKeys.ICON_URLS_AND_ICON_MURMUR2_HASHES, "randomUrl " + hash); bundle.putString(WebApkMetaDataKeys.ICON_URLS_AND_ICON_MURMUR2_HASHES, "randomUrl " + hash);
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
Map<String, String> iconUrlToMurmur2HashMap = info.iconUrlToMurmur2HashMap(); Map<String, String> iconUrlToMurmur2HashMap = info.iconUrlToMurmur2HashMap();
...@@ -303,9 +311,7 @@ public class WebApkInfoTest { ...@@ -303,9 +311,7 @@ public class WebApkInfoTest {
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
Assert.assertTrue(info.shouldForceNavigation()); Assert.assertTrue(info.shouldForceNavigation());
...@@ -323,9 +329,7 @@ public class WebApkInfoTest { ...@@ -323,9 +329,7 @@ public class WebApkInfoTest {
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.COUNT + 1); intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.COUNT + 1);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
...@@ -347,9 +351,7 @@ public class WebApkInfoTest { ...@@ -347,9 +351,7 @@ public class WebApkInfoTest {
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
Assert.assertEquals(name, info.name()); Assert.assertEquals(name, info.name());
...@@ -376,9 +378,7 @@ public class WebApkInfoTest { ...@@ -376,9 +378,7 @@ public class WebApkInfoTest {
WebApkIntentDataProvider.RESOURCE_STRING_TYPE, WEBAPK_PACKAGE_NAME, 2, shortName); WebApkIntentDataProvider.RESOURCE_STRING_TYPE, WEBAPK_PACKAGE_NAME, 2, shortName);
WebApkTestHelper.setResource(WEBAPK_PACKAGE_NAME, res); WebApkTestHelper.setResource(WEBAPK_PACKAGE_NAME, res);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
Assert.assertEquals(name, info.name()); Assert.assertEquals(name, info.name());
...@@ -398,9 +398,7 @@ public class WebApkInfoTest { ...@@ -398,9 +398,7 @@ public class WebApkInfoTest {
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.EXTERNAL_INTENT); intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.EXTERNAL_INTENT);
intent.putExtra( intent.putExtra(
Browser.EXTRA_APPLICATION_ID, RuntimeEnvironment.application.getPackageName()); Browser.EXTRA_APPLICATION_ID, RuntimeEnvironment.application.getPackageName());
...@@ -420,9 +418,7 @@ public class WebApkInfoTest { ...@@ -420,9 +418,7 @@ public class WebApkInfoTest {
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.EXTERNAL_INTENT); intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.EXTERNAL_INTENT);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, "com.google.android.talk"); intent.putExtra(Browser.EXTRA_APPLICATION_ID, "com.google.android.talk");
...@@ -442,9 +438,7 @@ public class WebApkInfoTest { ...@@ -442,9 +438,7 @@ public class WebApkInfoTest {
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_SELECTED_SHARE_TARGET_ACTIVITY_CLASS_NAME, intent.putExtra(WebApkConstants.EXTRA_WEBAPK_SELECTED_SHARE_TARGET_ACTIVITY_CLASS_NAME,
"something"); "something");
...@@ -469,9 +463,7 @@ public class WebApkInfoTest { ...@@ -469,9 +463,7 @@ public class WebApkInfoTest {
bundle.putString(WebApkMetaDataKeys.START_URL, START_URL); bundle.putString(WebApkMetaDataKeys.START_URL, START_URL);
WebApkTestHelper.registerWebApkWithMetaData( WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */); WEBAPK_PACKAGE_NAME, bundle, null /* shareTargetMetaData */);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
Assert.assertEquals(WebApkDistributor.BROWSER, info.distributor()); Assert.assertEquals(WebApkDistributor.BROWSER, info.distributor());
...@@ -494,15 +486,87 @@ public class WebApkInfoTest { ...@@ -494,15 +486,87 @@ public class WebApkInfoTest {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(WebApkMetaDataKeys.START_URL, START_URL); bundle.putString(WebApkMetaDataKeys.START_URL, START_URL);
WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, bundle, null); WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, bundle, null);
Intent intent = new Intent(); Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME);
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL);
WebApkInfo info = WebApkInfo.create(intent); WebApkInfo info = WebApkInfo.create(intent);
Assert.assertNotNull(info.shareTarget()); Assert.assertNotNull(info.shareTarget());
Assert.assertEquals("", info.shareTarget().getAction()); Assert.assertEquals("", info.shareTarget().getAction());
} }
/**
* Tests that {@link WebApkInfo.ShareTarget#getFileNames()} returns an empty list when the
* {@link shareParamNames} <meta-data> tag is not a JSON array.
*/
@Test
public void testPostShareTargetInvalidParamNames() {
Bundle bundle = new Bundle();
bundle.putString(WebApkMetaDataKeys.START_URL, START_URL);
Bundle shareActivityBundle = new Bundle();
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "not an array");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, bundle, new Bundle[] {shareActivityBundle});
Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
WebApkInfo info = WebApkInfo.create(intent);
WebApkInfo.ShareTarget shareTarget = info.shareTarget();
Assert.assertNotNull(shareTarget);
Assert.assertEquals(0, shareTarget.getFileNames().length);
}
/**
* Tests building {@link WebApkInfo.ShareData} when {@link Intent.EXTRA_STREAM} has a Uri value.
*/
@Test
public void testShareDataUriString() {
Bundle bundle = new Bundle();
bundle.putString(WebApkMetaDataKeys.START_URL, START_URL);
WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, bundle, null);
Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_SELECTED_SHARE_TARGET_ACTIVITY_CLASS_NAME,
WebApkTestHelper.getGeneratedShareTargetActivityClassName(0));
Uri sharedFileUri = Uri.parse("mock-uri-1");
intent.putExtra(Intent.EXTRA_STREAM, sharedFileUri);
WebApkInfo info = WebApkInfo.create(intent);
WebApkInfo.ShareData shareData = info.shareData();
Assert.assertNotNull(shareData);
Assert.assertNotNull(shareData.files);
Assert.assertThat(shareData.files, Matchers.contains(sharedFileUri));
}
/**
* Tests building {@link WebApkInfo.ShareData} when {@link Intent.EXTRA_STREAM} has an ArrayList
* value.
*/
@Test
public void testShareDataUriList() {
Bundle bundle = new Bundle();
bundle.putString(WebApkMetaDataKeys.START_URL, START_URL);
WebApkTestHelper.registerWebApkWithMetaData(WEBAPK_PACKAGE_NAME, bundle, null);
Intent intent = createMinimalWebApkIntent(WEBAPK_PACKAGE_NAME, START_URL);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_SELECTED_SHARE_TARGET_ACTIVITY_CLASS_NAME,
WebApkTestHelper.getGeneratedShareTargetActivityClassName(0));
Uri sharedFileUri = Uri.parse("mock-uri-1");
ArrayList<Uri> uris = new ArrayList<>();
uris.add(sharedFileUri);
intent.putExtra(Intent.EXTRA_STREAM, uris);
WebApkInfo info = WebApkInfo.create(intent);
WebApkInfo.ShareData shareData = info.shareData();
Assert.assertNotNull(shareData);
Assert.assertNotNull(shareData.files);
Assert.assertThat(shareData.files, Matchers.contains(sharedFileUri));
}
/** /**
* Test that {@link WebApkInfo#backgroundColorFallbackToDefault()} uses * Test that {@link WebApkInfo#backgroundColorFallbackToDefault()} uses
* {@link SplashLayout#getDefaultBackgroundColor()} as the default background color if there is * {@link SplashLayout#getDefaultBackgroundColor()} as the default background color if there is
......
...@@ -4,9 +4,10 @@ ...@@ -4,9 +4,10 @@
package org.chromium.chrome.browser.webapps; package org.chromium.chrome.browser.webapps;
import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle;
import androidx.browser.trusted.sharing.ShareData;
import androidx.browser.trusted.sharing.ShareTarget;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
...@@ -16,13 +17,10 @@ import org.robolectric.annotation.Implementation; ...@@ -16,13 +17,10 @@ import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements; import org.robolectric.annotation.Implements;
import org.chromium.base.test.BaseRobolectricTestRunner; import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.ShortcutHelper;
import org.chromium.webapk.lib.common.WebApkConstants;
import org.chromium.webapk.lib.common.WebApkMetaDataKeys;
import org.chromium.webapk.test.WebApkTestHelper;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** /**
* Tests WebApkShareTargetUtil. * Tests WebApkShareTargetUtil.
...@@ -31,25 +29,62 @@ import java.util.ArrayList; ...@@ -31,25 +29,62 @@ import java.util.ArrayList;
@Config(manifest = Config.NONE, @Config(manifest = Config.NONE,
shadows = {WebApkShareTargetUtilTest.WebApkShareTargetUtilShadow.class}) shadows = {WebApkShareTargetUtilTest.WebApkShareTargetUtilShadow.class})
public class WebApkShareTargetUtilTest { public class WebApkShareTargetUtilTest {
private static final String WEBAPK_PACKAGE_NAME = "org.chromium.webapk.test_package"; /**
* Builder class for {@link WebApkInfo.ShareTarget}
*/
public class ShareTargetBuilder {
private String mAction;
private @ShareTarget.RequestMethod String mMethod;
private @ShareTarget.EncodingType String mEncodingType;
private String mParamTitle;
private String mParamText;
private List<String> mParamFileNames = new ArrayList<>();
private List<String[]> mParamFileAccepts = new ArrayList<>();
public ShareTargetBuilder(String action) {
mAction = action;
}
// Android Manifest meta data for {@link PACKAGE_NAME}. public void setMethod(@ShareTarget.RequestMethod String method) {
private static final String START_URL = "https://www.google.com/scope/a_is_for_apple"; mMethod = method;
}
private void registerWebApk(Bundle shareTargetBundle) { public void setEncodingType(@ShareTarget.EncodingType String encodingType) {
Bundle appBundle = new Bundle(); mEncodingType = encodingType;
appBundle.putString(WebApkMetaDataKeys.START_URL, START_URL); }
WebApkTestHelper.registerWebApkWithMetaData(
WEBAPK_PACKAGE_NAME, appBundle, new Bundle[] {shareTargetBundle}); public void setParamTitle(String paramTitle) {
} mParamTitle = paramTitle;
}
public void setParamText(String paramText) {
mParamText = paramText;
}
public void addParamFile(String name, String[] accepts) {
mParamFileNames.add(name);
mParamFileAccepts.add(accepts);
}
public void setParamFiles(List<String> names, List<String[]> accepts) {
mParamFileNames = names;
mParamFileAccepts = accepts;
}
private static Intent createBasicShareIntent() { WebApkInfo.ShareTarget build() {
Intent intent = new Intent(); String[] paramFileNames = null;
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, WEBAPK_PACKAGE_NAME); if (mParamFileNames != null) {
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_SELECTED_SHARE_TARGET_ACTIVITY_CLASS_NAME, paramFileNames = mParamFileNames.toArray(new String[0]);
WebApkTestHelper.getGeneratedShareTargetActivityClassName(0)); }
intent.putExtra(ShortcutHelper.EXTRA_URL, START_URL); String[][] paramFileAccepts = null;
return intent; if (mParamFileAccepts != null) {
paramFileAccepts = mParamFileAccepts.toArray(new String[0][]);
}
return new WebApkInfo.ShareTarget(mAction, mParamTitle, mParamText, null,
ShareTarget.METHOD_POST.equalsIgnoreCase(mMethod),
ShareTarget.ENCODING_TYPE_MULTIPART.equalsIgnoreCase(mEncodingType),
paramFileNames, paramFileAccepts);
}
} }
private static void assertPostData(WebApkShareTargetUtil.PostData postData, String[] names, private static void assertPostData(WebApkShareTargetUtil.PostData postData, String[] names,
...@@ -87,6 +122,9 @@ public class WebApkShareTargetUtilTest { ...@@ -87,6 +122,9 @@ public class WebApkShareTargetUtilTest {
} }
} }
/**
* Shadow class for {@link WebApkShareTargetUtil} which mocks out ContentProvider queries.
*/
@Implements(WebApkShareTargetUtil.class) @Implements(WebApkShareTargetUtil.class)
public static class WebApkShareTargetUtilShadow extends WebApkShareTargetUtil { public static class WebApkShareTargetUtilShadow extends WebApkShareTargetUtil {
@Implementation @Implementation
...@@ -114,27 +152,16 @@ public class WebApkShareTargetUtilTest { ...@@ -114,27 +152,16 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testGET() { public void testGET() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html"); shareTargetBuilder.setMethod(ShareTarget.METHOD_GET);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "GET"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_URL_ENCODED);
shareActivityBundle.putString(
WebApkMetaDataKeys.SHARE_ENCTYPE, "application/x-www-form-urlencoded"); ShareData shareData = new ShareData(null /* title */, null /* title */, null /* uris */);
registerWebApk(shareActivityBundle);
Assert.assertEquals(null, computePostData(shareTargetBuilder.build(), shareData));
Intent intent = createBasicShareIntent();
WebApkInfo infoWithShareMethodGet = WebApkInfo.create(intent); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
Assert.assertNotNull(computePostData(shareTargetBuilder.build(), shareData));
Assert.assertEquals(null,
WebApkShareTargetUtilShadow.computePostData(
infoWithShareMethodGet.shareTarget(), infoWithShareMethodGet.shareData()));
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST");
registerWebApk(shareActivityBundle);
// recreating the WebApkInfo because the shareActivityBundle used for registerWebApk() has
// changed
WebApkInfo infoWithShareMethodPost = WebApkInfo.create(intent);
Assert.assertNotEquals(null, computePostData(infoWithShareMethodPost));
} }
/** /**
...@@ -143,22 +170,16 @@ public class WebApkShareTargetUtilTest { ...@@ -143,22 +170,16 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostUrlEncoded() throws UnsupportedEncodingException { public void testPostUrlEncoded() throws UnsupportedEncodingException {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TITLE, "title"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "text"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_URL_ENCODED);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setParamTitle("title");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html"); shareTargetBuilder.setParamText("text");
shareActivityBundle.putString( ShareData shareData = new ShareData("extra_subject", "extra_text", null /* uris */);
WebApkMetaDataKeys.SHARE_ENCTYPE, "application/x-www-form-urlencoded");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent(); WebApkShareTargetUtil.PostData postData =
intent.putExtra(Intent.EXTRA_SUBJECT, "extra_subject"); computePostData(shareTargetBuilder.build(), shareData);
intent.putExtra(Intent.EXTRA_TEXT, "extra_text");
WebApkInfo info = WebApkInfo.create(intent);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info);
assertPostData(postData, new String[] {"title", "text"}, new boolean[] {false, false}, assertPostData(postData, new String[] {"title", "text"}, new boolean[] {false, false},
new String[] {"extra_subject", "extra_text"}, new String[] {"", ""}, new String[] {"extra_subject", "extra_text"}, new String[] {"", ""},
...@@ -171,21 +192,16 @@ public class WebApkShareTargetUtilTest { ...@@ -171,21 +192,16 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostMultipartWithNoNamesNoAccepts() { public void testPostMultipartWithNoNamesNoAccepts() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
// Note that names and accepts are not specified
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("mock-uri-1")); uris.add(Uri.parse("mock-uri-1"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData(null /* title */, null /* text */, uris);
WebApkInfo info = WebApkInfo.create(intent); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info);
assertPostData(postData, new String[] {}, new boolean[] {}, new String[] {}, assertPostData(postData, new String[] {}, new boolean[] {}, new String[] {},
new String[] {}, new String[] {}); new String[] {}, new String[] {});
...@@ -197,19 +213,13 @@ public class WebApkShareTargetUtilTest { ...@@ -197,19 +213,13 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostMultipartWithNoFilesNorText() { public void testPostMultipartWithNoFilesNorText() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"name\"]"); shareTargetBuilder.addParamFile("name", new String[] {"image/*"});
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
// Intent.EXTRA_STREAM is not specified.
WebApkInfo info = WebApkInfo.create(intent);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info); WebApkShareTargetUtil.PostData postData = computePostData(shareTargetBuilder.build(),
new ShareData(null /* title */, null /* text */, null /* uris */));
assertPostData(postData, new String[] {}, new boolean[] {}, new String[] {}, assertPostData(postData, new String[] {}, new boolean[] {}, new String[] {},
new String[] {}, new String[] {}); new String[] {}, new String[] {});
...@@ -217,22 +227,17 @@ public class WebApkShareTargetUtilTest { ...@@ -217,22 +227,17 @@ public class WebApkShareTargetUtilTest {
@Test @Test
public void testPostMultipartWithFiles() { public void testPostMultipartWithFiles() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"name\"]"); shareTargetBuilder.addParamFile("name", new String[] {"image/*"});
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("mock-uri-2")); uris.add(Uri.parse("mock-uri-2"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData(null /* title */, null /* text */, uris);
WebApkInfo info = WebApkInfo.create(intent);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
assertPostData(postData, new String[] {"name"}, new boolean[] {true}, assertPostData(postData, new String[] {"name"}, new boolean[] {true},
new String[] {"mock-uri-2"}, new String[] {"file-name-for-mock-uri-2"}, new String[] {"mock-uri-2"}, new String[] {"file-name-for-mock-uri-2"},
...@@ -241,25 +246,18 @@ public class WebApkShareTargetUtilTest { ...@@ -241,25 +246,18 @@ public class WebApkShareTargetUtilTest {
@Test @Test
public void testPostMultipartWithTexts() { public void testPostMultipartWithTexts() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"name\"]"); shareTargetBuilder.addParamFile("name", new String[] {"image/*"});
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]"); shareTargetBuilder.setParamText("share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text"); shareTargetBuilder.setParamTitle("share-title");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TITLE, "share-title");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_URL, "share-url");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent(); ShareData shareData =
intent.putExtra(Intent.EXTRA_SUBJECT, "shared_subject_value"); new ShareData("shared_subject_value", "shared_text_value", null /* uris */);
intent.putExtra(Intent.EXTRA_TEXT, "shared_text_value");
WebApkInfo info = WebApkInfo.create(intent); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info);
assertPostData(postData, new String[] {"share-title", "share-text"}, assertPostData(postData, new String[] {"share-title", "share-text"},
new boolean[] {false, false}, new boolean[] {false, false},
...@@ -269,25 +267,17 @@ public class WebApkShareTargetUtilTest { ...@@ -269,25 +267,17 @@ public class WebApkShareTargetUtilTest {
@Test @Test
public void testPostMultipartWithTextsOnlyTarget() { public void testPostMultipartWithTextsOnlyTarget() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[]"); shareTargetBuilder.setParamText("share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[]"); shareTargetBuilder.setParamTitle("share-title");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TITLE, "share-title");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_URL, "share-url");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent(); ShareData shareData =
intent.putExtra(Intent.EXTRA_SUBJECT, "shared_subject_value"); new ShareData("shared_subject_value", "shared_text_value", null /* uris */);
intent.putExtra(Intent.EXTRA_TEXT, "shared_text_value");
WebApkInfo info = WebApkInfo.create(intent); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info);
assertPostData(postData, new String[] {"share-title", "share-text"}, assertPostData(postData, new String[] {"share-title", "share-text"},
new boolean[] {false, false}, new boolean[] {false, false},
...@@ -297,29 +287,19 @@ public class WebApkShareTargetUtilTest { ...@@ -297,29 +287,19 @@ public class WebApkShareTargetUtilTest {
@Test @Test
public void testPostMultipartWithFileAndTexts() { public void testPostMultipartWithFileAndTexts() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"name\"]"); shareTargetBuilder.addParamFile("name", new String[] {"image/*"});
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]"); shareTargetBuilder.setParamText("share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text"); shareTargetBuilder.setParamTitle("share-title");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TITLE, "share-title");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_URL, "share-url");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
intent.putExtra(Intent.EXTRA_SUBJECT, "shared_subject_value");
intent.putExtra(Intent.EXTRA_TEXT, "shared_text_value");
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("mock-uri-3")); uris.add(Uri.parse("mock-uri-3"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData("shared_subject_value", "shared_text_value", uris);
WebApkInfo info = WebApkInfo.create(intent);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
assertPostData(postData, new String[] {"share-title", "share-text", "name"}, assertPostData(postData, new String[] {"share-title", "share-text", "name"},
new boolean[] {false, false, true}, new boolean[] {false, false, true},
...@@ -334,25 +314,18 @@ public class WebApkShareTargetUtilTest { ...@@ -334,25 +314,18 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostMultipartSharedTextFileMimeTypeNotInAccepts() { public void testPostMultipartSharedTextFileMimeTypeNotInAccepts() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"name\"]"); shareTargetBuilder.addParamFile("name", new String[] {"image/*"});
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]"); shareTargetBuilder.setParamText("share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("text-file-mock-uri")); uris.add(Uri.parse("text-file-mock-uri"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData(null /* title */, null /* text */, uris);
WebApkInfo info = WebApkInfo.create(intent); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info);
assertPostData(postData, new String[] {"share-text"}, new boolean[] {true}, assertPostData(postData, new String[] {"share-text"}, new boolean[] {true},
new String[] {"text-file-mock-uri"}, new String[] {""}, new String[] {"text-file-mock-uri"}, new String[] {""},
...@@ -365,28 +338,20 @@ public class WebApkShareTargetUtilTest { ...@@ -365,28 +338,20 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostMultipartSharedTextFileMimeTypeNotInAcceptsMultiple() { public void testPostMultipartSharedTextFileMimeTypeNotInAcceptsMultiple() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"name\"]"); shareTargetBuilder.addParamFile("name", new String[] {"image/*"});
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]"); shareTargetBuilder.setParamText("share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("text-file-mock-uri")); uris.add(Uri.parse("text-file-mock-uri"));
uris.add(Uri.parse("text-file-mock-uri2")); uris.add(Uri.parse("text-file-mock-uri2"));
uris.add(Uri.parse("text-file-mock-uri3")); uris.add(Uri.parse("text-file-mock-uri3"));
ShareData shareData = new ShareData(null /* title */, null /* text */, uris);
intent.putExtra(Intent.EXTRA_STREAM, uris); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
WebApkInfo info = WebApkInfo.create(intent);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info);
assertPostData(postData, new String[] {"share-text"}, new boolean[] {true}, assertPostData(postData, new String[] {"share-text"}, new boolean[] {true},
new String[] {"text-file-mock-uri"}, new String[] {""}, new String[] {"text-file-mock-uri"}, new String[] {""},
...@@ -400,26 +365,18 @@ public class WebApkShareTargetUtilTest { ...@@ -400,26 +365,18 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostMultipartSharedTextFileAndSharedSelection() { public void testPostMultipartSharedTextFileAndSharedSelection() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"name\"]"); shareTargetBuilder.addParamFile("name", new String[] {"image/*"});
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]"); shareTargetBuilder.setParamText("share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
intent.putExtra(Intent.EXTRA_TEXT, "shared_text_value");
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("text-file-mock-uri")); uris.add(Uri.parse("text-file-mock-uri"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData(null /* title */, "shared_text_value", uris);
WebApkInfo info = WebApkInfo.create(intent);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
assertPostData(postData, new String[] {"share-text"}, new boolean[] {false}, assertPostData(postData, new String[] {"share-text"}, new boolean[] {false},
new String[] {"shared_text_value"}, new String[] {""}, new String[] {"text/plain"}); new String[] {"shared_text_value"}, new String[] {""}, new String[] {"text/plain"});
...@@ -431,26 +388,18 @@ public class WebApkShareTargetUtilTest { ...@@ -431,26 +388,18 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostMultipartSharedTextFileMimeTypeInAccepts() { public void testPostMultipartSharedTextFileMimeTypeInAccepts() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString( shareTargetBuilder.addParamFile("share-text-file", new String[] {"text/*"});
WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"share-text-file\"]"); shareTargetBuilder.setParamText("share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"text/*\"]]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("text-mock-uri")); uris.add(Uri.parse("text-mock-uri"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData(null /* title */, null /* text */, uris);
WebApkInfo info = WebApkInfo.create(intent);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
assertPostData(postData, new String[] {"share-text-file"}, new boolean[] {true}, assertPostData(postData, new String[] {"share-text-file"}, new boolean[] {true},
new String[] {"text-mock-uri"}, new String[] {"file-name-for-text-mock-uri"}, new String[] {"text-mock-uri"}, new String[] {"file-name-for-text-mock-uri"},
...@@ -463,26 +412,17 @@ public class WebApkShareTargetUtilTest { ...@@ -463,26 +412,17 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostMultipartSharedTextSelectionNoParamTextPlainInAccepts() { public void testPostMultipartSharedTextSelectionNoParamTextPlainInAccepts() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString( shareTargetBuilder.addParamFile("share-text-file", new String[] {"text/*"});
WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"share-text-file\"]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"text/*\"]]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
intent.putExtra(Intent.EXTRA_TEXT, "shared_text_value");
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("text-mock-uri")); uris.add(Uri.parse("text-mock-uri"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData(null /* title */, "shared_text_value", uris);
WebApkInfo info = WebApkInfo.create(intent); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info);
assertPostData(postData, new String[] {"share-text-file", "share-text-file"}, assertPostData(postData, new String[] {"share-text-file", "share-text-file"},
new boolean[] {false, true}, new String[] {"shared_text_value", "text-mock-uri"}, new boolean[] {false, true}, new String[] {"shared_text_value", "text-mock-uri"},
...@@ -496,27 +436,18 @@ public class WebApkShareTargetUtilTest { ...@@ -496,27 +436,18 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostMultipartSharedTextSelectionHasParamText() { public void testPostMultipartSharedTextSelectionHasParamText() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString( shareTargetBuilder.addParamFile("share-text-file", new String[] {"text/*"});
WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"share-text-file\"]"); shareTargetBuilder.setParamText("share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"text/*\"]]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
intent.putExtra(Intent.EXTRA_TEXT, "shared_text_value");
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("text-mock-uri")); uris.add(Uri.parse("text-mock-uri"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData(null /* title */, "shared_text_value", uris);
WebApkInfo info = WebApkInfo.create(intent);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
assertPostData(postData, new String[] {"share-text", "share-text-file"}, assertPostData(postData, new String[] {"share-text", "share-text-file"},
new boolean[] {false, true}, new String[] {"shared_text_value", "text-mock-uri"}, new boolean[] {false, true}, new String[] {"shared_text_value", "text-mock-uri"},
...@@ -533,26 +464,17 @@ public class WebApkShareTargetUtilTest { ...@@ -533,26 +464,17 @@ public class WebApkShareTargetUtilTest {
*/ */
@Test @Test
public void testPostMultipartSharedTextSelectionNoParamTextPlainNotInAccepts() { public void testPostMultipartSharedTextSelectionNoParamTextPlainNotInAccepts() {
Bundle shareActivityBundle = new Bundle(); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString( shareTargetBuilder.addParamFile("share-text-file", new String[] {"image/*"});
WebApkMetaDataKeys.SHARE_PARAM_NAMES, "[\"share-text-file\"]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
intent.putExtra(Intent.EXTRA_TEXT, "shared_text_value");
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("mock-uri")); uris.add(Uri.parse("mock-uri"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData(null /* title */, "shared_text_value", uris);
WebApkInfo info = WebApkInfo.create(intent); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info);
assertPostData(postData, new String[] {"share-text-file"}, new boolean[] {true}, assertPostData(postData, new String[] {"share-text-file"}, new boolean[] {true},
new String[] {"mock-uri"}, new String[] {"file-name-for-mock-uri"}, new String[] {"mock-uri"}, new String[] {"file-name-for-mock-uri"},
...@@ -561,29 +483,22 @@ public class WebApkShareTargetUtilTest { ...@@ -561,29 +483,22 @@ public class WebApkShareTargetUtilTest {
@Test @Test
public void testPostMultipartWithFileAndInValidParamNames() { public void testPostMultipartWithFileAndInValidParamNames() {
Bundle shareActivityBundle = new Bundle(); List<String[]> paramFileAccepts = new ArrayList<>();
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_METHOD, "POST"); paramFileAccepts.add(new String[] {"image/*"});
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ENCTYPE, "multipart/form-data");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_NAMES, "not a array"); ShareTargetBuilder shareTargetBuilder = new ShareTargetBuilder("/share.html");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_ACCEPTS, "[[\"image/*\"]]"); shareTargetBuilder.setMethod(ShareTarget.METHOD_POST);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TEXT, "share-text"); shareTargetBuilder.setEncodingType(ShareTarget.ENCODING_TYPE_MULTIPART);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_TITLE, "share-title"); shareTargetBuilder.setParamFiles(null, paramFileAccepts);
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_PARAM_URL, "share-url"); shareTargetBuilder.setParamText("share-text");
shareActivityBundle.putString(WebApkMetaDataKeys.SHARE_ACTION, "/share.html"); shareTargetBuilder.setParamTitle("share-title");
registerWebApk(shareActivityBundle);
Intent intent = createBasicShareIntent();
intent.putExtra(Intent.EXTRA_SUBJECT, "shared_subject_value");
intent.putExtra(Intent.EXTRA_TEXT, "shared_text_value");
ArrayList<Uri> uris = new ArrayList<>(); ArrayList<Uri> uris = new ArrayList<>();
uris.add(Uri.parse("mock-uri")); uris.add(Uri.parse("mock-uri"));
intent.putExtra(Intent.EXTRA_STREAM, uris); ShareData shareData = new ShareData("shared_subject_value", "shared_text_value", uris);
WebApkInfo info = WebApkInfo.create(intent); WebApkShareTargetUtil.PostData postData =
computePostData(shareTargetBuilder.build(), shareData);
WebApkShareTargetUtilShadow.PostData postData = computePostData(info);
// with invalid name parameter from Android manifest, we ignore the file sharing part. // with invalid name parameter from Android manifest, we ignore the file sharing part.
assertPostData(postData, new String[] {"share-title", "share-text"}, assertPostData(postData, new String[] {"share-title", "share-text"},
...@@ -592,7 +507,15 @@ public class WebApkShareTargetUtilTest { ...@@ -592,7 +507,15 @@ public class WebApkShareTargetUtilTest {
new String[] {"text/plain", "text/plain"}); new String[] {"text/plain", "text/plain"});
} }
private WebApkShareTargetUtil.PostData computePostData(WebApkInfo info) { private WebApkShareTargetUtil.PostData computePostData(
return WebApkShareTargetUtilShadow.computePostData(info.shareTarget(), info.shareData()); WebApkInfo.ShareTarget shareTarget, ShareData shareData) {
WebApkInfo.ShareData webApkShareData = new WebApkInfo.ShareData();
webApkShareData.subject = shareData.title;
webApkShareData.text = shareData.text;
if (shareData.uris != null) {
webApkShareData.files = new ArrayList(shareData.uris);
}
return WebApkShareTargetUtil.computePostData(shareTarget, webApkShareData);
} }
} }
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