Bug 725052 - Tests for inserting passwords. r=rnewman

This commit is contained in:
Wes Johnston 2012-03-12 10:09:09 -07:00
parent 9f0cd8785e
commit 09f7a02759
2 changed files with 99 additions and 0 deletions

View File

@ -12,6 +12,7 @@
[testWebContentContextMenu] [testWebContentContextMenu]
[testPasswordProvider] [testPasswordProvider]
[testPasswordEncrypt] [testPasswordEncrypt]
[testPasswordUndeletion]
[testFormHistory] [testFormHistory]
# Used for Talos, please don't use in mochitest # Used for Talos, please don't use in mochitest

View File

@ -0,0 +1,98 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#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;
import android.util.Log;
import java.util.ArrayList;
import java.lang.reflect.Method;
public class testPasswordUndeletion extends BaseTest {
public void testPasswordUndeletion() {
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();
File db = new File(mProfile, "signons.sqlite");
String dbPath = db.getPath();
Uri passwordUri;
Uri deletedPasswordUri;
try {
ClassLoader classLoader = getActivity().getClassLoader();
Class pwds = classLoader.loadClass("org.mozilla.gecko.db.BrowserContract$Passwords");
Class deletedpwds = classLoader.loadClass("org.mozilla.gecko.db.BrowserContract$DeletedPasswords");
Class nss = classLoader.loadClass("org.mozilla.gecko.NSSBridge");
Class contextClass = classLoader.loadClass("android.content.Context");
Class stringClass = classLoader.loadClass("java.lang.String");
Class appshell = classLoader.loadClass("org.mozilla.gecko.GeckoAppShell");
Method ensureNSSLibsLoaded = appshell.getMethod("ensureNSSLibsLoaded", contextClass, stringClass);
Method decrypt = nss.getMethod("decrypt", contextClass, stringClass, stringClass);
Method encrypt = nss.getMethod("encrypt", contextClass, stringClass, stringClass);
cvs[0].put("guid", "MtaVURdK-eRl");
// Attempt to insert into the database.
deletedPasswordUri = (Uri) deletedpwds.getField("CONTENT_URI").get(null);
deletedPasswordUri = deletedPasswordUri.buildUpon().appendQueryParameter("profilePath", mProfile).build();
passwordUri = (Uri) pwds.getField("CONTENT_URI").get(null);
passwordUri = passwordUri.buildUpon().appendQueryParameter("profilePath", mProfile).build();
Uri uri = cr.insert(deletedPasswordUri, cvs[0]);
mAsserter.is(uri, null, "Insert returned null correctly");
// This 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");
contentEventExpecter.blockForEvent();
cr.insert(deletedPasswordUri, cvs[0]);
// Attempting to insert another item with the same guid should
// remove the item from deletedPasswords database.
cr.insert(passwordUri, cvs[0]);
Cursor c = cr.query(deletedPasswordUri, null, null, null, null);
SqliteCompare(c, new ContentValues[0]);
c = cr.query(passwordUri, null, null, null, null);
SqliteCompare(c, cvs);
} catch(ClassNotFoundException ex) {
mAsserter.ok(false, "Error getting class", ex.toString());
return;
} catch(NoSuchFieldException ex) {
mAsserter.ok(false, "Error getting field", ex.toString());
return;
} catch(IllegalAccessException ex) {
mAsserter.ok(false, "Error using field", ex.toString());
return;
} catch(java.lang.NoSuchMethodException ex) {
mAsserter.ok(false, "Error getting method", ex.toString());
return;
}
}
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();
}
}