-
Notifications
You must be signed in to change notification settings - Fork 929
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
Showing
52 changed files
with
1,006 additions
and
293 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
69 changes: 69 additions & 0 deletions
69
app/src/androidTest/java/com/duckduckgo/app/browser/BrowserChromeClientTest.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,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() | ||
} | ||
} |
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
107 changes: 107 additions & 0 deletions
107
app/src/androidTest/java/com/duckduckgo/app/browser/WebViewLongPressHandlerTest.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,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) | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
app/src/androidTest/java/com/duckduckgo/app/launch/LaunchViewModelTest.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,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)) | ||
} | ||
|
||
} |
Oops, something went wrong.