OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.browseractions; |
| 6 |
| 7 import static org.junit.Assert.assertFalse; |
| 8 import static org.junit.Assert.assertTrue; |
| 9 import static org.mockito.Mockito.doAnswer; |
| 10 |
| 11 import android.app.PendingIntent; |
| 12 import android.content.Context; |
| 13 import android.content.Intent; |
| 14 import android.net.Uri; |
| 15 import android.support.customtabs.browseractions.BrowserActionsIntent; |
| 16 |
| 17 import org.junit.Before; |
| 18 import org.junit.Test; |
| 19 import org.junit.runner.RunWith; |
| 20 import org.mockito.Mock; |
| 21 import org.mockito.MockitoAnnotations; |
| 22 import org.mockito.invocation.InvocationOnMock; |
| 23 import org.mockito.stubbing.Answer; |
| 24 import org.robolectric.RuntimeEnvironment; |
| 25 import org.robolectric.annotation.Config; |
| 26 |
| 27 import org.chromium.base.test.util.Feature; |
| 28 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 29 |
| 30 /** |
| 31 * Unit tests for BrowserActionActivity. |
| 32 */ |
| 33 @RunWith(LocalRobolectricTestRunner.class) |
| 34 @Config(manifest = Config.NONE) |
| 35 public class BrowserActionActivityTest { |
| 36 private static final String HTTP_SCHEME_TEST_URL = "http://www.example.com"; |
| 37 private static final String HTTPS_SCHEME_TEST_URL = "https://www.example.com
"; |
| 38 private static final String CHROME_SCHEME_TEST_URL = "chrome://example"; |
| 39 private static final String CONTENT_SCHEME_TEST_URL = "content://example"; |
| 40 |
| 41 private BrowserActionActivity mActivity = new BrowserActionActivity(); |
| 42 private Context mContext; |
| 43 |
| 44 @Mock |
| 45 private PendingIntent mPendingIntent; |
| 46 |
| 47 @Before |
| 48 public void setUp() throws Exception { |
| 49 MockitoAnnotations.initMocks(this); |
| 50 mContext = RuntimeEnvironment.application; |
| 51 Answer<String> answer = new Answer<String>() { |
| 52 @Override |
| 53 public String answer(InvocationOnMock invocation) { |
| 54 return "some.other.app.package.name"; |
| 55 } |
| 56 }; |
| 57 doAnswer(answer).when(mPendingIntent).getCreatorPackage(); |
| 58 } |
| 59 |
| 60 @Test |
| 61 @Feature({"BrowserActions"}) |
| 62 public void testStartedUpCorrectly() { |
| 63 assertFalse(mActivity.isStartedUpCorrectly(null)); |
| 64 assertFalse(mActivity.isStartedUpCorrectly(new Intent())); |
| 65 |
| 66 Intent mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL); |
| 67 assertTrue(mActivity.isStartedUpCorrectly(mIntent)); |
| 68 |
| 69 mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL); |
| 70 mIntent.removeExtra(BrowserActionsIntent.EXTRA_APP_ID); |
| 71 assertFalse(mActivity.isStartedUpCorrectly(mIntent)); |
| 72 |
| 73 mIntent = createBaseBrowserActionsIntent(HTTPS_SCHEME_TEST_URL); |
| 74 assertTrue(mActivity.isStartedUpCorrectly(mIntent)); |
| 75 |
| 76 mIntent = createBaseBrowserActionsIntent(CHROME_SCHEME_TEST_URL); |
| 77 assertFalse(mActivity.isStartedUpCorrectly(mIntent)); |
| 78 |
| 79 mIntent = createBaseBrowserActionsIntent(CONTENT_SCHEME_TEST_URL); |
| 80 assertFalse(mActivity.isStartedUpCorrectly(mIntent)); |
| 81 |
| 82 mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL); |
| 83 mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 84 assertFalse(mActivity.isStartedUpCorrectly(mIntent)); |
| 85 |
| 86 mIntent = createBaseBrowserActionsIntent(HTTP_SCHEME_TEST_URL); |
| 87 mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 88 assertFalse(mActivity.isStartedUpCorrectly(mIntent)); |
| 89 } |
| 90 |
| 91 /** |
| 92 * Creates a simple Intent for Browser Actions which contains a url, the {@l
ink |
| 93 * BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN} action and source packa
ge name. |
| 94 * @param url The url for the data of the Intent. |
| 95 * @return The simple Intent for Browser Actions. |
| 96 */ |
| 97 private Intent createBaseBrowserActionsIntent(String url) { |
| 98 return new BrowserActionsIntent.Builder(mContext, Uri.parse(url)) |
| 99 .build() |
| 100 .getIntent() |
| 101 .putExtra(BrowserActionsIntent.EXTRA_APP_ID, mPendingIntent); |
| 102 } |
| 103 } |
OLD | NEW |