| author | Da Risk <da_risk@geekorum.com> |
| Wed, 12 Jan 2022 17:29:59 -0400 | |
| changeset 882 | 7a74abf66c49 |
| parent 846 | ac0863af5ef6 |
| child 943 | 298742859784 |
| permissions | -rw-r--r-- |
| 80 | 1 |
/* |
| 0 | 2 |
* Geekttrss is a RSS feed reader application on the Android Platform. |
3 |
* |
|
| 882 | 4 |
* Copyright (C) 2017-2022 by Frederic-Charles Barthelery. |
| 0 | 5 |
* |
6 |
* This file is part of Geekttrss. |
|
7 |
* |
|
8 |
* Geekttrss is free software: you can redistribute it and/or modify |
|
9 |
* it under the terms of the GNU General Public License as published by |
|
10 |
* the Free Software Foundation, either version 3 of the License, or |
|
11 |
* (at your option) any later version. |
|
12 |
* |
|
13 |
* Geekttrss is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with Geekttrss. If not, see <http://www.gnu.org/licenses/>. |
|
20 |
*/ |
|
21 |
package com.geekorum.ttrss.accounts |
|
22 |
||
23 |
import android.accounts.AccountManager |
|
24 |
import android.content.ContentResolver |
|
25 |
import android.content.PeriodicSync |
|
26 |
import android.os.Bundle |
|
27 |
import androidx.test.core.app.ApplicationProvider |
|
|
744
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
28 |
import androidx.work.testing.WorkManagerTestInitHelper |
| 0 | 29 |
import com.geekorum.geekdroid.security.SecretCipher |
30 |
import com.geekorum.geekdroid.security.SecretEncryption |
|
|
265
bc4d82c760f6
Move BackgroundJobManager into its own package
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
31 |
import com.geekorum.ttrss.background_job.BackgroundJobManager |
| 0 | 32 |
import com.geekorum.ttrss.providers.ArticlesContract |
33 |
import com.geekorum.ttrss.sync.SyncContract |
|
34 |
import com.google.common.truth.Truth.assertThat |
|
|
744
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
35 |
import dagger.hilt.android.testing.HiltAndroidRule |
|
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
36 |
import dagger.hilt.android.testing.HiltAndroidTest |
| 0 | 37 |
import kotlinx.coroutines.delay |
38 |
import kotlinx.coroutines.runBlocking |
|
39 |
import org.junit.After |
|
40 |
import org.junit.Before |
|
|
744
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
41 |
import org.junit.Rule |
| 0 | 42 |
import org.junit.Test |
43 |
||
44 |
||
|
744
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
45 |
@HiltAndroidTest |
| 0 | 46 |
class AndroidTinyrssAccountManagerTest {
|
47 |
||
|
744
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
48 |
@get:Rule |
|
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
49 |
val hiltRule = HiltAndroidRule(this) |
|
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
50 |
|
| 0 | 51 |
lateinit var accountManager: AndroidTinyrssAccountManager |
52 |
lateinit var androidAccountManager: AccountManager |
|
53 |
lateinit var secretCipher: SecretCipher |
|
54 |
||
55 |
private val modelAccount = Account("test", "https://exemple.com")
|
|
56 |
private val androidAccount = android.accounts.Account("test", AndroidTinyrssAccountManager.ACCOUNT_TYPE)
|
|
57 |
||
58 |
@Before |
|
59 |
fun setup() {
|
|
|
744
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
60 |
// sync get triggered during test and try to access WorkManager from HiltTestApplication |
|
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
61 |
// provide a TestWorkManager |
|
90fd7d86d034
app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents:
611
diff
changeset
|
62 |
WorkManagerTestInitHelper.initializeTestWorkManager(ApplicationProvider.getApplicationContext()) |
| 0 | 63 |
androidAccountManager = AccountManager.get(ApplicationProvider.getApplicationContext()) |
64 |
secretCipher = SecretEncryption().getSecretCipher("instrumented test")
|
|
65 |
accountManager = AndroidTinyrssAccountManager(androidAccountManager, secretCipher) |
|
66 |
} |
|
67 |
||
68 |
||
69 |
@After |
|
70 |
fun tearDown() {
|
|
71 |
androidAccountManager.removeAccountExplicitly(androidAccount) |
|
72 |
} |
|
73 |
||
74 |
@Test |
|
75 |
fun testThatAddAccountWorks() {
|
|
76 |
val account = modelAccount |
|
| 80 | 77 |
val password = "password" |
78 |
val result = accountManager.addAccount(account, password) |
|
| 0 | 79 |
assertThat(result).isTrue() |
80 |
assertThat(androidAccountManager.getAccountsByType(AndroidTinyrssAccountManager.ACCOUNT_TYPE)).asList().contains( |
|
81 |
androidAccount) |
|
| 80 | 82 |
val retrievedPassword = accountManager.getPassword(account) |
83 |
assertThat(retrievedPassword).isEqualTo(password) |
|
84 |
} |
|
85 |
||
86 |
@Test |
|
87 |
fun testThatAccountWithLongPasswordWorks() {
|
|
88 |
val account = modelAccount |
|
89 |
val password = "9a266fc8b42966fb624d852bafa241d8fd05b47d36153ff6684ab344bd1ae57bba96a7de8fc12ec0bb016583735d7f5bca6dd7d9bc6482c2a3ac6bf6f9ec323f" |
|
90 |
val result = accountManager.addAccount(account, password) |
|
91 |
assertThat(result).isTrue() |
|
92 |
assertThat(androidAccountManager.getAccountsByType(AndroidTinyrssAccountManager.ACCOUNT_TYPE)).asList().contains( |
|
93 |
androidAccount) |
|
94 |
val retrievedPassword = accountManager.getPassword(account) |
|
95 |
assertThat(retrievedPassword).isEqualTo(password) |
|
| 0 | 96 |
} |
97 |
||
98 |
||
99 |
@Test |
|
100 |
fun testThatInitializeAccountSyncWorks() {
|
|
101 |
val urlBundle = Bundle().apply {
|
|
102 |
putString(AccountAuthenticator.USERDATA_URL, "https://exemple.com") |
|
103 |
} |
|
104 |
androidAccountManager.addAccountExplicitly(androidAccount, "password", urlBundle) |
|
105 |
val periodicRefreshSync = PeriodicSync(androidAccount, ArticlesContract.AUTHORITY, Bundle.EMPTY, |
|
106 |
BackgroundJobManager.PERIODIC_REFRESH_JOB_INTERVAL_S) |
|
107 |
||
108 |
val fullExtra = Bundle().apply {
|
|
109 |
putInt(SyncContract.EXTRA_NUMBER_OF_LATEST_ARTICLES_TO_REFRESH, -1) |
|
|
501
caf68bb56590
AndroidTinyrssAccountManagerTest: fix test for sync initialization
Da Risk <da_risk@geekorum.com>
parents:
265
diff
changeset
|
110 |
putBoolean(SyncContract.EXTRA_UPDATE_FEED_ICONS, true) |
| 0 | 111 |
} |
112 |
val periodicFullRefreshSync = PeriodicSync(androidAccount, ArticlesContract.AUTHORITY, fullExtra, |
|
113 |
BackgroundJobManager.PERIODIC_FULL_REFRESH_JOB_INTERVAL_S) |
|
114 |
||
115 |
accountManager.initializeAccountSync(modelAccount) |
|
116 |
// the system needs some time to initialize |
|
| 783 | 117 |
runBlocking{ delay(1000) }
|
| 0 | 118 |
|
119 |
val syncAutomatically = ContentResolver.getSyncAutomatically(androidAccount, ArticlesContract.AUTHORITY) |
|
120 |
assertThat(syncAutomatically).isTrue() |
|
121 |
val periodicSyncs = ContentResolver.getPeriodicSyncs(androidAccount, ArticlesContract.AUTHORITY) |
|
122 |
assertThat(periodicSyncs).hasSize(2) |
|
123 |
assertThat(periodicSyncs).containsExactly(periodicFullRefreshSync, periodicRefreshSync) |
|
124 |
} |
|
125 |
} |