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

Laurislopata/edit profile #82

Merged
merged 11 commits into from
Mar 17, 2022
66 changes: 55 additions & 11 deletions app/src/androidTest/java/ch/sdp/vibester/SignInActivityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,83 @@ class SignInActivityTest {
Register::class.java
)


@Test
fun logInCorrect() {
val username = "john@test.com"
fun logInIncorrect() {
val username = "johnyyy@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(3_000)
onView(withId(R.id.email)).check(matches(withText("[email protected]")))
onView(withId(R.id.email)).check(matches(withText("Authentication error")))
}

@Test
fun logInIncorrect() {
val username = "johnyyy@test.com"
fun createAccountIncorrect() {
val username = "john@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())
onView(withId(R.id.createAcc)).perform(click())
Thread.sleep(3_000)
onView(withId(R.id.email)).check(matches(withText("Authentication error")))
}

@Test
fun createAccountIncorrect() {
val username = "[email protected]"
fun stringValidationEmptyUsername() {
onView(withId(R.id.createAcc)).perform(click())
onView(withId(R.id.email)).check(matches(withText("Empty email or password")))
}

@Test
fun stringValidationEmptyUPassword() {
onView(withId(R.id.createAcc)).perform(click())
onView(withId(R.id.email)).check(matches(withText("Empty email or password")))
}

@Test
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")))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a "TextView" here, is it shown explicitly on the screen?

}

@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 = "[email protected]"
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(3_000)
onView(withId(R.id.email)).check(matches(withText("Authentication error")))
onView(withId(R.id.email)).check(matches(withText("[email protected]")))
}

@Test
Expand All @@ -85,5 +130,4 @@ class SignInActivityTest {
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,14 @@ package ch.sdp.vibester.profile
import android.content.Intent
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import ch.sdp.vibester.GreetingActivity
import ch.sdp.vibester.R
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.io.Serializable


@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -49,5 +40,59 @@ class ProfileSetupTest {
onView(withId(R.id.username)).check(matches(isDisplayed()))
// onView(withId(R.id.avatar)).check(matches(isDisplayed()))
}

@Test
fun checkEditProfile() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileSetup::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileSetup> = ActivityScenario.launch(intent)

val newUsername = "newUser"
onView(withId(R.id.editUser)).perform(ViewActions.click())
onView(withId(0)).perform(ViewActions.typeText(newUsername), ViewActions.closeSoftKeyboard())
onView(withText("OK")).perform(ViewActions.click())
onView(withId(R.id.username)).check(matches(withText(newUsername)))

}

@Test
fun checkEditProfileClickCancel() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileSetup::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileSetup> = ActivityScenario.launch(intent)

onView(withId(R.id.editUser)).perform(ViewActions.click())
onView(withText("Cancel")).perform(ViewActions.click())
onView(withId(R.id.username)).check(matches(withText("username0")))
}

@Test
fun checkEditHandle() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileSetup::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileSetup> = ActivityScenario.launch(intent)

val newUserHandle = "newHandle"
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")))

}

@Test
fun checkEditHandleClickCancel() {
val inputProfile = UserProfile("user0", "username0","bit.ly/3IUnyAF", 5, 8, 34, 2)
val intent = Intent(ApplicationProvider.getApplicationContext(), ProfileSetup::class.java)
intent.putExtra("userProfile", inputProfile)
val scn: ActivityScenario<ProfileSetup> = ActivityScenario.launch(intent)

onView(withId(R.id.editHandle)).perform(ViewActions.click())
onView(withText("Cancel")).perform(ViewActions.click())
onView(withId(R.id.handle)).check(matches(withText("user0")))
}

}
82 changes: 50 additions & 32 deletions app/src/main/java/ch/sdp/vibester/Register.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.google.firebase.auth.AuthResult

class Register : AppCompatActivity() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May add javadoc to class and methods ! Maybe some comments too


// private lateinit var googleSignInClient: GoogleSignInClient
private lateinit var googleSignInClient: GoogleSignInClient

private lateinit var authenticator: FireBaseAuthenticator

Expand All @@ -27,61 +27,79 @@ class Register : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_google_log_in)

// @FixMe
// Commenting this for now until we find a proper way to test it, then will merge it to main
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()

// val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
// .requestEmail()
// .build()

// googleSignInClient = GoogleSignIn.getClient(this, gso)

// Initialize Firebase Auth
googleSignInClient = GoogleSignIn.getClient(this, gso)
authenticator = FireBaseAuthenticator()

val btCreateAcc = findViewById<Button>(R.id.createAcc)
val btLogIn = findViewById<Button>(R.id.logIn)
// val googleSignIn = findViewById<Button>(R.id.googleBtn)

val googleSignIn = findViewById<Button>(R.id.googleBtn)

val username = findViewById<EditText>(R.id.username)
val password = findViewById<EditText>(R.id.password)
email = findViewById<TextView>(R.id.email)
email = findViewById(R.id.email)

btCreateAcc.setOnClickListener {
createAccount(username.text.toString(), password.text.toString())
authenticate(username.text.toString(), password.text.toString(), true)
}

btLogIn.setOnClickListener {
signIn(username.text.toString(), password.text.toString())
authenticate(username.text.toString(), password.text.toString(), false)
}

// googleSignIn.setOnClickListener {
// signInGoogle()
// }


googleSignIn.setOnClickListener {
signInGoogle()
}
}

public override fun onStart() {
super.onStart()
val currentUser = authenticator.auth.currentUser
if (currentUser != null) {
reload();
reload()
}
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
updateUI(authenticator.googleActivityResult(requestCode, resultCode, data));
}

private fun stringValidation(username: String, password: String): Boolean{
if(username.isEmpty() || password.isEmpty()) {
email.text = "Empty email or password"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be using string.xml

return false
}

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

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

private fun authenticate(email: String, password: String, creatAcc: Boolean) {
if(stringValidation(email, password)) {
if(creatAcc) {
createAccount(email, password)
}
else {
signIn(email, password)
}
}
}

// @FixMe
// Commenting this for now until we find a proper way to test it, then will merge it to main
// public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// super.onActivityResult(requestCode, resultCode, data)
// updateUI(authenticator.googleActivityResult(requestCode, resultCode, data));
// }

// private fun signInGoogle() {
// val intent = googleSignInClient.signInIntent
// startActivityForResult(intent, 1000)
// }
private fun signInGoogle() {
val intent = googleSignInClient.signInIntent
startActivityForResult(intent, 1000)
}

private fun createAccount(email: String, password: String) {
authenticator.createAccount(email, password)
Expand Down
27 changes: 13 additions & 14 deletions app/src/main/java/ch/sdp/vibester/auth/FireBaseAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ class FireBaseAuthenticator() {

// @FixMe
// Commenting this for now until we find a proper way to test it, then will merge it to main
// fun googleActivityResult(requestCode: Int, resultCode: Int, data: Intent?): String? {
// return if(requestCode == 1000) {
// val task = GoogleSignIn.getSignedInAccountFromIntent(data)
// try {
// val account = task.getResult(ApiException::class.java)!!
// account.email
// } catch (e: ApiException) {
// "Authentication error"
// }
// } else {
// "Authentication error"
// }
// }

fun googleActivityResult(requestCode: Int, resultCode: Int, data: Intent?): String? {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc ? javadoc ?

return if(requestCode == 1000) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe replace this magic number by a more meaningful name

val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
val account = task.getResult(ApiException::class.java)!!
account.email
} catch (e: ApiException) {
"Authentication error"
}
} else {
"Authentication error"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.sdp.vibester.profile

class ProfileDataProvider(userID: String, users: List<UserProfile> = emptyList(), scoreboard: List<Int> = emptyList()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc ?

private lateinit var user: UserProfile
private val userID:Int = userID.toInt()
private val userID: Int = userID.toInt()
private lateinit var users: List<UserProfile>
private lateinit var scoreboard: List<Int>

Expand Down Expand Up @@ -53,7 +53,7 @@ class ProfileDataProvider(userID: String, users: List<UserProfile> = emptyList()
/**
* Retrieve profile data of user.
*/
private fun getUserData():UserProfile {
private fun getUserData(): UserProfile {
return users[userID]
}

Expand Down
Loading