| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.customtabs; | 5 package org.chromium.chrome.browser.customtabs; |
| 6 | 6 |
| 7 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_NON_LOW_E
ND_DEVICE; | 7 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_NON_LOW_E
ND_DEVICE; |
| 8 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_PHONE; | 8 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_PHONE; |
| 9 | 9 |
| 10 import android.app.Activity; | 10 import android.app.Activity; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 import android.os.Build; | 22 import android.os.Build; |
| 23 import android.os.Bundle; | 23 import android.os.Bundle; |
| 24 import android.os.IBinder; | 24 import android.os.IBinder; |
| 25 import android.support.customtabs.CustomTabsCallback; | 25 import android.support.customtabs.CustomTabsCallback; |
| 26 import android.support.customtabs.CustomTabsIntent; | 26 import android.support.customtabs.CustomTabsIntent; |
| 27 import android.support.customtabs.ICustomTabsCallback; | 27 import android.support.customtabs.ICustomTabsCallback; |
| 28 import android.test.suitebuilder.annotation.SmallTest; | 28 import android.test.suitebuilder.annotation.SmallTest; |
| 29 import android.view.Menu; | 29 import android.view.Menu; |
| 30 import android.view.MenuItem; | 30 import android.view.MenuItem; |
| 31 import android.view.View; | 31 import android.view.View; |
| 32 import android.view.ViewGroup; |
| 32 import android.widget.ImageButton; | 33 import android.widget.ImageButton; |
| 33 | 34 |
| 34 import org.chromium.base.ThreadUtils; | 35 import org.chromium.base.ThreadUtils; |
| 35 import org.chromium.base.library_loader.LibraryProcessType; | 36 import org.chromium.base.library_loader.LibraryProcessType; |
| 36 import org.chromium.base.test.util.CommandLineFlags; | 37 import org.chromium.base.test.util.CommandLineFlags; |
| 37 import org.chromium.base.test.util.Restriction; | 38 import org.chromium.base.test.util.Restriction; |
| 38 import org.chromium.chrome.R; | 39 import org.chromium.chrome.R; |
| 39 import org.chromium.chrome.browser.ChromeActivity; | 40 import org.chromium.chrome.browser.ChromeActivity; |
| 40 import org.chromium.chrome.browser.ChromeSwitches; | 41 import org.chromium.chrome.browser.ChromeSwitches; |
| 41 import org.chromium.chrome.browser.ChromeTabbedActivity; | 42 import org.chromium.chrome.browser.ChromeTabbedActivity; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 private static final int MAX_MENU_CUSTOM_ITEMS = 5; | 93 private static final int MAX_MENU_CUSTOM_ITEMS = 5; |
| 93 private static final int NUM_CHROME_MENU_ITEMS = 3; | 94 private static final int NUM_CHROME_MENU_ITEMS = 3; |
| 94 private static final String | 95 private static final String |
| 95 TEST_ACTION = "org.chromium.chrome.browser.customtabs.TEST_PENDING_I
NTENT_SENT"; | 96 TEST_ACTION = "org.chromium.chrome.browser.customtabs.TEST_PENDING_I
NTENT_SENT"; |
| 96 private static final String TEST_PAGE = TestHttpServerClient.getUrl( | 97 private static final String TEST_PAGE = TestHttpServerClient.getUrl( |
| 97 "chrome/test/data/android/google.html"); | 98 "chrome/test/data/android/google.html"); |
| 98 private static final String TEST_PAGE_2 = TestHttpServerClient.getUrl( | 99 private static final String TEST_PAGE_2 = TestHttpServerClient.getUrl( |
| 99 "chrome/test/data/android/test.html"); | 100 "chrome/test/data/android/test.html"); |
| 100 private static final String TEST_MENU_TITLE = "testMenuTitle"; | 101 private static final String TEST_MENU_TITLE = "testMenuTitle"; |
| 101 | 102 |
| 103 private static int sIdToIncrement = 1; |
| 104 |
| 102 private CustomTabActivity mActivity; | 105 private CustomTabActivity mActivity; |
| 103 | 106 |
| 104 @Override | 107 @Override |
| 105 protected void startActivityCompletely(Intent intent) { | 108 protected void startActivityCompletely(Intent intent) { |
| 106 super.startActivityCompletely(intent); | 109 super.startActivityCompletely(intent); |
| 107 mActivity = getActivity(); | 110 mActivity = getActivity(); |
| 108 } | 111 } |
| 109 | 112 |
| 110 /** | 113 /** |
| 111 * @see CustomTabsTestUtils#createMinimalCustomTabIntent(Context, String, IB
inder). | 114 * @see CustomTabsTestUtils#createMinimalCustomTabIntent(Context, String, IB
inder). |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 bundle.putParcelable(CustomTabsIntent.KEY_ICON, icon); | 157 bundle.putParcelable(CustomTabsIntent.KEY_ICON, icon); |
| 155 bundle.putString(CustomTabsIntent.KEY_DESCRIPTION, description); | 158 bundle.putString(CustomTabsIntent.KEY_DESCRIPTION, description); |
| 156 PendingIntent pi = PendingIntent.getBroadcast(getInstrumentation().getTa
rgetContext(), 0, | 159 PendingIntent pi = PendingIntent.getBroadcast(getInstrumentation().getTa
rgetContext(), 0, |
| 157 actionIntent, 0); | 160 actionIntent, 0); |
| 158 bundle.putParcelable(CustomTabsIntent.KEY_PENDING_INTENT, pi); | 161 bundle.putParcelable(CustomTabsIntent.KEY_PENDING_INTENT, pi); |
| 159 | 162 |
| 160 intent.putExtra(CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE, bundle); | 163 intent.putExtra(CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE, bundle); |
| 161 return pi; | 164 return pi; |
| 162 } | 165 } |
| 163 | 166 |
| 167 private Bundle makeBottomBarBundle(int id, Bitmap icon, String description)
{ |
| 168 Bundle bundle = new Bundle(); |
| 169 Intent testIntent = new Intent(); |
| 170 testIntent.setClass(getInstrumentation().getContext(), DummyBroadcastRec
eiver.class); |
| 171 testIntent.setAction(TEST_ACTION); |
| 172 PendingIntent pi = PendingIntent.getBroadcast(getInstrumentation().getTa
rgetContext(), 0, |
| 173 testIntent, 0); |
| 174 |
| 175 bundle.putInt(CustomTabsIntent.KEY_ID, sIdToIncrement++); |
| 176 bundle.putString(CustomTabsIntent.KEY_DESCRIPTION, description); |
| 177 bundle.putParcelable(CustomTabsIntent.KEY_PENDING_INTENT, pi); |
| 178 bundle.putParcelable(CustomTabsIntent.KEY_ICON, icon); |
| 179 return bundle; |
| 180 } |
| 181 |
| 164 private void openAppMenuAndAssertMenuShown() throws InterruptedException { | 182 private void openAppMenuAndAssertMenuShown() throws InterruptedException { |
| 165 ThreadUtils.runOnUiThread(new Runnable() { | 183 ThreadUtils.runOnUiThread(new Runnable() { |
| 166 @Override | 184 @Override |
| 167 public void run() { | 185 public void run() { |
| 168 mActivity.onMenuOrKeyboardAction(R.id.show_menu, false); | 186 mActivity.onMenuOrKeyboardAction(R.id.show_menu, false); |
| 169 } | 187 } |
| 170 }); | 188 }); |
| 171 | 189 |
| 172 CriteriaHelper.pollForUIThreadCriteria(new Criteria("App menu was not sh
own") { | 190 CriteriaHelper.pollForUIThreadCriteria(new Criteria("App menu was not sh
own") { |
| 173 @Override | 191 @Override |
| 174 public boolean isSatisfied() { | 192 public boolean isSatisfied() { |
| 175 return mActivity.getAppMenuHandler().isAppMenuShowing(); | 193 return mActivity.getAppMenuHandler().isAppMenuShowing(); |
| 176 } | 194 } |
| 177 }); | 195 }); |
| 178 } | 196 } |
| 179 | 197 |
| 180 /** | 198 /** |
| 181 * @return The number of visible and enabled items in the given menu. | 199 * @return The number of visible and enabled items in the given menu. |
| 182 */ | 200 */ |
| 183 private int getActualMenuSize(Menu menu) { | 201 private int getActualMenuSize(Menu menu) { |
| 184 int actualMenuSize = 0; | 202 int actualMenuSize = 0; |
| 185 for (int i = 0; i < menu.size(); i++) { | 203 for (int i = 0; i < menu.size(); i++) { |
| 186 MenuItem item = menu.getItem(i); | 204 MenuItem item = menu.getItem(i); |
| 187 if (item.isVisible() && item.isEnabled()) actualMenuSize++; | 205 if (item.isVisible() && item.isEnabled()) actualMenuSize++; |
| 188 } | 206 } |
| 189 return actualMenuSize; | 207 return actualMenuSize; |
| 190 } | 208 } |
| 191 | 209 |
| 210 private Bitmap createTestBitmap(int widthDp, int heightDp) { |
| 211 Resources testRes = getInstrumentation().getTargetContext().getResources
(); |
| 212 float density = testRes.getDisplayMetrics().density; |
| 213 return Bitmap.createBitmap((int) (widthDp * density), |
| 214 (int) (heightDp * density), Bitmap.Config.ARGB_8888); |
| 215 } |
| 216 |
| 192 /** | 217 /** |
| 193 * Test the entries in the context menu shown when long clicking an image. | 218 * Test the entries in the context menu shown when long clicking an image. |
| 194 */ | 219 */ |
| 195 @SmallTest | 220 @SmallTest |
| 196 public void testContextMenuEntriesForImage() throws InterruptedException, Ti
meoutException { | 221 public void testContextMenuEntriesForImage() throws InterruptedException, Ti
meoutException { |
| 197 startCustomTabActivityWithIntent(createMinimalCustomTabIntent()); | 222 startCustomTabActivityWithIntent(createMinimalCustomTabIntent()); |
| 198 | 223 |
| 199 final int expectedMenuSize = 9; | 224 final int expectedMenuSize = 9; |
| 200 Menu menu = ContextMenuUtils.openContextMenu(this, getActivity().getActi
vityTab(), "logo"); | 225 Menu menu = ContextMenuUtils.openContextMenu(this, getActivity().getActi
vityTab(), "logo"); |
| 201 assertEquals(expectedMenuSize, menu.size()); | 226 assertEquals(expectedMenuSize, menu.size()); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 getActivity().getWindow().getStatusBarColor()); | 430 getActivity().getWindow().getStatusBarColor()); |
| 406 } | 431 } |
| 407 } | 432 } |
| 408 | 433 |
| 409 /** | 434 /** |
| 410 * Test if an action button is shown with correct image and size, and clicki
ng it sends the | 435 * Test if an action button is shown with correct image and size, and clicki
ng it sends the |
| 411 * correct {@link PendingIntent}. | 436 * correct {@link PendingIntent}. |
| 412 */ | 437 */ |
| 413 @SmallTest | 438 @SmallTest |
| 414 public void testActionButton() throws InterruptedException { | 439 public void testActionButton() throws InterruptedException { |
| 415 final int iconHeightDp = 48; | 440 Bitmap expectedIcon = createTestBitmap(96, 48); |
| 416 final int iconWidthDp = 96; | |
| 417 Resources testRes = getInstrumentation().getTargetContext().getResources
(); | |
| 418 float density = testRes.getDisplayMetrics().density; | |
| 419 Bitmap expectedIcon = Bitmap.createBitmap((int) (iconWidthDp * density), | |
| 420 (int) (iconHeightDp * density), Bitmap.Config.ARGB_8888); | |
| 421 | |
| 422 Intent intent = createMinimalCustomTabIntent(); | 441 Intent intent = createMinimalCustomTabIntent(); |
| 423 final PendingIntent pi = addActionButtonToIntent(intent, expectedIcon, "
Good test"); | 442 final PendingIntent pi = addActionButtonToIntent(intent, expectedIcon, "
Good test"); |
| 424 startCustomTabActivityWithIntent(intent); | 443 startCustomTabActivityWithIntent(intent); |
| 425 | 444 |
| 426 final OnFinishedForTest onFinished = new OnFinishedForTest(pi); | 445 final OnFinishedForTest onFinished = new OnFinishedForTest(pi); |
| 427 mActivity.getIntentDataProvider().setPendingIntentOnFinishedForTesting(o
nFinished); | 446 mActivity.getIntentDataProvider().setPendingIntentOnFinishedForTesting(o
nFinished); |
| 428 | 447 |
| 429 View toolbarView = getActivity().findViewById(R.id.toolbar); | 448 View toolbarView = getActivity().findViewById(R.id.toolbar); |
| 430 assertTrue("A custom tab toolbar is never shown", toolbarView instanceof
CustomTabToolbar); | 449 assertTrue("A custom tab toolbar is never shown", toolbarView instanceof
CustomTabToolbar); |
| 431 CustomTabToolbar toolbar = (CustomTabToolbar) toolbarView; | 450 CustomTabToolbar toolbar = (CustomTabToolbar) toolbarView; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 453 } | 472 } |
| 454 }); | 473 }); |
| 455 } | 474 } |
| 456 | 475 |
| 457 /** | 476 /** |
| 458 * Test the case that the action button should not be shown, given a bitmap
with unacceptable | 477 * Test the case that the action button should not be shown, given a bitmap
with unacceptable |
| 459 * height/width ratio. | 478 * height/width ratio. |
| 460 */ | 479 */ |
| 461 @SmallTest | 480 @SmallTest |
| 462 public void testActionButtonBadRatio() throws InterruptedException { | 481 public void testActionButtonBadRatio() throws InterruptedException { |
| 463 final int iconHeightDp = 20; | 482 Bitmap expectedIcon = createTestBitmap(60, 20); |
| 464 final int iconWidthDp = 60; | |
| 465 Resources testRes = getInstrumentation().getTargetContext().getResources
(); | |
| 466 float density = testRes.getDisplayMetrics().density; | |
| 467 Bitmap expectedIcon = Bitmap.createBitmap((int) (iconWidthDp * density), | |
| 468 (int) (iconHeightDp * density), Bitmap.Config.ARGB_8888); | |
| 469 | |
| 470 Intent intent = createMinimalCustomTabIntent(); | 483 Intent intent = createMinimalCustomTabIntent(); |
| 471 addActionButtonToIntent(intent, expectedIcon, "Good test"); | 484 addActionButtonToIntent(intent, expectedIcon, "Good test"); |
| 472 startCustomTabActivityWithIntent(intent); | 485 startCustomTabActivityWithIntent(intent); |
| 473 | 486 |
| 474 View toolbarView = getActivity().findViewById(R.id.toolbar); | 487 View toolbarView = getActivity().findViewById(R.id.toolbar); |
| 475 assertTrue("A custom tab toolbar is never shown", toolbarView instanceof
CustomTabToolbar); | 488 assertTrue("A custom tab toolbar is never shown", toolbarView instanceof
CustomTabToolbar); |
| 476 CustomTabToolbar toolbar = (CustomTabToolbar) toolbarView; | 489 CustomTabToolbar toolbar = (CustomTabToolbar) toolbarView; |
| 477 final ImageButton actionButton = toolbar.getCustomActionButtonForTest(); | 490 final ImageButton actionButton = toolbar.getCustomActionButtonForTest(); |
| 478 | 491 |
| 479 assertNotNull(actionButton); | 492 assertNotNull(actionButton); |
| 480 assertTrue("Action button should not be shown", | 493 assertTrue("Action button should not be shown", |
| 481 View.VISIBLE != actionButton.getVisibility()); | 494 View.VISIBLE != actionButton.getVisibility()); |
| 482 | 495 |
| 483 CustomTabIntentDataProvider dataProvider = mActivity.getIntentDataProvid
er(); | 496 CustomTabIntentDataProvider dataProvider = mActivity.getIntentDataProvid
er(); |
| 484 assertNull(dataProvider.getActionButtonParams()); | 497 assertNull(dataProvider.getCustomButtonOnToolbar()); |
| 485 } | 498 } |
| 486 | 499 |
| 487 @SmallTest | 500 @SmallTest |
| 501 public void testBottomBar() throws InterruptedException { |
| 502 final int numItems = 3; |
| 503 final Bitmap expectedIcon = createTestBitmap(48, 24); |
| 504 |
| 505 Intent intent = createMinimalCustomTabIntent(); |
| 506 ArrayList<Bundle> bundles = new ArrayList<>(); |
| 507 for (int i = 1; i <= numItems; i++) { |
| 508 Bundle bundle = makeBottomBarBundle(i, expectedIcon, Integer.toStrin
g(i)); |
| 509 bundles.add(bundle); |
| 510 } |
| 511 intent.putExtra(CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE, bundles); |
| 512 startCustomTabActivityWithIntent(intent); |
| 513 |
| 514 ViewGroup bottomBar = (ViewGroup) getActivity().findViewById(R.id.bottom
bar); |
| 515 assertNotNull(bottomBar); |
| 516 assertEquals("Bottom Bar showing incorrect number of buttons.", |
| 517 numItems, bottomBar.getChildCount()); |
| 518 for (int i = 1; i <= numItems; i++) { |
| 519 ImageButton button = (ImageButton) bottomBar.getChildAt(i - 1); |
| 520 assertTrue("Bottom Bar button does not have the correct bitmap.", |
| 521 expectedIcon.sameAs(((BitmapDrawable) button.getDrawable()).
getBitmap())); |
| 522 assertEquals("Bottom Bar button does not have correct content descri
ption", |
| 523 Integer.toString(i), button.getContentDescription()); |
| 524 } |
| 525 } |
| 526 |
| 527 @SmallTest |
| 488 public void testLaunchWithSession() throws InterruptedException { | 528 public void testLaunchWithSession() throws InterruptedException { |
| 489 IBinder session = warmUpAndLaunchUrlWithSession(); | 529 IBinder session = warmUpAndLaunchUrlWithSession(); |
| 490 assertEquals(mActivity.getIntentDataProvider().getSession(), session); | 530 assertEquals(mActivity.getIntentDataProvider().getSession(), session); |
| 491 } | 531 } |
| 492 | 532 |
| 493 @SmallTest | 533 @SmallTest |
| 494 public void testLoadNewUrlWithSession() throws InterruptedException { | 534 public void testLoadNewUrlWithSession() throws InterruptedException { |
| 495 final IBinder session = warmUpAndLaunchUrlWithSession(); | 535 final IBinder session = warmUpAndLaunchUrlWithSession(); |
| 496 final Context context = getInstrumentation().getTargetContext(); | 536 final Context context = getInstrumentation().getTargetContext(); |
| 497 assertEquals(mActivity.getIntentDataProvider().getSession(), session); | 537 assertEquals(mActivity.getIntentDataProvider().getSession(), session); |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 @Override | 850 @Override |
| 811 public void onSendFinished(PendingIntent pendingIntent, Intent intent, i
nt resultCode, | 851 public void onSendFinished(PendingIntent pendingIntent, Intent intent, i
nt resultCode, |
| 812 String resultData, Bundle resultExtras) { | 852 String resultData, Bundle resultExtras) { |
| 813 if (pendingIntent.equals(mPi)) { | 853 if (pendingIntent.equals(mPi)) { |
| 814 mUri = intent.getDataString(); | 854 mUri = intent.getDataString(); |
| 815 mIsSent.set(true); | 855 mIsSent.set(true); |
| 816 } | 856 } |
| 817 } | 857 } |
| 818 } | 858 } |
| 819 } | 859 } |
| OLD | NEW |