-
Notifications
You must be signed in to change notification settings - Fork 871
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
627f283
commit 1aa07e2
Showing
13 changed files
with
505 additions
and
37 deletions.
There are no files selected for viewing
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
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
84 changes: 84 additions & 0 deletions
84
app/src/androidTest/java/com/duckduckgo/app/history/HistoryDaoTest.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,84 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.app.history | ||
|
||
import androidx.room.Room | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.duckduckgo.app.history.store.HistoryDao | ||
import com.duckduckgo.app.history.store.HistoryDatabase | ||
import com.duckduckgo.app.history.store.HistoryEntryEntity | ||
import com.duckduckgo.app.history.store.HistoryEntryWithVisits | ||
import io.reactivex.observers.TestObserver | ||
import org.junit.After | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class HistoryDaoTest { | ||
|
||
private lateinit var historyDao: HistoryDao | ||
private lateinit var db: HistoryDatabase | ||
|
||
@Before | ||
fun setup() { | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
db = Room.inMemoryDatabaseBuilder(context, HistoryDatabase::class.java).build() | ||
historyDao = db.historyDao() | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
db.close() | ||
} | ||
|
||
@Test | ||
fun testGetHistoryEntryByUrl() { | ||
val historyEntry = HistoryEntryEntity(url = "url", title = "title", query = "query", isSerp = false) | ||
historyDao.insertHistoryEntry(historyEntry) | ||
|
||
val retrievedEntry = historyDao.getHistoryEntryByUrl("url") | ||
Assert.assertNotNull(retrievedEntry) | ||
Assert.assertEquals(historyEntry.url, retrievedEntry?.url) | ||
} | ||
|
||
@Test | ||
fun whenInsertSameUrlWithSameDateTwiceThenOnlyOneEntryAndOneVisitAreStored() { | ||
historyDao.updateOrInsertVisit("url", "title", "query", false, 1L) | ||
historyDao.updateOrInsertVisit("url", "title", "query", false, 1L) | ||
|
||
val historyEntriesWithVisits = historyDao.getHistoryEntriesWithVisits() | ||
val testObserver = TestObserver<List<HistoryEntryWithVisits>>() | ||
historyEntriesWithVisits.subscribe(testObserver) | ||
Assert.assertEquals(1, testObserver.valueCount()) | ||
Assert.assertEquals(1, testObserver.values().first().first().visits.count()) | ||
} | ||
|
||
@Test | ||
fun whenInsertSameUrlWithDifferentDateTwiceThenOneEntryAndTwoVisitsAreStored() { | ||
historyDao.updateOrInsertVisit("url", "title", "query", false, 1L) | ||
historyDao.updateOrInsertVisit("url", "title", "query", false, 2L) | ||
|
||
val historyEntriesWithVisits = historyDao.getHistoryEntriesWithVisits() | ||
val testObserver = TestObserver<List<HistoryEntryWithVisits>>() | ||
historyEntriesWithVisits.subscribe(testObserver) | ||
Assert.assertEquals(1, testObserver.valueCount()) | ||
Assert.assertEquals(2, testObserver.values().first().first().visits.count()) | ||
} | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/duckduckgo/app/history/HistoryEntry.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,52 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.app.history | ||
|
||
import android.net.Uri | ||
import androidx.core.net.toUri | ||
import com.duckduckgo.app.history.HistoryEntry.VisitedPage | ||
import com.duckduckgo.app.history.HistoryEntry.VisitedSERP | ||
import com.duckduckgo.app.history.store.HistoryEntryWithVisits | ||
import java.util.Date | ||
|
||
sealed class HistoryEntry( | ||
open val url: Uri, | ||
open val title: String, | ||
open val visits: List<Date>, | ||
) { | ||
|
||
data class VisitedPage( | ||
override val url: Uri, | ||
override val title: String, | ||
override val visits: List<Date>, | ||
) : HistoryEntry(url = url, title = title, visits = visits) | ||
|
||
data class VisitedSERP( | ||
override val url: Uri, | ||
override val title: String, | ||
val query: String, | ||
val queryTokens: List<String>? = null, | ||
override val visits: List<Date>, | ||
) : HistoryEntry(url = url, title, visits = visits) | ||
} | ||
|
||
fun HistoryEntryWithVisits.toHistoryEntry(): HistoryEntry { | ||
return when (historyEntry.isSerp) { | ||
true -> VisitedSERP(historyEntry.url.toUri(), historyEntry.title, historyEntry.query ?: "", visits = visits.map { Date(it.date) }) | ||
false -> VisitedPage(historyEntry.url.toUri(), historyEntry.title, visits.map { Date(it.date) }) | ||
} | ||
} |
Oops, something went wrong.