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