gecko/mobile/android/base/tests/testPasswordProvider.java.in
Brian Nicholson 2d98f0cf71 Bug 850487 - More code cleanup (@Overrides and unused imports). r=kats
--HG--
extra : rebase_source : 376574e0c41b91c16a6be335584a4a61768bb4a9
2013-03-13 13:20:57 -07:00

98 lines
3.5 KiB
Java

#filter substitution
package @ANDROID_PACKAGE_NAME@.tests;
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";
@Override
protected int getTestType() {
return TEST_MOCHITEST;
}
public void testPasswordProvider() {
Context context = (Context)getActivity();
ContentResolver cr = context.getContentResolver();
ContentValues[] cvs = new ContentValues[1];
cvs[0] = new ContentValues();
blockForGeckoReady();
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", "1");
// 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;
}
Uri uri = cr.insert(passwordUri, cvs[0]);
Uri expectedUri = passwordUri.buildUpon().appendPath("1").build();
mAsserter.is(uri.toString(), expectedUri.toString(), "Insert returned correct uri");
Cursor c = cr.query(passwordUri, null, null, null, null);
SqliteCompare(c, cvs);
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");
c = cr.query(passwordUri, null, null, null, null);
SqliteCompare(c, cvs);
int numDeleted = cr.delete(passwordUri, null, null);
mAsserter.is(1, numDeleted, "Correct number deleted");
cvs = new ContentValues[0];
c = cr.query(passwordUri, null, null, null, null);
SqliteCompare(c, cvs);
}
@Override
public void tearDown() throws Exception {
// remove the entire signons.sqlite file
File profile = new File(mProfile);
File db = new File(profile, "signons.sqlite");
if (db.delete()) {
mAsserter.dumpLog("tearDown deleted "+db.toString());
} else {
mAsserter.dumpLog("tearDown did not delete "+db.toString());
}
super.tearDown();
}
}