Bug 859659 - Part 3: xpcshell-tests. r=vicamo
authorYoshi Huang <allstars.chh@mozilla.com>
Tue, 07 May 2013 11:29:40 +0800
changeset 131677 7ec0e93663cddd3816524c53b7e46a04aec0b159
parent 131676 03b8e37c5c6aeb6fe8f73a428e18245cfb83538d
child 131678 b40c41c73962a3f3e1fd07a541d90a117dbe8216
push idunknown
push userunknown
push dateunknown
reviewersvicamo
bugs859659
milestone23.0a1
Bug 859659 - Part 3: xpcshell-tests. r=vicamo
dom/system/gonk/tests/test_ril_worker_icc.js
--- a/dom/system/gonk/tests/test_ril_worker_icc.js
+++ b/dom/system/gonk/tests/test_ril_worker_icc.js
@@ -1817,19 +1817,110 @@ add_test(function test_read_email() {
   doTestReadEmail(ICC_USIM_TYPE1_TAG, "email@mozilla.com$#");
   doTestReadEmail(ICC_USIM_TYPE2_TAG, "email@mozilla.com");
   do_check_eq(record._emailRecordSize, recordSize);
 
   run_next_test();
 });
 
 /**
+ * Verify ICCRecordHelper.updateEmail
+ */
+add_test(function test_update_email() {
+  const recordSize = 0x20;
+  const recordNumber = 1;
+  const fileId = 0x4f50;
+  const NUM_TESTS = 2;
+  let worker = newUint8Worker();
+  let pduHelper = worker.GsmPDUHelper;
+  let ril = worker.RIL;
+  ril.appType = CARD_APPTYPE_USIM;
+  let recordHelper = worker.ICCRecordHelper;
+  let buf = worker.Buf;
+  let ioHelper = worker.ICCIOHelper;
+  let pbr = {email: {fileId: fileId, fileType: ICC_USIM_TYPE1_TAG},
+             adn: {sfi: 1}};
+  let count = 0;
+
+  // Override.
+  ioHelper.updateLinearFixedEF = function (options) {
+    options.pathId = worker.ICCFileHelper.getEFPath(options.fileId);
+    options.command = ICC_COMMAND_UPDATE_RECORD;
+    options.p1 = options.recordNumber;
+    options.p2 = READ_RECORD_ABSOLUTE_MODE;
+    options.p3 = recordSize;
+    ril.iccIO(options);
+  };
+
+  function do_test(pbr, expectedEmail, expectedAdnRecordId) {
+    buf.sendParcel = function () {
+      count++;
+
+      // Request Type.
+      do_check_eq(this.readUint32(), REQUEST_SIM_IO);
+
+      // Token : we don't care
+      this.readUint32();
+
+      // command.
+      do_check_eq(this.readUint32(), ICC_COMMAND_UPDATE_RECORD);
+
+      // fileId.
+      do_check_eq(this.readUint32(), fileId);
+
+      // pathId.
+      do_check_eq(this.readString(),
+                  EF_PATH_MF_SIM + EF_PATH_DF_TELECOM + EF_PATH_DF_PHONEBOOK);
+
+      // p1.
+      do_check_eq(this.readUint32(), recordNumber);
+
+      // p2.
+      do_check_eq(this.readUint32(), READ_RECORD_ABSOLUTE_MODE);
+
+      // p3.
+      do_check_eq(this.readUint32(), recordSize);
+
+      // data.
+      let strLen = this.readUint32();
+      let email;
+      if (pbr.email.fileType === ICC_USIM_TYPE1_TAG) {
+        email = pduHelper.read8BitUnpackedToString(recordSize);
+      } else {
+        email = pduHelper.read8BitUnpackedToString(recordSize - 2);
+        do_check_eq(pduHelper.readHexOctet(), pbr.adn.sfi);
+        do_check_eq(pduHelper.readHexOctet(), expectedAdnRecordId);
+      }
+      this.readStringDelimiter(strLen);
+      do_check_eq(email, expectedEmail);
+
+      // pin2.
+      do_check_eq(this.readString(), null);
+
+      if (!worker.RILQUIRKS_V5_LEGACY) {
+        // AID. Ignore because it's from modem.
+        this.readUint32();
+      }
+
+      if (count == NUM_TESTS) {
+        run_next_test();
+      }
+    };
+    recordHelper.updateEmail(pbr, recordNumber, expectedEmail, expectedAdnRecordId);
+  }
+
+  do_test(pbr, "test@mail.com");
+  pbr.email.fileType = ICC_USIM_TYPE2_TAG;
+  do_test(pbr, "test@mail.com", 1);
+});
+
+/**
  * Verify ICCRecordHelper.readANR
  */
-add_test(function test_read_email() {
+add_test(function test_read_anr() {
   let worker = newUint8Worker();
   let helper = worker.GsmPDUHelper;
   let record = worker.ICCRecordHelper;
   let buf    = worker.Buf;
   let io     = worker.ICCIOHelper;
   let recordSize;
 
   io.loadLinearFixedEF = function fakeLoadLinearFixedEF(options)  {
@@ -1867,16 +1958,240 @@ add_test(function test_read_email() {
 
   doTestReadAnr(ICC_USIM_TYPE1_TAG, "0123456");
   do_check_eq(record._anrRecordSize, recordSize);
 
   run_next_test();
 });
 
 /**
+ * Verify ICCRecordHelper.updateANR
+ */
+add_test(function test_update_anr() {
+  const recordSize = 0x20;
+  const recordNumber = 1;
+  const fileId = 0x4f11;
+  const NUM_TESTS = 2;
+  let worker = newUint8Worker();
+  let pduHelper = worker.GsmPDUHelper;
+  let ril = worker.RIL;
+  ril.appType = CARD_APPTYPE_USIM;
+  let recordHelper = worker.ICCRecordHelper;
+  let buf = worker.Buf;
+  let ioHelper = worker.ICCIOHelper;
+  let pbr = {anr0: {fileId: fileId, fileType: ICC_USIM_TYPE1_TAG},
+             adn: {sfi: 1}};
+  let count = 0;
+
+  // Override.
+  ioHelper.updateLinearFixedEF = function (options) {
+    options.pathId = worker.ICCFileHelper.getEFPath(options.fileId);
+    options.command = ICC_COMMAND_UPDATE_RECORD;
+    options.p1 = options.recordNumber;
+    options.p2 = READ_RECORD_ABSOLUTE_MODE;
+    options.p3 = recordSize;
+    ril.iccIO(options);
+  };
+
+  function do_test(pbr, expectedANR, expectedAdnRecordId) {
+    buf.sendParcel = function () {
+      count++;
+
+      // Request Type.
+      do_check_eq(this.readUint32(), REQUEST_SIM_IO);
+
+      // Token : we don't care
+      this.readUint32();
+
+      // command.
+      do_check_eq(this.readUint32(), ICC_COMMAND_UPDATE_RECORD);
+
+      // fileId.
+      do_check_eq(this.readUint32(), fileId);
+
+      // pathId.
+      do_check_eq(this.readString(),
+                  EF_PATH_MF_SIM + EF_PATH_DF_TELECOM + EF_PATH_DF_PHONEBOOK);
+
+      // p1.
+      do_check_eq(this.readUint32(), recordNumber);
+
+      // p2.
+      do_check_eq(this.readUint32(), READ_RECORD_ABSOLUTE_MODE);
+
+      // p3.
+      do_check_eq(this.readUint32(), recordSize);
+
+      // data.
+      let strLen = this.readUint32();
+      // EF_AAS, ignore.
+      pduHelper.readHexOctet();
+      do_check_eq(pduHelper.readNumberWithLength(), expectedANR);
+      // EF_CCP, ignore.
+      pduHelper.readHexOctet();
+      // EF_EXT1, ignore.
+      pduHelper.readHexOctet();
+      if (pbr.anr0.fileType === ICC_USIM_TYPE2_TAG) {
+        do_check_eq(pduHelper.readHexOctet(), pbr.adn.sfi);
+        do_check_eq(pduHelper.readHexOctet(), expectedAdnRecordId);
+      }
+      this.readStringDelimiter(strLen);
+
+      // pin2.
+      do_check_eq(this.readString(), null);
+
+      if (!worker.RILQUIRKS_V5_LEGACY) {
+        // AID. Ignore because it's from modem.
+        this.readUint32();
+      }
+
+      if (count == NUM_TESTS) {
+        run_next_test();
+      }
+    };
+    recordHelper.updateANR(pbr, recordNumber, expectedANR, expectedAdnRecordId);
+  }
+
+  do_test(pbr, "+123456789");
+  pbr.anr0.fileType = ICC_USIM_TYPE2_TAG;
+  do_test(pbr, "123456789", 1);
+});
+
+/**
+ * Verify ICCRecordHelper.readIAP
+ */
+add_test(function test_read_iap() {
+  let worker = newUint8Worker();
+  let helper = worker.GsmPDUHelper;
+  let record = worker.ICCRecordHelper;
+  let buf    = worker.Buf;
+  let io     = worker.ICCIOHelper;
+  let recordSize;
+
+  io.loadLinearFixedEF = function fakeLoadLinearFixedEF(options)  {
+    let iap_1 = [0x01, 0x02];
+
+    // Write data size/
+    buf.writeUint32(iap_1.length * 2);
+
+    // Write iap.
+    for (let i = 0; i < iap_1.length; i++) {
+      helper.writeHexOctet(iap_1[i]);
+    }
+
+    // Write string delimiter.
+    buf.writeStringDelimiter(iap_1.length * 2);
+
+    recordSize = iap_1.length;
+    options.recordSize = recordSize;
+    if (options.callback) {
+      options.callback(options);
+    }
+  };
+
+  function doTestReadIAP(expectedIAP) {
+    const fileId = 0x4f17;
+    const recordNumber = 1;
+
+    let successCb = function successCb(iap) {
+      for (let i = 0; i < iap.length; i++) {
+        do_check_eq(expectedIAP[i], iap[i]);
+      }
+      run_next_test();
+    }.bind(this);
+
+    let errorCb = function errorCb(errorMsg) {
+      do_print(errorMsg);
+      do_check_true(false);
+      run_next_test();
+    }.bind(this);
+
+    record.readIAP(fileId, recordNumber, successCb, errorCb);
+  };
+
+  doTestReadIAP([1, 2]);
+});
+
+/**
+ * Verify ICCRecordHelper.updateIAP
+ */
+add_test(function test_update_iap() {
+  const recordSize = 2;
+  const recordNumber = 1;
+  const fileId = 0x4f17;
+  let worker = newUint8Worker();
+  let pduHelper = worker.GsmPDUHelper;
+  let ril = worker.RIL;
+  ril.appType = CARD_APPTYPE_USIM;
+  let recordHelper = worker.ICCRecordHelper;
+  let buf = worker.Buf;
+  let ioHelper = worker.ICCIOHelper;
+  let count = 0;
+
+  // Override.
+  ioHelper.updateLinearFixedEF = function (options) {
+    options.pathId = worker.ICCFileHelper.getEFPath(options.fileId);
+    options.command = ICC_COMMAND_UPDATE_RECORD;
+    options.p1 = options.recordNumber;
+    options.p2 = READ_RECORD_ABSOLUTE_MODE;
+    options.p3 = recordSize;
+    ril.iccIO(options);
+  };
+
+  function do_test(expectedIAP) {
+    buf.sendParcel = function () {
+      // Request Type.
+      do_check_eq(this.readUint32(), REQUEST_SIM_IO);
+
+      // Token : we don't care
+      this.readUint32();
+
+      // command.
+      do_check_eq(this.readUint32(), ICC_COMMAND_UPDATE_RECORD);
+
+      // fileId.
+      do_check_eq(this.readUint32(), fileId);
+
+      // pathId.
+      do_check_eq(this.readString(),
+                  EF_PATH_MF_SIM + EF_PATH_DF_TELECOM + EF_PATH_DF_PHONEBOOK);
+
+      // p1.
+      do_check_eq(this.readUint32(), recordNumber);
+
+      // p2.
+      do_check_eq(this.readUint32(), READ_RECORD_ABSOLUTE_MODE);
+
+      // p3.
+      do_check_eq(this.readUint32(), recordSize);
+
+      // data.
+      let strLen = this.readUint32();
+      for (let i = 0; i < recordSize; i++) {
+        do_check_eq(expectedIAP[i], pduHelper.readHexOctet());
+      }
+      this.readStringDelimiter(strLen);
+
+      // pin2.
+      do_check_eq(this.readString(), null);
+
+      if (!worker.RILQUIRKS_V5_LEGACY) {
+        // AID. Ignore because it's from modem.
+        this.readUint32();
+      }
+
+      run_next_test();
+    };
+    recordHelper.updateIAP(fileId, recordNumber, expectedIAP);
+  }
+
+  do_test([1, 2]);
+});
+
+/**
  * Verify ICCRecordHelper.updateADNLike.
  */
 add_test(function test_update_adn_like() {
   let worker = newUint8Worker();
   let ril = worker.RIL;
   let record = worker.ICCRecordHelper;
   let io = worker.ICCIOHelper;
   let pdu = worker.GsmPDUHelper;
@@ -1923,35 +2238,34 @@ add_test(function test_update_adn_like()
 
     // data.
     let contact = pdu.readAlphaIdDiallingNumber(0x20);
     do_check_eq(contact.alphaId, "test");
     do_check_eq(contact.number, "123456");
 
     // pin2.
     if (fileId == ICC_EF_ADN) {
-      do_check_eq(this.readString(), "");
+      do_check_eq(this.readString(), null);
     } else {
       do_check_eq(this.readString(), "1111");
     }
 
     if (!worker.RILQUIRKS_V5_LEGACY) {
       // AID. Ignore because it's from modem.
       this.readUint32();
     }
 
     if (fileId == ICC_EF_FDN) {
       run_next_test();
     }
   };
 
   fileId = ICC_EF_ADN;
   record.updateADNLike(fileId,
-                       {recordId: 1, alphaId: "test", number: "123456"},
-                       "");
+                       {recordId: 1, alphaId: "test", number: "123456"});
 
   fileId = ICC_EF_FDN;
   record.updateADNLike(fileId,
                        {recordId: 1, alphaId: "test", number: "123456"},
                        "1111");
 });
 
 /**
@@ -2086,32 +2400,105 @@ add_test(function test_update_icc_contac
 
 /**
  * Verify ICCContactHelper.updateICCContact with appType is CARD_APPTYPE_USIM.
  */
 add_test(function test_update_icc_contact() {
   let worker = newUint8Worker();
   let recordHelper = worker.ICCRecordHelper;
   let contactHelper = worker.ICCContactHelper;
-  const FILE_ID = 0x4f3a;
-
-  recordHelper.readPBR = function (onsuccess, onerror) {
-    onsuccess([{adn:{fileId: FILE_ID}}]);
-  };
-
-  let oldContact = {recordId: 1, alphaId: "test", number: "123456"};
-  recordHelper.updateADNLike = function (fileId, contact, pin2, onsuccess, onerror) {
-    do_check_eq(fileId, FILE_ID);
-    do_check_eq(contact.alphaId, oldContact.alphaId);
-    do_check_eq(contact.number, oldContact.number);
-
+  const NUM_TESTS = 2;
+  const ADN_RECORD_ID = 100;
+  const ADN_FILE_ID = 0x4f3a;
+  const ADN_SFI = 1;
+  const IAP_FILE_ID = 0x4f17;
+  const EMAIL_FILE_ID = 0x4f50;
+  const EMAIL_RECORD_ID = 20;
+  const ANR0_FILE_ID = 0x4f11;
+  const ANR0_RECORD_ID = 30;
+  let count = 0;
+  let oldContact = {recordId: ADN_RECORD_ID,
+                    alphaId: "test",
+                    number: "123456",
+                    email: "test@mail.com",
+                    anr: ["+654321"]};
+
+  function do_test(pbrs, onsuccess, onerror) {
+    recordHelper.readPBR = function (onsuccess, onerror) {
+      onsuccess(pbrs) ;
+    };
+
+    recordHelper.updateADNLike = function (fileId, contact, pin2, onsuccess, onerror) {
+      do_check_eq(fileId, ADN_FILE_ID);
+      do_check_eq(contact.alphaId, oldContact.alphaId);
+      do_check_eq(contact.number, oldContact.number);
+      onsuccess();
+    };
+
+    recordHelper.readIAP = function (fileId, recordNumber, onsuccess, onerror) {
+      do_check_eq(fileId, IAP_FILE_ID);
+      do_check_eq(recordNumber, ADN_RECORD_ID);
+      onsuccess([EMAIL_RECORD_ID, ANR0_RECORD_ID]);
+    };
+
+    recordHelper.updateEmail = function (pbr, recordNumber, email, adnRecordId, onsuccess, onerror) {
+      do_check_eq(pbr.email.fileId, EMAIL_FILE_ID);
+      if (pbr.email.fileType === ICC_USIM_TYPE1_TAG) {
+        do_check_eq(recordNumber, ADN_RECORD_ID);
+      } else if (pbr.email.fileType === ICC_USIM_TYPE2_TAG) {
+        do_check_eq(recordNumber, EMAIL_RECORD_ID);
+      }
+      do_check_eq(email, oldContact.email);
+      onsuccess();
+    };
+
+    recordHelper.updateANR = function (pbr, recordNumber, number, adnRecordId, onsuccess, onerror) {
+      do_check_eq(pbr.anr0.fileId, ANR0_FILE_ID);
+      if (pbr.anr0.fileType === ICC_USIM_TYPE1_TAG) {
+        do_check_eq(recordNumber, ADN_RECORD_ID);
+      } else if (pbr.anr0.fileType === ICC_USIM_TYPE2_TAG) {
+        do_check_eq(recordNumber, ANR0_RECORD_ID);
+      }
+      do_check_eq(number, oldContact.anr[0]);
+      onsuccess();
+    };
+
+    contactHelper.updateICCContact(CARD_APPTYPE_USIM, "adn", oldContact, null, onsuccess, onerror);
+  }
+
+  let successCb = function () {
+    count++;
+    if (count == NUM_TESTS) {
+      run_next_test();
+    }
+  }.bind(this);
+
+  let errorCb = function (errorMsg) {
+    do_print("updateICCContact failed " + errorMsg);
+    do_check_true(false);
     run_next_test();
-  };
-
-  contactHelper.updateICCContact(CARD_APPTYPE_USIM, "adn", oldContact);
+  }.bind(this);
+
+  do_test([{adn:{fileId: ADN_FILE_ID},
+            email: {fileId: EMAIL_FILE_ID, fileType: ICC_USIM_TYPE1_TAG},
+            anr0: {fileId: ANR0_FILE_ID, fileType: ICC_USIM_TYPE1_TAG}}],
+          successCb,
+          errorCb);
+
+  do_test([{adn:{fileId: ADN_FILE_ID, sfi: ADN_SFI},
+            iap: {fileId: IAP_FILE_ID},
+            email: {fileId: EMAIL_FILE_ID,
+                    fileType: ICC_USIM_TYPE2_TAG,
+                    indexInIAP: 0},
+            anr0: {fileId: ANR0_FILE_ID,
+                   fileType: ICC_USIM_TYPE2_TAG,
+                   indexInIAP: 1}}],
+          successCb,
+          errorCb);
+
 });
 
 /**
  * Verify ICCContactHelper.findFreeICCContact
  */
 add_test(function test_find_free_icc_contact() {
   let worker = newUint8Worker();
   let recordHelper = worker.ICCRecordHelper;