1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.contacts.tests; 17 18 import static org.hamcrest.Matchers.allOf; 19 20 import android.database.Cursor; 21 import android.provider.ContactsContract; 22 23 import com.android.contacts.model.SimContact; 24 25 import org.hamcrest.BaseMatcher; 26 import org.hamcrest.Description; 27 import org.hamcrest.Matcher; 28 29 30 /** 31 * Has useful {@link org.hamcrest.Matchers}s for the Contacts app 32 */ 33 public class ContactsMatchers { 34 ContactsMatchers()35 private ContactsMatchers() { 36 } 37 38 /** 39 * Matchers for {@link Cursor}s returned by queries to 40 * {@link android.provider.ContactsContract.Data#CONTENT_URI} 41 */ 42 public static class DataCursor { 43 hasMimeType(String type)44 public static Matcher<Cursor> hasMimeType(String type) { 45 return hasValueForColumn(ContactsContract.Data.MIMETYPE, type); 46 } 47 hasName(final String name)48 public static Matcher<Cursor> hasName(final String name) { 49 return hasRowMatching(allOf( 50 hasMimeType(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE), 51 hasValueForColumn( 52 ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name))); 53 } 54 hasPhone(final String phone)55 public static Matcher<Cursor> hasPhone(final String phone) { 56 return hasRowMatching(allOf( 57 hasMimeType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE), 58 hasValueForColumn( 59 ContactsContract.CommonDataKinds.Phone.NUMBER, phone))); 60 } 61 hasEmail(final String email)62 public static Matcher<Cursor> hasEmail(final String email) { 63 return hasRowMatching(allOf( 64 hasMimeType(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE), 65 hasValueForColumn( 66 ContactsContract.CommonDataKinds.Email.ADDRESS, email))); 67 } 68 } 69 hasCount(final int count)70 public static Matcher<Cursor> hasCount(final int count) { 71 return new BaseMatcher<Cursor>() { 72 @Override 73 public boolean matches(Object o) { 74 if (!(o instanceof Cursor)) return false; 75 return ((Cursor)o).getCount() == count; 76 } 77 78 @Override 79 public void describeTo(Description description) { 80 description.appendText("Cursor with " + count + " rows"); 81 } 82 }; 83 } 84 85 public static Matcher<Cursor> hasValueForColumn(final String column, final String value) { 86 return new BaseMatcher<Cursor>() { 87 88 @Override 89 public boolean matches(Object o) { 90 if (!(o instanceof Cursor)) return false; 91 final Cursor cursor = (Cursor)o; 92 93 final int index = cursor.getColumnIndexOrThrow(column); 94 return value.equals(cursor.getString(index)); 95 } 96 97 @Override 98 public void describeTo(Description description) { 99 description.appendText("Cursor with " + column + "=" + value); 100 } 101 }; 102 } 103 104 public static Matcher<Cursor> hasRowMatching(final Matcher<Cursor> rowMatcher) { 105 return new BaseMatcher<Cursor>() { 106 @Override 107 public boolean matches(Object o) { 108 if (!(o instanceof Cursor)) return false; 109 final Cursor cursor = (Cursor)o; 110 111 cursor.moveToPosition(-1); 112 while (cursor.moveToNext()) { 113 if (rowMatcher.matches(cursor)) return true; 114 } 115 116 return false; 117 } 118 119 @Override 120 public void describeTo(Description description) { 121 description.appendText("Cursor with row matching "); 122 rowMatcher.describeTo(description); 123 } 124 }; 125 } 126 127 public static Matcher<SimContact> isSimContactWithNameAndPhone(final String name, 128 final String phone) { 129 return new BaseMatcher<SimContact>() { 130 @Override 131 public boolean matches(Object o) { 132 if (!(o instanceof SimContact)) return false; 133 134 SimContact other = (SimContact) o; 135 136 return name.equals(other.getName()) 137 && phone.equals(other.getPhone()); 138 } 139 140 @Override 141 public void describeTo(Description description) { 142 description.appendText("SimContact with name=" + name + " and phone=" + 143 phone); 144 } 145 }; 146 } 147 } 148