-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ContactStoreTestBase.kt
128 lines (110 loc) · 4.29 KB
/
ContactStoreTestBase.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.alexstyl.contactstore
import android.app.Application
import android.provider.ContactsContract
import android.util.Log
import androidx.test.core.app.ApplicationProvider
import androidx.test.rule.GrantPermissionRule
import kotlinx.coroutines.flow.first
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Before
import org.junit.Rule
abstract class ContactStoreTestBase {
@get:Rule
var grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(
android.Manifest.permission.READ_CONTACTS,
android.Manifest.permission.WRITE_CONTACTS
)
suspend fun assertContactUpdated(editedContact: MutableContact) {
store.execute { update(editedContact) }
val actual = store.fetchContacts(
predicate = ContactPredicate.ContactLookup(inContactIds = listOf(editedContact.contactId)),
columnsToFetch = editedContact.columns
).first().first()
assertThat(actual, equalTo(editedContact))
}
suspend fun assertContactUpdatedNoId(expected: MutableContact) {
store.execute { update(expected) }
val actual = store.fetchContacts(
predicate = ContactPredicate.ContactLookup(inContactIds = listOf(expected.contactId)),
columnsToFetch = expected.columns
).first().first()
assertThat(actual, equalContents(expected))
}
protected lateinit var store: ContactStore
@Before
open fun before() {
deleteAllContacts()
store = ContactStore.newInstance(context)
}
private fun deleteAllContacts() {
Log.w(javaClass::class.simpleName, "CLEANING UP CONTACTS")
val contentResolver = context.contentResolver
val result = contentResolver.delete(ContactsContract.RawContacts.CONTENT_URI, null, null)
Log.w(javaClass::class.simpleName, "Deleted $result contacts")
}
protected open val context: Application = ApplicationProvider.getApplicationContext()
protected fun contact(
displayName: String? = null,
firstName: String? = null,
lastName: String? = null,
organization: String? = null,
jobTitle: String? = null,
columns: List<ContactColumn> = emptyList(),
phones: List<LabeledValue<PhoneNumber>> = emptyList(),
mails: List<LabeledValue<MailAddress>> = emptyList(),
postalAddresses: List<LabeledValue<PostalAddress>> = emptyList(),
events: List<LabeledValue<EventDate>> = emptyList(),
webAddresses: List<LabeledValue<WebAddress>> = emptyList(),
imAddresses: List<LabeledValue<ImAddress>> = emptyList(),
sipAddresses: List<LabeledValue<SipAddress>> = emptyList(),
relations : List<LabeledValue<Relation>> = emptyList(),
note: Note? = null,
prefix: String? = null,
middleName: String? = null,
suffix: String? = null
): PartialContact {
return PartialContact(
contactId = IGNORED,
lookupKey = null,
displayName = displayName,
prefix = prefix,
middleName = middleName,
suffix = suffix,
organization = organization,
jobTitle = jobTitle,
firstName = firstName,
lastName = lastName,
events = events,
postalAddresses = postalAddresses,
sipAddresses = sipAddresses,
note = note,
columns = columns,
webAddresses = webAddresses,
imAddresses = imAddresses,
phones = phones,
mails = mails,
isStarred = false,
relations = relations
)
}
suspend fun buildStoreContact(
vararg withColumns: ContactColumn,
contactBuilder: MutableContact.() -> Unit
): Contact {
store.execute {
insert(
MutableContact().apply(contactBuilder)
)
}
val contactsBefore = store.fetchContacts(columnsToFetch = withColumns.toList()).first()
return contactsBefore.first()
}
protected fun assertOnlyContact(actual: List<Contact>, expected: Contact) {
assertThat(actual.size, equalTo(1))
assertThat(actual.first(), equalContents(expected))
}
private companion object {
const val IGNORED: Long = -1
}
}