gecko/mobile/android/base/tests/testFormHistory.java.in

92 lines
3.6 KiB
Java
Raw Normal View History

#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 java.lang.ClassLoader;
import java.util.ArrayList;
/**
* A basic form history contentprovider test.
* - inserts an element in form history when it is not yet set up
* - inserts an element in form history
* - updates an element in form history
* - deletes an element in form history
*/
public class testFormHistory extends BaseTest {
private static final String DB_NAME = "formhistory.sqlite";
public void testFormHistory() {
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 formHistoryUri;
try {
ClassLoader classLoader = getActivity().getClassLoader();
Class fh = classLoader.loadClass("org.mozilla.gecko.db.BrowserContract$FormHistory");
cvs[0].put("fieldname", "fieldname");
cvs[0].put("value", "value");
cvs[0].put("timesUsed", "0");
cvs[0].put("guid", "guid");
// Attempt to insert into the db
formHistoryUri = (Uri)fh.getField("CONTENT_URI").get(null);
Uri.Builder builder = formHistoryUri.buildUpon();
formHistoryUri = 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 insert should fail the first time round because there is no form history database
// Wait for gecko to reply and then we'll try again
contentEventExpecter = mActions.expectGeckoEvent("FormHistory:Init:Return");
Uri uri = cr.insert(formHistoryUri, cvs[0]);
mAsserter.is(uri, null, "Insert returned null correctly");
contentEventExpecter.blockForEvent();
uri = cr.insert(formHistoryUri, cvs[0]);
Uri expectedUri = formHistoryUri.buildUpon().appendPath("1").build();
mAsserter.is(expectedUri.toString(), uri.toString(), "Insert returned correct uri");
SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
cvs[0].put("fieldname", "fieldname2");
int numUpdated = cr.update(formHistoryUri, cvs[0], null, null);
mAsserter.is(1, numUpdated, "Correct number updated");
SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
int numDeleted = cr.delete(formHistoryUri, null, null);
mAsserter.is(1, numDeleted, "Correct number deleted");
cvs = new ContentValues[0];
SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
}
public void tearDown() throws Exception {
super.tearDown();
// remove the entire signons.sqlite file
File profile = new File(mProfile);
File db = new File(profile, "formhistory.sqlite");
db.delete();
}
}