-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LocalAddressBook
: move contacts when renaming the address book acco…
…unt (#1084) * LocalAddressBook: move contacts when renaming the address book account * Don't make contacts dirty when moving * Move isDirty to tests because it's only required for tests * We don't have to set the user-data twice * Add test for groups
- Loading branch information
Showing
4 changed files
with
384 additions
and
135 deletions.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
app/src/androidTest/kotlin/at/bitfire/davdroid/resource/LocalAddressBookTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details. | ||
*/ | ||
|
||
package at.bitfire.davdroid.resource | ||
|
||
import android.Manifest | ||
import android.accounts.Account | ||
import android.content.ContentProviderClient | ||
import android.content.ContentUris | ||
import android.content.Context | ||
import android.provider.ContactsContract | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.rule.GrantPermissionRule | ||
import at.bitfire.vcard4android.Contact | ||
import at.bitfire.vcard4android.GroupMethod | ||
import at.bitfire.vcard4android.LabeledProperty | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import ezvcard.property.Telephone | ||
import org.junit.After | ||
import org.junit.AfterClass | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Before | ||
import org.junit.BeforeClass | ||
import org.junit.ClassRule | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.util.LinkedList | ||
import javax.inject.Inject | ||
|
||
@HiltAndroidTest | ||
class LocalAddressBookTest { | ||
|
||
@get:Rule | ||
val hiltRule = HiltAndroidRule(this) | ||
|
||
@Inject | ||
lateinit var addressbookFactory: LocalTestAddressBook.Factory | ||
|
||
@Inject | ||
@ApplicationContext | ||
lateinit var context: Context | ||
|
||
lateinit var addressBook: LocalTestAddressBook | ||
|
||
|
||
@Before | ||
fun setUp() { | ||
hiltRule.inject() | ||
|
||
addressBook = addressbookFactory.create(provider, GroupMethod.CATEGORIES) | ||
LocalTestAddressBook.createAccount(context) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
// remove address book | ||
addressBook.deleteCollection() | ||
} | ||
|
||
|
||
/** | ||
* Tests whether contacts are moved (and not lost) when an address book is renamed. | ||
*/ | ||
@Test | ||
fun test_renameAccount_retainsContacts() { | ||
// insert contact with data row | ||
val uid = "12345" | ||
val contact = Contact( | ||
uid = uid, | ||
displayName = "Test Contact", | ||
phoneNumbers = LinkedList(listOf(LabeledProperty(Telephone("1234567890")))) | ||
) | ||
val uri = LocalContact(addressBook, contact, null, null, 0).add() | ||
val id = ContentUris.parseId(uri) | ||
val localContact = addressBook.findContactById(id) | ||
localContact.resetDirty() | ||
assertFalse("Contact is dirty before moving", addressBook.isContactDirty(id)) | ||
|
||
// rename address book | ||
val newName = "New Name" | ||
addressBook.renameAccount(newName) | ||
assertEquals(Account(newName, LocalTestAddressBook.ACCOUNT.type), addressBook.account) | ||
|
||
// check whether contact is still here (including data rows) and not dirty | ||
val result = addressBook.findContactById(id) | ||
assertFalse("Contact is dirty after moving", addressBook.isContactDirty(id)) | ||
|
||
val contact2 = result.getContact() | ||
assertEquals(uid, contact2.uid) | ||
assertEquals("Test Contact", contact2.displayName) | ||
assertEquals("1234567890", contact2.phoneNumbers.first().component1().text) | ||
} | ||
|
||
/** | ||
* Tests whether groups are moved (and not lost) when an address book is renamed. | ||
*/ | ||
@Test | ||
fun test_renameAccount_retainsGroups() { | ||
// insert group | ||
val localGroup = LocalGroup(addressBook, Contact(displayName = "Test Group"), null, null, 0) | ||
val uri = localGroup.add() | ||
val id = ContentUris.parseId(uri) | ||
|
||
// make sure it's not dirty | ||
localGroup.clearDirty(null, null, null) | ||
assertFalse("Group is dirty before moving", addressBook.isGroupDirty(id)) | ||
|
||
// rename address book | ||
val newName = "New Name" | ||
addressBook.renameAccount(newName) | ||
assertEquals(Account(newName, LocalTestAddressBook.ACCOUNT.type), addressBook.account) | ||
|
||
// check whether group is still here and not dirty | ||
val result = addressBook.findGroupById(id) | ||
assertFalse("Group is dirty after moving", addressBook.isGroupDirty(id)) | ||
|
||
val group = result.getContact() | ||
assertEquals("Test Group", group.displayName) | ||
} | ||
|
||
|
||
|
||
companion object { | ||
|
||
@JvmField | ||
@ClassRule | ||
val permissionRule = GrantPermissionRule.grant(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)!! | ||
|
||
private lateinit var provider: ContentProviderClient | ||
|
||
@BeforeClass | ||
@JvmStatic | ||
fun connect() { | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
provider = context.contentResolver.acquireContentProviderClient(ContactsContract.AUTHORITY)!! | ||
assertNotNull(provider) | ||
} | ||
|
||
@AfterClass | ||
@JvmStatic | ||
fun disconnect() { | ||
provider.close() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.