This source file includes following definitions.
- onSendBroadcast
- reset
- setUp
- testAddWebappShortcuts
- testAddBookmarkShortcut
- testAddWebappShortcutsWithoutTitleEdit
- testAddWebappShortcutsWithTitleEdit
- addShortcutToURL
package org.chromium.chrome.browser;
import android.content.Intent;
import android.test.FlakyTest;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.chrome.shell.ChromeShellActivity;
import org.chromium.chrome.shell.ChromeShellApplication;
import org.chromium.chrome.shell.ChromeShellApplicationObserver;
import org.chromium.chrome.shell.ChromeShellTestBase;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
public class ShortcutHelperTest extends ChromeShellTestBase {
private static final String WEBAPP_ACTION_NAME = "WEBAPP_ACTION";
private static final String WEBAPP_TITLE = "Webapp shortcut";
private static final String WEBAPP_HTML = UrlUtils.encodeHtmlDataUri(
"<html><head>"
+ "<meta name=\"mobile-web-app-capable\" content=\"yes\" />"
+ "<title>" + WEBAPP_TITLE + "</title>"
+ "</head><body>Webapp capable</body></html>");
private static final String EDITED_WEBAPP_TITLE = "Webapp shortcut edited";
private static final String SECOND_WEBAPP_TITLE = "Webapp shortcut #2";
private static final String SECOND_WEBAPP_HTML = UrlUtils.encodeHtmlDataUri(
"<html><head>"
+ "<meta name=\"mobile-web-app-capable\" content=\"yes\" />"
+ "<title>" + SECOND_WEBAPP_TITLE + "</title>"
+ "</head><body>Webapp capable again</body></html>");
private static final String NORMAL_TITLE = "Plain shortcut";
private static final String NORMAL_HTML = UrlUtils.encodeHtmlDataUri(
"<html>"
+ "<head><title>" + NORMAL_TITLE + "</title></head>"
+ "<body>Not Webapp capable</body></html>");
private static class TestObserver implements ChromeShellApplicationObserver {
Intent firedIntent;
@Override
public boolean onSendBroadcast(Intent intent) {
if (intent.hasExtra(Intent.EXTRA_SHORTCUT_NAME)) {
firedIntent = intent;
return false;
}
return true;
}
public void reset() {
firedIntent = null;
}
}
private ChromeShellActivity mActivity;
private TestObserver mTestObserver;
@Override
public void setUp() throws Exception {
ShortcutHelper.setFullScreenAction(WEBAPP_ACTION_NAME);
mActivity = launchChromeShellWithBlankPage();
mTestObserver = new TestObserver();
ChromeShellApplication application =
(ChromeShellApplication) mActivity.getApplication();
application.addObserver(mTestObserver);
super.setUp();
}
@FlakyTest
public void testAddWebappShortcuts() throws InterruptedException {
addShortcutToURL(WEBAPP_HTML, "");
Intent firedIntent = mTestObserver.firedIntent;
assertEquals(WEBAPP_TITLE, firedIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME));
Intent launchIntent = firedIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
assertEquals(WEBAPP_HTML, launchIntent.getStringExtra(ShortcutHelper.EXTRA_URL));
assertEquals(WEBAPP_ACTION_NAME, launchIntent.getAction());
assertEquals(mActivity.getPackageName(), launchIntent.getPackage());
mTestObserver.reset();
addShortcutToURL(SECOND_WEBAPP_HTML, "");
Intent newFiredIntent = mTestObserver.firedIntent;
assertEquals(SECOND_WEBAPP_TITLE,
newFiredIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME));
Intent newLaunchIntent = newFiredIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
assertEquals(SECOND_WEBAPP_HTML, newLaunchIntent.getStringExtra(ShortcutHelper.EXTRA_URL));
assertEquals(WEBAPP_ACTION_NAME, newLaunchIntent.getAction());
assertEquals(mActivity.getPackageName(), newLaunchIntent.getPackage());
}
@FlakyTest
public void testAddBookmarkShortcut() throws InterruptedException {
addShortcutToURL(NORMAL_HTML, "");
Intent firedIntent = mTestObserver.firedIntent;
assertEquals(NORMAL_TITLE, firedIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME));
Intent launchIntent = firedIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
assertEquals(mActivity.getPackageName(), launchIntent.getPackage());
assertEquals(Intent.ACTION_VIEW, launchIntent.getAction());
assertEquals(NORMAL_HTML, launchIntent.getDataString());
}
@FlakyTest
public void testAddWebappShortcutsWithoutTitleEdit() throws InterruptedException {
addShortcutToURL(WEBAPP_HTML, "");
Intent firedIntent = mTestObserver.firedIntent;
assertEquals(WEBAPP_TITLE, firedIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME));
}
@FlakyTest
public void testAddWebappShortcutsWithTitleEdit() throws InterruptedException {
addShortcutToURL(WEBAPP_HTML, EDITED_WEBAPP_TITLE);
Intent firedIntent = mTestObserver.firedIntent;
assertEquals(EDITED_WEBAPP_TITLE , firedIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME));
}
private void addShortcutToURL(String url, final String title) throws InterruptedException {
loadUrlWithSanitization(url);
assertTrue(waitForActiveShellToBeDoneLoading());
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
ShortcutHelper.addShortcut(mActivity.getApplicationContext(),
mActivity.getActiveTab(), title);
}
});
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return mTestObserver.firedIntent != null;
}
}));
}