|
1 <!DOCTYPE html> |
|
2 <html> |
|
3 <head> |
|
4 <title>Migration tests</title> |
|
5 <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> |
|
6 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
8 </head> |
|
9 <body> |
|
10 <h1>migration tests</h1> |
|
11 <p id="display"></p> |
|
12 <div id="content" style="display: none"> |
|
13 |
|
14 </div> |
|
15 <pre id="test"> |
|
16 <script type="text/javascript;version=1.8" src="shared.js"></script> |
|
17 <script class="testbody" type="text/javascript"> |
|
18 "use strict"; |
|
19 |
|
20 var backend, contactsCount, allContacts; |
|
21 function loadChromeScript() { |
|
22 var url = SimpleTest.getTestFileURL("test_migration_chrome.js"); |
|
23 backend = SpecialPowers.loadChromeScript(url); |
|
24 } |
|
25 |
|
26 function addBackendEvents() { |
|
27 backend.addMessageListener("createDB.success", function(count) { |
|
28 contactsCount = count; |
|
29 ok(true, "Created the database"); |
|
30 next(); |
|
31 }); |
|
32 backend.addMessageListener("createDB.error", function(err) { |
|
33 ok(false, err); |
|
34 next(); |
|
35 }); |
|
36 |
|
37 backend.addMessageListener("deleteDB.success", function() { |
|
38 ok(true, "Deleted the database"); |
|
39 next(); |
|
40 }); |
|
41 backend.addMessageListener("deleteDB.error", function(err) { |
|
42 ok(false, err); |
|
43 next(); |
|
44 }); |
|
45 } |
|
46 |
|
47 function createDB(version) { |
|
48 info("Will create the DB at version " + version); |
|
49 backend.sendAsyncMessage("createDB", version); |
|
50 } |
|
51 |
|
52 function deleteDB() { |
|
53 info("Will delete the DB."); |
|
54 backend.sendAsyncMessage("deleteDB"); |
|
55 } |
|
56 |
|
57 function setSubstringMatching(value) { |
|
58 info("Setting substring matching to " + value); |
|
59 |
|
60 if (value) { |
|
61 SpecialPowers.setIntPref("dom.phonenumber.substringmatching.BR", value); |
|
62 |
|
63 // this is the Mcc for Brazil, so that we trigger the previous pref |
|
64 SpecialPowers.setCharPref("ril.lastKnownSimMcc", "724"); |
|
65 } else { |
|
66 SpecialPowers.clearUserPref("dom.phonenumber.substringmatching.BR"); |
|
67 SpecialPowers.clearUserPref("ril.lastKnownSimMcc"); |
|
68 } |
|
69 |
|
70 next(); |
|
71 } |
|
72 |
|
73 var steps = [ |
|
74 function setupChromeScript() { |
|
75 loadChromeScript(); |
|
76 addBackendEvents(); |
|
77 next(); |
|
78 }, |
|
79 |
|
80 deleteDB, // let's be sure the DB does not exist yet |
|
81 createDB.bind(null, 12), |
|
82 setSubstringMatching.bind(null, 7), |
|
83 |
|
84 function testAccessMozContacts() { |
|
85 info("Checking we have the right number of contacts: " + contactsCount); |
|
86 var req = mozContacts.getCount(); |
|
87 req.onsuccess = function onsuccess() { |
|
88 ok(true, "Could access the mozContacts API"); |
|
89 ise(this.result, contactsCount, "Contacts count is correct"); |
|
90 next(); |
|
91 }; |
|
92 |
|
93 req.onerror = function onerror() { |
|
94 ok(false, "Couldn't access the mozContacts API"); |
|
95 next(); |
|
96 }; |
|
97 }, |
|
98 |
|
99 function testRetrieveAllContacts() { |
|
100 /* if the migration does not work right, either we'll have an error, or the |
|
101 contacts won't be migrated properly and thus will fail WebIDL conversion, |
|
102 which will manifest as a timeout */ |
|
103 info("Checking the contacts are corrected to obey WebIDL constraints. (upgrades 14 to 17)"); |
|
104 var req = mozContacts.find(); |
|
105 req.onsuccess = function onsuccess() { |
|
106 if (this.result) { |
|
107 ise(this.result.length, contactsCount, "Contacts array length is correct"); |
|
108 allContacts = this.result; |
|
109 next(); |
|
110 } else { |
|
111 ok(false, "Could access the mozContacts API but got no contacts!"); |
|
112 next(); |
|
113 } |
|
114 }; |
|
115 |
|
116 req.onerror = function onerror() { |
|
117 ok(false, "Couldn't access the mozContacts API"); |
|
118 next(); |
|
119 }; |
|
120 }, |
|
121 |
|
122 function checkNameIndex() { |
|
123 info("Checking name index migration (upgrades 17 to 19)."); |
|
124 if (!allContacts) { |
|
125 next(); |
|
126 } |
|
127 |
|
128 var count = allContacts.length; |
|
129 |
|
130 function finishRequest() { |
|
131 count--; |
|
132 if (!count) { |
|
133 next(); |
|
134 } |
|
135 } |
|
136 |
|
137 allContacts.forEach(function(contact) { |
|
138 var name = contact.name && contact.name[0]; |
|
139 if (!name) { |
|
140 count--; |
|
141 return; |
|
142 } |
|
143 |
|
144 var req = mozContacts.find({ |
|
145 filterBy: ["name"], |
|
146 filterValue: name, |
|
147 filterOp: "equals" |
|
148 }); |
|
149 |
|
150 req.onsuccess = function onsuccess() { |
|
151 if (this.result) { |
|
152 info("Found contact '" + name + "', checking it's the correct one."); |
|
153 checkContacts(this.result[0], contact); |
|
154 } else { |
|
155 ok(false, "Could not find contact with name '" + name + "'"); |
|
156 } |
|
157 |
|
158 finishRequest(); |
|
159 }; |
|
160 |
|
161 req.onerror = function onerror() { |
|
162 ok(false, "Error while finding contact with name '" + name + "'!"); |
|
163 finishRequest(); |
|
164 } |
|
165 }); |
|
166 |
|
167 if (!count) { |
|
168 ok(false, "No contact had a name, this is unexpected."); |
|
169 next(); |
|
170 } |
|
171 }, |
|
172 |
|
173 function checkSubstringMatching() { |
|
174 var subject = "0004567890"; // the last 7 digits are the same that at least one contact |
|
175 info("Looking for a contact matching " + subject); |
|
176 var req = mozContacts.find({ |
|
177 filterValue: subject, |
|
178 filterOp: "match", |
|
179 filterBy: ["tel"], |
|
180 filterLimit: 1 |
|
181 }); |
|
182 |
|
183 req.onsuccess = function onsuccess() { |
|
184 if (this.result && this.result[0]) { |
|
185 ok(true, "Found a contact with number " + this.result[0].tel[0].value); |
|
186 } |
|
187 next(); |
|
188 }; |
|
189 |
|
190 req.onerror = function onerror() { |
|
191 ok(false, "Error while finding contact for substring matching check!"); |
|
192 next(); |
|
193 }; |
|
194 }, |
|
195 |
|
196 deleteDB, |
|
197 setSubstringMatching.bind(null, null), |
|
198 |
|
199 function finish() { |
|
200 backend.destroy(); |
|
201 info("all done!\n"); |
|
202 SimpleTest.finish(); |
|
203 } |
|
204 ]; |
|
205 |
|
206 start_tests(); |
|
207 </script> |
|
208 </pre> |
|
209 </body> |
|
210 </html> |