Github user alsorokin commented on a diff in the pull request:

    
https://github.com/apache/cordova-plugin-contacts/pull/101#discussion_r55803966
  
    --- Diff: appium-tests/helpers/contactsHelper.js ---
    @@ -0,0 +1,222 @@
    +/* jshint node: true */
    +/*
    + *
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + *
    +*/
    +
    +'use strict';
    +
    +function prepare(item) {
    +    if (typeof item === 'object') {
    +        return JSON.stringify(item);
    +    }
    +    if (typeof item === 'string') {
    +        return '"' + item + '"';
    +    }
    +    return undefined;
    +}
    +
    +module.exports.getContactName = function (firstName, lastName) {
    +    return {
    +        formatted: firstName == lastName === undefined ? null : firstName 
+ ' ' + lastName,
    +        familyName: lastName,
    +        givenName: firstName,
    +        middleName: ''
    +    };
    +};
    +
    +module.exports.getAddContactCode = function (displayName, contactName, 
phoneNumber) {
    +    var preparedDisplayName = prepare(displayName);
    +    console.log('preparedDisplayName is ' + preparedDisplayName);
    +    var preparedContactName = prepare(contactName);
    +    var preparedPhoneNumber = prepare(phoneNumber);
    +
    +    var result =
    +        'try {\n' +
    +        '    var results = document.getElementById("info");\n' +
    +        '    var contact = navigator.contacts.create({ "displayName": ' + 
preparedDisplayName + ', "name": ' + preparedContactName + ', "note": 
"DeleteMe" });\n' +
    +
    +        '    var phoneNumbers = [1];\n' +
    +        '    phoneNumbers[0] = new ContactField("work", ' + 
preparedPhoneNumber + ', true);\n' +
    +        '    contact.phoneNumbers = phoneNumbers;\n' +
    +
    +        '    contact.save(function() {\n' +
    +        '        results.innerHTML = "' + (displayName || 'Nameless 
contact') + ' saved.";\n' +
    +        '    }, function(e) {\n' +
    +        '        if (e.code === ContactError.NOT_SUPPORTED_ERROR) {\n' +
    +        '            results.innerHTML = "Saving contacts not 
supported.";\n' +
    +        '        } else {\n' +
    +        '            results.innerHTML = "Contact save failed: error " + 
e.code;\n' +
    +        '        }\n' +
    +        '    });\n' +
    +        '} catch (e) {\n' +
    +        '    var results = document.getElementById("info");\n' +
    +        '    results.innerHTML = "ERROR: " + e.code;\n' +
    +        '}\n';
    +
    +    return result;
    +};
    +
    +module.exports.getGetContactsCode = function (filter) {
    +    var preparedFilter = prepare(filter);
    +
    +    var result =
    +        'var results = document.getElementById("info");\n' +
    +        'var obj = new ContactFindOptions();\n' +
    +        'if (' + preparedFilter + ') {\n' +
    +        '    obj.filter = ' + preparedFilter + ';\n' +
    +        '}\n' +
    +        'obj.multiple = true;\n' +
    +        'navigator.contacts.find(["displayName", "name", "phoneNumbers", 
"emails", "urls", "note"], function(contacts) {\n' +
    +        '    var s = "";\n' +
    +        '    if (contacts.length === 0) {\n' +
    +        '        s = "No contacts found";\n' +
    +        '    } else {\n' +
    +        '        s = "Number of contacts: " + contacts.length + 
"<br><table width=100%><tr><th>Name</th><td>Phone</td><td>Email</td></tr>";\n' +
    +        '        for (var i = 0; i < contacts.length; i++) {\n' +
    +        '            var contact = contacts[i];\n' +
    +        '            var contactNameTag = contact.name ? "<tr><td>" + 
contact.name.formatted + "</td><td>" : "<tr><td>(No Name)</td><td>";\n' +
    +        '            s = s + contactNameTag;\n' +
    +        '            if (contact.phoneNumbers && 
contact.phoneNumbers.length > 0) {\n' +
    +        '                s = s + contact.phoneNumbers[0].value;\n' +
    +        '            }\n' +
    +        '            s = s + "</td><td>";\n' +
    +        '            if (contact.emails && contact.emails.length > 0) {\n' 
+
    +        '                s = s + contact.emails[0].value;\n' +
    +        '            }\n' +
    +        '            s = s + "</td></tr>";\n' +
    +        '    }\n' +
    +        '        s = s + "</table>";\n' +
    +        '    }\n' +
    +        '    results.innerHTML = s;\n' +
    +        '}, function(e) {\n' +
    +        '   results.innerHTML = "Search failed: error " + e.code;\n' +
    +        '}, obj);';
    +
    +    return result;
    +};
    +
    +module.exports.getPickContactCode = function () {
    +    var result =
    +        'var results = document.getElementById("info");\n' +
    +        'navigator.contacts.pickContact(\n' +
    +        '    function (contact) {\n' +
    +        '        results.innerHTML = contact ?\n' +
    +        '            "Picked contact: <pre>" + JSON.stringify(contact, 
null, 4) + "</pre>" :\n' +
    +        '            "No contacts found";\n' +
    +        '    },\n' +
    +        '    function (e) {\n' +
    +        '        results.innerHTML = e && e.code === 
ContactError.OPERATION_CANCELLED_ERROR ?\n' +
    +        '                "Pick cancelled" :\n' +
    +        '                "Pick failed: " + (e && e.code);\n' +
    +        '    }\n' +
    +        ');';
    +
    +    return result;
    +};
    +
    +module.exports.getRenameContactCode = function (oldDisplayName, 
newDisplayName, newName) {
    +    // these default values are copied from manual contacts tests
    +    if (!oldDisplayName) {
    +        oldDisplayName = 'Dooney Evans';
    +    }
    +    if (!newDisplayName) {
    +        newDisplayName = 'Urist McContact';
    +    }
    +    if (!newName) {
    +        newName = module.exports.getContactName('Urist', 'McContact');
    +    }
    +    var preparedOldDisplayName = prepare(oldDisplayName);
    +    var preparedNewDisplayName = prepare(newDisplayName);
    +    var preparedNewName = prepare(newName);
    +
    +    var result =
    +        'var results = document.getElementById("info");\n' +
    +        'var obj = new ContactFindOptions();\n' +
    +        'obj.filter = ' + preparedOldDisplayName + ';\n' +
    +        'obj.multiple = false;\n' +
    +
    +        'navigator.contacts.find(["displayName", "name"], 
function(contacts) {\n' +
    +        '    if (contacts.length === 0) {\n' +
    +        '        results.innerHTML = "No contacts to update.";\n' +
    +        '        return;\n' +
    +        '    }\n' +
    +        '    var contact = contacts[0];\n' +
    +        '    contact.displayName = ' + preparedNewDisplayName + ';\n' +
    +        '    contact.name = ' + preparedNewName + ';\n' +
    +        '    contact.save(function(updated) {\n' +
    +        '        results.innerHTML = "Contact updated.";\n' +
    +        '    },function(e) {\n' +
    +        '        results.innerHTML = "Update failed: error " + e.code;\n' +
    +        '    });\n' +
    +        '}, function(e) {\n' +
    +        '    results.innerHTML = "Search failed: error " + e.code;\n' +
    +        '}, obj);';
    +
    +    return result;
    +};
    +
    +module.exports.getRemoveTestContactsCode = function () {
    +    var result =
    +        'var results = document.getElementById("info");\n' +
    +        'results.innerHTML = "";\n' +
    +        'var obj = new ContactFindOptions();\n' +
    +        'obj.filter = "DeleteMe";\n' +
    +        'obj.multiple = true;\n' +
    +        'navigator.contacts.find(["note"], function(contacts) {\n' +
    +        '    var removes = [];\n' +
    +        '    contacts.forEach(function(contact) {\n' +
    +        '        removes.push(contact);\n' +
    +        '    });\n' +
    +        '    if (removes.length === 0) {\n' +
    +        '        results.innerHTML = "No contacts to remove";\n' +
    +        '        return;\n' +
    +        '    }\n' +
    +
    +        '   var nextToRemove;\n' +
    +        '   if (removes.length > 0) {\n' +
    +        '        nextToRemove = removes.shift();\n' +
    +        '    }\n' +
    +
    +        '    function removeNext(item) {\n' +
    +        '        if (typeof item === "undefined") {\n' +
    +        '            return;\n' +
    +        '        }\n' +
    +
    +        '        if (removes.length > 0) {\n' +
    +        '            nextToRemove = removes.shift();\n' +
    +        '        } else {\n' +
    +        '            nextToRemove = undefined;\n' +
    +        '        }\n' +
    +
    +        '        item.remove(function removeSucceeded() {\n' +
    +        '            results.innerHTML += "Removed a contact with ID " + 
item.id + "<br/>";\n' +
    +        '            removeNext(nextToRemove);\n' +
    +        '        }, function removeFailed() {\n' +
    +        '            results.innerHTML += "Failed to remove a contact with 
ID " + item.id + "<br/>";\n' +
    +        '            removeNext(nextToRemove);\n' +
    +        '        });\n' +
    +        '    }\n' +
    +        '    removeNext(nextToRemove);\n' +
    +        '}, function(e) {\n' +
    +        '   results.innerHTML = "Search failed: error " + e.code;\n' +
    +        '}, obj);';
    +
    +    return result;
    +};
    --- End diff --
    
    This is the code to run on the client side. We inject it by calling 
`driver.execute()` from the specs.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org

Reply via email to