Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,42 +237,42 @@ class BrowserTabViewModelTest {

@Test
fun whenViewModelNotifiedThatWebViewHasFinishedLoadingThenViewStateIsUpdated() {
testee.loadingFinished()
testee.loadingFinished(null, false, false)
assertFalse(loadingViewState().isLoading)
}

@Test
fun whenLoadingFinishedWithUrlThenSiteVisitedEntryAddedToLeaderboardDao() {
testee.url.value = "http://example.com/abc"
testee.loadingFinished()
testee.loadingFinished(null, false, false)
verify(mockNetworkLeaderboardDao).insert(SiteVisitedEntity("example.com"))
}

@Test
fun whenLoadingFinishedWithUrlThenOmnibarTextUpdatedToMatch() {
val exampleUrl = "http://example.com/abc"
testee.loadingFinished(exampleUrl)
testee.loadingFinished(exampleUrl, false, false)
assertEquals(exampleUrl, omnibarViewState().omnibarText)
}

@Test
fun whenLoadingFinishedWithQueryUrlThenOmnibarTextUpdatedToShowQuery() {
val queryUrl = "http://duckduckgo.com?q=test"
testee.loadingFinished(queryUrl)
testee.loadingFinished(queryUrl, false, false)
assertEquals("test", omnibarViewState().omnibarText)
}

@Test
fun whenLoadingFinishedWithNoUrlThenOmnibarTextUpdatedToMatch() {
val exampleUrl = "http://example.com/abc"
testee.urlChanged(exampleUrl)
testee.loadingFinished(null)
testee.loadingFinished(null, false, false)
assertEquals(exampleUrl, omnibarViewState().omnibarText)
}

@Test
fun whenLoadingFinishedWithNoUrlThenSiteVisitedEntryNotAddedToLeaderboardDao() {
testee.loadingFinished()
testee.loadingFinished(null, false, false)
verify(mockNetworkLeaderboardDao, never()).insert(SiteVisitedEntity("example.com"))
}

Expand Down Expand Up @@ -331,13 +331,13 @@ class BrowserTabViewModelTest {

@Test
fun whenViewModelGetsProgressUpdateThenViewStateIsUpdated() {
testee.progressChanged(0)
testee.progressChanged(0, false, false)
assertEquals(0, loadingViewState().progress)

testee.progressChanged(50)
testee.progressChanged(50, false, false)
assertEquals(50, loadingViewState().progress)

testee.progressChanged(100)
testee.progressChanged(100, false, false)
assertEquals(100, loadingViewState().progress)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ class BrowserChromeClient @Inject constructor() : WebChromeClient() {
customView = null
}

override fun onProgressChanged(view: WebView, newProgress: Int) {
webViewClientListener?.progressChanged(newProgress)
override fun onProgressChanged(webView: WebView, newProgress: Int) {
val canGoBack = webView.canGoBack()
val canGoForward = webView.canGoForward()
webViewClientListener?.progressChanged(newProgress, canGoBack, canGoForward)
}

override fun onReceivedTitle(view: WebView, title: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,8 +817,8 @@ class BrowserTabFragment : Fragment(), FindListener {

private fun renderPopupMenus(browserShowing: Boolean, viewState: BrowserViewState) {
popupMenu.contentView.apply {
backPopupMenuItem.isEnabled = browserShowing && webView?.canGoBack() ?: false
forwardPopupMenuItem.isEnabled = browserShowing && webView?.canGoForward() ?: false
backPopupMenuItem.isEnabled = browserShowing && viewState.canGoBack
forwardPopupMenuItem.isEnabled = browserShowing && viewState.canGoForward
refreshPopupMenuItem.isEnabled = browserShowing
newTabPopupMenuItem.isEnabled = browserShowing
addBookmarksPopupMenuItem?.isEnabled = viewState.canAddBookmarks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ class BrowserTabViewModel(
val showFireButton: Boolean = true,
val showMenuButton: Boolean = true,
val canSharePage: Boolean = false,
val canAddBookmarks: Boolean = false
val canAddBookmarks: Boolean = false,
val canGoBack: Boolean = false,
val canGoForward: Boolean = false
)

data class OmnibarViewState(
Expand Down Expand Up @@ -244,11 +246,12 @@ class BrowserTabViewModel(
autoCompleteViewState.value = AutoCompleteViewState(false)
}

override fun progressChanged(newProgress: Int) {
override fun progressChanged(newProgress: Int, canGoBack: Boolean, canGoForward: Boolean) {
Timber.v("Loading in progress $newProgress")

val progress = currentLoadingViewState()
loadingViewState.value = progress.copy(progress = newProgress)
browserViewState.value = currentBrowserViewState().copy(canGoBack = canGoBack, canGoForward = canGoForward)
Copy link
Contributor

@subsymbolic subsymbolic Jul 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: It feels like the browserViewState and loadingViewState should be merged as they are closely coupled and we are updating both anyway.

Copy link
Member Author

@CDRussell CDRussell Jul 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tricky to draw the lines between them, as they do all feel pretty related.

However, I'd leave those two separate because they don't always both get updated. While a change in progress here does result in changes to both, there are other places where a change to the browser state does not result in changes to progress. So they are, at times, completely independent, even if other times they are coupled such as in the code above.

}

override fun goFullScreen(view: View) {
Expand All @@ -271,7 +274,7 @@ class BrowserTabViewModel(
onSiteChanged()
}

override fun loadingFinished(url: String?) {
override fun loadingFinished(url: String?, canGoBack: Boolean, canGoForward: Boolean) {
Timber.v("Loading finished")

val currentOmnibarViewState = currentOmnibarViewState()
Expand All @@ -281,6 +284,7 @@ class BrowserTabViewModel(

loadingViewState.value = currentLoadingViewState.copy(isLoading = false)
omnibarViewState.value = currentOmnibarViewState.copy(omnibarText = omnibarText)
browserViewState.value = currentBrowserViewState().copy(canGoBack = canGoBack, canGoForward = canGoForward)
registerSiteVisit()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ class BrowserWebViewClient @Inject constructor(
}
}

override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
override fun onPageStarted(webView: WebView, url: String?, favicon: Bitmap?) {
currentUrl = url
webViewClientListener?.loadingStarted()
webViewClientListener?.urlChanged(url)
}

override fun onPageFinished(view: WebView?, url: String?) {
webViewClientListener?.loadingFinished(url)
override fun onPageFinished(webView: WebView, url: String?) {
val canGoBack = webView.canGoBack()
val canGoForward = webView.canGoForward()

webViewClientListener?.loadingFinished(url, canGoBack, canGoForward)
}

@WorkerThread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import com.duckduckgo.app.trackerdetection.model.TrackingEvent

interface WebViewClientListener {
fun loadingStarted()
fun loadingFinished(url: String? = null)
fun progressChanged(newProgress: Int)
fun loadingFinished(url: String? = null, canGoBack: Boolean, canGoForward: Boolean)
fun progressChanged(newProgress: Int, canGoBack: Boolean, canGoForward: Boolean)
fun titleReceived(title: String)
fun urlChanged(url: String?)
fun trackerDetected(event: TrackingEvent)
Expand Down