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 @@ -23,6 +23,7 @@ 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
Expand Down Expand Up @@ -51,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 @@ -91,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 @@ -117,6 +125,7 @@ class BrowserViewModelTest {
autoCompleteApi = mockAutoCompleteApi,
appSettingsPreferencesStore = mockSettingsStore,
bookmarksDao = bookmarksDao,
longPressHandler = mockLongPressHandler,
appConfigurationDao = appConfigurationDao)

testee.url.observeForever(mockQueryObserver)
Expand Down Expand Up @@ -241,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 @@ -395,10 +403,9 @@ 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
Expand All @@ -410,11 +417,10 @@ class BrowserViewModelTest {

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

@Test
Expand All @@ -427,4 +433,18 @@ class BrowserViewModelTest {
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)
}
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name="com.duckduckgo.app.global.DuckDuckGoApplication"
Expand Down
Loading