Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
run #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Oana Horvath committed Feb 18, 2022
1 parent b5e4881 commit b4cebe5
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 89 deletions.
1 change: 1 addition & 0 deletions app/src/androidTest/assets/pages/generic4.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Test_Page_4</title>
</head>
<body>
Expand Down
13 changes: 0 additions & 13 deletions app/src/androidTest/assets/pages/searchPage.html

This file was deleted.

11 changes: 0 additions & 11 deletions app/src/androidTest/assets/pages/searchResults.html

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.helpers

import android.net.Uri
import android.os.Handler
import android.os.Looper
import androidx.test.platform.app.InstrumentationRegistry
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import okio.Buffer
import okio.source
import java.io.IOException
import java.io.InputStream

/**
* A [MockWebServer] [Dispatcher] that will return a generic search results page in the body of
* requests and responds with status 200.
*
* If the dispatcher is unable to read a requested asset, it will fail the test by throwing an
* Exception on the main thread.
*
* @sample [org.mozilla.fenix.ui.SearchTest]
*/
class SearchDispatcher: Dispatcher() {
private val mainThreadHandler = Handler(Looper.getMainLooper())

override fun dispatch(request: RecordedRequest): MockResponse {
val assetManager = InstrumentationRegistry.getInstrumentation().context.assets
try {
if (request.path!!.contains("searchPage.html")) {
val pathWithoutQueryParams = Uri.parse(request.path!!.drop(1)).path
assetManager.open(pathWithoutQueryParams!!).use { inputStream ->
return fileToResponse(inputStream)
}
}
if (request.path!!.contains("search=testapp")) {
// return MockResponse().setBody("searchResults.html") // Use regex to identify search term. Not sure what the return should be. Test and experiment
val path = "pages/searchResults.html"
// When we perform a search with the custom search engine, returns the generic4.html test page as search results
if (request.path!!.contains("searchResults.html?search=")) {
val path = "pages/generic4.html"
assetManager.open(path).use { inputStream ->
return fileToResponse(inputStream)
}
}
// Adding a new custom search engine
if (request.path!!.contains("searchResults.html?search=%s")) {
return MockResponse().setResponseCode(200)
}
return MockResponse().setResponseCode(404)
} catch (e: IOException) { // e.g. file not found.
} catch (e: IOException) {
// e.g. file not found.
// We're on a background thread so we need to forward the exception to the main thread.
mainThreadHandler.postAtFrontOfQueue { throw e }
return MockResponse().setResponseCode(HTTP_NOT_FOUND)
Expand All @@ -47,11 +56,11 @@ class SearchDispatcher: Dispatcher() {
private fun fileToResponse(file: InputStream): MockResponse {
return MockResponse()
.setResponseCode(HTTP_OK)
.setBody(fileToBytes(file)!!)
.setBody(fileToBytes(file))
}

@Throws(IOException::class)
private fun fileToBytes(file: InputStream): Buffer? {
private fun fileToBytes(file: InputStream): Buffer {
val result = Buffer()
result.writeAll(file.source())
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.mozilla.fenix.helpers.ext.toUri
object TestAssetHelper {
@Suppress("MagicNumber")
val waitingTime: Long = TimeUnit.SECONDS.toMillis(15)
val waitingTimeShort: Long = TimeUnit.SECONDS.toMillis(1)
val waitingTimeShort: Long = TimeUnit.SECONDS.toMillis(3)

data class TestAsset(val url: Uri, val content: String, val title: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.Until
import java.io.File
import kotlinx.coroutines.runBlocking
import mozilla.components.browser.state.search.SearchEngine
import mozilla.components.support.ktx.android.content.appName
import org.hamcrest.CoreMatchers
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.Matcher
import org.junit.Assert
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTimeShort
import org.mozilla.fenix.helpers.ext.waitNotNull
Expand Down Expand Up @@ -252,4 +254,11 @@ object TestHelper {
}

fun getStringResource(id: Int) = appContext.resources.getString(id, appName)

fun setCustomSearchEngine(searchEngine: SearchEngine) {
with (appContext.components.useCases.searchUseCases) {
addSearchEngine(searchEngine)
selectSearchEngine(searchEngine)
}
}
}
4 changes: 2 additions & 2 deletions app/src/androidTest/java/org/mozilla/fenix/ui/HistoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ class HistoryTest {
historyListIdlingResource =
RecyclerViewIdlingResource(activityTestRule.activity.findViewById(R.id.history_list), 2)
IdlingRegistry.getInstance().register(historyListIdlingResource!!)
verifyHistoryItemExists(firstWebPage.url.toString())
verifyHistoryItemExists(secondWebPage.url.toString())
verifyHistoryItemExists(true, firstWebPage.url.toString())
verifyHistoryItemExists(true, secondWebPage.url.toString())
longTapSelectItem(firstWebPage.url)
longTapSelectItem(secondWebPage.url)
openActionBarOverflowOrOptionsMenu(activityTestRule.activity)
Expand Down
132 changes: 105 additions & 27 deletions app/src/androidTest/java/org/mozilla/fenix/ui/SearchTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
package org.mozilla.fenix.ui

import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.core.net.toUri
import androidx.test.uiautomator.UiSelector
import mozilla.components.browser.icons.IconRequest
import mozilla.components.browser.icons.generator.DefaultIconGenerator
import mozilla.components.browser.state.search.SearchEngine
import mozilla.components.feature.search.ext.createSearchEngine
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.customannotations.SmokeTest
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.helpers.FeatureSettingsHelper
import org.mozilla.fenix.helpers.HomeActivityTestRule
import org.mozilla.fenix.helpers.SearchDispatcher
import org.mozilla.fenix.helpers.TestHelper.appContext
import org.mozilla.fenix.helpers.TestHelper.setCustomSearchEngine
import org.mozilla.fenix.ui.robots.homeScreen
import org.mozilla.fenix.ui.robots.mDevice
import org.mozilla.fenix.ui.robots.navigationToolbar
import org.mozilla.fenix.ui.robots.searchSettingsScreen

/**
Expand All @@ -33,18 +36,27 @@ import org.mozilla.fenix.ui.robots.searchSettingsScreen
*/

class SearchTest {
private val featureSettingsHelper = FeatureSettingsHelper()
private lateinit var mockWebServer: MockWebServer

@get:Rule
val activityTestRule = AndroidComposeTestRule(
HomeActivityTestRule(),
{ it.activity }
)

private val featureSettingsHelper = FeatureSettingsHelper()
lateinit var searchMockServer: MockWebServer
// private fun searchMockServer() = MockWebServer().apply {
// dispatcher = SearchDispatcher()
// }

// private val customSearchEngine = createSearchEngine(
// name = "TestSearchEngine",
// url = "http://localhost:${searchMockServer.port}/searchResults.html?search={searchTerms}",
// icon = DefaultIconGenerator().generate(appContext, IconRequest("http://localhost:${searchMockServer.port}/searchResults.html?search={searchTerms}")).bitmap
// )

@Before
fun setUp() {
mockWebServer = MockWebServer().apply {
searchMockServer = MockWebServer().apply {
dispatcher = SearchDispatcher()
start()
}
Expand All @@ -53,6 +65,7 @@ class SearchTest {

@After
fun tearDown() {
searchMockServer.shutdown()
featureSettingsHelper.resetAllFeatureFlags()
}

Expand Down Expand Up @@ -127,35 +140,100 @@ class SearchTest {

@SmokeTest
@Test
fun testSearchGroupShowsInRecentlyVisited() {
// val searchPage = mockWebServer.url("pages/searchPage.html").toString().toUri()
val searchEngine = object {
var title = "TestSearchEngine"
var url = "http://localhost:${mockWebServer.port}/searchResults.html?search=%s"
fun testSearchGroupInRecentlyVisited() {
val firstPage = searchMockServer.url("generic1.html").toString()
val secondPage = searchMockServer.url("generic2.html").toString()
val searchString = "http://localhost:${searchMockServer.port}/searchResults.html?search={searchTerms}"
val customSearchEngine = createSearchEngine(
name = "TestSearchEngine",
url = searchString,
icon = DefaultIconGenerator().generate(appContext, IconRequest(searchString)).bitmap
)
setCustomSearchEngine(customSearchEngine)

// Performs a search and opens 2 dummy search results links to create a search group
homeScreen {
}.openSearch {
}.submitQuery("test") {
longClickMatchingText("Link 1")
clickContextOpenLinkInNewTab()
longClickMatchingText("Link 2")
clickContextOpenLinkInNewTab()
}.goToHomescreen {
verifyJumpBackInSectionIsDisplayed()
verifyCurrentSearchGroupIsDisplayed(true,"test",3)
verifyRecentlyVisitedSearchGroupDisplayed(false, "test", 3)
}.openTabDrawer {
}.openTabFromGroup(firstPage) {
}.openTabDrawer {
}.openTabFromGroup(secondPage) {
}.openTabDrawer {
}.openTabsListThreeDotMenu {
}.closeAllTabs {
verifyRecentlyVisitedSearchGroupDisplayed(true,"test", 3)
}
}

// Adds our custom search page as default search engine
searchSettingsScreen{
addCustomSearchEngine(searchEngine.title, searchEngine.url)
setCustomEngineAsDefault(searchEngine.title)
@SmokeTest
@Test
fun noCurrentSearchGroupFromPrivateBrowsingTest() {
val searchString = "http://localhost:${searchMockServer.port}/searchResults.html?search={searchTerms}"
val customSearchEngine = createSearchEngine(
name = "TestSearchEngine",
url = searchString,
icon = DefaultIconGenerator().generate(appContext, IconRequest(searchString)).bitmap
)
setCustomSearchEngine(customSearchEngine)

// Performs a search and opens 2 dummy search results links to create a search group
homeScreen {
}.openSearch {
}.submitQuery("test") {
longClickMatchingText("Link 1")
clickContextOpenLinkInPrivateTab()
longClickMatchingText("Link 2")
clickContextOpenLinkInPrivateTab()
}.goToHomescreen {
verifyCurrentSearchGroupIsDisplayed(false, "test", 3)
}.openThreeDotMenu {
}.openHistory {
verifyHistoryItemExists(false, "3 sites")
}
}

@SmokeTest
@Test
fun noRecentlyVisitedSearchGroupInPrivateBrowsingTest() {
val firstPage = searchMockServer.url("generic1.html").toString()
val secondPage = searchMockServer.url("generic2.html").toString()
val searchString = "http://localhost:${searchMockServer.port}/searchResults.html?search={searchTerms}"
val customSearchEngine = createSearchEngine(
name = "TestSearchEngine",
url = searchString,
icon = DefaultIconGenerator().generate(appContext, IconRequest(searchString)).bitmap
)
setCustomSearchEngine(customSearchEngine)

// Performs a search and opens 2 dummy search results links to create a search group
// navigationToolbar {
// }.enterURLAndEnterToBrowser(searchPage) {
// mDevice.findObject(UiSelector().resourceId("searchBox")).text = "testapp"
// mDevice.findObject(UiSelector().resourceId("submit")).click()
homeScreen {
}.togglePrivateBrowsingMode()
homeScreen {
}.openSearch {
}.submitQuery("testapp") {
longClickMatchingText("downloads")
clickContextOpenLinkInNewTab()
longClickMatchingText("permissions")
clickContextOpenLinkInNewTab()
}.submitQuery("test") {
longClickMatchingText("Link 1")
clickContextOpenLinkInPrivateTab()
longClickMatchingText("Link 2")
clickContextOpenLinkInPrivateTab()
}.openTabDrawer {
}.openTab(firstPage) {
}.openTabDrawer {
}.openTab(secondPage) {
}.openTabDrawer {
}.openTabsListThreeDotMenu {
}.closeAllTabs {
verifyRecentlyVisitedItemDisplayed("3 sites")
homeScreen {
}.togglePrivateBrowsingMode()
verifyRecentlyVisitedSearchGroupDisplayed(false, "test", 3)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import org.mozilla.fenix.customannotations.SmokeTest
import org.mozilla.fenix.helpers.AndroidAssetDispatcher
import org.mozilla.fenix.helpers.FeatureSettingsHelper
import org.mozilla.fenix.helpers.HomeActivityIntentTestRule
import org.mozilla.fenix.helpers.SearchDispatcher
import org.mozilla.fenix.helpers.TestAssetHelper.getGenericAsset
import org.mozilla.fenix.helpers.TestHelper.exitMenu
import org.mozilla.fenix.ui.robots.homeScreen
import org.mozilla.fenix.ui.robots.navigationToolbar

class SettingsSearchTest {
private lateinit var mockWebServer: MockWebServer
private lateinit var searchMockServer: MockWebServer
private val featureSettingsHelper = FeatureSettingsHelper()

@get:Rule
Expand Down Expand Up @@ -147,12 +149,16 @@ class SettingsSearchTest {
@SmokeTest
@Test
// Verifies setting as default a customized search engine name and URL
@Ignore("Failing intermittently https://github.com/mozilla-mobile/fenix/issues/22256")
//@Ignore("Failing intermittently https://github.com/mozilla-mobile/fenix/issues/22256")
fun editCustomSearchEngineTest() {
searchMockServer = MockWebServer().apply {
dispatcher = SearchDispatcher()
start()
}
val searchEngine = object {
var title = "Elefant"
var url = "https://www.elefant.ro/search?SearchTerm=%s"
var newTitle = "Test"
val title = "TestSearchEngine"
val url = "http://localhost:${searchMockServer.port}/searchResults.html?search=%s"
val newTitle = "Test"
}

homeScreen {
Expand Down
Loading

0 comments on commit b4cebe5

Please sign in to comment.