#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.content.Context; import android.net.Uri; import java.io.File; /** * A basic password contentprovider test. * - inserts a password when the database is not yet set up * - inserts a password * - updates a password * - deletes a password */ public class testPasswordProvider extends BaseTest { private static final String DB_NAME = "signons.sqlite"; public void testPasswordProvider() { setTestType("mochitest"); Context context = (Context)getActivity(); ContentResolver cr = context.getContentResolver(); ContentValues[] cvs = new ContentValues[1]; cvs[0] = new ContentValues(); Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("Gecko:Ready"); contentEventExpecter.blockForEvent(); Uri passwordUri; try { ClassLoader classLoader = getActivity().getClassLoader(); Class pwds = classLoader.loadClass("org.mozilla.gecko.db.BrowserContract$Passwords"); cvs[0].put("hostname", "http://www.example.com"); cvs[0].put("httpRealm", "http://www.example.com"); cvs[0].put("formSubmitURL", "http://www.example.com"); cvs[0].put("usernameField", "usernameField"); cvs[0].put("passwordField", "passwordField"); cvs[0].put("encryptedUsername", "username"); cvs[0].put("encryptedPassword", "password"); cvs[0].put("encType", "0"); // Attempt to insert into the db passwordUri = (Uri)pwds.getField("CONTENT_URI").get(null); Uri.Builder builder = passwordUri.buildUpon(); passwordUri = builder.appendQueryParameter("profilePath", mProfile).build(); } catch(ClassNotFoundException ex) { mAsserter.is(false, true, "Error getting class"); return; } catch(NoSuchFieldException ex) { mAsserter.is(false, true, "Error getting field"); return; } catch(IllegalAccessException ex) { mAsserter.is(false, true, "Error using field"); return; } // Trying to inset should fail the first time round because there is no pw database // Wait for gecko to reply and then we'll try again contentEventExpecter = mActions.expectGeckoEvent("Passwords:Init:Return"); Uri uri = cr.insert(passwordUri, cvs[0]); mAsserter.is(uri, null, "Insert returned null correctly"); contentEventExpecter.blockForEvent(); uri = cr.insert(passwordUri, cvs[0]); SqliteCompare(DB_NAME, "SELECT * FROM moz_logins", cvs); Uri expectedUri = passwordUri.buildUpon().appendPath("1").build(); mAsserter.is(expectedUri.toString(), uri.toString(), "Insert returned correct uri"); cvs[0].put("usernameField", "usernameField2"); cvs[0].put("passwordField", "passwordField2"); int numUpdated = cr.update(passwordUri, cvs[0], null, null); mAsserter.is(1, numUpdated, "Correct number updated"); SqliteCompare(DB_NAME, "SELECT * FROM moz_logins", cvs); int numDeleted = cr.delete(passwordUri, null, null); mAsserter.is(1, numDeleted, "Correct number deleted"); cvs = new ContentValues[0]; SqliteCompare(DB_NAME, "SELECT * FROM moz_logins", cvs); } public void tearDown() throws Exception { super.tearDown(); // remove the entire signons.sqlite file File profile = new File(mProfile); File db = new File(profile, "signons.sqlite"); db.delete(); } }