1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 package org.mozilla.gecko.tests;
6 
7 import java.io.File;
8 
9 import org.mozilla.gecko.db.BrowserContract.FormHistory;
10 
11 import android.content.ContentResolver;
12 import android.content.ContentValues;
13 import android.content.Context;
14 import android.net.Uri;
15 
16 /**
17  * A basic form history contentprovider test.
18  * - inserts an element in form history when it is not yet set up
19  * - inserts an element in form history
20  * - updates an element in form history
21  * - deletes an element in form history
22  */
23 public class testFormHistory extends OldBaseTest {
24     private static final String DB_NAME = "formhistory.sqlite";
25 
testFormHistory()26     public void testFormHistory() {
27         Context context = (Context)getActivity();
28         ContentResolver cr = context.getContentResolver();
29         ContentValues[] cvs = new ContentValues[1];
30         cvs[0] = new ContentValues();
31 
32         blockForGeckoReady();
33         blockForFormHistoryReady();
34 
35         Uri formHistoryUri;
36         Uri insertUri;
37         Uri expectedUri;
38         int numUpdated;
39         int numDeleted;
40 
41         cvs[0].put("fieldname", "fieldname");
42         cvs[0].put("value", "value");
43         cvs[0].put("timesUsed", "0");
44         cvs[0].put("guid", "guid");
45 
46         // Attempt to insert into the db
47         formHistoryUri = FormHistory.CONTENT_URI;
48         Uri.Builder builder = formHistoryUri.buildUpon();
49         formHistoryUri = builder.appendQueryParameter("profilePath", mProfile).build();
50 
51         insertUri = cr.insert(formHistoryUri, cvs[0]);
52         expectedUri = formHistoryUri.buildUpon().appendPath("1").build();
53         mAsserter.is(expectedUri.toString(), insertUri.toString(), "Insert returned correct uri");
54         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
55 
56         cvs[0].put("fieldname", "fieldname2");
57         cvs[0].putNull("guid");
58 
59         numUpdated = cr.update(formHistoryUri, cvs[0], null, null);
60         mAsserter.is(1, numUpdated, "Correct number updated");
61         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
62 
63         numDeleted = cr.delete(formHistoryUri, null, null);
64         mAsserter.is(1, numDeleted, "Correct number deleted");
65         cvs = new ContentValues[0];
66         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
67 
68         cvs = new ContentValues[1];
69         cvs[0] = new ContentValues();
70         cvs[0].put("fieldname", "fieldname");
71         cvs[0].put("value", "value");
72         cvs[0].put("timesUsed", "0");
73         cvs[0].putNull("guid");
74 
75         insertUri = cr.insert(formHistoryUri, cvs[0]);
76         expectedUri = formHistoryUri.buildUpon().appendPath("1").build();
77         mAsserter.is(expectedUri.toString(), insertUri.toString(), "Insert returned correct uri");
78         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
79 
80         cvs[0].put("guid", "guid");
81 
82         numUpdated = cr.update(formHistoryUri, cvs[0], null, null);
83         mAsserter.is(1, numUpdated, "Correct number updated");
84         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
85 
86         numDeleted = cr.delete(formHistoryUri, null, null);
87         mAsserter.is(1, numDeleted, "Correct number deleted");
88         cvs = new ContentValues[0];
89         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
90     }
91 
92     @Override
tearDown()93     public void tearDown() throws Exception {
94         // remove the entire signons.sqlite file
95         File profile = new File(mProfile);
96         File db = new File(profile, "formhistory.sqlite");
97         if (db.delete()) {
98             mAsserter.dumpLog("tearDown deleted "+db.toString());
99         } else {
100             mAsserter.dumpLog("tearDown did not delete "+db.toString());
101         }
102 
103         super.tearDown();
104     }
105 }
106