Skip to content

Commit 155618b

Browse files
committed
Add migration test
1 parent aadcc01 commit 155618b

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
3+
*/
4+
5+
package at.bitfire.davdroid.settings.migration
6+
7+
import android.accounts.Account
8+
import android.content.ContentResolver
9+
import android.content.Context
10+
import android.content.SyncRequest
11+
import android.provider.CalendarContract
12+
import android.provider.ContactsContract
13+
import at.bitfire.davdroid.sync.account.TestAccount
14+
import dagger.hilt.android.qualifiers.ApplicationContext
15+
import dagger.hilt.android.testing.HiltAndroidRule
16+
import dagger.hilt.android.testing.HiltAndroidTest
17+
import org.junit.AfterClass
18+
import org.junit.Assert.assertFalse
19+
import org.junit.Assert.assertTrue
20+
import org.junit.Before
21+
import org.junit.BeforeClass
22+
import org.junit.Rule
23+
import org.junit.Test
24+
import java.util.logging.Logger
25+
import javax.inject.Inject
26+
27+
@HiltAndroidTest
28+
class AccountSettingsMigration21Test {
29+
30+
@get:Rule
31+
val hiltRule = HiltAndroidRule(this)
32+
33+
@Inject
34+
lateinit var migration: AccountSettingsMigration21
35+
36+
@Inject
37+
@ApplicationContext
38+
lateinit var context: Context
39+
40+
@Inject
41+
lateinit var logger: Logger
42+
43+
lateinit var account: Account
44+
val authority = CalendarContract.AUTHORITY
45+
46+
@Before
47+
fun setUp() {
48+
hiltRule.inject()
49+
50+
account = TestAccount.create()
51+
52+
// Enable sync globally and for the test account
53+
ContentResolver.setIsSyncable(account, authority, 1)
54+
}
55+
56+
57+
@Test
58+
fun testCancelsSyncs() {
59+
val authority = ContactsContract.AUTHORITY
60+
61+
// Register state change listener
62+
ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_PENDING) {
63+
// Check state
64+
val pending = ContentResolver.isSyncPending(account, authority)
65+
66+
// Verify the sync is now pending
67+
assertTrue(pending)
68+
69+
// Run the migration which should cancel the sync for all accounts (we can pass an invalid fake)
70+
migration.migrate(Account("",""))
71+
Thread.sleep(1000)
72+
73+
// Check the sync is not pending anymore
74+
assertFalse(ContentResolver.isSyncPending(account, authority))
75+
}
76+
77+
// Request sync
78+
ContentResolver.requestSync(syncRequest())
79+
}
80+
81+
82+
// helpers
83+
84+
private fun syncRequest() = SyncRequest.Builder()
85+
.setSyncAdapter(account, authority)
86+
.syncOnce()
87+
.setExpedited(true) // sync request will be scheduled at the front of the sync request queue
88+
.setManual(true) // equivalent of setting both SYNC_EXTRAS_IGNORE_SETTINGS and SYNC_EXTRAS_IGNORE_BACKOFF
89+
.build()
90+
91+
companion object {
92+
93+
var globalAutoSyncBeforeTest = false
94+
95+
@BeforeClass
96+
@JvmStatic
97+
fun before() {
98+
globalAutoSyncBeforeTest = ContentResolver.getMasterSyncAutomatically()
99+
100+
// We'll request syncs explicitly and with SYNC_EXTRAS_IGNORE_SETTINGS
101+
ContentResolver.setMasterSyncAutomatically(false)
102+
}
103+
104+
@AfterClass
105+
@JvmStatic
106+
fun after() {
107+
ContentResolver.setMasterSyncAutomatically(globalAutoSyncBeforeTest)
108+
}
109+
110+
}
111+
112+
}

0 commit comments

Comments
 (0)