Bug 463154 - Form history should record creation/usage timestamps. r=mconnor, r=sdwilsh
[wine-gecko.git] / toolkit / components / satchel / test / unit / test_db_update_v1.js
blobceed7c22bb2c0aabe88fc1bc71670f14b0d6e5d2
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Satchel Test Code.
16 * The Initial Developer of the Original Code is
17 * Mozilla Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Justin Dolske <dolske@mozilla.com> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 // Returns true if the timestamp is within 30 seconds of now.
39 function is_about_now(timestamp) {
40 var delta = Math.abs(timestamp - 1000 * Date.now());
41 var seconds = 30 * 1000000;
42 return delta < seconds;
45 function run_test()
47 try {
48 var testnum = 0;
50 // ===== test init =====
51 var testfile = do_get_file("toolkit/components/satchel/test/unit/formhistory_v0.sqlite");
52 var profileDir = dirSvc.get("ProfD", Ci.nsIFile);
54 // Cleanup from any previous tests or failures.
55 var destFile = profileDir.clone();
56 destFile.append("formhistory.sqlite");
57 if (destFile.exists())
58 destFile.remove(false);
60 testfile.copyTo(profileDir, "formhistory.sqlite");
62 var fh = Cc["@mozilla.org/satchel/form-history;1"].
63 getService(Ci.nsIFormHistory2);
66 // ===== 1 =====
67 testnum++;
68 // Check for expected contents.
69 do_check_true(fh.entryExists("name-A", "value-A"));
70 do_check_true(fh.entryExists("name-B", "value-B"));
71 do_check_true(fh.entryExists("name-C", "value-C1"));
72 do_check_true(fh.entryExists("name-C", "value-C2"));
75 // ===== 2 =====
76 testnum++;
77 // Exercise adding and removing a name/value pair
78 do_check_false(fh.entryExists("name-D", "value-D"));
79 fh.addEntry("name-D", "value-D");
80 do_check_true(fh.entryExists("name-D", "value-D"));
81 fh.removeEntry("name-D", "value-D");
82 do_check_false(fh.entryExists("name-D", "value-D"));
85 // ===== 3 =====
86 testnum++;
87 // Add a new entry, check expected properties
88 do_check_false(fh.entryExists("name-E", "value-E"));
89 fh.addEntry("name-E", "value-E");
90 do_check_true(fh.entryExists("name-E", "value-E"));
92 var query = "SELECT timesUsed, firstUsed, lastUsed " +
93 "FROM moz_formhistory WHERE fieldname = 'name-E'";
94 var stmt = fh.DBConnection.createStatement(query);
95 stmt.executeStep();
97 var timesUsed = stmt.getInt32(0);
98 var firstUsed = stmt.getInt64(1);
99 var lastUsed = stmt.getInt64(2);
101 do_check_eq(1, timesUsed);
102 do_check_true(firstUsed == lastUsed);
103 do_check_true(is_about_now(firstUsed));
105 // ===== 4 =====
106 testnum++;
107 // Add entry again, check for updated properties.
108 do_check_true(fh.entryExists("name-E", "value-E"));
109 fh.addEntry("name-E", "value-E");
110 do_check_true(fh.entryExists("name-E", "value-E"));
112 query = "SELECT timesUsed, firstUsed, lastUsed " +
113 "FROM moz_formhistory WHERE fieldname = 'name-E'";
114 stmt = fh.DBConnection.createStatement(query);
115 stmt.executeStep();
117 timesUsed = stmt.getInt32(0);
118 var firstUsed2 = stmt.getInt64(1);
119 var lastUsed2 = stmt.getInt64(2);
121 do_check_eq(2, timesUsed);
122 do_check_true(is_about_now(lastUsed2));
124 do_check_true(firstUsed == firstUsed2); //unchanged
125 do_check_true(lastUsed != lastUsed2); //changed
126 do_check_true(firstUsed2 != lastUsed2);
129 } catch (e) {
130 throw "FAILED in test #" + testnum + " -- " + e;