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 #130 from MaximeZmt/laurislopata/DB-refactor
Browse files Browse the repository at this point in the history
Laurislopata/db refactor
  • Loading branch information
laurislopata authored Mar 31, 2022
2 parents 597e67a + 5bca5eb commit 724dddb
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ 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.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
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 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
Expand All @@ -28,6 +32,15 @@ import kotlin.random.Random
class AuthenticationActivityTest {
private val sleepTime: Long = 5000

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

@After
fun clean() {
Intents.release()
}
@Test
fun useAppContext() {
// Context of the app under test.
Expand Down Expand Up @@ -110,13 +123,14 @@ class AuthenticationActivityTest {

@Test
fun logInCorrect() {
val username = "john@test.com"
val username = "lisa@test.com"
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.logIn)).perform(click())
Thread.sleep(sleepTime)
onView(withId(R.id.email)).check(matches(withText("[email protected]")))
Intents.intended(IntentMatchers.hasComponent(ProfileActivity::class.java.name))
Intents.intended(IntentMatchers.hasExtra("email", username))
}

@Test
Expand All @@ -128,8 +142,8 @@ class AuthenticationActivityTest {
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(randomInt.toString().plus("@gg.com"))))
Intents.intended(IntentMatchers.hasComponent(ProfileActivity::class.java.name))
Intents.intended(IntentMatchers.hasExtra("email", username))
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,40 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R
import ch.sdp.vibester.profile.UserProfile
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4::class)
class ProfileActivityTest {

private val sleepTime: Long = 4000

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

@After
fun clean() {
Intents.release()
}

@Test
fun checkProfileData() {
val inputProfile = UserProfile("@lisa", "Lalisa Bon", "bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent =
Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", "[email protected]", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("email", inputProfile.email)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
Thread.sleep(3_000)
Thread.sleep(sleepTime)
onView(withId(R.id.handle)).check(matches(withText(inputProfile.handle)))
onView(withId(R.id.username)).check(matches(withText(inputProfile.username)))
onView(withId(R.id.correctSongs)).check(matches(withText(inputProfile.correctSongs.toString())))
Expand All @@ -34,12 +49,11 @@ class ProfileActivityTest {

@Test
fun checkProfileLayout() {
val inputProfile = UserProfile("@lisa", "Lalisa Bon", "bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent =
Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", "[email protected]", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("email", inputProfile.email)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
Thread.sleep(3_000)
Thread.sleep(sleepTime)
onView(withId(R.id.profileStatistics)).check(matches(isDisplayed()))
onView(withId(R.id.handle)).check(matches(isDisplayed()))
onView(withId(R.id.username)).check(matches(isDisplayed()))
Expand All @@ -48,13 +62,12 @@ class ProfileActivityTest {

@Test
fun checkEditProfile() {
val inputProfile = UserProfile("@lisa", "Lalisa Bon", "bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent =
Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", "[email protected]", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("email", inputProfile.email)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)

val newUsername = "newUser"
val newUsername = "Lalisa Bon"
onView(withId(R.id.editUser)).perform(ViewActions.click())
onView(withId(0)).perform(
ViewActions.typeText(newUsername),
Expand All @@ -67,44 +80,41 @@ class ProfileActivityTest {

@Test
fun checkEditProfileClickCancel() {
val inputProfile = UserProfile("@lisa", "Lalisa Bon", "bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent =
Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", "[email protected]", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("email", inputProfile.email)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
Thread.sleep(3_000)
Thread.sleep(sleepTime)
onView(withId(R.id.editUser)).perform(ViewActions.click())
onView(withText("Cancel")).perform(ViewActions.click())
onView(withId(R.id.username)).check(matches(withText("Lalisa Bon")))
}

@Test
fun checkEditHandle() {
val inputProfile = UserProfile("@lisa", "Lalisa Bon", "bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent =
Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", "[email protected]", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("email", inputProfile.email)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
Thread.sleep(3_000)
val newUserHandle = "newHandle"
Thread.sleep(sleepTime)
val newUserHandle = "@lisa"
onView(withId(R.id.editHandle)).perform(ViewActions.click())
onView(withId(0)).perform(
ViewActions.typeText(newUserHandle),
ViewActions.closeSoftKeyboard()
)
onView(withText("OK")).perform(ViewActions.click())
onView(withId(R.id.handle)).check(matches(withText("newHandle")))
onView(withId(R.id.handle)).check(matches(withText("@lisa")))

}

@Test
fun checkEditHandleClickCancel() {
val inputProfile = UserProfile("@lisa", "Lalisa Bon", "bit.ly/3IUnyAF", 12, 8, 29, 0)
val intent =
Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("userProfile", inputProfile)
val inputProfile = UserProfile("@lisa", "Lalisa Bon","bit.ly/3IUnyAF", "[email protected]", 12, 8, 29, 0)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileActivity::class.java)
intent.putExtra("email", inputProfile.email)
val scn: ActivityScenario<ProfileActivity> = ActivityScenario.launch(intent)
Thread.sleep(3_000)
Thread.sleep(sleepTime)
onView(withId(R.id.editHandle)).perform(ViewActions.click())
onView(withText("Cancel")).perform(ViewActions.click())
onView(withId(R.id.handle)).check(matches(withText("@lisa")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,28 @@ class AuthenticationActivity : AppCompatActivity() {
*/
private fun stringValidation(username: String, password: String): Boolean {
if (username.isEmpty() || password.isEmpty()) {
email.text = "Empty email or password"
email.setText(R.string.emptyField)
return false
}

if (!username.contains('@')) {
email.text = "Not an email"
email.setText(R.string.notAnEmail)
return false
}

if (password.length < 6) {
email.text = "Password has to be at least 6 symbols"
email.setText(R.string.shortPassword)
return false
}
return true
}

private fun authenticate(email: String, password: String, creatAcc: Boolean) {
if (stringValidation(email, password)) {
var auth: Task<AuthResult>
if (creatAcc) {
auth = authenticator.createAccount(email, password)
//createAccount(email, password)
} else {
auth = authenticator.signIn(email, password)
//signIn(email, password)
var auth: Task<AuthResult> = if (creatAcc) {
authenticator.createAccount(email, password)
}else {
authenticator.signIn(email, password)
}
auth.addOnCompleteListener(this) { task ->
onCompleteAuthentication(task)
Expand Down Expand Up @@ -154,6 +151,15 @@ class AuthenticationActivity : AppCompatActivity() {
}

private fun updateUI(emailText: String?) {
email.text = emailText
if (emailText != null) {
if('@' in emailText) {
val newIntent = Intent(this, ProfileActivity::class.java)
newIntent.putExtra("email", emailText)
startActivity(newIntent)
}
else {
email.text = emailText
}
}
}
}
Loading

0 comments on commit 724dddb

Please sign in to comment.