| 1 | // Copyright 2013 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; |
| 6 | |
| 7 | import android.content.Intent; |
| 8 | import android.os.Parcel; |
| 9 | import android.test.suitebuilder.annotation.MediumTest; |
| 10 | import android.util.Log; |
| 11 | import org.chromium.base.test.util.Feature; |
| 12 | import org.chromium.base.test.util.UrlUtils; |
| 13 | import org.chromium.chrome.testshell.ChromiumTestShellActivity; |
| 14 | import org.chromium.chrome.testshell.ChromiumTestShellApplication; |
| 15 | import org.chromium.chrome.testshell.ChromiumTestShellApplicationObserver; |
| 16 | import org.chromium.chrome.testshell.ChromiumTestShellTestBase; |
| 17 | import org.chromium.chrome.testshell.TestShellTab; |
| 18 | import org.chromium.content.browser.test.util.Criteria; |
| 19 | import org.chromium.content.browser.test.util.CriteriaHelper; |
| 20 | |
| 21 | public class ShortcutHelperTest extends ChromiumTestShellTestBase { |
| 22 | private static final String WEBAPP_PACKAGE_NAME = "packageName"; |
| 23 | private static final String WEBAPP_CLASS_NAME = "className"; |
| 24 | |
| 25 | private static final String WEBAPP_TITLE = "Webapp shortcut"; |
| 26 | private static final String WEBAPP_HTML = UrlUtils.encodeHtmlDataUri( |
| 27 | "<html><head>" |
| 28 | + "<meta name=\"mobile-web-app-capable\" content=\"yes\" />" |
| 29 | + "<title>" + WEBAPP_TITLE + "</title>" |
| 30 | + "</head><body>Webapp capable</body></html>"); |
| 31 | |
| 32 | private static final String NORMAL_TITLE = "Plain shortcut"; |
| 33 | private static final String NORMAL_HTML = UrlUtils.encodeHtmlDataUri( |
| 34 | "<html>" |
| 35 | + "<head><title>" + NORMAL_TITLE + "</title></head>" |
| 36 | + "<body>Not Webapp capable</body></html>"); |
| 37 | |
| 38 | private static class TestObserver implements ChromiumTestShellApplicationObserver { |
| 39 | Intent firedIntent; |
| 40 | |
| 41 | @Override |
| 42 | public boolean onSendBroadcast(Intent intent) { |
| 43 | if (intent.hasExtra(Intent.EXTRA_SHORTCUT_NAME)) { |
| 44 | // Stop a shortcut from really being added. |
| 45 | firedIntent = intent; |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | return true; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private ChromiumTestShellActivity mActivity; |
| 54 | private TestObserver mTestObserver; |
| 55 | |
| 56 | @Override |
| 57 | public void setUp() throws Exception { |
| 58 | ShortcutHelper.setWebappActivityInfo(WEBAPP_PACKAGE_NAME, WEBAPP_CLASS_NAME); |
| 59 | mActivity = launchChromiumTestShellWithBlankPage(); |
| 60 | |
| 61 | // Set up the observer. |
| 62 | mTestObserver = new TestObserver(); |
| 63 | ChromiumTestShellApplication application = |
| 64 | (ChromiumTestShellApplication) mActivity.getApplication(); |
| 65 | application.addObserver(mTestObserver); |
| 66 | |
| 67 | super.setUp(); |
| 68 | } |
| 69 | |
| 70 | @MediumTest |
| 71 | @Feature("{Webapp}") |
| 72 | public void testAddWebappShortcut() throws InterruptedException { |
| 73 | addShortcutToURL(WEBAPP_HTML); |
| 74 | |
| 75 | // Make sure the intent's parameters make sense. |
| 76 | Intent firedIntent = mTestObserver.firedIntent; |
| 77 | assertEquals(WEBAPP_TITLE, firedIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME)); |
| 78 | |
| 79 | Intent launchIntent = firedIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); |
| 80 | assertEquals(WEBAPP_HTML, launchIntent.getStringExtra(ShortcutHelper.EXTRA_URL)); |
| 81 | assertEquals(WEBAPP_PACKAGE_NAME, launchIntent.getComponent().getPackageName()); |
| 82 | assertEquals(WEBAPP_CLASS_NAME, launchIntent.getComponent().getClassName()); |
| 83 | } |
| 84 | |
| 85 | @MediumTest |
| 86 | @Feature("{Webapp}") |
| 87 | public void testAddBookmarkShortcut() throws InterruptedException { |
| 88 | addShortcutToURL(NORMAL_HTML); |
| 89 | |
| 90 | // Make sure the intent's parameters make sense. |
| 91 | Intent firedIntent = mTestObserver.firedIntent; |
| 92 | assertEquals(NORMAL_TITLE, firedIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME)); |
| 93 | |
| 94 | Intent launchIntent = firedIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); |
| 95 | assertEquals(Intent.ACTION_VIEW, launchIntent.getAction()); |
| 96 | assertEquals(NORMAL_HTML, launchIntent.getDataString()); |
| 97 | assertNull(launchIntent.getComponent()); |
| 98 | } |
| 99 | |
| 100 | private void addShortcutToURL(String url) throws InterruptedException { |
| 101 | loadUrlWithSanitization(url); |
| 102 | assertTrue(waitForActiveShellToBeDoneLoading()); |
| 103 | |
| 104 | // Add the shortcut. |
| 105 | getInstrumentation().runOnMainSync(new Runnable() { |
| 106 | @Override |
| 107 | public void run() { |
| 108 | ShortcutHelper.addShortcut(mActivity.getActiveTab()); |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | // Make sure that the shortcut was added. |
| 113 | assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 114 | @Override |
| 115 | public boolean isSatisfied() { |
| 116 | return mTestObserver.firedIntent != null; |
| 117 | } |
| 118 | })); |
| 119 | } |
| 120 | } |