Skip to content

Commit 2b7498e

Browse files
committed
Merge branch 'hotfix/5.76.1' into main
2 parents d81c4ea + 1ac7a86 commit 2b7498e

File tree

8 files changed

+51
-224
lines changed

8 files changed

+51
-224
lines changed

app/schemas/com.duckduckgo.app.browser.httpauth.db.HttpAuthDatabase/1.json

Lines changed: 0 additions & 58 deletions
This file was deleted.

app/src/androidTest/java/com/duckduckgo/app/browser/httpauth/WebViewHttpAuthStoreTest.kt

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
package com.duckduckgo.app.browser.httpauth
1818

1919
import android.webkit.WebView
20+
import android.webkit.WebViewDatabase
21+
import androidx.test.filters.SdkSuppress
2022
import com.duckduckgo.app.CoroutineTestRule
21-
import com.duckduckgo.app.browser.httpauth.db.HttpAuthDao
22-
import com.duckduckgo.app.browser.httpauth.db.HttpAuthEntity
23-
import com.duckduckgo.app.fire.fireproofwebsite.data.FireproofWebsiteDao
24-
import com.duckduckgo.app.fire.fireproofwebsite.data.FireproofWebsiteEntity
2523
import com.nhaarman.mockitokotlin2.mock
2624
import com.nhaarman.mockitokotlin2.verify
2725
import com.nhaarman.mockitokotlin2.whenever
@@ -37,14 +35,14 @@ class WebViewHttpAuthStoreTest {
3735
@Suppress("unused")
3836
val coroutineRule = CoroutineTestRule()
3937

40-
private val fireproofWebsiteDao: FireproofWebsiteDao = mock()
41-
private val httpAuthDao: HttpAuthDao = mock()
38+
private val webViewDatabase: WebViewDatabase = mock()
4239
private val webView: WebView = mock()
4340

44-
private val webViewHttpAuthStore = RealWebViewHttpAuthStore(coroutineRule.testDispatcherProvider, fireproofWebsiteDao, httpAuthDao)
41+
private val webViewHttpAuthStore = RealWebViewHttpAuthStore(webViewDatabase)
4542

4643
@Test
47-
fun whenSetHttpAuthUsernamePasswordThenInsertHttpAuthEntity() {
44+
@SdkSuppress(minSdkVersion = android.os.Build.VERSION_CODES.O)
45+
fun whenSetHttpAuthUsernamePasswordApi26AndAboveThenInsertHttpAuthEntity() {
4846
webViewHttpAuthStore.setHttpAuthUsernamePassword(
4947
webView = webView,
5048
host = "host",
@@ -53,32 +51,42 @@ class WebViewHttpAuthStoreTest {
5351
password = "pass",
5452
)
5553

56-
verify(httpAuthDao).insert(
57-
HttpAuthEntity(
58-
host = "host",
59-
realm = "realm",
60-
username = "name",
61-
password = "pass"
62-
)
63-
)
54+
verify(webViewDatabase).setHttpAuthUsernamePassword("host", "realm", "name", "pass")
6455
}
6556

6657
@Test
67-
fun whenGetHttpAuthUsernamePasswordThenReturnWebViewHttpAuthCredentials() {
68-
whenever(httpAuthDao.getAuthCredentials("host", "realm"))
69-
.thenReturn(HttpAuthEntity(1, "host", "realm", "name", "pass"))
58+
@SdkSuppress(minSdkVersion = android.os.Build.VERSION_CODES.O)
59+
fun whenGetHttpAuthUsernamePasswordApi26AndAboveThenReturnWebViewHttpAuthCredentials() {
60+
whenever(webViewDatabase.getHttpAuthUsernamePassword("host", "realm"))
61+
.thenReturn(arrayOf("name", "pass"))
7062
val credentials = webViewHttpAuthStore.getHttpAuthUsernamePassword(webView, "host", "realm")
7163

7264
assertEquals(WebViewHttpAuthCredentials("name", "pass"), credentials)
7365
}
7466

7567
@Test
76-
fun whenClearHttpAuthUsernamePasswordThenDeleteAllCredentialsExceptExclusions() {
77-
whenever(fireproofWebsiteDao.fireproofWebsitesSync())
78-
.thenReturn(listOf(FireproofWebsiteEntity("http://fireproofed.me")))
68+
@SdkSuppress(maxSdkVersion = android.os.Build.VERSION_CODES.N_MR1)
69+
@Suppress("DEPRECATION")
70+
fun whenSetHttpAuthUsernamePasswordApiBelow26ThenInsertHttpAuthEntity() {
71+
webViewHttpAuthStore.setHttpAuthUsernamePassword(
72+
webView = webView,
73+
host = "host",
74+
realm = "realm",
75+
username = "name",
76+
password = "pass",
77+
)
78+
79+
verify(webView).setHttpAuthUsernamePassword("host", "realm", "name", "pass")
80+
}
7981

80-
webViewHttpAuthStore.clearHttpAuthUsernamePassword(webView)
82+
@Test
83+
@SdkSuppress(maxSdkVersion = android.os.Build.VERSION_CODES.N_MR1)
84+
@Suppress("DEPRECATION")
85+
fun whenGetHttpAuthUsernamePasswordApiBelow26ThenReturnWebViewHttpAuthCredentials() {
86+
whenever(webView.getHttpAuthUsernamePassword("host", "realm"))
87+
.thenReturn(arrayOf("name", "pass"))
88+
val credentials = webViewHttpAuthStore.getHttpAuthUsernamePassword(webView, "host", "realm")
8189

82-
verify(httpAuthDao).deleteAll(listOf("http://fireproofed.me"))
90+
assertEquals(WebViewHttpAuthCredentials("name", "pass"), credentials)
8391
}
8492
}

app/src/androidTest/java/com/duckduckgo/app/di/StubDatabaseModule.kt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
package com.duckduckgo.app.di
1818

1919
import android.content.Context
20+
import android.webkit.WebViewDatabase
2021
import androidx.room.Room
2122
import com.duckduckgo.app.browser.httpauth.RealWebViewHttpAuthStore
2223
import com.duckduckgo.app.browser.httpauth.WebViewHttpAuthStore
23-
import com.duckduckgo.app.browser.httpauth.db.HttpAuthDatabase
24-
import com.duckduckgo.app.fire.fireproofwebsite.data.FireproofWebsiteDao
25-
import com.duckduckgo.app.global.DefaultDispatcherProvider
2624
import com.duckduckgo.app.global.db.AppDatabase
2725
import dagger.Module
2826
import dagger.Provides
@@ -43,12 +41,7 @@ class StubDatabaseModule {
4341
@Singleton
4442
fun provideWebViewHttpAuthStore(
4543
context: Context,
46-
fireproofWebsiteDao: FireproofWebsiteDao
4744
): WebViewHttpAuthStore {
48-
val db = Room.inMemoryDatabaseBuilder(context, HttpAuthDatabase::class.java)
49-
.allowMainThreadQueries()
50-
.build()
51-
52-
return RealWebViewHttpAuthStore(DefaultDispatcherProvider(), fireproofWebsiteDao, db.httpAuthDao())
45+
return RealWebViewHttpAuthStore(WebViewDatabase.getInstance(context))
5346
}
5447
}

app/src/main/java/com/duckduckgo/app/browser/httpauth/WebViewHttpAuthStore.kt

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,8 @@
1717
package com.duckduckgo.app.browser.httpauth
1818

1919
import android.webkit.WebView
20+
import android.webkit.WebViewDatabase
2021
import androidx.annotation.UiThread
21-
import com.duckduckgo.app.browser.httpauth.db.HttpAuthDao
22-
import com.duckduckgo.app.browser.httpauth.db.HttpAuthEntity
23-
import com.duckduckgo.app.fire.fireproofwebsite.data.FireproofWebsiteDao
24-
import com.duckduckgo.app.fire.fireproofwebsite.data.website
25-
import com.duckduckgo.app.global.DispatcherProvider
26-
import kotlinx.coroutines.runBlocking
27-
import kotlinx.coroutines.withContext
2822

2923
data class WebViewHttpAuthCredentials(val username: String, val password: String)
3024

@@ -40,37 +34,28 @@ interface WebViewHttpAuthStore {
4034
}
4135

4236
class RealWebViewHttpAuthStore(
43-
private val dispatcherProvider: DispatcherProvider,
44-
private val fireproofWebsiteDao: FireproofWebsiteDao,
45-
private val httpAuthDao: HttpAuthDao
37+
private val webViewDatabase: WebViewDatabase
4638
) : WebViewHttpAuthStore {
4739
override fun setHttpAuthUsernamePassword(webView: WebView, host: String, realm: String, username: String, password: String) {
48-
runBlocking {
49-
withContext(dispatcherProvider.io()) {
50-
httpAuthDao.insert(
51-
HttpAuthEntity(
52-
host = host,
53-
realm = realm,
54-
username = username,
55-
password = password
56-
)
57-
)
58-
}
40+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
41+
webViewDatabase.setHttpAuthUsernamePassword(host, realm, username, password)
42+
} else {
43+
webView.setHttpAuthUsernamePassword(host, realm, username, password)
5944
}
6045
}
6146

6247
override fun getHttpAuthUsernamePassword(webView: WebView, host: String, realm: String): WebViewHttpAuthCredentials? {
63-
return runBlocking(dispatcherProvider.io()) {
64-
val credentials = httpAuthDao.getAuthCredentials(host = host, realm = realm) ?: return@runBlocking null
65-
if (credentials.username == null || credentials.password == null) return@runBlocking null
66-
return@runBlocking WebViewHttpAuthCredentials(credentials.username!!, credentials.password!!)
67-
}
48+
val credentials = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
49+
webViewDatabase.getHttpAuthUsernamePassword(host, realm)
50+
} else {
51+
@Suppress("DEPRECATION")
52+
webView.getHttpAuthUsernamePassword(host, realm)
53+
} ?: return null
54+
55+
return WebViewHttpAuthCredentials(username = credentials[0], password = credentials[1])
6856
}
6957

7058
override fun clearHttpAuthUsernamePassword(webView: WebView) {
71-
runBlocking(dispatcherProvider.io()) {
72-
val exclusions = fireproofWebsiteDao.fireproofWebsitesSync().map { it.website() }
73-
httpAuthDao.deleteAll(exclusions)
74-
}
59+
webViewDatabase.clearHttpAuthUsernamePassword()
7560
}
7661
}

app/src/main/java/com/duckduckgo/app/browser/httpauth/db/HttpAuthDao.kt

Lines changed: 0 additions & 34 deletions
This file was deleted.

app/src/main/java/com/duckduckgo/app/browser/httpauth/db/HttpAuthDatabase.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/src/main/java/com/duckduckgo/app/browser/httpauth/db/HttpAuthEntity.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.

app/src/main/java/com/duckduckgo/app/di/DatabaseModule.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717
package com.duckduckgo.app.di
1818

1919
import android.content.Context
20+
import android.webkit.WebViewDatabase
2021
import androidx.room.Room
21-
import androidx.room.RoomDatabase
2222
import com.duckduckgo.app.browser.addtohome.AddToHomeCapabilityDetector
2323
import com.duckduckgo.app.browser.httpauth.RealWebViewHttpAuthStore
2424
import com.duckduckgo.app.browser.httpauth.WebViewHttpAuthStore
25-
import com.duckduckgo.app.browser.httpauth.db.HttpAuthDatabase
26-
import com.duckduckgo.app.fire.fireproofwebsite.data.FireproofWebsiteDao
27-
import com.duckduckgo.app.global.DispatcherProvider
2825
import com.duckduckgo.app.global.db.AppDatabase
2926
import com.duckduckgo.app.global.db.MigrationsProvider
3027
import com.duckduckgo.app.settings.db.SettingsDataStore
@@ -39,14 +36,8 @@ class DatabaseModule {
3936
@Singleton
4037
fun provideWebViewHttpAuthStore(
4138
context: Context,
42-
fireproofWebsiteDao: FireproofWebsiteDao,
43-
dispatcherProvider: DispatcherProvider
4439
): WebViewHttpAuthStore {
45-
val httpAuthDb = Room.databaseBuilder(context, HttpAuthDatabase::class.java, "http_auth.db")
46-
.setJournalMode(RoomDatabase.JournalMode.TRUNCATE)
47-
.build()
48-
49-
return RealWebViewHttpAuthStore(dispatcherProvider, fireproofWebsiteDao, httpAuthDb.httpAuthDao())
40+
return RealWebViewHttpAuthStore(WebViewDatabase.getInstance(context))
5041
}
5142

5243
@Provides

0 commit comments

Comments
 (0)