dom/contacts/tests/test_migration.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/contacts/tests/test_migration.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,210 @@
     1.4 +<!DOCTYPE html>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Migration tests</title>
     1.8 +  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
     1.9 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body>
    1.13 +<h1>migration tests</h1>
    1.14 +<p id="display"></p>
    1.15 +<div id="content" style="display: none">
    1.16 +
    1.17 +</div>
    1.18 +<pre id="test">
    1.19 +<script type="text/javascript;version=1.8" src="shared.js"></script>
    1.20 +<script class="testbody" type="text/javascript">
    1.21 +"use strict";
    1.22 +
    1.23 +var backend, contactsCount, allContacts;
    1.24 +function loadChromeScript() {
    1.25 +  var url = SimpleTest.getTestFileURL("test_migration_chrome.js");
    1.26 +  backend = SpecialPowers.loadChromeScript(url);
    1.27 +}
    1.28 +
    1.29 +function addBackendEvents() {
    1.30 +  backend.addMessageListener("createDB.success", function(count) {
    1.31 +    contactsCount = count;
    1.32 +    ok(true, "Created the database");
    1.33 +    next();
    1.34 +  });
    1.35 +  backend.addMessageListener("createDB.error", function(err) {
    1.36 +    ok(false, err);
    1.37 +    next();
    1.38 +  });
    1.39 +
    1.40 +  backend.addMessageListener("deleteDB.success", function() {
    1.41 +    ok(true, "Deleted the database");
    1.42 +    next();
    1.43 +  });
    1.44 +  backend.addMessageListener("deleteDB.error", function(err) {
    1.45 +    ok(false, err);
    1.46 +    next();
    1.47 +  });
    1.48 +}
    1.49 +
    1.50 +function createDB(version) {
    1.51 +  info("Will create the DB at version " + version);
    1.52 +  backend.sendAsyncMessage("createDB", version);
    1.53 +}
    1.54 +
    1.55 +function deleteDB() {
    1.56 +  info("Will delete the DB.");
    1.57 +  backend.sendAsyncMessage("deleteDB");
    1.58 +}
    1.59 +
    1.60 +function setSubstringMatching(value) {
    1.61 +  info("Setting substring matching to " + value);
    1.62 +
    1.63 +  if (value) {
    1.64 +    SpecialPowers.setIntPref("dom.phonenumber.substringmatching.BR", value);
    1.65 +
    1.66 +    // this is the Mcc for Brazil, so that we trigger the previous pref
    1.67 +    SpecialPowers.setCharPref("ril.lastKnownSimMcc", "724");
    1.68 +  } else {
    1.69 +    SpecialPowers.clearUserPref("dom.phonenumber.substringmatching.BR");
    1.70 +    SpecialPowers.clearUserPref("ril.lastKnownSimMcc");
    1.71 +  }
    1.72 +
    1.73 +  next();
    1.74 +}
    1.75 +
    1.76 +var steps = [
    1.77 +  function setupChromeScript() {
    1.78 +    loadChromeScript();
    1.79 +    addBackendEvents();
    1.80 +    next();
    1.81 +  },
    1.82 +
    1.83 +  deleteDB, // let's be sure the DB does not exist yet
    1.84 +  createDB.bind(null, 12),
    1.85 +  setSubstringMatching.bind(null, 7),
    1.86 +
    1.87 +  function testAccessMozContacts() {
    1.88 +    info("Checking we have the right number of contacts: " + contactsCount);
    1.89 +    var req = mozContacts.getCount();
    1.90 +    req.onsuccess = function onsuccess() {
    1.91 +      ok(true, "Could access the mozContacts API");
    1.92 +      ise(this.result, contactsCount, "Contacts count is correct");
    1.93 +      next();
    1.94 +    };
    1.95 +
    1.96 +    req.onerror = function onerror() {
    1.97 +      ok(false, "Couldn't access the mozContacts API");
    1.98 +      next();
    1.99 +    };
   1.100 +  },
   1.101 +
   1.102 +  function testRetrieveAllContacts() {
   1.103 +    /* if the migration does not work right, either we'll have an error, or the
   1.104 +       contacts won't be migrated properly and thus will fail WebIDL conversion,
   1.105 +       which will manifest as a timeout */
   1.106 +    info("Checking the contacts are corrected to obey WebIDL constraints.  (upgrades 14 to 17)");
   1.107 +    var req = mozContacts.find();
   1.108 +    req.onsuccess = function onsuccess() {
   1.109 +      if (this.result) {
   1.110 +        ise(this.result.length, contactsCount, "Contacts array length is correct");
   1.111 +        allContacts = this.result;
   1.112 +        next();
   1.113 +      } else {
   1.114 +        ok(false, "Could access the mozContacts API but got no contacts!");
   1.115 +        next();
   1.116 +      }
   1.117 +    };
   1.118 +
   1.119 +    req.onerror = function onerror() {
   1.120 +      ok(false, "Couldn't access the mozContacts API");
   1.121 +      next();
   1.122 +    };
   1.123 +  },
   1.124 +
   1.125 +  function checkNameIndex() {
   1.126 +    info("Checking name index migration (upgrades 17 to 19).");
   1.127 +    if (!allContacts) {
   1.128 +      next();
   1.129 +    }
   1.130 +
   1.131 +    var count = allContacts.length;
   1.132 +
   1.133 +    function finishRequest() {
   1.134 +      count--;
   1.135 +      if (!count) {
   1.136 +        next();
   1.137 +      }
   1.138 +    }
   1.139 +
   1.140 +    allContacts.forEach(function(contact) {
   1.141 +      var name = contact.name && contact.name[0];
   1.142 +      if (!name) {
   1.143 +        count--;
   1.144 +        return;
   1.145 +      }
   1.146 +
   1.147 +      var req = mozContacts.find({
   1.148 +        filterBy: ["name"],
   1.149 +        filterValue: name,
   1.150 +        filterOp: "equals"
   1.151 +      });
   1.152 +
   1.153 +      req.onsuccess = function onsuccess() {
   1.154 +        if (this.result) {
   1.155 +          info("Found contact '" + name + "', checking it's the correct one.");
   1.156 +          checkContacts(this.result[0], contact);
   1.157 +        } else {
   1.158 +          ok(false, "Could not find contact with name '" + name + "'");
   1.159 +        }
   1.160 +
   1.161 +        finishRequest();
   1.162 +      };
   1.163 +
   1.164 +      req.onerror = function onerror() {
   1.165 +        ok(false, "Error while finding contact with name '" + name + "'!");
   1.166 +        finishRequest();
   1.167 +      }
   1.168 +    });
   1.169 +
   1.170 +    if (!count) {
   1.171 +      ok(false, "No contact had a name, this is unexpected.");
   1.172 +      next();
   1.173 +    }
   1.174 +  },
   1.175 +
   1.176 +  function checkSubstringMatching() {
   1.177 +    var subject = "0004567890"; // the last 7 digits are the same that at least one contact
   1.178 +    info("Looking for a contact matching " + subject);
   1.179 +    var req = mozContacts.find({
   1.180 +      filterValue: subject,
   1.181 +      filterOp: "match",
   1.182 +      filterBy: ["tel"],
   1.183 +      filterLimit: 1
   1.184 +    });
   1.185 +
   1.186 +    req.onsuccess = function onsuccess() {
   1.187 +      if (this.result && this.result[0]) {
   1.188 +        ok(true, "Found a contact with number " + this.result[0].tel[0].value);
   1.189 +      }
   1.190 +      next();
   1.191 +    };
   1.192 +
   1.193 +    req.onerror = function onerror() {
   1.194 +      ok(false, "Error while finding contact for substring matching check!");
   1.195 +      next();
   1.196 +    };
   1.197 +  },
   1.198 +
   1.199 +  deleteDB,
   1.200 +  setSubstringMatching.bind(null, null),
   1.201 +
   1.202 +  function finish() {
   1.203 +    backend.destroy();
   1.204 +    info("all done!\n");
   1.205 +    SimpleTest.finish();
   1.206 +  }
   1.207 +];
   1.208 +
   1.209 +start_tests();
   1.210 +</script>
   1.211 +</pre>
   1.212 +</body>
   1.213 +</html>

mercurial