|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 MARIONETTE_TIMEOUT = 60000; |
|
5 MARIONETTE_HEAD_JS = 'mmdb_head.js'; |
|
6 |
|
7 const DBNAME = "test_mmdb_foreachmatchedmmsdeliveryinfo:" + newUUID(); |
|
8 |
|
9 const PHONE_0 = "+15555215500"; |
|
10 const PHONE_1 = "+15555215501"; |
|
11 const PHONE_2 = "+15555215502"; |
|
12 const PHONE_2_NET = "5555215502"; |
|
13 const PHONE_3 = "+15555215503"; |
|
14 const PHONE_3_NET = "5555215503"; |
|
15 const EMAIL_1 = "foo@bar.com"; |
|
16 let deliveryInfo = [ |
|
17 { receiver: PHONE_1 }, |
|
18 { receiver: PHONE_2 }, |
|
19 { receiver: PHONE_1 }, |
|
20 { receiver: PHONE_2_NET }, |
|
21 { receiver: PHONE_3_NET }, |
|
22 { receiver: EMAIL_1 }, |
|
23 { receiver: PHONE_3 }, |
|
24 ]; |
|
25 |
|
26 function clearTraversed(aDeliveryInfo) { |
|
27 for (let element of aDeliveryInfo) { |
|
28 delete element.traversed; |
|
29 } |
|
30 } |
|
31 |
|
32 function doTest(aMmdb, aNeedle, aVerifyFunc, aCount) { |
|
33 log(" '" + aNeedle + "': " + aCount); |
|
34 |
|
35 clearTraversed(deliveryInfo); |
|
36 |
|
37 let count = 0; |
|
38 aMmdb.forEachMatchedMmsDeliveryInfo(deliveryInfo, aNeedle, function(aElement) { |
|
39 ok(true, "checking " + aElement.receiver); |
|
40 ok(!aElement.hasOwnProperty("traversed"), "element.traversed"); |
|
41 aVerifyFunc(aElement); |
|
42 |
|
43 aElement.traversed = true; |
|
44 ++count; |
|
45 }); |
|
46 is(count, aCount, "matched count"); |
|
47 } |
|
48 |
|
49 function testNotFound(aMmdb) { |
|
50 log("Testing unavailable"); |
|
51 |
|
52 doTest(aMmdb, PHONE_0, function(aElement) { |
|
53 ok(false, "Should never have a match"); |
|
54 }, 0); |
|
55 } |
|
56 |
|
57 function testDirectMatch(aMmdb) { |
|
58 log("Testing direct matching"); |
|
59 |
|
60 for (let needle of [PHONE_1, EMAIL_1]) { |
|
61 let count = deliveryInfo.reduce(function(aCount, aElement) { |
|
62 return aElement.receiver == needle ? aCount + 1 : aCount; |
|
63 }, 0); |
|
64 doTest(aMmdb, needle, function(aElement) { |
|
65 is(aElement.receiver, needle, "element.receiver"); |
|
66 }, count); |
|
67 } |
|
68 } |
|
69 |
|
70 function testPhoneMatch(aMmdb) { |
|
71 log("Testing phone matching"); |
|
72 |
|
73 let verifyFunc = function(aValid, aElement) { |
|
74 ok(aValid.indexOf(aElement.receiver) >= 0, "element.receiver"); |
|
75 }; |
|
76 |
|
77 let matchingGroups = [ |
|
78 [PHONE_2, PHONE_2_NET], |
|
79 [PHONE_3, PHONE_3_NET], |
|
80 ]; |
|
81 for (let group of matchingGroups) { |
|
82 for (let item of group) { |
|
83 doTest(aMmdb, item, verifyFunc.bind(null, group), group.length); |
|
84 } |
|
85 } |
|
86 } |
|
87 |
|
88 startTestBase(function testCaseMain() { |
|
89 let mmdb = newMobileMessageDB(); |
|
90 return initMobileMessageDB(mmdb, DBNAME, 0) |
|
91 .then(() => testNotFound(mmdb)) |
|
92 .then(() => testDirectMatch(mmdb)) |
|
93 .then(() => testPhoneMatch(mmdb)) |
|
94 .then(() => closeMobileMessageDB(mmdb)); |
|
95 }); |