Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test build #72

Merged
merged 2 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')

// Crashlytics Kit
implementation('com.crashlytics.sdk.android:crashlytics:2.9.4@aar') {
transitive = true
Expand All @@ -68,10 +69,12 @@ dependencies {
implementation 'me.saket:better-link-movement-method:1.2'
implementation 'com.github.gabrielemariotti.changeloglib:changelog:2.1.0'
implementation 'com.github.bumptech.glide:glide:4.7.1'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.9.5'
testImplementation 'com.squareup.okhttp:mockwebserver:2.4.0'
kapt 'com.github.bumptech.glide:compiler:4.7.1'

// Testing
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-RC2"
testImplementation 'com.squareup.okhttp:mockwebserver:2.4.0'
}

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android-extensions'
35 changes: 35 additions & 0 deletions app/src/test/java/com/tristanwiley/chatse/TestUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.tristanwiley.chatse

import android.content.SharedPreferences
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever

fun getMockPrefs(): SharedPreferences {
val prefs = mock<SharedPreferences>()
val editor = object : SharedPreferences.Editor {
override fun clear() = this

override fun putLong(key: String?, value: Long) = this

override fun putInt(key: String?, value: Int) = this

override fun remove(key: String?) = this

override fun putBoolean(key: String?, value: Boolean) = this

override fun putStringSet(key: String?, values: MutableSet<String>?) = this

override fun commit() = false

override fun putFloat(key: String?, value: Float) = this

override fun apply() = Unit

override fun putString(key: String?, value: String?) = this

}

whenever(prefs.edit()).doReturn(editor)
return prefs
}
63 changes: 63 additions & 0 deletions app/src/test/java/com/tristanwiley/chatse/network/ClientTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.tristanwiley.chatse.network

import android.content.Context
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import com.squareup.okhttp.mockwebserver.MockResponse
import com.squareup.okhttp.mockwebserver.MockWebServer
import com.tristanwiley.chatse.getMockPrefs
import com.tristanwiley.chatse.network.cookie.PersistentCookieStore
import org.junit.Test
import kotlin.test.assertEquals

class ClientTest {

@Test
fun testClientCustomStore() {
val cookieStore = givenCookieStore()
val httpClient = OkHttpClient()
val client = Client(httpClient, cookieStore)

client.putCookie("http://stackexchange.com", "acct", "content")

val expected = "content"
val actual = client.getCookie("http://stackexchange.com", "acct")
assertEquals(expected, actual)
}

@Test
fun testClientGetFromWeb() {
val server = MockWebServer()
server.enqueue(MockResponse().addHeader("Set-Cookie", "acct=content"))
server.start()

val cookieStore = givenCookieStore()
val httpClient = OkHttpClient()
val client = Client(httpClient, cookieStore)

val request = Request.Builder().apply {
url(server.getUrl("/"))
}.build()

client.newCall(request).execute()

val expected = "content"
val actual = client.getCookie(server.getUrl("/").toURI(), "acct")
assertEquals(expected, actual)

}

private fun givenCookieStore(): PersistentCookieStore {
val sharedPefs = getMockPrefs()
val context = mock<Context>()

whenever(context.getSharedPreferences(any(), any())).doReturn(sharedPefs)

return PersistentCookieStore(context)
}

}
68 changes: 0 additions & 68 deletions app/src/test/java/me/shreyasr/chatse/TestUtils.java

This file was deleted.

61 changes: 0 additions & 61 deletions app/src/test/java/me/shreyasr/chatse/network/ClientTest.java

This file was deleted.