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 BaseTest {
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 
34         Uri formHistoryUri;
35         Uri insertUri;
36         Uri expectedUri;
37         int numUpdated;
38         int numDeleted;
39 
40         cvs[0].put("fieldname", "fieldname");
41         cvs[0].put("value", "value");
42         cvs[0].put("timesUsed", "0");
43         cvs[0].put("guid", "guid");
44 
45         // Attempt to insert into the db
46         formHistoryUri = FormHistory.CONTENT_URI;
47         Uri.Builder builder = formHistoryUri.buildUpon();
48         formHistoryUri = builder.appendQueryParameter("profilePath", mProfile).build();
49 
50         insertUri = cr.insert(formHistoryUri, cvs[0]);
51         expectedUri = formHistoryUri.buildUpon().appendPath("1").build();
52         mAsserter.is(expectedUri.toString(), insertUri.toString(), "Insert returned correct uri");
53         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
54 
55         cvs[0].put("fieldname", "fieldname2");
56         cvs[0].putNull("guid");
57 
58         numUpdated = cr.update(formHistoryUri, cvs[0], null, null);
59         mAsserter.is(1, numUpdated, "Correct number updated");
60         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
61 
62         numDeleted = cr.delete(formHistoryUri, null, null);
63         mAsserter.is(1, numDeleted, "Correct number deleted");
64         cvs = new ContentValues[0];
65         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
66 
67         cvs = new ContentValues[1];
68         cvs[0] = new ContentValues();
69         cvs[0].put("fieldname", "fieldname");
70         cvs[0].put("value", "value");
71         cvs[0].put("timesUsed", "0");
72         cvs[0].putNull("guid");
73 
74         insertUri = cr.insert(formHistoryUri, cvs[0]);
75         expectedUri = formHistoryUri.buildUpon().appendPath("1").build();
76         mAsserter.is(expectedUri.toString(), insertUri.toString(), "Insert returned correct uri");
77         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
78 
79         cvs[0].put("guid", "guid");
80 
81         numUpdated = cr.update(formHistoryUri, cvs[0], null, null);
82         mAsserter.is(1, numUpdated, "Correct number updated");
83         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
84 
85         numDeleted = cr.delete(formHistoryUri, null, null);
86         mAsserter.is(1, numDeleted, "Correct number deleted");
87         cvs = new ContentValues[0];
88         SqliteCompare(DB_NAME, "SELECT * FROM moz_formhistory", cvs);
89     }
90 
91     @Override
tearDown()92     public void tearDown() throws Exception {
93         // remove the entire signons.sqlite file
94         File profile = new File(mProfile);
95         File db = new File(profile, "formhistory.sqlite");
96         if (db.delete()) {
97             mAsserter.dumpLog("tearDown deleted "+db.toString());
98         } else {
99             mAsserter.dumpLog("tearDown did not delete "+db.toString());
100         }
101 
102         super.tearDown();
103     }
104 }
105