Commit fa849f85 authored by Lei Tian's avatar Lei Tian Committed by Commit Bot

Chrome retrieves Browser Actions' custom item icons through resource ids

Browser Actions will pass only the resource ids of the custom items'
icons through the Intent, instead of passing the whole Bitmaps. Then
with the source package name, Chrome can retrieve the client app's
Resources by PackageManager.getResourcesForApplication() and use it to
get the icons.

Bug: 775798
Change-Id: Ia63f07a6245673df63e71b550ffff2f63753964f
Reviewed-on: https://chromium-review.googlesource.com/758745
Commit-Queue: Lei Tian <ltian@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#516036}
parent a317bd1a
...@@ -303,7 +303,7 @@ deps = { ...@@ -303,7 +303,7 @@ deps = {
}, },
'src/third_party/custom_tabs_client/src': { 'src/third_party/custom_tabs_client/src': {
'url': Var('chromium_git') + '/external/github.com/GoogleChrome/custom-tabs-client.git' + '@' + 'cff061038b852d647f7044d828a9df78aa135f38', 'url': Var('chromium_git') + '/external/github.com/GoogleChrome/custom-tabs-client.git' + '@' + 'afe8a13457103292fb1ba49877a0b0e5abdc2f36',
'condition': 'checkout_android', 'condition': 'checkout_android',
}, },
......
...@@ -731,7 +731,7 @@ by a child template that "extends" this file. ...@@ -731,7 +731,7 @@ by a child template that "extends" this file.
android:exported="true" android:exported="true"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|mcc|mnc|screenLayout|smallestScreenSize"> android:configChanges="orientation|keyboardHidden|keyboard|screenSize|mcc|mnc|screenLayout|smallestScreenSize">
<intent-filter> <intent-filter>
<action android:name="android.support.customtabs.browseractions.browser_action_open" /> <action android:name="androidx.browser.browseractions.browser_action_open" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" /> <data android:scheme="http" />
<data android:scheme="https" /> <data android:scheme="https" />
......
...@@ -40,8 +40,10 @@ public class BrowserActionActivity extends AsyncInitializationActivity { ...@@ -40,8 +40,10 @@ public class BrowserActionActivity extends AsyncInitializationActivity {
private int mType; private int mType;
private Uri mUri; private Uri mUri;
private String mCreatorPackageName; @VisibleForTesting
private List<BrowserActionItem> mActions = new ArrayList<>(); String mCreatorPackageName;
@VisibleForTesting
List<BrowserActionItem> mActions = new ArrayList<>();
private PendingIntent mOnBrowserActionSelectedCallback; private PendingIntent mOnBrowserActionSelectedCallback;
private BrowserActionsContextMenuHelper mHelper; private BrowserActionsContextMenuHelper mHelper;
......
...@@ -9,6 +9,11 @@ import android.app.PendingIntent; ...@@ -9,6 +9,11 @@ import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException; import android.app.PendingIntent.CanceledException;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.support.customtabs.browseractions.BrowserActionItem; import android.support.customtabs.browseractions.BrowserActionItem;
...@@ -22,7 +27,9 @@ import android.view.View; ...@@ -22,7 +27,9 @@ import android.view.View;
import android.view.View.OnAttachStateChangeListener; import android.view.View.OnAttachStateChangeListener;
import android.view.View.OnCreateContextMenuListener; import android.view.View.OnCreateContextMenuListener;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Callback; import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.base.metrics.RecordHistogram; import org.chromium.base.metrics.RecordHistogram;
...@@ -166,7 +173,7 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe ...@@ -166,7 +173,7 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe
mOnBrowserActionSelectedCallback = onBrowserActionSelectedCallback; mOnBrowserActionSelectedCallback = onBrowserActionSelectedCallback;
mProgressDialog = new ProgressDialog(mActivity); mProgressDialog = new ProgressDialog(mActivity);
mItems = buildContextMenuItems(customItems); mItems = buildContextMenuItems(customItems, sourcePackageName);
} }
/** /**
...@@ -183,11 +190,11 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe ...@@ -183,11 +190,11 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe
* Builds items for Browser Actions context menu. * Builds items for Browser Actions context menu.
*/ */
private List<Pair<Integer, List<ContextMenuItem>>> buildContextMenuItems( private List<Pair<Integer, List<ContextMenuItem>>> buildContextMenuItems(
List<BrowserActionItem> customItems) { List<BrowserActionItem> customItems, String sourcePackageName) {
List<Pair<Integer, List<ContextMenuItem>>> menuItems = new ArrayList<>(); List<Pair<Integer, List<ContextMenuItem>>> menuItems = new ArrayList<>();
List<ContextMenuItem> items = new ArrayList<>(); List<ContextMenuItem> items = new ArrayList<>();
items.addAll(mBrowserActionsLinkGroup); items.addAll(mBrowserActionsLinkGroup);
addBrowserActionItems(items, customItems); addBrowserActionItems(items, customItems, sourcePackageName);
menuItems.add(new Pair<>(R.string.contextmenu_link_title, items)); menuItems.add(new Pair<>(R.string.contextmenu_link_title, items));
return menuItems; return menuItems;
...@@ -197,12 +204,30 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe ...@@ -197,12 +204,30 @@ public class BrowserActionsContextMenuHelper implements OnCreateContextMenuListe
* Adds custom items to the context menu list and populates custom item action map. * Adds custom items to the context menu list and populates custom item action map.
* @param items List of {@link ContextMenuItem} to display the context menu. * @param items List of {@link ContextMenuItem} to display the context menu.
* @param customItems List of {@link BrowserActionItem} for custom items. * @param customItems List of {@link BrowserActionItem} for custom items.
* @param sourcePackageName The package name of the requested app.
*/ */
private void addBrowserActionItems( private void addBrowserActionItems(List<ContextMenuItem> items,
List<ContextMenuItem> items, List<BrowserActionItem> customItems) { List<BrowserActionItem> customItems, String sourcePackageName) {
PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
Resources resources = null;
try {
resources = pm.getResourcesForApplication(sourcePackageName);
} catch (NameNotFoundException e) {
Log.e(TAG, "Fail to find the resources", e);
}
for (int i = 0; i < customItems.size() && i < BrowserActionsIntent.MAX_CUSTOM_ITEMS; i++) { for (int i = 0; i < customItems.size() && i < BrowserActionsIntent.MAX_CUSTOM_ITEMS; i++) {
items.add(new BrowserActionsCustomContextMenuItem( Drawable drawable = null;
CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(i), customItems.get(i))); if (resources != null && customItems.get(i).getIconId() != 0) {
try {
drawable = ApiCompatibilityUtils.getDrawable(
resources, customItems.get(i).getIconId());
} catch (NotFoundException e) {
Log.e(TAG, "Cannot get Drawable for %s", customItems.get(i).getTitle(), e);
}
}
items.add(
new BrowserActionsCustomContextMenuItem(CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(i),
customItems.get(i).getTitle(), drawable));
mCustomItemActionMap.put( mCustomItemActionMap.put(
CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(i), customItems.get(i).getAction()); CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(i), customItems.get(i).getAction());
} }
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
package org.chromium.chrome.browser.browseractions; package org.chromium.chrome.browser.browseractions;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes; import android.support.annotation.IdRes;
import android.support.customtabs.browseractions.BrowserActionItem; import android.support.customtabs.browseractions.BrowserActionItem;
...@@ -20,17 +18,18 @@ public class BrowserActionsCustomContextMenuItem implements ContextMenuItem { ...@@ -20,17 +18,18 @@ public class BrowserActionsCustomContextMenuItem implements ContextMenuItem {
@IdRes @IdRes
private final int mMenuId; private final int mMenuId;
private final String mTitle; private final String mTitle;
private final Bitmap mIcon; private final Drawable mIcon;
/** /**
* Constructor to build a custom context menu item from {@link BrowserActionItem}. * Constructor to build a custom context menu item from {@link BrowserActionItem}.
* @param id The {@link IdRes} of the custom context menu item. * @param id The {@link IdRes} of the custom context menu item.
* @param item The {@link BrowserActionItem} specifies the title and action of the menu item. * @param title The title of the custom context menu item.
* @param icon The icon of the custom context menu item.
*/ */
BrowserActionsCustomContextMenuItem(@IdRes int id, BrowserActionItem item) { BrowserActionsCustomContextMenuItem(@IdRes int id, String title, Drawable icon) {
mMenuId = id; mMenuId = id;
mTitle = item.getTitle(); mTitle = title;
mIcon = item.getIcon(); mIcon = icon;
} }
@Override @Override
...@@ -45,6 +44,6 @@ public class BrowserActionsCustomContextMenuItem implements ContextMenuItem { ...@@ -45,6 +44,6 @@ public class BrowserActionsCustomContextMenuItem implements ContextMenuItem {
@Override @Override
public Drawable getDrawable(Context context) { public Drawable getDrawable(Context context) {
return new BitmapDrawable(context.getResources(), mIcon); return mIcon;
} }
} }
\ No newline at end of file
...@@ -16,6 +16,8 @@ import android.net.Uri; ...@@ -16,6 +16,8 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.StrictMode; import android.os.StrictMode;
import android.support.annotation.DrawableRes;
import android.support.customtabs.browseractions.BrowserActionItem;
import android.support.customtabs.browseractions.BrowserActionsIntent; import android.support.customtabs.browseractions.BrowserActionsIntent;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
...@@ -76,7 +78,16 @@ public class BrowserActionActivityTest { ...@@ -76,7 +78,16 @@ public class BrowserActionActivityTest {
private static final String TEST_PAGE = "/chrome/test/data/android/google.html"; private static final String TEST_PAGE = "/chrome/test/data/android/google.html";
private static final String TEST_PAGE_2 = "/chrome/test/data/android/test.html"; private static final String TEST_PAGE_2 = "/chrome/test/data/android/test.html";
private static final String TEST_PAGE_3 = "/chrome/test/data/android/simple.html"; private static final String TEST_PAGE_3 = "/chrome/test/data/android/simple.html";
private static final String CUSTOM_ITEM_TITLE = "Custom item"; private static final String CUSTOM_ITEM_TITLE_1 = "Custom item with drawable icon";
private static final String CUSTOM_ITEM_TITLE_2 = "Custom item with vector drawable icon";
private static final String CUSTOM_ITEM_TITLE_3 = "Custom item wit invalid icon id";
private static final String CUSTOM_ITEM_TITLE_4 = "Custom item without icon";
@DrawableRes
private static final int CUSTOM_ITEM_ICON_BITMAP_DRAWABLE_ID = R.drawable.star_green;
@DrawableRes
private static final int CUSTOM_ITEM_ICON_VECTOR_DRAWABLE_ID = R.drawable.ic_add;
@DrawableRes
private static final int CUSTOM_ITEM_ICON_INVALID_DRAWABLE_ID = -1;
private final CallbackHelper mOnBrowserActionsMenuShownCallback = new CallbackHelper(); private final CallbackHelper mOnBrowserActionsMenuShownCallback = new CallbackHelper();
private final CallbackHelper mOnFinishNativeInitializationCallback = new CallbackHelper(); private final CallbackHelper mOnFinishNativeInitializationCallback = new CallbackHelper();
...@@ -94,7 +105,6 @@ public class BrowserActionActivityTest { ...@@ -94,7 +105,6 @@ public class BrowserActionActivityTest {
private String mTestPage; private String mTestPage;
private String mTestPage2; private String mTestPage2;
private String mTestPage3; private String mTestPage3;
private PendingIntent mCustomPendingItent;
@Rule @Rule
public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule(); public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
@Rule @Rule
...@@ -168,8 +178,13 @@ public class BrowserActionActivityTest { ...@@ -168,8 +178,13 @@ public class BrowserActionActivityTest {
@Test @Test
@SmallTest @SmallTest
/**
* TODO(ltian): move this to a separate test class only for {@link
* BrowserActionsContextMenuHelper}.
*/
public void testMenuShownCorrectly() throws Exception { public void testMenuShownCorrectly() throws Exception {
startBrowserActionActivity(mTestPage); List<BrowserActionItem> items = createCustomItems();
BrowserActionActivity activity = startBrowserActionActivity(mTestPage, items, 0);
// Menu should be shown before native finish loading. // Menu should be shown before native finish loading.
mOnBrowserActionsMenuShownCallback.waitForCallback(0); mOnBrowserActionsMenuShownCallback.waitForCallback(0);
...@@ -181,19 +196,37 @@ public class BrowserActionActivityTest { ...@@ -181,19 +196,37 @@ public class BrowserActionActivityTest {
Assert.assertEquals(1, mOnBrowserActionsMenuShownCallback.getCallCount()); Assert.assertEquals(1, mOnBrowserActionsMenuShownCallback.getCallCount());
Assert.assertEquals(1, mOnFinishNativeInitializationCallback.getCallCount()); Assert.assertEquals(1, mOnFinishNativeInitializationCallback.getCallCount());
Context context = InstrumentationRegistry.getTargetContext();
Assert.assertEquals(context.getPackageName(), activity.mCreatorPackageName);
// Check menu populated correctly. // Check menu populated correctly.
List<Pair<Integer, List<ContextMenuItem>>> menus = mItems; List<Pair<Integer, List<ContextMenuItem>>> menus = mItems;
Assert.assertEquals(1, menus.size()); Assert.assertEquals(1, menus.size());
List<ContextMenuItem> items = menus.get(0).second; List<ContextMenuItem> contextMenuItems = menus.get(0).second;
Assert.assertEquals(6, items.size()); Assert.assertEquals(5 + items.size(), contextMenuItems.size());
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
Assert.assertTrue(items.get(i) instanceof ChromeContextMenuItem); Assert.assertTrue(contextMenuItems.get(i) instanceof ChromeContextMenuItem);
}
Assert.assertTrue(contextMenuItems.get(4) instanceof ShareContextMenuItem);
Assert.assertTrue(contextMenuItems.get(5) instanceof BrowserActionsCustomContextMenuItem);
// Load custom items correctly.
for (int i = 0; i < items.size(); i++) {
Assert.assertEquals(
items.get(i).getTitle(), contextMenuItems.get(5 + i).getTitle(context));
Assert.assertEquals(items.get(i).getAction(),
mCustomActions.get(
BrowserActionsContextMenuHelper.CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(
i)));
}
Assert.assertNotNull(contextMenuItems.get(5).getDrawable(context));
// Vector Drawable is not supported on pre-L so the icon will be null.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Assert.assertNotNull(contextMenuItems.get(6).getDrawable(context));
} else {
Assert.assertNull(contextMenuItems.get(6).getDrawable(context));
} }
Assert.assertTrue(items.get(4) instanceof ShareContextMenuItem); Assert.assertNull(contextMenuItems.get(7).getDrawable(context));
Assert.assertTrue(items.get(5) instanceof BrowserActionsCustomContextMenuItem); Assert.assertNull(contextMenuItems.get(8).getDrawable(context));
Assert.assertEquals(mCustomPendingItent,
mCustomActions.get(
BrowserActionsContextMenuHelper.CUSTOM_BROWSER_ACTIONS_ID_GROUP.get(0)));
} }
@Test @Test
...@@ -573,6 +606,12 @@ public class BrowserActionActivityTest { ...@@ -573,6 +606,12 @@ public class BrowserActionActivityTest {
private BrowserActionActivity startBrowserActionActivity(String url, int expectedCallCount) private BrowserActionActivity startBrowserActionActivity(String url, int expectedCallCount)
throws Exception { throws Exception {
Context context = InstrumentationRegistry.getTargetContext();
return startBrowserActionActivity(url, new ArrayList<>(), expectedCallCount);
}
private BrowserActionActivity startBrowserActionActivity(
String url, List<BrowserActionItem> items, int expectedCallCount) throws Exception {
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
ActivityMonitor browserActionMonitor = ActivityMonitor browserActionMonitor =
new ActivityMonitor(BrowserActionActivity.class.getName(), null, false); new ActivityMonitor(BrowserActionActivity.class.getName(), null, false);
...@@ -585,7 +624,7 @@ public class BrowserActionActivityTest { ...@@ -585,7 +624,7 @@ public class BrowserActionActivityTest {
Assert.assertEquals(expectedCallCount, mOnOpenTabInBackgroundStartCallback.getCallCount()); Assert.assertEquals(expectedCallCount, mOnOpenTabInBackgroundStartCallback.getCallCount());
// Fire an Intent to start the BrowserActionActivity. // Fire an Intent to start the BrowserActionActivity.
sendBrowserActionIntent(instrumentation, url); sendBrowserActionIntent(url, items);
Activity activity = instrumentation.waitForMonitorWithTimeout( Activity activity = instrumentation.waitForMonitorWithTimeout(
browserActionMonitor, CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL); browserActionMonitor, CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL);
...@@ -598,23 +637,24 @@ public class BrowserActionActivityTest { ...@@ -598,23 +637,24 @@ public class BrowserActionActivityTest {
return (BrowserActionActivity) activity; return (BrowserActionActivity) activity;
} }
private void sendBrowserActionIntent(Instrumentation instrumentation, String url) { private void sendBrowserActionIntent(String url, List<BrowserActionItem> items) {
Context context = instrumentation.getTargetContext(); Context context = InstrumentationRegistry.getTargetContext();
Intent intent = new Intent(BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN); Intent intent = new Intent(BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN);
intent.setData(Uri.parse(url)); intent.setData(Uri.parse(url));
intent.putExtra(BrowserActionsIntent.EXTRA_TYPE, BrowserActionsIntent.URL_TYPE_NONE); intent.putExtra(BrowserActionsIntent.EXTRA_TYPE, BrowserActionsIntent.URL_TYPE_NONE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
intent.putExtra(BrowserActionsIntent.EXTRA_APP_ID, pendingIntent); intent.putExtra(BrowserActionsIntent.EXTRA_APP_ID, pendingIntent);
// Add a custom item. ArrayList<Bundle> customItemBundles = new ArrayList<>();
Intent customIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); for (BrowserActionItem item : items) {
mCustomPendingItent = PendingIntent.getActivity(context, 0, customIntent, 0); Bundle customItemBundle = new Bundle();
Bundle item = new Bundle(); customItemBundle.putString(BrowserActionsIntent.KEY_TITLE, item.getTitle());
item.putString(BrowserActionsIntent.KEY_TITLE, CUSTOM_ITEM_TITLE); customItemBundle.putInt(BrowserActionsIntent.KEY_ICON_ID, item.getIconId());
item.putParcelable(BrowserActionsIntent.KEY_ACTION, mCustomPendingItent); customItemBundle.putParcelable(BrowserActionsIntent.KEY_ACTION, item.getAction());
ArrayList<Bundle> items = new ArrayList<>(); customItemBundles.add(customItemBundle);
items.add(item); }
intent.putParcelableArrayListExtra(BrowserActionsIntent.EXTRA_MENU_ITEMS, items); intent.putParcelableArrayListExtra(
BrowserActionsIntent.EXTRA_MENU_ITEMS, customItemBundles);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, BrowserActionActivity.class); intent.setClass(context, BrowserActionActivity.class);
...@@ -623,4 +663,30 @@ public class BrowserActionActivityTest { ...@@ -623,4 +663,30 @@ public class BrowserActionActivityTest {
// for test. // for test.
IntentUtils.safeStartActivity(context, intent); IntentUtils.safeStartActivity(context, intent);
} }
private PendingIntent createCustomItemAction(String url) {
Context context = InstrumentationRegistry.getTargetContext();
Intent customIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
return PendingIntent.getActivity(context, 0, customIntent, 0);
}
private List<BrowserActionItem> createCustomItems() {
List<BrowserActionItem> items = new ArrayList<>();
PendingIntent action1 = createCustomItemAction(mTestPage);
BrowserActionItem item1 = new BrowserActionItem(
CUSTOM_ITEM_TITLE_1, action1, CUSTOM_ITEM_ICON_BITMAP_DRAWABLE_ID);
PendingIntent action2 = createCustomItemAction(mTestPage);
BrowserActionItem item2 = new BrowserActionItem(
CUSTOM_ITEM_TITLE_2, action2, CUSTOM_ITEM_ICON_VECTOR_DRAWABLE_ID);
PendingIntent action3 = createCustomItemAction(mTestPage);
BrowserActionItem item3 = new BrowserActionItem(
CUSTOM_ITEM_TITLE_3, action3, CUSTOM_ITEM_ICON_INVALID_DRAWABLE_ID);
PendingIntent action4 = createCustomItemAction(mTestPage);
BrowserActionItem item4 = new BrowserActionItem(CUSTOM_ITEM_TITLE_4, action4);
items.add(item1);
items.add(item2);
items.add(item3);
items.add(item4);
return items;
}
} }
...@@ -42,7 +42,7 @@ public class ClientManagerTest { ...@@ -42,7 +42,7 @@ public class ClientManagerTest {
private ClientManager mClientManager; private ClientManager mClientManager;
private CustomTabsSessionToken mSession = private CustomTabsSessionToken mSession =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
private int mUid = Process.myUid(); private int mUid = Process.myUid();
@Before @Before
...@@ -123,7 +123,7 @@ public class ClientManagerTest { ...@@ -123,7 +123,7 @@ public class ClientManagerTest {
mClientManager.newSession(mSession, mUid, null, null); mClientManager.newSession(mSession, mUid, null, null);
Assert.assertEquals(ClientManager.SESSION_WARMUP, mClientManager.getWarmupState(mSession)); Assert.assertEquals(ClientManager.SESSION_WARMUP, mClientManager.getWarmupState(mSession));
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
mClientManager.newSession(token, mUid, null, null); mClientManager.newSession(token, mUid, null, null);
Assert.assertEquals(ClientManager.SESSION_WARMUP, mClientManager.getWarmupState(token)); Assert.assertEquals(ClientManager.SESSION_WARMUP, mClientManager.getWarmupState(token));
} }
......
...@@ -2197,7 +2197,7 @@ public class CustomTabActivityTest { ...@@ -2197,7 +2197,7 @@ public class CustomTabActivityTest {
public void testHiddenTabThirdPartyCookiesBlocked() throws Exception { public void testHiddenTabThirdPartyCookiesBlocked() throws Exception {
final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait(); final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait();
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
connection.newSession(token); connection.newSession(token);
connection.setSpeculationModeForSession( connection.setSpeculationModeForSession(
token, CustomTabsConnection.SpeculationParams.HIDDEN_TAB); token, CustomTabsConnection.SpeculationParams.HIDDEN_TAB);
...@@ -2228,7 +2228,7 @@ public class CustomTabActivityTest { ...@@ -2228,7 +2228,7 @@ public class CustomTabActivityTest {
.getTargetContext() .getTargetContext()
.getApplicationContext(); .getApplicationContext();
final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait(); final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait();
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
connection.newSession(token); connection.newSession(token);
connection.setSpeculationModeForSession(token, requestedSpeculationMode); connection.setSpeculationModeForSession(token, requestedSpeculationMode);
Assert.assertTrue(connection.mayLaunchUrl(token, Uri.parse(mTestPage), null, null)); Assert.assertTrue(connection.mayLaunchUrl(token, Uri.parse(mTestPage), null, null));
...@@ -2268,7 +2268,7 @@ public class CustomTabActivityTest { ...@@ -2268,7 +2268,7 @@ public class CustomTabActivityTest {
private void testSpeculateInvalidUrl(int speculationMode) throws Exception { private void testSpeculateInvalidUrl(int speculationMode) throws Exception {
final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait(); final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait();
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
connection.newSession(token); connection.newSession(token);
connection.setSpeculationModeForSession(token, speculationMode); connection.setSpeculationModeForSession(token, speculationMode);
Assert.assertFalse( Assert.assertFalse(
...@@ -2287,7 +2287,7 @@ public class CustomTabActivityTest { ...@@ -2287,7 +2287,7 @@ public class CustomTabActivityTest {
.getTargetContext() .getTargetContext()
.getApplicationContext(); .getApplicationContext();
final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait(); final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait();
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
connection.newSession(token); connection.newSession(token);
try { try {
mCustomTabActivityTestRule.startCustomTabActivityWithIntent( mCustomTabActivityTestRule.startCustomTabActivityWithIntent(
...@@ -2316,7 +2316,7 @@ public class CustomTabActivityTest { ...@@ -2316,7 +2316,7 @@ public class CustomTabActivityTest {
.getTargetContext() .getTargetContext()
.getApplicationContext(); .getApplicationContext();
final CustomTabsConnection connection = CustomTabsConnection.getInstance(); final CustomTabsConnection connection = CustomTabsConnection.getInstance();
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
connection.newSession(token); connection.newSession(token);
try { try {
...@@ -2352,7 +2352,7 @@ public class CustomTabActivityTest { ...@@ -2352,7 +2352,7 @@ public class CustomTabActivityTest {
.getTargetContext() .getTargetContext()
.getApplicationContext(); .getApplicationContext();
final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait(); final CustomTabsConnection connection = CustomTabsTestUtils.warmUpAndWait();
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
connection.newSession(token); connection.newSession(token);
connection.setSpeculationModeForSession(token, speculationMode); connection.setSpeculationModeForSession(token, speculationMode);
Assert.assertTrue(connection.mayLaunchUrl(token, Uri.parse(mTestPage), null, null)); Assert.assertTrue(connection.mayLaunchUrl(token, Uri.parse(mTestPage), null, null));
...@@ -2808,7 +2808,7 @@ public class CustomTabActivityTest { ...@@ -2808,7 +2808,7 @@ public class CustomTabActivityTest {
.getTargetContext() .getTargetContext()
.getApplicationContext(); .getApplicationContext();
CustomTabsConnection connection = CustomTabsTestUtils.setUpConnection(); CustomTabsConnection connection = CustomTabsTestUtils.setUpConnection();
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
connection.newSession(token); connection.newSession(token);
Bundle extras = null; Bundle extras = null;
if (speculationMode == CustomTabsConnection.SpeculationParams.NO_SPECULATION) { if (speculationMode == CustomTabsConnection.SpeculationParams.NO_SPECULATION) {
......
...@@ -79,7 +79,7 @@ public class CustomTabsConnectionTest { ...@@ -79,7 +79,7 @@ public class CustomTabsConnectionTest {
@SmallTest @SmallTest
public void testNewSession() { public void testNewSession() {
Assert.assertEquals(false, mCustomTabsConnection.newSession(null)); Assert.assertEquals(false, mCustomTabsConnection.newSession(null));
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertEquals(true, mCustomTabsConnection.newSession(token)); Assert.assertEquals(true, mCustomTabsConnection.newSession(token));
Assert.assertEquals(false, mCustomTabsConnection.newSession(token)); Assert.assertEquals(false, mCustomTabsConnection.newSession(token));
} }
...@@ -90,9 +90,9 @@ public class CustomTabsConnectionTest { ...@@ -90,9 +90,9 @@ public class CustomTabsConnectionTest {
@Test @Test
@SmallTest @SmallTest
public void testSeveralSessions() { public void testSeveralSessions() {
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertEquals(true, mCustomTabsConnection.newSession(token)); Assert.assertEquals(true, mCustomTabsConnection.newSession(token));
CustomTabsSessionToken token2 = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token2 = CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertEquals(true, mCustomTabsConnection.newSession(token2)); Assert.assertEquals(true, mCustomTabsConnection.newSession(token2));
} }
...@@ -200,7 +200,7 @@ public class CustomTabsConnectionTest { ...@@ -200,7 +200,7 @@ public class CustomTabsConnectionTest {
public void testMayLaunchUrlKeepsSpareRendererWithoutPrerendering() { public void testMayLaunchUrlKeepsSpareRendererWithoutPrerendering() {
CustomTabsTestUtils.warmUpAndWait(); CustomTabsTestUtils.warmUpAndWait();
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
Bundle extras = new Bundle(); Bundle extras = new Bundle();
...@@ -235,7 +235,7 @@ public class CustomTabsConnectionTest { ...@@ -235,7 +235,7 @@ public class CustomTabsConnectionTest {
@RetryOnFailure @RetryOnFailure
public void testOnlyOneHiddenTab() throws Exception { public void testOnlyOneHiddenTab() throws Exception {
Assert.assertTrue("Failed warmup()", mCustomTabsConnection.warmup(0)); Assert.assertTrue("Failed warmup()", mCustomTabsConnection.warmup(0));
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue("Failed newSession()", mCustomTabsConnection.newSession(token)); Assert.assertTrue("Failed newSession()", mCustomTabsConnection.newSession(token));
mCustomTabsConnection.setSpeculationModeForSession( mCustomTabsConnection.setSpeculationModeForSession(
token, CustomTabsConnection.SpeculationParams.HIDDEN_TAB); token, CustomTabsConnection.SpeculationParams.HIDDEN_TAB);
...@@ -288,7 +288,7 @@ public class CustomTabsConnectionTest { ...@@ -288,7 +288,7 @@ public class CustomTabsConnectionTest {
@Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE) @Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE)
public void testUnderstandsLowConfidenceMayLaunchUrl() { public void testUnderstandsLowConfidenceMayLaunchUrl() {
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
List<Bundle> urls = new ArrayList<>(); List<Bundle> urls = new ArrayList<>();
Bundle urlBundle = new Bundle(); Bundle urlBundle = new Bundle();
...@@ -310,7 +310,7 @@ public class CustomTabsConnectionTest { ...@@ -310,7 +310,7 @@ public class CustomTabsConnectionTest {
@SmallTest @SmallTest
public void testLowConfidenceMayLaunchUrlOnlyAcceptUris() { public void testLowConfidenceMayLaunchUrlOnlyAcceptUris() {
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
CustomTabsTestUtils.warmUpAndWait(); CustomTabsTestUtils.warmUpAndWait();
...@@ -337,7 +337,7 @@ public class CustomTabsConnectionTest { ...@@ -337,7 +337,7 @@ public class CustomTabsConnectionTest {
@SmallTest @SmallTest
public void testLowConfidenceMayLaunchUrlDoesntCrash() { public void testLowConfidenceMayLaunchUrlDoesntCrash() {
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
CustomTabsTestUtils.warmUpAndWait(); CustomTabsTestUtils.warmUpAndWait();
...@@ -364,7 +364,7 @@ public class CustomTabsConnectionTest { ...@@ -364,7 +364,7 @@ public class CustomTabsConnectionTest {
public void testStillHighConfidenceMayLaunchUrlWithSeveralUrls() { public void testStillHighConfidenceMayLaunchUrlWithSeveralUrls() {
CustomTabsConnection.getInstance().setForcePrerender(true); CustomTabsConnection.getInstance().setForcePrerender(true);
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
List<Bundle> urls = new ArrayList<>(); List<Bundle> urls = new ArrayList<>();
Bundle urlBundle = new Bundle(); Bundle urlBundle = new Bundle();
...@@ -389,7 +389,7 @@ public class CustomTabsConnectionTest { ...@@ -389,7 +389,7 @@ public class CustomTabsConnectionTest {
public void testPrefetchOnlyNoPrerenderHasSpareWebContents() { public void testPrefetchOnlyNoPrerenderHasSpareWebContents() {
CustomTabsTestUtils.warmUpAndWait(); CustomTabsTestUtils.warmUpAndWait();
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
Bundle extras = new Bundle(); Bundle extras = new Bundle();
...@@ -446,7 +446,7 @@ public class CustomTabsConnectionTest { ...@@ -446,7 +446,7 @@ public class CustomTabsConnectionTest {
CustomTabsSessionToken token, String url, boolean shouldSucceed) { CustomTabsSessionToken token, String url, boolean shouldSucceed) {
CustomTabsTestUtils.warmUpAndWait(); CustomTabsTestUtils.warmUpAndWait();
if (token == null) { if (token == null) {
token = CustomTabsSessionToken.createDummySessionTokenForTesting(); token = CustomTabsSessionToken.createMockSessionTokenForTesting();
mCustomTabsConnection.newSession(token); mCustomTabsConnection.newSession(token);
} }
Uri uri = url == null ? null : Uri.parse(url); Uri uri = url == null ? null : Uri.parse(url);
...@@ -465,7 +465,7 @@ public class CustomTabsConnectionTest { ...@@ -465,7 +465,7 @@ public class CustomTabsConnectionTest {
@SmallTest @SmallTest
public void testNoMayLaunchUrlWithInvalidSessionId() { public void testNoMayLaunchUrlWithInvalidSessionId() {
assertWarmupAndMayLaunchUrl( assertWarmupAndMayLaunchUrl(
CustomTabsSessionToken.createDummySessionTokenForTesting(), URL, false); CustomTabsSessionToken.createMockSessionTokenForTesting(), URL, false);
} }
/** /**
...@@ -621,7 +621,7 @@ public class CustomTabsConnectionTest { ...@@ -621,7 +621,7 @@ public class CustomTabsConnectionTest {
public void testBanningWorks() { public void testBanningWorks() {
mCustomTabsConnection.ban(Process.myUid()); mCustomTabsConnection.ban(Process.myUid());
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
Assert.assertTrue(mCustomTabsConnection.mayLaunchUrl(token, Uri.parse(URL), null, null)); Assert.assertTrue(mCustomTabsConnection.mayLaunchUrl(token, Uri.parse(URL), null, null));
...@@ -641,7 +641,7 @@ public class CustomTabsConnectionTest { ...@@ -641,7 +641,7 @@ public class CustomTabsConnectionTest {
public void testBanningDisabledForCellular() { public void testBanningDisabledForCellular() {
mCustomTabsConnection.ban(Process.myUid()); mCustomTabsConnection.ban(Process.myUid());
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true); mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true);
...@@ -663,7 +663,7 @@ public class CustomTabsConnectionTest { ...@@ -663,7 +663,7 @@ public class CustomTabsConnectionTest {
@SmallTest @SmallTest
@Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE) @Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE)
public void testCellularPrerenderingDoesntOverrideSettings() throws Exception { public void testCellularPrerenderingDoesntOverrideSettings() throws Exception {
CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken token = CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true); mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true);
CustomTabsTestUtils.warmUpAndWait(); CustomTabsTestUtils.warmUpAndWait();
...@@ -703,7 +703,7 @@ public class CustomTabsConnectionTest { ...@@ -703,7 +703,7 @@ public class CustomTabsConnectionTest {
@Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE) @Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE)
public void testCellularPrerenderingDoesntKillSpareRenderer() throws Exception { public void testCellularPrerenderingDoesntKillSpareRenderer() throws Exception {
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true); mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true);
CustomTabsTestUtils.warmUpAndWait(); CustomTabsTestUtils.warmUpAndWait();
...@@ -729,7 +729,7 @@ public class CustomTabsConnectionTest { ...@@ -729,7 +729,7 @@ public class CustomTabsConnectionTest {
@Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE) @Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE)
public void testUnmatchedCellularPrerenderingDoesntKillSpareRenderer() throws Exception { public void testUnmatchedCellularPrerenderingDoesntKillSpareRenderer() throws Exception {
final CustomTabsSessionToken token = final CustomTabsSessionToken token =
CustomTabsSessionToken.createDummySessionTokenForTesting(); CustomTabsSessionToken.createMockSessionTokenForTesting();
Assert.assertTrue(mCustomTabsConnection.newSession(token)); Assert.assertTrue(mCustomTabsConnection.newSession(token));
mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true); mCustomTabsConnection.setShouldPrerenderOnCellularForSession(token, true);
CustomTabsTestUtils.warmUpAndWait(); CustomTabsTestUtils.warmUpAndWait();
......
...@@ -32,7 +32,7 @@ public class CustomTabsTestUtils { ...@@ -32,7 +32,7 @@ public class CustomTabsTestUtils {
public static Intent createMinimalCustomTabIntent( public static Intent createMinimalCustomTabIntent(
Context context, String url) { Context context, String url) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder( CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(
CustomTabsSession.createDummySessionForTesting( CustomTabsSession.createMockSessionForTesting(
new ComponentName(context, ChromeLauncherActivity.class))); new ComponentName(context, ChromeLauncherActivity.class)));
CustomTabsIntent customTabsIntent = builder.build(); CustomTabsIntent customTabsIntent = builder.build();
Intent intent = customTabsIntent.intent; Intent intent = customTabsIntent.intent;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.browseractions; package org.chromium.chrome.browser.browseractions;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
...@@ -13,6 +14,8 @@ import android.app.PendingIntent; ...@@ -13,6 +14,8 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.DrawableRes;
import android.support.customtabs.browseractions.BrowserActionItem;
import android.support.customtabs.browseractions.BrowserActionsIntent; import android.support.customtabs.browseractions.BrowserActionsIntent;
import org.junit.Before; import org.junit.Before;
...@@ -27,6 +30,9 @@ import org.robolectric.annotation.Config; ...@@ -27,6 +30,9 @@ import org.robolectric.annotation.Config;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.testing.local.LocalRobolectricTestRunner; import org.chromium.testing.local.LocalRobolectricTestRunner;
import java.util.ArrayList;
import java.util.List;
/** /**
* Unit tests for BrowserActionsIntent. * Unit tests for BrowserActionsIntent.
*/ */
...@@ -39,6 +45,10 @@ public class BrowserActionsIntentTest { ...@@ -39,6 +45,10 @@ public class BrowserActionsIntentTest {
private static final String CONTENT_SCHEME_TEST_URL = "content://example"; private static final String CONTENT_SCHEME_TEST_URL = "content://example";
private static final String SENDER_PACKAGE_NAME = "some.other.app.package.sender_name"; private static final String SENDER_PACKAGE_NAME = "some.other.app.package.sender_name";
private static final String RECEIVER_PACKAGE_NAME = "some.other.app.package.receiver_name"; private static final String RECEIVER_PACKAGE_NAME = "some.other.app.package.receiver_name";
private static final String CUSTOM_ITEM_WITHOUT_ICON_TITLE = "Custom item without icon";
private static final String CUSTOM_ITEM_WITH_ICON_TITLE = "Custom item with icon";
@DrawableRes
private static final int CUSTOM_ITEM_WITH_ICON_ICONID = 1;
private Context mContext; private Context mContext;
@Mock @Mock
...@@ -85,18 +95,57 @@ public class BrowserActionsIntentTest { ...@@ -85,18 +95,57 @@ public class BrowserActionsIntentTest {
mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL); mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
assertFalse(mActivity.isStartedUpCorrectly(mIntent)); assertFalse(mActivity.isStartedUpCorrectly(mIntent));
mIntent = createBrowserActionsIntentWithCustomItems(
HTTP_SCHEME_TEST_URL, createCustomItems());
assertTrue(mActivity.isStartedUpCorrectly(mIntent));
testParseCustomItems(mActivity.mActions);
} }
/** /**
* Creates a simple Intent for Browser Actions which contains a url, the {@link * A convenient method to create a simple Intent for Browser Actions without custom item.
* BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN} action and source package name.
* @param url The url for the data of the Intent. * @param url The url for the data of the Intent.
* @return The simple Intent for Browser Actions. * @return The simple Intent for Browser Actions.
*/ */
private Intent createBaseBrowserActionsIntent(String url) { private Intent createBaseBrowserActionsIntent(String url) {
return createBrowserActionsIntentWithCustomItems(url, new ArrayList<>());
}
/**
* Creates an Intent for Browser Actions which contains a url and a list of custom items.
* @param url The url for the data of the Intent.
* @param items A List of custom items for Browser Actions menu.
* @return The Intent for Browser Actions.
*/
private Intent createBrowserActionsIntentWithCustomItems(
String url, ArrayList<BrowserActionItem> items) {
return new BrowserActionsIntent.Builder(mContext, Uri.parse(url)) return new BrowserActionsIntent.Builder(mContext, Uri.parse(url))
.setCustomItems(items)
.build() .build()
.getIntent() .getIntent()
.putExtra(BrowserActionsIntent.EXTRA_APP_ID, mPendingIntent); .putExtra(BrowserActionsIntent.EXTRA_APP_ID, mPendingIntent);
} }
private ArrayList<BrowserActionItem> createCustomItems() {
BrowserActionItem item1 =
new BrowserActionItem(CUSTOM_ITEM_WITHOUT_ICON_TITLE, mPendingIntent);
BrowserActionItem item2 = new BrowserActionItem(
CUSTOM_ITEM_WITH_ICON_TITLE, mPendingIntent, CUSTOM_ITEM_WITH_ICON_ICONID);
ArrayList<BrowserActionItem> items = new ArrayList<>();
items.add(item1);
items.add(item2);
return items;
}
private void testParseCustomItems(List<BrowserActionItem> items) {
assertEquals(2, mActivity.mActions.size());
assertEquals(CUSTOM_ITEM_WITHOUT_ICON_TITLE, mActivity.mActions.get(0).getTitle());
assertEquals(0, mActivity.mActions.get(0).getIconId());
assertEquals(mPendingIntent, mActivity.mActions.get(0).getAction());
assertEquals(CUSTOM_ITEM_WITH_ICON_TITLE, mActivity.mActions.get(1).getTitle());
assertEquals(CUSTOM_ITEM_WITH_ICON_ICONID, mActivity.mActions.get(1).getIconId());
assertEquals(mPendingIntent, mActivity.mActions.get(1).getAction());
}
} }
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