Update HomeScreenTest (#3882)
parent
00271cc20d
commit
e7d0bfe581
@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
package org.mozilla.fenix.helpers
|
||||||
|
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
|
||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
import android.graphics.Color
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts the two bitmaps are the same by ensuring their dimensions, config, and
|
||||||
|
* pixel data are the same (within the provided delta): this is the same metrics that
|
||||||
|
* [Bitmap.sameAs] uses.
|
||||||
|
*/
|
||||||
|
fun assertEqualsWithDelta(expectedB: Bitmap, actualB: Bitmap, delta: Float) {
|
||||||
|
assertEquals("widths should be equal", expectedB.width, actualB.width)
|
||||||
|
assertEquals("heights should be equal", expectedB.height, actualB.height)
|
||||||
|
assertEquals("config should be equal", expectedB.config, actualB.config)
|
||||||
|
|
||||||
|
for (i in 0 until expectedB.width) {
|
||||||
|
for (j in 0 until expectedB.height) {
|
||||||
|
val ePx = expectedB.getPixel(i, j)
|
||||||
|
val aPx = actualB.getPixel(i, j)
|
||||||
|
val warn = "Pixel ${i}x$j"
|
||||||
|
assertEquals("$warn a", Color.alpha(ePx).toFloat(), Color.alpha(aPx).toFloat(), delta)
|
||||||
|
assertEquals("$warn r", Color.red(ePx).toFloat(), Color.red(aPx).toFloat(), delta)
|
||||||
|
assertEquals("$warn g", Color.green(ePx).toFloat(), Color.green(aPx).toFloat(), delta)
|
||||||
|
assertEquals("$warn b", Color.blue(ePx).toFloat(), Color.blue(aPx).toFloat(), delta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
/* 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 java.io.IOException
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
|
import androidx.test.InstrumentationRegistry
|
||||||
|
import okhttp3.mockwebserver.Dispatcher
|
||||||
|
import okhttp3.mockwebserver.MockResponse
|
||||||
|
import okhttp3.mockwebserver.MockWebServer
|
||||||
|
import okhttp3.mockwebserver.RecordedRequest
|
||||||
|
import org.mozilla.fenix.helpers.ext.toUri
|
||||||
|
|
||||||
|
object MockWebServerHelper {
|
||||||
|
|
||||||
|
fun initMockWebServerAndReturnEndpoints(vararg messages: String): List<Uri> {
|
||||||
|
val mockServer = MockWebServer()
|
||||||
|
var uniquePath = 0
|
||||||
|
val uris = mutableListOf<Uri>()
|
||||||
|
messages.forEach { message ->
|
||||||
|
val response = MockResponse().setBody("<html><body>$message</body></html>")
|
||||||
|
mockServer.enqueue(response)
|
||||||
|
val endpoint = mockServer.url(uniquePath++.toString()).toString().toUri()!!
|
||||||
|
uris += endpoint
|
||||||
|
}
|
||||||
|
return uris
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A [MockWebServer] [Dispatcher] that will return Android assets in the body of requests.
|
||||||
|
*
|
||||||
|
* 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.tv.firefox.ui.BasicNavigationTest.basicNavigationTest]
|
||||||
|
*/
|
||||||
|
const val HTTP_OK = 200
|
||||||
|
const val HTTP_NOT_FOUND = 404
|
||||||
|
|
||||||
|
class AndroidAssetDispatcher : Dispatcher() {
|
||||||
|
private val mainThreadHandler = Handler(Looper.getMainLooper())
|
||||||
|
|
||||||
|
override fun dispatch(request: RecordedRequest): MockResponse {
|
||||||
|
val assetManager = InstrumentationRegistry.getContext().assets
|
||||||
|
val assetContents = try {
|
||||||
|
val pathNoLeadingSlash = request.path.drop(1)
|
||||||
|
assetManager.open(pathNoLeadingSlash).use { inputStream ->
|
||||||
|
inputStream.bufferedReader().use { it.readText() }
|
||||||
|
}
|
||||||
|
} 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)
|
||||||
|
}
|
||||||
|
return MockResponse().setResponseCode(HTTP_OK).setBody(assetContents)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
/* 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 okhttp3.mockwebserver.MockWebServer
|
||||||
|
import org.mozilla.fenix.helpers.ext.toUri
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper for hosting web pages locally for testing purposes.
|
||||||
|
*/
|
||||||
|
object TestAssetHelper {
|
||||||
|
@Suppress("MagicNumber")
|
||||||
|
val waitingTime: Long = TimeUnit.SECONDS.toMillis(15)
|
||||||
|
val waitingTimeShort: Long = TimeUnit.SECONDS.toMillis(1)
|
||||||
|
data class TestAsset(val url: Uri, val content: String)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hosts 3 simple websites, found at androidTest/assets/pages/generic[1|2|3].html
|
||||||
|
* Returns a list of TestAsset, which can be used to navigate to each and
|
||||||
|
* assert that the correct information is being displayed.
|
||||||
|
*
|
||||||
|
* Content for these pages all follow the same pattern. See [generic1.html] for
|
||||||
|
* content implementation details.
|
||||||
|
*/
|
||||||
|
fun getGenericAssets(server: MockWebServer): List<TestAsset> {
|
||||||
|
@Suppress("MagicNumber")
|
||||||
|
return (1..3).map {
|
||||||
|
TestAsset(
|
||||||
|
server.url("pages/generic$it.html").toString().toUri()!!,
|
||||||
|
"Page content: $it"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getGenericAsset(server: MockWebServer, pageNum: Int): TestAsset {
|
||||||
|
val url = server.url("pages/generic$pageNum.html").toString().toUri()!!
|
||||||
|
val content = "Page content: $pageNum"
|
||||||
|
|
||||||
|
return TestAsset(url, content)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLoremIpsumAsset(server: MockWebServer): TestAsset {
|
||||||
|
val url = server.url("pages/lorem-ipsum.html").toString().toUri()!!
|
||||||
|
val content = "Page content: lorem ipsum"
|
||||||
|
return TestAsset(url, content)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getRefreshAsset(server: MockWebServer): TestAsset {
|
||||||
|
val url = server.url("pages/refresh.html").toString().toUri()!!
|
||||||
|
val content = "Page content: refresh"
|
||||||
|
|
||||||
|
return TestAsset(url, content)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getUUIDPage(server: MockWebServer): TestAsset {
|
||||||
|
val url = server.url("pages/basic_nav_uuid.html").toString().toUri()!!
|
||||||
|
val content = "Page content: basic_nav_uuid"
|
||||||
|
|
||||||
|
return TestAsset(url, content)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/* 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.ext
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import java.net.URI
|
||||||
|
import java.net.URISyntaxException
|
||||||
|
|
||||||
|
// Extension functions for the String class
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If this string starts with the one or more of the given [prefixes] (in order and ignoring case),
|
||||||
|
* returns a copy of this string with the prefixes removed. Otherwise, returns this string.
|
||||||
|
*/
|
||||||
|
fun String.removePrefixesIgnoreCase(vararg prefixes: String): String {
|
||||||
|
var value = this
|
||||||
|
var lower = this.toLowerCase()
|
||||||
|
|
||||||
|
prefixes.forEach {
|
||||||
|
if (lower.startsWith(it.toLowerCase())) {
|
||||||
|
value = value.substring(it.length)
|
||||||
|
lower = lower.substring(it.length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
fun String?.toUri(): Uri? = if (this == null) {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
Uri.parse(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun String?.toJavaURI(): URI? = if (this == null) {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
URI(this)
|
||||||
|
} catch (e: URISyntaxException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue