dom/contacts/tests/test_contacts_basics.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE html>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=674720
     5 -->
     6 <head>
     7   <title>Test for Bug 674720 WebContacts</title>
     8   <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
     9   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    10   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    11 </head>
    12 <body>
    14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=674720">Mozilla Bug 674720</a>
    15 <p id="display"></p>
    16 <div id="content" style="display: none">
    18 </div>
    19 <pre id="test">
    20 <script type="text/javascript;version=1.8" src="http://mochi.test:8888/tests/dom/contacts/tests/shared.js"></script>
    21 <script class="testbody" type="text/javascript">
    22 "use strict";
    24 var initialRev;
    26 function checkRevision(revision, msg, then) {
    27   var revReq = mozContacts.getRevision();
    28   revReq.onsuccess = function(e) {
    29     is(e.target.result, initialRev+revision, msg);
    30     then();
    31   };
    32   // The revision function isn't supported on Android so treat on failure as success
    33   if (isAndroid) {
    34     revReq.onerror = function(e) {
    35       then();
    36     };
    37   } else {
    38     revReq.onerror = onFailure;
    39   }
    40 }
    42 var req;
    44 var steps = [
    45   function() {
    46     req = mozContacts.getRevision();
    47     req.onsuccess = function(e) {
    48       initialRev = e.target.result;
    49       next();
    50     };
    52     // Android does not support the revision function. Treat errors as success.
    53     if (isAndroid) {
    54       req.onerror = function(e) {
    55         initialRev = 0;
    56         next();
    57       };
    58     } else {
    59       req.onerror = onFailure;
    60     }
    61   },
    62   function () {
    63     ok(true, "Deleting database");
    64     checkRevision(0, "Initial revision is 0", function() {
    65       req = mozContacts.clear();
    66       req.onsuccess = function () {
    67         ok(true, "Deleted the database");
    68         checkCount(0, "No contacts after clear", function() {
    69           checkRevision(1, "Revision was incremented on clear", next);
    70         });
    71       };
    72       req.onerror = onFailure;
    73     });
    74   },
    75   function () {
    76     ok(true, "Retrieving all contacts");
    77     req = mozContacts.find(defaultOptions);
    78     req.onsuccess = function () {
    79       is(req.result.length, 0, "Empty database.");
    80       checkRevision(1, "Revision was not incremented on find", next);
    81     };
    82     req.onerror = onFailure;
    83   },
    84   function () {
    85     ok(true, "Adding empty contact");
    86     createResult1 = new mozContact({});
    87     req = navigator.mozContacts.save(createResult1);
    88     req.onsuccess = function () {
    89       ok(createResult1.id, "The contact now has an ID.");
    90       sample_id1 = createResult1.id;
    91       checkCount(1, "1 contact after adding empty contact", function() {
    92         checkRevision(2, "Revision was incremented on save", next);
    93       });
    94     };
    95     req.onerror = onFailure;
    96   },
    97   function () {
    98     ok(true, "Retrieving all contacts");
    99     req = mozContacts.find(defaultOptions);
   100     req.onsuccess = function () {
   101       is(req.result.length, 1, "One contact.");
   102       findResult1 = req.result[0];
   103       next();
   104     };
   105     req.onerror = onFailure;
   106   },
   107   function () {
   108     ok(true, "Deleting empty contact");
   109     req = navigator.mozContacts.remove(findResult1);
   110     req.onsuccess = function () {
   111       var req2 = mozContacts.find(defaultOptions);
   112       req2.onsuccess = function () {
   113         is(req2.result.length, 0, "Empty Database.");
   114         clearTemps();
   115         checkRevision(3, "Revision was incremented on remove", next);
   116       }
   117       req2.onerror = onFailure;
   118     }
   119     req.onerror = onFailure;
   120   },
   121   function () {
   122     ok(true, "Adding a new contact1");
   123     createResult1 = new mozContact(properties1);
   125     mozContacts.oncontactchange = function(event) {
   126       is(event.contactID, createResult1.id, "Same contactID");
   127       is(event.reason, "create", "Same reason");
   128       next();
   129     }
   131     req = navigator.mozContacts.save(createResult1);
   132     req.onsuccess = function () {
   133       ok(createResult1.id, "The contact now has an ID.");
   134       sample_id1 = createResult1.id;
   135       checkContacts(createResult1, properties1);
   136     };
   137     req.onerror = onFailure;
   138   },
   139   function () {
   140     ok(true, "Retrieving by substring 1");
   141     var options = {filterBy: ["givenName"],
   142                    filterOp: "startsWith",
   143                    filterValue: properties1.givenName[1].substring(0,3)};
   144     req = mozContacts.find(options);
   145     req.onsuccess = function () {
   146       is(req.result.length, 1, "Found exactly 1 contact.");
   147       findResult1 = req.result[0];
   148       ok(findResult1.id == sample_id1, "Same ID");
   149       checkContacts(createResult1, properties1);
   150       // Some manual testing. Testint the testfunctions
   151       // tel: [{type: ["work"], value: "123456", carrier: "testCarrier"} , {type: ["home", "fax"], value: "+55 (31) 9876-3456"}],
   152       is(findResult1.tel[0].carrier, "testCarrier", "Same Carrier");
   153       is(findResult1.tel[0].type, "work", "Same type");
   154       is(findResult1.tel[0].value, "123456", "Same Value");
   155       is(findResult1.tel[1].type[1], "fax", "Same type");
   156       is(findResult1.tel[1].value, "+55 (31) 9876-3456", "Same Value");
   158       is(findResult1.adr[0].countryName, "country 1", "Same country");
   160       // email: [{type: ["work"], value: "x@y.com"}]
   161       is(findResult1.email[0].type, "work", "Same Type");
   162       is(findResult1.email[0].value, "x@y.com", "Same Value");
   163       next();
   164     };
   165     req.onerror = onFailure;
   166   },
   167   function () {
   168     ok(true, "Searching for exact email");
   169     var options = {filterBy: ["email"],
   170                    filterOp: "equals",
   171                    filterValue: properties1.email[0].value};
   172     req = mozContacts.find(options);
   173     req.onsuccess = function () {
   174       is(req.result.length, 1, "Found exactly 1 contact.");
   175       findResult1 = req.result[0];
   176       ok(findResult1.id == sample_id1, "Same ID");
   177       checkContacts(findResult1, createResult1);
   178       next();
   179     };
   180     req.onerror = onFailure;
   181   },
   182   function () {
   183     ok(true, "Retrieving by substring and update");
   184     mozContacts.oncontactchange = function(event) {
   185        is(event.contactID, findResult1.id, "Same contactID");
   186        is(event.reason, "update", "Same reason");
   187      }
   188     var options = {filterBy: ["givenName"],
   189                    filterOp: "startsWith",
   190                    filterValue: properties1.givenName[0].substring(0,3)};
   191     req = mozContacts.find(options);
   192     req.onsuccess = function () {
   193       is(req.result.length, 1, "Found exactly 1 contact.");
   194       findResult1 = req.result[0];
   195       findResult1.jobTitle = ["new Job"];
   196       ok(findResult1.id == sample_id1, "Same ID");
   197       checkContacts(createResult1, properties1);
   198       next();
   199     };
   200     req.onerror = onFailure;
   201   },
   202   function () {
   203     ok(true, "Adding a new contact");
   204     mozContacts.oncontactchange = function(event) {
   205        is(event.contactID, createResult2.id, "Same contactID");
   206        is(event.reason, "create", "Same reason");
   207      }
   208     createResult2 = new mozContact({name: ["newName"]});
   209     req = navigator.mozContacts.save(createResult2);
   210     req.onsuccess = function () {
   211       ok(createResult2.id, "The contact now has an ID.");
   212       next();
   213     };
   214     req.onerror = onFailure;
   215   },
   216   function () {
   217     ok(true, "Retrieving by substring 2");
   218     var options = {filterBy: ["givenName"],
   219                    filterOp: "startsWith",
   220                    filterValue: properties1.givenName[0].substring(0,3)};
   221     req = mozContacts.find(options);
   222     req.onsuccess = function () {
   223       is(req.result.length, 1, "Found exactly 1 contact.");
   224       findResult1 = req.result[0];
   225       checkContacts(createResult1, findResult1);
   226       next();
   227     };
   228     req.onerror = onFailure;
   229   },
   230   function() {
   231     ok(true, "Retrieving by name equality 1");
   232     var options = {filterBy: ["name"],
   233                    filterOp: "equals",
   234                    filterValue: properties1.name[0]};
   235     req = mozContacts.find(options);
   236     req.onsuccess = function () {
   237       is(req.result.length, 1, "Found exactly 1 contact.");
   238       findResult1 = req.result[0];
   239       checkContacts(createResult1, findResult1);
   240       next();
   241     };
   242     req.onerror = onFailure;
   243   },
   244   function() {
   245     ok(true, "Retrieving by name equality 2");
   246     var options = {filterBy: ["name"],
   247                    filterOp: "equals",
   248                    filterValue: properties1.name[1]};
   249     req = mozContacts.find(options);
   250     req.onsuccess = function () {
   251       is(req.result.length, 1, "Found exactly 1 contact.");
   252       findResult1 = req.result[0];
   253       checkContacts(createResult1, findResult1);
   254       next();
   255     };
   256     req.onerror = onFailure;
   257   },
   258   function() {
   259     ok(true, "Retrieving by name substring 1");
   260     var options = {filterBy: ["name"],
   261                    filterOp: "startsWith",
   262                    filterValue: properties1.name[0].substring(0,3).toLowerCase()};
   263     req = mozContacts.find(options);
   264     req.onsuccess = function () {
   265       is(req.result.length, 1, "Found exactly 1 contact.");
   266       findResult1 = req.result[0];
   267       checkContacts(createResult1, findResult1);
   268       next();
   269     };
   270     req.onerror = onFailure;
   271   },
   272   function() {
   273     ok(true, "Retrieving by name substring 2");
   274     var options = {filterBy: ["name"],
   275                    filterOp: "startsWith",
   276                    filterValue: properties1.name[1].substring(0,3).toLowerCase()};
   277     req = mozContacts.find(options);
   278     req.onsuccess = function () {
   279       is(req.result.length, 1, "Found exactly 1 contact.");
   280       findResult1 = req.result[0];
   281       checkContacts(createResult1, findResult1);
   282       next();
   283     };
   284     req.onerror = onFailure;
   285   },
   286   function () {
   287     ok(true, "Remove contact1");
   288     mozContacts.oncontactchange = function(event) {
   289       is(event.contactID, createResult1.id, "Same contactID");
   290       is(event.reason, "remove", "Same reason");
   291     }
   292     req = navigator.mozContacts.remove(createResult1);
   293     req.onsuccess = function () {
   294       next();
   295     };
   296     req.onerror = onFailure;
   297   },
   298   function () {
   299     ok(true, "Retrieving by substring 3");
   300     var options = {filterBy: ["givenName"],
   301                    filterOp: "startsWith",
   302                    filterValue: properties1.givenName[1].substring(0,3)};
   303     req = mozContacts.find(options);
   304     req.onsuccess = function () {
   305       is(req.result.length, 0, "Found no contact.");
   306       next();
   307     };
   308     req.onerror = onFailure;
   309   },
   310   function () {
   311     ok(true, "Remove contact2");
   312     mozContacts.oncontactchange = function(event) {
   313       is(event.contactID, createResult2.id, "Same contactID");
   314       is(event.reason, "remove", "Same reason");
   315     }
   316     req = navigator.mozContacts.remove(createResult2);
   317     req.onsuccess = function () {
   318       next();
   319     };
   320     req.onerror = onFailure;
   321   },
   322   function () {
   323     ok(true, "Retrieving by substring 4");
   324     var options = {filterBy: ["givenName"],
   325                    filterOp: "startsWith",
   326                    filterValue: properties1.givenName[1].substring(0,3)};
   327     req = mozContacts.find(options);
   328     req.onsuccess = function () {
   329       is(req.result.length, 0, "Found no contact.");
   330       next();
   331     };
   332     req.onerror = onFailure;
   333   },
   334   function () {
   335     ok(true, "Deleting database");
   336     mozContacts.oncontactchange = function(event) {
   337       is(event.contactID, "undefined", "Same contactID");
   338       is(event.reason, "remove", "Same reason");
   339     }
   340     req = mozContacts.clear();
   341     req.onsuccess = function () {
   342       ok(true, "Deleted the database");
   343       next();
   344     };
   345     req.onerror = onFailure;
   346   },
   347   function () {
   348     ok(true, "Adding a new contact with properties1");
   349     createResult1 = new mozContact(properties1);
   350     mozContacts.oncontactchange = null;
   351     req = navigator.mozContacts.save(createResult1);
   352     req.onsuccess = function () {
   353       ok(createResult1.id, "The contact now has an ID.");
   354       sample_id1 = createResult1.id;
   355       checkContacts(createResult1, properties1);
   356       next();
   357     };
   358     req.onerror = onFailure;
   359   },
   360   function () {
   361     ok(true, "Retrieving by substring tel1");
   362     var options = {filterBy: ["tel"],
   363                    filterOp: "contains",
   364                    filterValue: properties1.tel[1].value.substring(2,5)};
   365     req = mozContacts.find(options);
   366     req.onsuccess = function () {
   367       is(req.result.length, 1, "Found exactly 1 contact.");
   368       findResult1 = req.result[0];
   369       ok(findResult1.id == sample_id1, "Same ID");
   370       checkContacts(createResult1, properties1);
   371       next();
   372     };
   373     req.onerror = onFailure;
   374   },
   375   function () {
   376     ok(true, "Retrieving by tel exact");
   377     var options = {filterBy: ["tel"],
   378                    filterOp: "equals",
   379                    filterValue: "+55 319 8 7 6 3456"};
   380     req = mozContacts.find(options);
   381     req.onsuccess = function () {
   382       is(req.result.length, 1, "Found exactly 1 contact.");
   383       findResult1 = req.result[0];
   384       ok(findResult1.id == sample_id1, "Same ID");
   385       checkContacts(createResult1, properties1);
   386       next();
   387     };
   388     req.onerror = onFailure;
   389   },
   390   function () {
   391     ok(true, "Retrieving by tel exact with substring");
   392     var options = {filterBy: ["tel"],
   393                    filterOp: "equals",
   394                    filterValue: "3456"};
   395     req = mozContacts.find(options);
   396     req.onsuccess = function () {
   397       is(req.result.length, 0, "Found no contacts.");
   398       next();
   399     };
   400     req.onerror = onFailure;
   401   },
   402   function () {
   403     ok(true, "Retrieving by tel exact with substring");
   404     var options = {filterBy: ["tel"],
   405                    filterOp: "equals",
   406                    filterValue: "+55 (31)"};
   407     req = mozContacts.find(options);
   408     req.onsuccess = function () {
   409       is(req.result.length, 0, "Found no contacts.");
   410       next();
   411     };
   412     req.onerror = onFailure;
   413   },
   414   function () {
   415     ok(true, "Retrieving by tel match national number");
   416     var options = {filterBy: ["tel"],
   417                    filterOp: "match",
   418                    filterValue: "3198763456"};
   419     req = mozContacts.find(options);
   420     req.onsuccess = function () {
   421       is(req.result.length, 1, "Found exactly 1 contact.");
   422       findResult1 = req.result[0];
   423       ok(findResult1.id == sample_id1, "Same ID");
   424       checkContacts(createResult1, properties1);
   425       next();
   426     };
   427     req.onerror = onFailure;
   428   },
   429   function () {
   430     ok(true, "Retrieving by tel match national format");
   431     var options = {filterBy: ["tel"],
   432                    filterOp: "match",
   433                    filterValue: "0451 491934"};
   434     req = mozContacts.find(options);
   435     req.onsuccess = function () {
   436       is(req.result.length, 1, "Found exactly 1 contact.");
   437       findResult1 = req.result[0];
   438       ok(findResult1.id == sample_id1, "Same ID");
   439       checkContacts(createResult1, properties1);
   440       next();
   441     };
   442     req.onerror = onFailure;
   443   },
   444   function () {
   445     ok(true, "Retrieving by tel match entered number");
   446     var options = {filterBy: ["tel"],
   447                    filterOp: "match",
   448                    filterValue: "123456"};
   449     req = mozContacts.find(options);
   450     req.onsuccess = function () {
   451       is(req.result.length, 1, "Found exactly 1 contact.");
   452       findResult1 = req.result[0];
   453       ok(findResult1.id == sample_id1, "Same ID");
   454       checkContacts(createResult1, properties1);
   455       next();
   456     };
   457     req.onerror = onFailure;
   458   },
   459   function () {
   460     ok(true, "Retrieving by tel match international number");
   461     var options = {filterBy: ["tel"],
   462                    filterOp: "match",
   463                    filterValue: "+55 31 98763456"};
   464     req = mozContacts.find(options);
   465     req.onsuccess = function () {
   466       is(req.result.length, 1, "Found exactly 1 contact.");
   467       findResult1 = req.result[0];
   468       ok(findResult1.id == sample_id1, "Same ID");
   469       checkContacts(createResult1, properties1);
   470       next();
   471     };
   472     req.onerror = onFailure;
   473   },
   474   function () {
   475     ok(true, "Retrieving by match with field other than tel");
   476     var options = {filterBy: ["givenName"],
   477                    filterOp: "match",
   478                    filterValue: "my friends call me 555-4040"};
   479     req = mozContacts.find(options);
   480     req.onsuccess = onUnwantedSuccess;
   481     req.onerror = function() {
   482       ok(true, "Failed");
   483       next();
   484     }
   485   },
   486   function () {
   487     ok(true, "Retrieving by substring tel2");
   488     var options = {filterBy: ["tel"],
   489                    filterOp: "startsWith",
   490                    filterValue: "9876"};
   491     req = mozContacts.find(options);
   492     req.onsuccess = function () {
   493       is(req.result.length, 1, "Found exactly 1 contact.");
   494       findResult1 = req.result[0];
   495       ok(findResult1.id == sample_id1, "Same ID");
   496       checkContacts(createResult1, properties1);
   497       next();
   498     };
   499     req.onerror = onFailure;
   500   },
   501   function () {
   502     ok(true, "Retrieving by substring tel3");
   503     var options = {filterBy: ["tel"],
   504                    filterOp: "startsWith",
   505                    filterValue: "98763456"};
   506     req = mozContacts.find(options);
   507     req.onsuccess = function () {
   508       is(req.result.length, 1, "Found exactly 1 contact.");
   509       findResult1 = req.result[0];
   510       ok(findResult1.id == sample_id1, "Same ID");
   511       checkContacts(createResult1, properties1);
   512       next();
   513     };
   514     req.onerror = onFailure;
   515   },
   516   function () {
   517     ok(true, "Retrieving by substring 5");
   518     var options = {filterBy: ["givenName"],
   519                    filterOp: "startsWith",
   520                    filterValue: properties1.givenName[0].substring(0,3)};
   521     req = mozContacts.find(options);
   522     req.onsuccess = function () {
   523       is(req.result.length, 1, "Found exactly 1 contact.");
   524       findResult1 = req.result[0];
   525       ok(findResult1.id == sample_id1, "Same ID");
   526       checkContacts(createResult1, properties1);
   527       next();
   528     };
   529     req.onerror = onFailure;
   530   },
   531   function () {
   532     ok(true, "Retrieving by substring 6");
   533     var options = {filterBy: ["familyName", "givenName"],
   534                    filterOp: "startsWith",
   535                    filterValue: properties1.givenName[0].substring(0,3)};
   536     req = mozContacts.find(options);
   537     req.onsuccess = function () {
   538       is(req.result.length, 1, "Found exactly 1 contact.");
   539       findResult1 = req.result[0];
   540       ok(findResult1.id == sample_id1, "Same ID");
   541       checkContacts(createResult1, properties1);
   542       next();
   543     };
   544     req.onerror = onFailure;
   545   },
   546   function () {
   547     ok(true, "Retrieving by substring3, Testing multi entry");
   548     var options = {filterBy: ["givenName", "familyName"],
   549                    filterOp: "startsWith",
   550                    filterValue: properties1.familyName[1].substring(0,3).toLowerCase()};
   551     req = mozContacts.find(options);
   552     req.onsuccess = function () {
   553       is(req.result.length, 1, "Found exactly 1 contact.");
   554       findResult1 = req.result[0];
   555       ok(findResult1.id == sample_id1, "Same ID");
   556       checkContacts(createResult1, properties1);
   557       next();
   558     };
   559     req.onerror = onFailure;
   560   },
   561   function () {
   562     ok(true, "Retrieving all contacts");
   563     req = mozContacts.find(defaultOptions);
   564     req.onsuccess = function() {
   565       is(req.result.length, 1, "Found exactly 1 contact.");
   566       findResult1 = req.result[0];
   567       ok(findResult1.id == sample_id1, "Same ID");
   568       checkContacts(createResult1, findResult1);
   569       if (!isAndroid) {
   570         ok(findResult1.updated, "Has updated field");
   571         ok(findResult1.published, "Has published field");
   572       }
   573       next();
   574     }
   575     req.onerror = onFailure;
   576   },
   577   function () {
   578     ok(true, "Modifying contact1");
   579     if (!findResult1) {
   580       SpecialPowers.executeSoon(next);
   581     } else {
   582       findResult1.impp = properties1.impp = [{value:"phil impp"}];
   583       req = navigator.mozContacts.save(findResult1);
   584       req.onsuccess = function () {
   585         var req2 = mozContacts.find(defaultOptions);
   586         req2.onsuccess = function() {
   587           is(req2.result.length, 1, "Found exactly 1 contact.");
   588           findResult2 = req2.result[0];
   589           ok(findResult2.id == sample_id1, "Same ID");
   590           checkContacts(findResult2, properties1);
   591           is(findResult2.impp.length, 1, "Found exactly 1 IMS info.");
   592           next();
   593         };
   594         req2.onerror = onFailure;
   595       };
   596       req.onerror = onFailure;
   597     }
   598   },
   599   function() {
   600     // Android does not support published/updated fields. Skip this.
   601     if (isAndroid) {
   602       next();
   603       return;
   604     }
   606     ok(true, "Saving old contact, should abort!");
   607     req = mozContacts.save(createResult1);
   608     req.onsuccess = onUnwantedSuccess;
   609     req.onerror   = function() { ok(true, "Successfully declined updating old contact!"); next(); };
   610   },
   611   function () {
   612     ok(true, "Retrieving a specific contact by ID");
   613     var options = {filterBy: ["id"],
   614                    filterOp: "equals",
   615                    filterValue: sample_id1};
   616     req = mozContacts.find(options);
   617     req.onsuccess = function () {
   618       is(req.result.length, 1, "Found exactly 1 contact.");
   619       findResult1 = req.result[0];
   620       ok(findResult1.id == sample_id1, "Same ID");
   621       checkContacts(findResult1, properties1);
   622       next();
   623     };
   624     req.onerror = onFailure;
   625   },
   626   function () {
   627     ok(true, "Retrieving a specific contact by givenName");
   628     var options = {filterBy: ["givenName"],
   629                    filterOp: "equals",
   630                    filterValue: properties1.givenName[0]};
   631     req = mozContacts.find(options);
   632     req.onsuccess = function () {
   633       is(req.result.length, 1, "Found exactly 1 contact.");
   634       findResult1 = req.result[0];
   635       ok(findResult1.id == sample_id1, "Same ID");
   636       checkContacts(findResult1, properties1);
   637       next();
   638     }
   639     req.onerror = onFailure;
   640   },
   641   function () {
   642     ok(true, "Modifying contact2");
   643     if (!findResult1) {
   644       SpecialPowers.executeSoon(next);
   645     } else {
   646       findResult1.impp = properties1.impp = [{value: "phil impp"}];
   647       req = mozContacts.save(findResult1);
   648       req.onsuccess = function () {
   649         var req2 = mozContacts.find(defaultOptions);
   650         req2.onsuccess = function () {
   651           is(req2.result.length, 1, "Found exactly 1 contact.");
   652           findResult1 = req2.result[0];
   653           ok(findResult1.id == sample_id1, "Same ID");
   654           checkContacts(findResult1, properties1);
   655           is(findResult1.impp.length, 1, "Found exactly 1 IMS info.");
   656           next();
   657         }
   658         req2.onerror = onFailure;
   659       };
   660       req.onerror = onFailure;
   661     }
   662   },
   663   function () {
   664     ok(true, "Searching contacts by query");
   665     var options = {filterBy: ["givenName", "email"],
   666                    filterOp: "startsWith",
   667                    filterValue: properties1.givenName[0].substring(0,4)};
   668     req = mozContacts.find(options);
   669     req.onsuccess = function () {
   670       is(req.result.length, 1, "Found exactly 1 contact.");
   671       findResult1 = req.result[0];
   672       ok(findResult1.id == sample_id1, "Same ID");
   673       checkContacts(findResult1, properties1);
   674       next();
   675     };
   676     req.onerror = onFailure;
   677   },
   678   function () {
   679     ok(true, "Searching contacts by query");
   680     var options = {filterBy: ["givenName", "email"],
   681                    filterOp: "startsWith",
   682                    filterValue: properties1.givenName[0]};
   683     req = mozContacts.find(options);
   684     req.onsuccess = function () {
   685       is(req.result.length, 1, "Found exactly 1 contact.");
   686       findResult1 = req.result[0];
   687       ok(findResult1.id == sample_id1, "Same ID");
   688       checkContacts(findResult1, properties1);
   689       next();
   690     };
   691     req.onerror = onFailure;
   692   },
   693   function () {
   694     ok(true, "Searching contacts with multiple indices");
   695     var options = {filterBy: ["email", "givenName"],
   696                    filterOp: "equals",
   697                    filterValue: properties1.givenName[1]};
   698     req = mozContacts.find(options);
   699     req.onsuccess = function () {
   700       is(req.result.length, 1, "Found exactly 1 contact.");
   701       findResult1 = req.result[0];
   702       ok(findResult1.id == sample_id1, "Same ID");
   703       checkContacts(findResult1, properties1);
   704       next();
   705     };
   706     req.onerror = onFailure;
   707   },
   708   function () {
   709     ok(true, "Modifying contact3");
   710     if (!findResult1) {
   711       SpecialPowers.executeSoon(next);
   712     } else {
   713       findResult1.email = [{value: properties1.nickname}];
   714       findResult1.nickname = ["TEST"];
   715       var newContact = new mozContact(findResult1);
   716       req = mozContacts.save(newContact);
   717       req.onsuccess = function () {
   718         var options = {filterBy: ["email", "givenName"],
   719                        filterOp: "startsWith",
   720                        filterValue: properties1.givenName[0]};
   721         // One contact has it in nickname and the other in email
   722         var req2 = mozContacts.find(options);
   723         req2.onsuccess = function () {
   724           is(req2.result.length, 2, "Found exactly 2 contacts.");
   725           ok(req2.result[0].id != req2.result[1].id, "Different ID");
   726           next();
   727         }
   728         req2.onerror = onFailure;
   729       };
   730       req.onerror = onFailure;
   731     }
   732   },
   733   function () {
   734     ok(true, "Deleting contact" + findResult1);
   735     req = mozContacts.remove(findResult1);
   736     req.onsuccess = function () {
   737       var req2 = mozContacts.find(defaultOptions);
   738       req2.onsuccess = function () {
   739         is(req2.result.length, 1, "One contact left.");
   740         findResult1 = req2.result[0];
   741         next();
   742       }
   743       req2.onerror = onFailure;
   744     }
   745     req.onerror = onFailure;
   746   },
   747   function () {
   748     ok(true, "Deleting database");
   749     req = mozContacts.remove(findResult1);
   750     req.onsuccess =  function () {
   751       clearTemps();
   752       next();
   753     };
   754     req.onerror = onFailure;
   755   },
   756   function() {
   757     ok(true, "Test JSON.stringify output for mozContact objects");
   758     var json = JSON.parse(JSON.stringify(new mozContact(properties1)));
   759     checkContacts(json, properties1);
   760     next();
   761   },
   762   function() {
   763     ok(true, "Test slice");
   764     var c = new mozContact();
   765     c.email = [{ type: ["foo"], value: "bar@baz" }]
   766     var arr = c.email;
   767     is(arr[0].value, "bar@baz", "Should have the right value");
   768     arr = arr.slice();
   769     is(arr[0].value, "bar@baz", "Should have the right value after slicing");
   770     next();
   771   },
   772   function () {
   773     ok(true, "all done!\n");
   774     clearTemps();
   776     SimpleTest.finish();
   777   }
   778 ];
   780 start_tests();
   781 </script>
   782 </pre>
   783 </body>
   784 </html>

mercurial