Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #151 from MaximeZmt/laurislopata/MockDB-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
laurislopata authored Apr 6, 2022
2 parents 0694769 + 551cfad commit 0b21685
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 127 deletions.
22 changes: 18 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'org.jetbrains.kotlin.android'
id 'jacoco'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}

jacoco {
Expand All @@ -29,7 +30,8 @@ android {
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "ch.sdp.vibester.CustomTestRunner"
}

buildTypes {
Expand All @@ -53,6 +55,11 @@ android {
unitTests {
includeAndroidResources = true
}
packagingOptions {
jniLibs {
useLegacyPackaging true
}
}
}
buildFeatures {
viewBinding true
Expand Down Expand Up @@ -87,7 +94,6 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
implementation platform('com.google.firebase:firebase-bom:29.1.0')
implementation 'com.google.dagger:dagger:2.38.1'
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
Expand All @@ -97,7 +103,16 @@ dependencies {
exclude group: 'com.android.support'
})
kapt 'com.github.bumptech.glide:compiler:4.13.0'
kapt 'com.google.dagger:dagger-compiler:2.28.3'

androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'

implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-compiler:2.38.1"

testImplementation "io.mockk:mockk:1.12.3"
androidTestImplementation "io.mockk:mockk-android:1.12.3"

}

tasks.withType(Test) {
Expand All @@ -118,7 +133,6 @@ task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'crea
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
// Exclude Hilt generated classes
'**/*Hilt*.*',
'hilt_aggregated_deps/**',
'**/*_Factory.class',
Expand Down
14 changes: 14 additions & 0 deletions app/src/androidTest/java/ch/sdp/vibester/CustomTestRunner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ch.sdp.vibester

import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner
import dagger.hilt.android.testing.HiltTestApplication

// A custom runner to set up the instrumented application class for tests.
class CustomTestRunner : AndroidJUnitRunner() {

override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ch.sdp.vibester.activity

import android.app.Activity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.closeSoftKeyboard
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.core.internal.deps.guava.base.Joiner.on
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
Expand All @@ -14,12 +16,25 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import ch.sdp.vibester.R
import ch.sdp.vibester.auth.FireBaseAuthenticator
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.gms.tasks.Task
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.FirebaseUser
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
//import io.mockk.mockk
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.mock
import java.util.concurrent.Executor
import kotlin.random.Random


Expand All @@ -29,11 +44,42 @@ import kotlin.random.Random
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class AuthenticationActivityTest {
private val sleepTime: Long = 5000

@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)

@get:Rule(order = 1)
val testRule = ActivityScenarioRule(
AuthenticationActivity::class.java
)

@BindValue @JvmField
val mockAuthenticator = mockk<FireBaseAuthenticator>()

private fun createMockTask(succesful: Boolean): Task<AuthResult> {
val taskResult = mockk<Task<AuthResult>>()

every {taskResult.isSuccessful} returns succesful
every {taskResult.addOnCompleteListener(any<Activity>(), any<OnCompleteListener<AuthResult>>())} answers {
secondArg<OnCompleteListener<AuthResult>>().onComplete(taskResult)
taskResult
}

return taskResult
}

private fun createMockUser(email: String): FirebaseUser {
val mockUser = mockk<FirebaseUser>()
every { mockUser.email } returns email
return mockUser
}

@Before
fun setUp() {
hiltRule.inject()
Intents.init()
}

Expand All @@ -48,31 +94,38 @@ class AuthenticationActivityTest {
assertEquals("ch.sdp.vibester", appContext.packageName)
}

@get:Rule
val testRule = ActivityScenarioRule(
AuthenticationActivity::class.java
)


@Test
fun logInIncorrect() {
val username = "[email protected]"
val password = "password"

val mockTask = createMockTask(false)
every { mockAuthenticator.signIn(username, password) } returns mockTask

onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.logIn)).perform(click())
Thread.sleep(sleepTime)
Thread.sleep(1000)
onView(withId(R.id.email)).check(matches(withText("Authentication error")))
}

@Test
fun createAccountIncorrect() {
val username = "[email protected]"
val password = "password"

val mockTask = createMockTask(false)
every { mockAuthenticator.createAccount(username, password) } returns mockTask

onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.createAcc)).perform(click())
Thread.sleep(sleepTime)

onView(withId(R.id.email)).check(matches(withText("Authentication error")))


}

@Test
Expand All @@ -91,57 +144,76 @@ class AuthenticationActivityTest {
fun stringValidationWrongEmail() {
val username = "john"
val password = "password"

onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.createAcc)).perform(click())

onView(withId(R.id.email)).check(matches(withText("Not an email")))
}

@Test
fun stringValidationShorPassword() {
val username = "[email protected]"
val password = "12345"

onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.createAcc)).perform(click())

onView(withId(R.id.email)).check(matches(withText("Password has to be at least 6 symbols")))
}

@Test
fun derinTest() {
val a = FireBaseAuthenticator()

a.googleActivityResult(-1, -1, null)

onView(withId(R.id.email)).check(matches(withText("TextView")))
}

@Test
fun ardaTest() {
val a = FireBaseAuthenticator()

a.googleActivityResult(1000, -1, null)

onView(withId(R.id.email)).check(matches(withText("TextView")))
}

@Test
fun logInCorrect() {
val username = "lisa@test.com"
val username = "mockUsername@test.com"
val password = "password"

val mockTask = createMockTask(true)
val mockUser = createMockUser(username)
every { mockAuthenticator.signIn(username, password) } returns mockTask
every { mockAuthenticator.getCurrUser()} returns mockUser

onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.logIn)).perform(click())
Thread.sleep(sleepTime)

Intents.intended(IntentMatchers.hasComponent(ProfileActivity::class.java.name))
Intents.intended(IntentMatchers.hasExtra("email", username))
}

@Test
fun createAccountCorrect() {
val randomInt = Random.nextInt(0, 10000)
val username = "[email protected]"
val password = "password"
val username = randomInt.toString().plus("@gg.com")

val mockTask = createMockTask(true)
val mockUser = createMockUser(username)
every { mockAuthenticator.createAccount(username, password) } returns mockTask
every { mockAuthenticator.getCurrUser()} returns mockUser

onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard())
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard())
onView(withId(R.id.createAcc)).perform(click())
Thread.sleep(sleepTime)

Intents.intended(IntentMatchers.hasComponent(ProfileActivity::class.java.name))
Intents.intended(IntentMatchers.hasExtra("email", username))
}
Expand Down
Loading

0 comments on commit 0b21685

Please sign in to comment.