inject SharedPreferences (#4441)
(this one is for @charlag)
Calling `PreferenceManager.getDefaultSharedPreferences()` will read the
preference file from disk every time. This PR makes `SharedPreferences`
a singleton so they will only be created once at appstart (with a few
exceptions where it is hard to inject, e.g. in the `openLink` helper)
which should help getting our ANRs down.
```
StrictMode policy violation; ~duration=285 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1666)
at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8054)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
at java.io.File.exists(File.java:813)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:790)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:781)
at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:737)
at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:962)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:583)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:221)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:221)
at androidx.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:119)
at com.keylesspalace.tusky.BaseActivity.onCreate(BaseActivity.java:96)
...
```
This commit is contained in:
parent
78b1fcb9c6
commit
d554d71958
27 changed files with 159 additions and 135 deletions
|
|
@ -140,6 +140,13 @@ class MainActivityTest {
|
|||
onBlocking { accountVerifyCredentials() } doReturn NetworkResult.success(account)
|
||||
onBlocking { listAnnouncements(false) } doReturn NetworkResult.success(emptyList())
|
||||
}
|
||||
activity.preferences = mock(defaultAnswer = {
|
||||
when (it.method.returnType) {
|
||||
String::class.java -> "test"
|
||||
Boolean::class.java -> false
|
||||
else -> null
|
||||
}
|
||||
})
|
||||
controller.create().start()
|
||||
return activity
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,14 +16,20 @@
|
|||
package com.keylesspalace.tusky
|
||||
|
||||
import android.app.Application
|
||||
import android.content.SharedPreferences
|
||||
import com.keylesspalace.tusky.di.PreferencesEntryPoint
|
||||
import dagger.hilt.internal.GeneratedComponent
|
||||
import de.c1710.filemojicompat_defaults.DefaultEmojiPackList
|
||||
import de.c1710.filemojicompat_ui.helpers.EmojiPackHelper
|
||||
import org.mockito.kotlin.mock
|
||||
|
||||
// override TuskyApplication for Robolectric tests, only initialize the necessary stuff
|
||||
class TuskyApplication : Application() {
|
||||
class TuskyApplication : Application(), PreferencesEntryPoint, GeneratedComponent {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
EmojiPackHelper.init(this, DefaultEmojiPackList.get(this))
|
||||
}
|
||||
|
||||
override fun preferences(): SharedPreferences = mock {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,6 +163,14 @@ class ComposeActivityTest {
|
|||
activity.accountManager = accountManagerMock
|
||||
activity.viewModelProviderFactory = testViewModelFactory
|
||||
|
||||
activity.preferences = mock(defaultAnswer = {
|
||||
when (it.method.returnType) {
|
||||
String::class.java -> "test"
|
||||
Boolean::class.java -> false
|
||||
else -> null
|
||||
}
|
||||
})
|
||||
|
||||
controller.create().start()
|
||||
shadowOf(getMainLooper()).idle()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue