This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #21002: Ads new search group UI tests
Fixed and re-enabled editCustomSearchEngineTest with custom MockWebServer URL
- Loading branch information
Oana Horvath
committed
Mar 15, 2022
1 parent
787d506
commit 7da49c2
Showing
13 changed files
with
382 additions
and
41 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
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> | ||
|
64 changes: 64 additions & 0 deletions
64
app/src/androidTest/java/org/mozilla/fenix/helpers/SearchDispatcher.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,64 @@ | ||
/* 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.os.Handler | ||
import android.os.Looper | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import okhttp3.mockwebserver.Dispatcher | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import okhttp3.mockwebserver.RecordedRequest | ||
import okio.Buffer | ||
import okio.source | ||
|
||
/** | ||
* 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 { | ||
// 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=")) { | ||
MockResponse().setResponseCode(HTTP_OK) | ||
val path = "pages/generic4.html" | ||
assetManager.open(path).use { inputStream -> | ||
return fileToResponse(inputStream) | ||
} | ||
} | ||
return MockResponse().setResponseCode(HTTP_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) | ||
} | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun fileToResponse(file: InputStream): MockResponse { | ||
return MockResponse() | ||
.setResponseCode(HTTP_OK) | ||
.setBody(fileToBytes(file)) | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun fileToBytes(file: InputStream): Buffer { | ||
val result = Buffer() | ||
result.writeAll(file.source()) | ||
return result | ||
} |
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
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
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
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
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
Oops, something went wrong.