| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.content.Intent; | |
| 9 import android.graphics.Bitmap; | |
| 10 import android.test.suitebuilder.annotation.SmallTest; | |
| 11 import android.text.TextUtils; | |
| 12 | |
| 13 import org.chromium.base.ThreadUtils; | |
| 14 import org.chromium.base.test.util.Feature; | |
| 15 import org.chromium.base.test.util.UrlUtils; | |
| 16 import org.chromium.chrome.browser.tab.Tab; | |
| 17 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | |
| 18 import org.chromium.chrome.test.util.browser.TabLoadObserver; | |
| 19 import org.chromium.content.browser.test.util.Criteria; | |
| 20 import org.chromium.content.browser.test.util.CriteriaHelper; | |
| 21 | |
| 22 import java.util.concurrent.Callable; | |
| 23 | |
| 24 /** | |
| 25 * Tests org.chromium.chrome.browser.ShortcutHelper and it's C++ counterpart. | |
| 26 */ | |
| 27 public class ShortcutHelperTest extends ChromeActivityTestCaseBase<ChromeActivit
y> { | |
| 28 private static final String WEBAPP_ACTION_NAME = "WEBAPP_ACTION"; | |
| 29 | |
| 30 private static final String WEBAPP_TITLE = "Webapp shortcut"; | |
| 31 private static final String WEBAPP_HTML = UrlUtils.encodeHtmlDataUri( | |
| 32 "<html><head>" | |
| 33 + "<meta name=\"mobile-web-app-capable\" content=\"yes\" />" | |
| 34 + "<title>" + WEBAPP_TITLE + "</title>" | |
| 35 + "</head><body>Webapp capable</body></html>"); | |
| 36 private static final String EDITED_WEBAPP_TITLE = "Webapp shortcut edited"; | |
| 37 | |
| 38 private static final String SECOND_WEBAPP_TITLE = "Webapp shortcut #2"; | |
| 39 private static final String SECOND_WEBAPP_HTML = UrlUtils.encodeHtmlDataUri( | |
| 40 "<html><head>" | |
| 41 + "<meta name=\"mobile-web-app-capable\" content=\"yes\" />" | |
| 42 + "<title>" + SECOND_WEBAPP_TITLE + "</title>" | |
| 43 + "</head><body>Webapp capable again</body></html>"); | |
| 44 | |
| 45 private static final String NORMAL_TITLE = "Plain shortcut"; | |
| 46 private static final String NORMAL_HTML = UrlUtils.encodeHtmlDataUri( | |
| 47 "<html>" | |
| 48 + "<head><title>" + NORMAL_TITLE + "</title></head>" | |
| 49 + "<body>Not Webapp capable</body></html>"); | |
| 50 | |
| 51 private static final String META_APP_NAME_PAGE_TITLE = "Not the right title"
; | |
| 52 private static final String META_APP_NAME_TITLE = "Web application-name"; | |
| 53 private static final String META_APP_NAME_HTML = UrlUtils.encodeHtmlDataUri( | |
| 54 "<html><head>" | |
| 55 + "<meta name=\"mobile-web-app-capable\" content=\"yes\" />" | |
| 56 + "<meta name=\"application-name\" content=\"" + META_APP_NAME_TITLE
+ "\">" | |
| 57 + "<title>" + META_APP_NAME_PAGE_TITLE + "</title>" | |
| 58 + "</head><body>Webapp capable</body></html>"); | |
| 59 | |
| 60 private static class TestShortcutHelperDelegate extends ShortcutHelper.Deleg
ate { | |
| 61 public Intent mBroadcastedIntent; | |
| 62 | |
| 63 @Override | |
| 64 public void sendBroadcast(Context context, Intent intent) { | |
| 65 mBroadcastedIntent = intent; | |
| 66 } | |
| 67 | |
| 68 @Override | |
| 69 public String getFullscreenAction() { | |
| 70 return WEBAPP_ACTION_NAME; | |
| 71 } | |
| 72 | |
| 73 public void clearBroadcastedIntent() { | |
| 74 mBroadcastedIntent = null; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 private ChromeActivity mActivity; | |
| 79 private TestShortcutHelperDelegate mShortcutHelperDelegate; | |
| 80 | |
| 81 public ShortcutHelperTest() { | |
| 82 super(ChromeActivity.class); | |
| 83 } | |
| 84 | |
| 85 @Override | |
| 86 public void startMainActivity() throws InterruptedException { | |
| 87 startMainActivityOnBlankPage(); | |
| 88 } | |
| 89 | |
| 90 @Override | |
| 91 public void setUp() throws Exception { | |
| 92 super.setUp(); | |
| 93 mShortcutHelperDelegate = new TestShortcutHelperDelegate(); | |
| 94 ShortcutHelper.setDelegateForTests(mShortcutHelperDelegate); | |
| 95 mActivity = getActivity(); | |
| 96 } | |
| 97 | |
| 98 @SmallTest | |
| 99 @Feature("{Webapp}") | |
| 100 public void testAddWebappShortcuts() throws InterruptedException { | |
| 101 // Add a webapp shortcut and make sure the intent's parameters make sens
e. | |
| 102 addShortcutToURL(WEBAPP_HTML, WEBAPP_TITLE, ""); | |
| 103 Intent firedIntent = mShortcutHelperDelegate.mBroadcastedIntent; | |
| 104 assertEquals(WEBAPP_TITLE, firedIntent.getStringExtra(Intent.EXTRA_SHORT
CUT_NAME)); | |
| 105 | |
| 106 Intent launchIntent = firedIntent.getParcelableExtra(Intent.EXTRA_SHORTC
UT_INTENT); | |
| 107 assertEquals(WEBAPP_HTML, launchIntent.getStringExtra(ShortcutHelper.EXT
RA_URL)); | |
| 108 assertEquals(WEBAPP_ACTION_NAME, launchIntent.getAction()); | |
| 109 assertEquals(mActivity.getPackageName(), launchIntent.getPackage()); | |
| 110 | |
| 111 // Add a second shortcut and make sure it matches the second webapp's pa
rameters. | |
| 112 mShortcutHelperDelegate.clearBroadcastedIntent(); | |
| 113 addShortcutToURL(SECOND_WEBAPP_HTML, SECOND_WEBAPP_TITLE, ""); | |
| 114 Intent newFiredIntent = mShortcutHelperDelegate.mBroadcastedIntent; | |
| 115 assertEquals(SECOND_WEBAPP_TITLE, | |
| 116 newFiredIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME)); | |
| 117 | |
| 118 Intent newLaunchIntent = newFiredIntent.getParcelableExtra(Intent.EXTRA_
SHORTCUT_INTENT); | |
| 119 assertEquals(SECOND_WEBAPP_HTML, newLaunchIntent.getStringExtra(Shortcut
Helper.EXTRA_URL)); | |
| 120 assertEquals(WEBAPP_ACTION_NAME, newLaunchIntent.getAction()); | |
| 121 assertEquals(mActivity.getPackageName(), newLaunchIntent.getPackage()); | |
| 122 } | |
| 123 | |
| 124 @SmallTest | |
| 125 @Feature("{Webapp}") | |
| 126 public void testAddBookmarkShortcut() throws InterruptedException { | |
| 127 addShortcutToURL(NORMAL_HTML, NORMAL_TITLE, ""); | |
| 128 | |
| 129 // Make sure the intent's parameters make sense. | |
| 130 Intent firedIntent = mShortcutHelperDelegate.mBroadcastedIntent; | |
| 131 assertEquals(NORMAL_TITLE, firedIntent.getStringExtra(Intent.EXTRA_SHORT
CUT_NAME)); | |
| 132 | |
| 133 Intent launchIntent = firedIntent.getParcelableExtra(Intent.EXTRA_SHORTC
UT_INTENT); | |
| 134 assertEquals(mActivity.getPackageName(), launchIntent.getPackage()); | |
| 135 assertEquals(Intent.ACTION_VIEW, launchIntent.getAction()); | |
| 136 assertEquals(NORMAL_HTML, launchIntent.getDataString()); | |
| 137 } | |
| 138 | |
| 139 @SmallTest | |
| 140 @Feature("{Webapp}") | |
| 141 public void testAddWebappShortcutsWithoutTitleEdit() throws InterruptedExcep
tion { | |
| 142 // Add a webapp shortcut using the page's title. | |
| 143 addShortcutToURL(WEBAPP_HTML, WEBAPP_TITLE, ""); | |
| 144 Intent firedIntent = mShortcutHelperDelegate.mBroadcastedIntent; | |
| 145 assertEquals(WEBAPP_TITLE, firedIntent.getStringExtra(Intent.EXTRA_SHORT
CUT_NAME)); | |
| 146 } | |
| 147 | |
| 148 @SmallTest | |
| 149 @Feature("{Webapp}") | |
| 150 public void testAddWebappShortcutsWithTitleEdit() throws InterruptedExceptio
n { | |
| 151 // Add a webapp shortcut with a custom title. | |
| 152 addShortcutToURL(WEBAPP_HTML, WEBAPP_TITLE, EDITED_WEBAPP_TITLE); | |
| 153 Intent firedIntent = mShortcutHelperDelegate.mBroadcastedIntent; | |
| 154 assertEquals(EDITED_WEBAPP_TITLE, firedIntent.getStringExtra(Intent.EXTR
A_SHORTCUT_NAME)); | |
| 155 } | |
| 156 | |
| 157 @SmallTest | |
| 158 @Feature("{Webapp}") | |
| 159 public void testAddWebappShortcutsWithApplicationName() throws InterruptedEx
ception { | |
| 160 addShortcutToURL(META_APP_NAME_HTML, META_APP_NAME_PAGE_TITLE, ""); | |
| 161 Intent firedIntent = mShortcutHelperDelegate.mBroadcastedIntent; | |
| 162 assertEquals(META_APP_NAME_TITLE, firedIntent.getStringExtra(Intent.EXTR
A_SHORTCUT_NAME)); | |
| 163 } | |
| 164 | |
| 165 private void addShortcutToURL(String url, final String expectedPageTitle, fi
nal String title) | |
| 166 throws InterruptedException { | |
| 167 final Tab activeTab = mActivity.getActivityTab(); | |
| 168 TabLoadObserver observer = new TabLoadObserver(activeTab, url) { | |
| 169 @Override | |
| 170 public boolean isSatisfied() { | |
| 171 // The page title is often updated over several iterations. Wai
t until the right | |
| 172 // one appears. | |
| 173 return super.isSatisfied() | |
| 174 && TextUtils.equals(activeTab.getTitle(), expectedPageTi
tle); | |
| 175 } | |
| 176 }; | |
| 177 assertTrue(CriteriaHelper.pollForUIThreadCriteria(observer)); | |
| 178 | |
| 179 // Add the shortcut. | |
| 180 Callable<ShortcutHelper> callable = new Callable<ShortcutHelper>() { | |
| 181 @Override | |
| 182 public ShortcutHelper call() { | |
| 183 final ShortcutHelper helper = new ShortcutHelper( | |
| 184 mActivity.getApplicationContext(), mActivity.getActivity
Tab()); | |
| 185 // Calling initialize() isn't strictly required but it is testin
g this code path. | |
| 186 helper.initialize(new ShortcutHelper.ShortcutHelperObserver() { | |
| 187 @Override | |
| 188 public void onUserTitleAvailable(String t) { | |
| 189 } | |
| 190 | |
| 191 @Override | |
| 192 public void onIconAvailable(Bitmap icon) { | |
| 193 helper.addShortcut(title); | |
| 194 } | |
| 195 }); | |
| 196 return helper; | |
| 197 } | |
| 198 }; | |
| 199 final ShortcutHelper helper = ThreadUtils.runOnUiThreadBlockingNoExcepti
on(callable); | |
| 200 | |
| 201 // Make sure that the shortcut was added. | |
| 202 assertTrue(CriteriaHelper.pollForUIThreadCriteria(new Criteria() { | |
| 203 @Override | |
| 204 public boolean isSatisfied() { | |
| 205 return mShortcutHelperDelegate.mBroadcastedIntent != null; | |
| 206 } | |
| 207 })); | |
| 208 | |
| 209 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 210 @Override | |
| 211 public void run() { | |
| 212 helper.destroy(); | |
| 213 } | |
| 214 }); | |
| 215 } | |
| 216 } | |
| OLD | NEW |