This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from MaximeZmt/laurislopata/MockDB-refactor
- Loading branch information
Showing
15 changed files
with
288 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/src/androidTest/java/ch/sdp/vibester/CustomTestRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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() | ||
} | ||
|
||
|
@@ -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 | ||
|
@@ -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)) | ||
} | ||
|
Oops, something went wrong.