dimanche 26 juin 2016

Android Intent builder method Unit Test failing

My error is quite strange (for me at least, it might be because I'm new to testing in Android), I have a class which has a method called "intentAddBuilder", it receives a User, an Intent Action, and a Drawable and it builds an Intent with some extras and returns it. The thing is, when I run my app, it works perfect (I even have a method that logs the info after the Intent is returned), but when I rum the test doing exactly the same thing the log method does, it fails because the intent is being returned as null.

Here's my method:

public static Intent intentAddBuilder(final User user, String action, Drawable picture){
    Intent intent = new Intent(action);

    final ArrayList<ContentValues> data = new ArrayList<>();

    String fullName = user.getFirstName() + " " + user.getLastName();
    intent.putExtra(ContactsContract.Intents.Insert.NAME, fullName.trim());

    ContentValues nickname = new ContentValues();
    nickname.put(ContactsContract.CommonDataKinds.Nickname.MIMETYPE, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE);
    nickname.put(ContactsContract.CommonDataKinds.Nickname.NAME, user.getNickname());
    data.add(nickname);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Bitmap bitmap = ((BitmapDrawable) picture).getBitmap();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    ContentValues photo = new ContentValues();
    photo.put(ContactsContract.CommonDataKinds.Photo.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
    photo.put(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArray);
    data.add(photo);

    for (Email email : user.getEmails()) {
        ContentValues emailValue = new ContentValues();
        emailValue.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
        emailValue.put(ContactsContract.CommonDataKinds.Email.ADDRESS, email.getEmail());
        data.add(emailValue);
    }

    String website = user.getWebpage();
    if (website != null && !website.equals("")){
        ContentValues websiteValue = new ContentValues();
        websiteValue.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
        websiteValue.put(ContactsContract.CommonDataKinds.Website.URL, website);
        data.add(websiteValue);
    }

    intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);

    String company = user.getCompany();
    if(company != null && !company.equals("")){
        intent.putExtra(ContactsContract.Intents.Insert.COMPANY, company);
    }

    String jobTitle = user.getJobPosition();
    if(jobTitle != null && !jobTitle.equals("")){
        intent.putExtra(ContactsContract.Intents.Insert.JOB_TITLE, jobTitle);
    }

    return intent;
}

And my test method for this is:

@Test
public void contactIntentBuilderTest() {
    User user = TestUtils.getUserToTest();
    Intent intent = User.intentAddBuilder(user,Intent.ACTION_INSERT,drawable);

    assertEquals((user.getFirstName()+" "+user.getLastName()).trim(),intent.getStringExtra(ContactsContract.Intents.Insert.NAME));

    if (user.getCompany() != null && !user.getCompany().equals("")){
        assertEquals(user.getCompany(),intent.getStringExtra(ContactsContract.Intents.Insert.COMPANY));
    }
    if (user.getJobPosition() != null && !user.getJobPosition().equals("")){
        assertEquals(user.getJobPosition(),intent.getStringExtra(ContactsContract.Intents.Insert.JOB_TITLE));
    }

    ArrayList<ContentValues> data = intent.getParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA);
    int position = 0;
    assertEquals(user.getNickname(),data.get(position).get(ContactsContract.CommonDataKinds.Nickname.NAME));
    position++;

    byte[] b =(byte[]) data.get(position).get(ContactsContract.CommonDataKinds.Photo.PHOTO);
    ByteArrayInputStream is = new ByteArrayInputStream(b);
    Drawable drw = Drawable.createFromStream(is,null);
    assertEquals(drawable,drw);
    position++;

    int positionBeforeEmails = position;
    for (int j=position; j < positionBeforeEmails + user.getEmails().size(); j++){
        Email email = user.getEmails().get(position-user.getEmails().size());
        assertEquals(email.getEmail(),data.get(j).get(ContactsContract.CommonDataKinds.Email.ADDRESS));
        position++;
    }

    if (user.getWebpage() != null && !user.getWebpage().equals("")){
        assertEquals(user.getWebpage(),data.get(position).get(ContactsContract.CommonDataKinds.Website.URL));
    }

}

I might be doing something wrong since I'm new to this, but please can anyone give me an answer?

Thaaanks!

Aucun commentaire:

Enregistrer un commentaire