aboutsummaryrefslogtreecommitdiff
path: root/app/src/androidTest/kotlin/com/pitchedapps/frost/activities/ImageActivityTest.kt
blob: 57441a623ca37ed9a2bad8896d1f4524a2890545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * 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 ca.allanwang.kau.utils.isVisible
import com.pitchedapps.frost.FrostTestRule
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.internal.closeQuietly
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import okio.Buffer
import okio.source
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.rules.Timeout
import org.junit.runner.RunWith
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

@RunWith(AndroidJUnit4::class)
class ImageActivityTest {

    val activity: ActivityTestRule<ImageActivity> =
        ActivityTestRule(ImageActivity::class.java, true, false)

    @get:Rule
    val rule: TestRule = RuleChain.outerRule(FrostTestRule()).around(activity)

    @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)
    }

    lateinit var mockServer: MockWebServer

    @Before
    fun before() {
        mockServer = mockServer()
    }

    @After
    fun after() {
        mockServer.closeQuietly()
    }

    private fun mockServer(): MockWebServer {
        val img = Buffer()
        img.writeAll(getResource("bayer-pattern.jpg").source())
        return MockWebServer().apply {
            dispatcher = object : Dispatcher() {
                override fun dispatch(request: RecordedRequest): MockResponse =
                    when {
                        request.path?.contains("text") == true -> MockResponse().setResponseCode(200).setBody(
                            "Valid mock text response"
                        )
                        request.path?.contains("image") == true -> MockResponse().setResponseCode(
                            200
                        ).setBody(
                            img
                        )
                        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(
//                FabStates.DOWNLOAD,
//                fabAction,
//                "Image should be successful, image should be downloaded"
//            )
            assertFalse(binding.error.isVisible, "Error should not be shown")
            val tempFile = assertNotNull(tempFile, "Temp file not created")
            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")
            assertTrue(binding.error.isVisible, "Error should be shown")

//            assertEquals(
//                FabStates.ERROR,
//                fabAction,
//                "Text should not be a valid image format, error state expected"
//            )
            assertEquals("Image format not supported", errorRef?.message, "Error message mismatch")
            assertFalse(tempFile?.exists() == true, "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")
            assertTrue(binding.error.isVisible, "Error should be shown")
//            assertEquals(FabStates.ERROR, fabAction, "Error response code, error state expected")
            assertEquals(
                "Unsuccessful response for image: Error mock response",
                errorRef?.message,
                "Error message mismatch"
            )
            assertFalse(tempFile?.exists() == true, "Temp file should have been removed")
        }
    }
}