gecko-dev/mobile/android/base/tests/testPasswordProvider.java.in

91 строка
3.4 KiB
Java

#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", "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);
}
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();
}
}