2012-02-22 18:26:04 -08:00
|
|
|
#filter substitution
|
|
|
|
package @ANDROID_PACKAGE_NAME@.tests;
|
|
|
|
|
|
|
|
import @ANDROID_PACKAGE_NAME@.*;
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.provider.Browser;
|
|
|
|
|
|
|
|
public class testBookmarklets extends BaseTest {
|
2012-06-27 16:56:49 -07:00
|
|
|
@Override
|
|
|
|
protected int getTestType() {
|
|
|
|
return TEST_MOCHITEST;
|
|
|
|
}
|
|
|
|
|
2012-02-22 18:26:04 -08:00
|
|
|
public void testBookmarklets() {
|
|
|
|
final String url = getAbsoluteUrl("/robocop/robocop_blank_01.html");
|
|
|
|
final String title = "alertBookmarklet";
|
|
|
|
final String js = "javascript:alert(12 + .34)";
|
|
|
|
boolean alerted;
|
|
|
|
|
|
|
|
mActions.expectGeckoEvent("Gecko:Ready").blockForEvent();
|
|
|
|
|
|
|
|
// load a standard page so bookmarklets work
|
|
|
|
loadUrl(url);
|
|
|
|
|
|
|
|
// verify that user-entered bookmarklets do *not* work
|
|
|
|
enterUrl(js);
|
|
|
|
mActions.sendSpecialKey(Actions.SpecialKey.ENTER);
|
|
|
|
alerted = waitForTest(new BooleanTest() {
|
|
|
|
public boolean test() {
|
|
|
|
return mSolo.searchButton("OK", true) || mSolo.searchText("12.34", true);
|
|
|
|
}
|
|
|
|
}, 3000);
|
|
|
|
mAsserter.is(alerted, false, "Alert was not shown for user-entered bookmarklet");
|
|
|
|
|
|
|
|
// add the bookmarklet to the database. there's currently no way to
|
|
|
|
// add this using the UI, so we go through the content provider.
|
|
|
|
addOrUpdateBookmarklet(title, js);
|
|
|
|
|
|
|
|
// verify that bookmarklets clicked in awesomescreen work
|
|
|
|
Activity awesomeBarActivity = clickOnAwesomeBar();
|
|
|
|
mActions.sendSpecialKey(Actions.SpecialKey.RIGHT);
|
|
|
|
getInstrumentation().waitForIdleSync();
|
|
|
|
mSolo.clickOnText(title);
|
|
|
|
alerted = waitForTest(new BooleanTest() {
|
|
|
|
public boolean test() {
|
|
|
|
return mSolo.searchButton("OK", true) && mSolo.searchText("12.34", true);
|
|
|
|
}
|
|
|
|
}, 3000);
|
|
|
|
mAsserter.is(alerted, true, "Alert was shown for clicked bookmarklet");
|
2012-06-27 06:53:53 -07:00
|
|
|
|
|
|
|
// remove the bookmarklet
|
|
|
|
removeBookmarklet(js);
|
2012-02-22 18:26:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addOrUpdateBookmarklet(String title, String url) {
|
|
|
|
ContentResolver resolver = getActivity().getContentResolver();
|
|
|
|
Uri bookmarksUri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/bookmarks");
|
|
|
|
bookmarksUri = bookmarksUri.buildUpon().appendQueryParameter("profile", "default").build();
|
|
|
|
long mobileFolderId = -1;
|
|
|
|
|
|
|
|
Cursor c = resolver.query(bookmarksUri,
|
|
|
|
new String[] { "_id" },
|
|
|
|
"guid = ?",
|
|
|
|
new String[] { "mobile" },
|
|
|
|
null);
|
|
|
|
if (c.moveToFirst())
|
|
|
|
mobileFolderId = c.getLong(c.getColumnIndexOrThrow("_id"));
|
|
|
|
c.close();
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(Browser.BookmarkColumns.TITLE, title);
|
|
|
|
values.put("url", url);
|
|
|
|
values.put("parent", mobileFolderId);
|
|
|
|
values.put("modified", System.currentTimeMillis());
|
|
|
|
|
|
|
|
int updated = resolver.update(bookmarksUri,
|
|
|
|
values,
|
|
|
|
"url = ?",
|
|
|
|
new String[] { url });
|
|
|
|
if (updated == 0)
|
|
|
|
resolver.insert(bookmarksUri, values);
|
|
|
|
}
|
2012-06-27 06:53:53 -07:00
|
|
|
|
|
|
|
private void removeBookmarklet(String url) {
|
|
|
|
ContentResolver resolver = getActivity().getContentResolver();
|
|
|
|
Uri bookmarksUri = Uri.parse("content://@ANDROID_PACKAGE_NAME@.db.browser/bookmarks");
|
|
|
|
bookmarksUri = bookmarksUri.buildUpon().appendQueryParameter("profile", "default").build();
|
|
|
|
|
|
|
|
resolver.delete(bookmarksUri, "url = ?", new String[] { url });
|
|
|
|
}
|
2012-02-22 18:26:04 -08:00
|
|
|
}
|