Skip to content

Commit

Permalink
Merge branch 'release/4.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
subsymbolic committed Feb 8, 2018
2 parents b6aa866 + f6cd551 commit 4b800c7
Show file tree
Hide file tree
Showing 52 changed files with 1,006 additions and 293 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'kotlin-kapt'
apply from: '../versioning.gradle'

ext {
VERSION_NAME = "4.2.0"
VERSION_NAME = "4.3.0"
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2018 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.browser

import android.support.test.InstrumentationRegistry
import android.view.View
import android.webkit.WebChromeClient
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.times
import com.nhaarman.mockito_kotlin.verify
import org.junit.Before
import org.junit.Test

class BrowserChromeClientTest {

private lateinit var testee: BrowserChromeClient
private lateinit var mockWebViewClientListener: WebViewClientListener
private val fakeView = View(InstrumentationRegistry.getTargetContext())

@Before
fun setup() {
testee = BrowserChromeClient()
mockWebViewClientListener = mock()
testee.webViewClientListener = mockWebViewClientListener
}

@Test
fun whenCustomViewShownForFirstTimeListenerInstructedToGoFullScreen() {
testee.onShowCustomView(fakeView, null)
verify(mockWebViewClientListener).goFullScreen(fakeView)
}

@Test
fun whenCustomViewShownMultipleTimesListenerInstructedToGoFullScreenOnlyOnce() {
testee.onShowCustomView(fakeView, null)
testee.onShowCustomView(fakeView, null)
testee.onShowCustomView(fakeView, null)
verify(mockWebViewClientListener, times(1)).goFullScreen(fakeView)
}

@Test
fun whenCustomViewShownMultipleTimesCallbackInstructedToHideForAllButTheFirstCall() {
val mockCustomViewCallback: WebChromeClient.CustomViewCallback = mock()
testee.onShowCustomView(fakeView, mockCustomViewCallback)
testee.onShowCustomView(fakeView, mockCustomViewCallback)
testee.onShowCustomView(fakeView, mockCustomViewCallback)
verify(mockCustomViewCallback, times(2)).onCustomViewHidden()
}

@Test
fun whenHideCustomViewCalledThenListenerInstructedToExistFullScreen() {
testee.onHideCustomView()
verify(mockWebViewClientListener).exitFullScreen()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import android.arch.lifecycle.Observer
import android.arch.persistence.room.Room
import android.net.Uri
import android.support.test.InstrumentationRegistry
import android.view.MenuItem
import android.view.View
import com.duckduckgo.app.autocomplete.api.AutoCompleteApi
import com.duckduckgo.app.bookmarks.db.BookmarkEntity
import com.duckduckgo.app.bookmarks.db.BookmarksDao
Expand Down Expand Up @@ -50,7 +52,7 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.*
import org.mockito.ArgumentCaptor.forClass
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mockito.never
import org.mockito.Mockito.verify

Expand Down Expand Up @@ -90,11 +92,18 @@ class BrowserViewModelTest {
@Mock
private lateinit var bookmarksDao: BookmarksDao

@Mock
private lateinit var mockLongPressHandler: LongPressHandler

@Mock
private lateinit var mockOmnibarConverter: OmnibarEntryConverter

@Captor
private lateinit var commandCaptor: ArgumentCaptor<Command>

private lateinit var db: AppDatabase
private lateinit var appConfigurationDao: AppConfigurationDao

private val mockOmnibarConverter: OmnibarEntryConverter = mock()

private lateinit var testee: BrowserViewModel

@Before
Expand All @@ -116,6 +125,7 @@ class BrowserViewModelTest {
autoCompleteApi = mockAutoCompleteApi,
appSettingsPreferencesStore = mockSettingsStore,
bookmarksDao = bookmarksDao,
longPressHandler = mockLongPressHandler,
appConfigurationDao = appConfigurationDao)

testee.url.observeForever(mockQueryObserver)
Expand Down Expand Up @@ -240,10 +250,9 @@ class BrowserViewModelTest {
@Test
fun whenSharedTextReceivedThenNavigationTriggered() {
testee.onSharedTextReceived("http://example.com")
val captor: ArgumentCaptor<Command> = forClass(Command::class.java)
verify(mockCommandObserver, times(2)).onChanged(captor.capture())
assertNotNull(captor.value)
assertTrue(captor.value is Navigate)
verify(mockCommandObserver, times(2)).onChanged(commandCaptor.capture())
assertNotNull(commandCaptor.value)
assertTrue(commandCaptor.value is Navigate)
}

@Test
Expand Down Expand Up @@ -394,9 +403,48 @@ class BrowserViewModelTest {

@Test
fun whenEnteringNonEmptyQueryThenHideKeyboardCommandIssued() {
val captor = ArgumentCaptor.forClass(BrowserViewModel.Command::class.java)
testee.onUserSubmittedQuery("foo")
verify(mockCommandObserver, Mockito.atLeastOnce()).onChanged(captor.capture())
assertTrue(captor.value == Command.HideKeyboard)
verify(mockCommandObserver, Mockito.atLeastOnce()).onChanged(commandCaptor.capture())
assertTrue(commandCaptor.value == Command.HideKeyboard)
}

@Test
fun whenNotifiedEnteringFullScreenThenViewStateUpdatedWithFullScreenFlag() {
val stubView = View(InstrumentationRegistry.getTargetContext())
testee.goFullScreen(stubView)
assertTrue(testee.viewState.value!!.isFullScreen)
}

@Test
fun whenNotifiedEnteringFullScreenThenEnterFullScreenCommandIssued() {
val stubView = View(InstrumentationRegistry.getTargetContext())
testee.goFullScreen(stubView)
verify(mockCommandObserver, Mockito.atLeastOnce()).onChanged(commandCaptor.capture())
assertTrue(commandCaptor.lastValue is Command.ShowFullScreen)
}

@Test
fun whenNotifiedLeavingFullScreenThenViewStateUpdatedWithFullScreenFlagDisabled() {
testee.exitFullScreen()
assertFalse(testee.viewState.value!!.isFullScreen)
}

@Test
fun whenViewModelInitialisedThenFullScreenFlagIsDisabled() {
assertFalse(testee.viewState.value!!.isFullScreen)
}

@Test
fun whenUserSelectsDownloadImageOptionFromContextMenuThenDownloadFileCommandIssued() {
whenever(mockLongPressHandler.userSelectedMenuItem(anyString(), any()))
.thenReturn(LongPressHandler.RequiredAction.DownloadFile("example.com"))

val mockMenuItem : MenuItem = mock()
testee.userSelectedItemFromLongPressMenu("example.com", mockMenuItem)
verify(mockCommandObserver, Mockito.atLeastOnce()).onChanged(commandCaptor.capture())
assertTrue(commandCaptor.lastValue is Command.DownloadImage)

val lastCommand = commandCaptor.lastValue as Command.DownloadImage
assertEquals("example.com", lastCommand.url)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2018 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.browser

import android.view.ContextMenu
import android.view.MenuItem
import android.webkit.WebView.HitTestResult
import com.nhaarman.mockito_kotlin.eq
import com.nhaarman.mockito_kotlin.never
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import org.mockito.MockitoAnnotations

class WebViewLongPressHandlerTest {

private lateinit var testee: WebViewLongPressHandler

@Mock
private lateinit var mockMenu: ContextMenu

@Mock
private lateinit var mockMenuItem: MenuItem

@Before
fun setup() {
MockitoAnnotations.initMocks(this)
testee = WebViewLongPressHandler()
}

@Test
fun whenLongPressedWithImageTypeThenImageOptionsHeaderAddedToMenu() {
testee.handleLongPress(HitTestResult.IMAGE_TYPE, mockMenu)
verify(mockMenu).setHeaderTitle(R.string.imageOptions)
}

@Test
fun whenLongPressedWithAnchorImageTypeThenImageOptionsHeaderAddedToMenu() {
testee.handleLongPress(HitTestResult.SRC_IMAGE_ANCHOR_TYPE, mockMenu)
verify(mockMenu).setHeaderTitle(R.string.imageOptions)
}

@Test
fun whenLongPressedWithImageTypeThenDownloadImageMenuAdded() {
testee.handleLongPress(HitTestResult.IMAGE_TYPE, mockMenu)
verify(mockMenu).add(anyInt(), eq(WebViewLongPressHandler.CONTEXT_MENU_ID_DOWNLOAD_IMAGE), anyInt(), eq(R.string.downloadImage))
}

@Test
fun whenLongPressedWithAnchorImageTypeThenDownloadImageMenuAdded() {
testee.handleLongPress(HitTestResult.SRC_IMAGE_ANCHOR_TYPE, mockMenu)
verify(mockMenu).add(anyInt(), eq(WebViewLongPressHandler.CONTEXT_MENU_ID_DOWNLOAD_IMAGE), anyInt(), eq(R.string.downloadImage))
}

@Test
fun whenLongPressedWithOtherImageTypeThenMenuNotAltered() {
testee.handleLongPress(HitTestResult.UNKNOWN_TYPE, mockMenu)
verify(mockMenu, never()).setHeaderTitle(anyString())
verify(mockMenu, never()).setHeaderTitle(anyInt())
verify(mockMenu, never()).add(anyInt())
verify(mockMenu, never()).add(anyString())
verify(mockMenu, never()).add(anyInt(), anyInt(), anyInt(), anyInt())
verify(mockMenu, never()).add(anyInt(), anyInt(), anyInt(), anyString())
}

@Test
fun whenUserSelectedDownloadImageOptionThenActionIsDownloadFileActionRequired() {
whenever(mockMenuItem.itemId).thenReturn(WebViewLongPressHandler.CONTEXT_MENU_ID_DOWNLOAD_IMAGE)
val action = testee.userSelectedMenuItem("example.com", mockMenuItem)
assertTrue(action is LongPressHandler.RequiredAction.DownloadFile)
}

@Test
fun whenUserSelectedDownloadImageOptionThenDownloadFileWithCorrectUrlReturned() {
whenever(mockMenuItem.itemId).thenReturn(WebViewLongPressHandler.CONTEXT_MENU_ID_DOWNLOAD_IMAGE)
val action = testee.userSelectedMenuItem("example.com", mockMenuItem) as LongPressHandler.RequiredAction.DownloadFile
assertEquals("example.com", action.url)
}

@Test
fun whenUserSelectedUnknownOptionThenNoActionRequiredReturned() {
val unknownMenuId = 123
whenever(mockMenuItem.itemId).thenReturn(unknownMenuId)
val action = testee.userSelectedMenuItem("example.com", mockMenuItem)
assertTrue(action == LongPressHandler.RequiredAction.None)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2018 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.launch

import android.arch.core.executor.testing.InstantTaskExecutorRule
import android.arch.lifecycle.Observer
import com.duckduckgo.app.launch.LaunchViewModel.Command.*
import com.duckduckgo.app.onboarding.store.OnboardingStore
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito.any


class LaunchViewModelTest {

@get:Rule
@Suppress("unused")
var instantTaskExecutorRule = InstantTaskExecutorRule()

private var onboardingStore: OnboardingStore = mock()
private var mockCommandObserver: Observer<LaunchViewModel.Command> = mock()

private val testee: LaunchViewModel by lazy {
LaunchViewModel(onboardingStore)
}

@After
fun after() {
testee.command.removeObserver(mockCommandObserver)
}

@Test
fun whenOnboardingShouldShowThenCommandIsOnboarding() {
whenever(onboardingStore.shouldShow).thenReturn(true)
testee.command.observeForever(mockCommandObserver)
verify(mockCommandObserver).onChanged(any(Onboarding::class.java))
}

@Test
fun whenOnboardingShouldNotShowThenCommandIsHome() {
whenever(onboardingStore.shouldShow).thenReturn(false)
testee.command.observeForever(mockCommandObserver)
verify(mockCommandObserver).onChanged(any(Home::class.java))
}

@Test
fun whenOnboardingDoneThenCommandIsHome() {
whenever(onboardingStore.shouldShow).thenReturn(true)
testee.command.observeForever(mockCommandObserver)
verify(mockCommandObserver).onChanged(any(Onboarding::class.java))

testee.onOnboardingDone()
verify(mockCommandObserver).onChanged(any(Home::class.java))
}

}
Loading

0 comments on commit 4b800c7

Please sign in to comment.