1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/mobilemessage/tests/marionette/test_mmdb_foreachmatchedmmsdeliveryinfo.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +MARIONETTE_TIMEOUT = 60000; 1.8 +MARIONETTE_HEAD_JS = 'mmdb_head.js'; 1.9 + 1.10 +const DBNAME = "test_mmdb_foreachmatchedmmsdeliveryinfo:" + newUUID(); 1.11 + 1.12 +const PHONE_0 = "+15555215500"; 1.13 +const PHONE_1 = "+15555215501"; 1.14 +const PHONE_2 = "+15555215502"; 1.15 +const PHONE_2_NET = "5555215502"; 1.16 +const PHONE_3 = "+15555215503"; 1.17 +const PHONE_3_NET = "5555215503"; 1.18 +const EMAIL_1 = "foo@bar.com"; 1.19 +let deliveryInfo = [ 1.20 + { receiver: PHONE_1 }, 1.21 + { receiver: PHONE_2 }, 1.22 + { receiver: PHONE_1 }, 1.23 + { receiver: PHONE_2_NET }, 1.24 + { receiver: PHONE_3_NET }, 1.25 + { receiver: EMAIL_1 }, 1.26 + { receiver: PHONE_3 }, 1.27 +]; 1.28 + 1.29 +function clearTraversed(aDeliveryInfo) { 1.30 + for (let element of aDeliveryInfo) { 1.31 + delete element.traversed; 1.32 + } 1.33 +} 1.34 + 1.35 +function doTest(aMmdb, aNeedle, aVerifyFunc, aCount) { 1.36 + log(" '" + aNeedle + "': " + aCount); 1.37 + 1.38 + clearTraversed(deliveryInfo); 1.39 + 1.40 + let count = 0; 1.41 + aMmdb.forEachMatchedMmsDeliveryInfo(deliveryInfo, aNeedle, function(aElement) { 1.42 + ok(true, "checking " + aElement.receiver); 1.43 + ok(!aElement.hasOwnProperty("traversed"), "element.traversed"); 1.44 + aVerifyFunc(aElement); 1.45 + 1.46 + aElement.traversed = true; 1.47 + ++count; 1.48 + }); 1.49 + is(count, aCount, "matched count"); 1.50 +} 1.51 + 1.52 +function testNotFound(aMmdb) { 1.53 + log("Testing unavailable"); 1.54 + 1.55 + doTest(aMmdb, PHONE_0, function(aElement) { 1.56 + ok(false, "Should never have a match"); 1.57 + }, 0); 1.58 +} 1.59 + 1.60 +function testDirectMatch(aMmdb) { 1.61 + log("Testing direct matching"); 1.62 + 1.63 + for (let needle of [PHONE_1, EMAIL_1]) { 1.64 + let count = deliveryInfo.reduce(function(aCount, aElement) { 1.65 + return aElement.receiver == needle ? aCount + 1 : aCount; 1.66 + }, 0); 1.67 + doTest(aMmdb, needle, function(aElement) { 1.68 + is(aElement.receiver, needle, "element.receiver"); 1.69 + }, count); 1.70 + } 1.71 +} 1.72 + 1.73 +function testPhoneMatch(aMmdb) { 1.74 + log("Testing phone matching"); 1.75 + 1.76 + let verifyFunc = function(aValid, aElement) { 1.77 + ok(aValid.indexOf(aElement.receiver) >= 0, "element.receiver"); 1.78 + }; 1.79 + 1.80 + let matchingGroups = [ 1.81 + [PHONE_2, PHONE_2_NET], 1.82 + [PHONE_3, PHONE_3_NET], 1.83 + ]; 1.84 + for (let group of matchingGroups) { 1.85 + for (let item of group) { 1.86 + doTest(aMmdb, item, verifyFunc.bind(null, group), group.length); 1.87 + } 1.88 + } 1.89 +} 1.90 + 1.91 +startTestBase(function testCaseMain() { 1.92 + let mmdb = newMobileMessageDB(); 1.93 + return initMobileMessageDB(mmdb, DBNAME, 0) 1.94 + .then(() => testNotFound(mmdb)) 1.95 + .then(() => testDirectMatch(mmdb)) 1.96 + .then(() => testPhoneMatch(mmdb)) 1.97 + .then(() => closeMobileMessageDB(mmdb)); 1.98 +});