aboutsummaryrefslogtreecommitdiff
path: root/app/src/androidTest
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-25 22:17:14 -0500
committerGitHub <noreply@github.com>2018-12-25 22:17:14 -0500
commit9fb5d8a3d21eea3501740d01a254c7ce51e2a9af (patch)
treefb01f3a4c2e17236bd229f7846b8f813e94c2a78 /app/src/androidTest
parent697d01882ba8b1dbb85484ba3bf6e810e32448fc (diff)
parent49a67bc7c6d0ea38c88d8b424a2f188941dc609e (diff)
downloadfrost-9fb5d8a3d21eea3501740d01a254c7ce51e2a9af.tar.gz
frost-9fb5d8a3d21eea3501740d01a254c7ce51e2a9af.tar.bz2
frost-9fb5d8a3d21eea3501740d01a254c7ce51e2a9af.zip
Merge pull request #1260 from AllanWang/update/coroutines
Update/coroutines
Diffstat (limited to 'app/src/androidTest')
-rw-r--r--app/src/androidTest/kotlin/com/pitchedapps/frost/activities/ImageActivityTest.kt124
-rw-r--r--app/src/androidTest/kotlin/com/pitchedapps/frost/helper/Helper.kt32
-rw-r--r--app/src/androidTest/resources/bayer-pattern.jpgbin0 -> 6195 bytes
-rw-r--r--app/src/androidTest/resources/magenta.pngbin0 -> 165 bytes
4 files changed, 156 insertions, 0 deletions
diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/activities/ImageActivityTest.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/activities/ImageActivityTest.kt
new file mode 100644
index 00000000..23f6dab9
--- /dev/null
+++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/activities/ImageActivityTest.kt
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.pitchedapps.frost.activities
+
+import android.content.Intent
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.rule.ActivityTestRule
+import com.pitchedapps.frost.helper.getResource
+import com.pitchedapps.frost.utils.ARG_COOKIE
+import com.pitchedapps.frost.utils.ARG_IMAGE_URL
+import com.pitchedapps.frost.utils.ARG_TEXT
+import com.pitchedapps.frost.utils.isIndirectImageUrl
+import okhttp3.mockwebserver.Dispatcher
+import okhttp3.mockwebserver.MockResponse
+import okhttp3.mockwebserver.MockWebServer
+import okhttp3.mockwebserver.RecordedRequest
+import okio.Buffer
+import okio.Okio
+import org.junit.Rule
+import org.junit.Test
+import org.junit.rules.Timeout
+import org.junit.runner.RunWith
+import kotlin.test.assertEquals
+import kotlin.test.assertFalse
+import kotlin.test.assertNull
+import kotlin.test.assertTrue
+
+@RunWith(AndroidJUnit4::class)
+class ImageActivityTest {
+
+ @get:Rule
+ val activity: ActivityTestRule<ImageActivity> = ActivityTestRule(ImageActivity::class.java, true, false)
+
+ @get:Rule
+ val globalTimeout: Timeout = Timeout.seconds(15)
+
+ private fun launchActivity(imageUrl: String, text: String? = null, cookie: String? = null) {
+ assertFalse(
+ imageUrl.isIndirectImageUrl,
+ "For simplicity, urls that are direct will be used without modifications in the production code."
+ )
+ val intent = Intent().apply {
+ putExtra(ARG_IMAGE_URL, imageUrl)
+ putExtra(ARG_TEXT, text)
+ putExtra(ARG_COOKIE, cookie)
+ }
+ activity.launchActivity(intent)
+ }
+
+ private val mockServer: MockWebServer by lazy {
+ val magentaImg = Buffer()
+ magentaImg.writeAll(Okio.source(getResource("bayer-pattern.jpg")))
+ MockWebServer().apply {
+ setDispatcher(object : Dispatcher() {
+ override fun dispatch(request: RecordedRequest): MockResponse =
+ when {
+ request.path.contains("text") -> MockResponse().setResponseCode(200).setBody("Valid mock text response")
+ request.path.contains("image") -> MockResponse().setResponseCode(200).setBody(magentaImg)
+ else -> MockResponse().setResponseCode(404).setBody("Error mock response")
+ }
+ })
+ start()
+ }
+ }
+
+ @Test
+ fun validImageTest() {
+ launchActivity(mockServer.url("image").toString())
+ mockServer.takeRequest()
+ with(activity.activity) {
+ assertEquals(1, mockServer.requestCount, "One http request expected")
+ assertEquals(fabAction, FabStates.DOWNLOAD, "Image should be successful, image should be downloaded")
+ assertTrue(tempFile.exists(), "Image should be located at temp file")
+ assertTrue(
+ System.currentTimeMillis() - tempFile.lastModified() < 2000L,
+ "Image should have been modified within the last few seconds"
+ )
+ assertNull(errorRef, "No error should exist")
+ tempFile.delete()
+ }
+ }
+
+ @Test
+ fun invalidImageTest() {
+ launchActivity(mockServer.url("text").toString())
+ mockServer.takeRequest()
+ with(activity.activity) {
+ assertEquals(1, mockServer.requestCount, "One http request expected")
+ assertEquals(fabAction, FabStates.ERROR, "Text should not be a valid image format, error state expected")
+ assertEquals("Image format not supported", errorRef?.message, "Error message mismatch")
+ assertFalse(tempFile.exists(), "Temp file should have been removed")
+ }
+ }
+
+ @Test
+ fun errorTest() {
+ launchActivity(mockServer.url("error").toString())
+ mockServer.takeRequest()
+ with(activity.activity) {
+ assertEquals(1, mockServer.requestCount, "One http request expected")
+ assertEquals(fabAction, FabStates.ERROR, "Error response code, error state expected")
+ assertEquals(
+ "Unsuccessful response for image: Error mock response",
+ errorRef?.message,
+ "Error message mismatch"
+ )
+ assertFalse(tempFile.exists(), "Temp file should have been removed")
+ }
+ }
+}
diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/helper/Helper.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/helper/Helper.kt
new file mode 100644
index 00000000..f7484cb3
--- /dev/null
+++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/helper/Helper.kt
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.pitchedapps.frost.helper
+
+import android.content.Context
+import androidx.test.platform.app.InstrumentationRegistry
+import java.io.InputStream
+
+val context: Context
+ get() = InstrumentationRegistry.getInstrumentation().targetContext
+
+fun getAsset(asset: String): InputStream =
+ context.assets.open(asset)
+
+private class Helper
+
+fun getResource(resource: String): InputStream =
+ Helper::class.java.classLoader!!.getResource(resource).openStream()
diff --git a/app/src/androidTest/resources/bayer-pattern.jpg b/app/src/androidTest/resources/bayer-pattern.jpg
new file mode 100644
index 00000000..672ac178
--- /dev/null
+++ b/app/src/androidTest/resources/bayer-pattern.jpg
Binary files differ
diff --git a/app/src/androidTest/resources/magenta.png b/app/src/androidTest/resources/magenta.png
new file mode 100644
index 00000000..14afbce8
--- /dev/null
+++ b/app/src/androidTest/resources/magenta.png
Binary files differ