Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main Application code
- Resource file activity_login
- <?xml version="1.0" encoding="utf-8"?>
- <layout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools">
- <data>
- <variable
- name="loginViewModel"
- type="be.ysebie.multimedlembeke_app.login.LoginViewModel" />
- </data>
- <FrameLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <androidx.constraintlayout.widget.ConstraintLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/emailLabel"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginStart="24dp"
- android:layout_marginEnd="24dp"
- android:text="@string/email"
- android:textAlignment="center"
- app:layout_constraintBottom_toTopOf="@+id/emailtextbox"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.921" />
- <EditText
- android:id="@+id/emailtextbox"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginStart="24dp"
- android:layout_marginEnd="24dp"
- android:layout_marginBottom="44dp"
- android:autofillHints="@string/emailhints"
- android:ems="10"
- android:gravity="center"
- android:hint="@string/emailhints"
- android:inputType="textEmailAddress"
- app:layout_constraintBottom_toTopOf="@+id/passwordlabel"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintStart_toStartOf="parent"
- tools:targetApi="o" />
- <TextView
- android:id="@+id/passwordlabel"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginStart="24dp"
- android:layout_marginEnd="24dp"
- android:layout_marginBottom="24dp"
- android:text="@string/wachtwoord"
- android:textAlignment="center"
- app:layout_constraintBottom_toTopOf="@+id/passwordTextBox"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintStart_toStartOf="parent" />
- <EditText
- android:id="@+id/passwordTextBox"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginStart="24dp"
- android:layout_marginEnd="24dp"
- android:layout_marginBottom="56dp"
- android:autofillHints="@string/wachtwoordhints"
- android:ems="10"
- android:gravity="center"
- android:hint="@string/wachtwoordhints"
- android:inputType="textPassword"
- app:layout_constraintBottom_toTopOf="@+id/loginbutton"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="1.0"
- app:layout_constraintStart_toStartOf="parent"
- tools:targetApi="o" />
- <Button
- android:id="@+id/loginbutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="22dp"
- android:autofillHints="Druk op die knop om u in te loggen."
- android:text="@string/login"
- android:textAlignment="center"
- android:textColor="#FFFFFF"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.499"
- app:layout_constraintStart_toStartOf="parent"
- tools:targetApi="o" />
- <TextView
- android:id="@+id/errorTextLogin"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="24dp"
- android:layout_marginTop="80dp"
- android:layout_marginEnd="24dp"
- android:layout_marginBottom="6dp"
- android:gravity="center"
- android:text="@string/ongeldigheidsstring"
- android:textColor="#FF0000"
- android:visibility="invisible"
- app:layout_constraintBottom_toTopOf="@+id/loginbutton"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.492"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="@+id/passwordTextBox"
- app:layout_constraintVertical_bias="0.0" />
- <TextView
- android:id="@+id/Welkomtekst"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginStart="24dp"
- android:layout_marginEnd="24dp"
- android:text="@string/welkom"
- android:textAlignment="center"
- android:textSize="30sp"
- app:layout_constraintBottom_toTopOf="@+id/emailLabel"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:contentDescription="@string/multimedtext"
- app:srcCompat="@drawable/custom_progress" />
- </FrameLayout>
- </layout>
- LoginLocator.kt
- package be.ysebie.multimedlembeke_app
- import android.content.Context
- import androidx.annotation.VisibleForTesting
- import be.ysebie.multimedlembeke_app.repositories.ILoginRepository
- import be.ysebie.multimedlembeke_app.repositories.LoginRepository
- object LoginLocator {
- @Volatile
- var loginRepository: ILoginRepository?=null
- @VisibleForTesting set
- fun provideLoginRepository(context: Context):ILoginRepository{
- synchronized(this) {
- return loginRepository ?: createLoginRepository()
- }
- }
- private fun createLoginRepository(): ILoginRepository {
- return LoginRepository()
- }
- }
- Activity MainActivity changes to be added to OnCreate:
- //ophalen informatie van de gebruiker
- val sharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
- if(sharedPreferences.contains("currentUser")){
- val encodeduser : String = sharedPreferences.getString("currentUser", "") ?: ""
- try{
- expirationDate = JWTUtils.getExpiracyDate(encodeduser)
- if(expirationDate.after(Date())){
- val mEditor = sharedPreferences.edit()
- val userValue = "currentUser"
- mEditor.remove(userValue)
- val email = sharedPreferences.getString("email",null)
- val password = sharedPreferences.getString("password",null)
- if(!email.isNullOrEmpty() || !password.isNullOrEmpty()){
- val givenEmail = email ?: ""
- val givenPassword = password ?: ""
- val loginRepository = LoginRepository()
- val loginViewModel = LoginViewModel(loginRepository)
- loginViewModel.login(givenEmail, givenPassword)
- mEditor.putString(userValue, loginViewModel.authenticationKey.toString())
- } else{
- switchToLogin()
- }
- switchToLogin()
- }
- }catch(e: UnsupportedEncodingException){
- }
- }else{
- switchToLogin()
- }
- Activity MainActivity added method:
- fun switchToLogin(){
- val intent = Intent(this, LoginActivity::class.java)
- intent.putExtra("hi", "HI")
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
- startActivity(intent)
- }
- Login/LoginViewModel.kt
- package be.ysebie.multimedlembeke_app.login
- import android.app.Application
- import android.util.Log
- import androidx.lifecycle.LiveData
- import androidx.lifecycle.MutableLiveData
- import androidx.lifecycle.ViewModel
- import be.ysebie.multimedlembeke_app.database.LocalDb
- import be.ysebie.multimedlembeke_app.database.getDatabase
- import be.ysebie.multimedlembeke_app.repositories.ILoginRepository
- import be.ysebie.multimedlembeke_app.repositories.LoginRepository
- import kotlinx.coroutines.*
- import retrofit2.Response
- import java.io.Console
- class LoginViewModel(private val loginRepository: ILoginRepository) : ViewModel() {
- private var _authenticationKey = MutableLiveData<Any?>()
- val authenticationKey: LiveData<Any?>
- get() = _authenticationKey
- fun login(email: String, password: String) = runBlocking{
- _authenticationKey.value = loginRepository.login(email,password)
- }
- }
- login/LoginViewModelFactory.kt
- package be.ysebie.multimedlembeke_app.login
- import androidx.lifecycle.ViewModel
- import androidx.lifecycle.ViewModelProvider
- import be.ysebie.multimedlembeke_app.repositories.ILoginRepository
- import be.ysebie.multimedlembeke_app.repositories.LoginRepository
- import java.lang.IllegalArgumentException
- @Suppress("UNCHECKED CAST")
- class LoginViewModelFactory(private val loginRepository: ILoginRepository): ViewModelProvider.Factory {
- override fun <T : ViewModel?> create(modelClass: Class<T>): T {
- if(modelClass.isAssignableFrom(LoginViewModel::class.java)){
- return LoginViewModel(loginRepository) as T
- }
- throw IllegalArgumentException("Unknown ViewModelClass")
- }
- }
- login/LoginRepository.kt
- package be.ysebie.multimedlembeke_app.repositories
- import androidx.lifecycle.LiveData
- import androidx.lifecycle.MutableLiveData
- import be.ysebie.multimedlembeke_app.network.LoginUser
- import be.ysebie.multimedlembeke_app.network.Web
- import kotlinx.coroutines.*
- import retrofit2.Response
- interface ILoginRepository {
- suspend fun login(email: String, password: String): Any?
- }
- class LoginRepository : ILoginRepository {
- override suspend fun login(email: String, password: String):Any?{
- return withContext(Dispatchers.IO){
- val accountDealer = Web.retrofitService.getAccount(LoginUser(email, password))
- val result = accountDealer.execute()
- result.body()
- }
- }
- }
- network/WebService.kt
- import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
- import com.squareup.moshi.Moshi
- import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
- import be.ysebie.multimedlembeke_app.domain.Challenge
- import retrofit2.Call
- import retrofit2.Retrofit
- import retrofit2.converter.moshi.MoshiConverterFactory
- import retrofit2.http.Body
- import retrofit2.http.GET
- import retrofit2.http.POST
- import retrofit2.http.Query
- private const val BASE_URL="https://api.ysebie.be/api/"
- private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
- private val retrofit = Retrofit.Builder().addConverterFactory(MoshiConverterFactory.create(moshi)).addCallAdapterFactory(CoroutineCallAdapterFactory()).baseUrl(BASE_URL).build()
- interface WebService{
- //TODO Logic om te communiceren met de API
- @GET("Challenge")
- fun getTaken(@Query("employeeId") employeeId: String) : Call<Challenge>
- @POST("Account")
- fun getAccount(@Body user:LoginUser): Call<Any>
- }
- object Web{
- val retrofitService: WebService by lazy{
- retrofit.create(WebService::class.java)
- }
- androidTest code
- data/FakeLoginRepository.kt
- package be.ysebie.multimedlembeke_app.data
- import be.ysebie.multimedlembeke_app.repositories.ILoginRepository
- class FakeLoginRepository(var datasource: Any?) : ILoginRepository {
- override suspend fun login(email: String, password: String): Any? {
- return datasource
- }
- }
- LoginActivityFalseTest.kt
- import be.ysebie.multimedlembeke_app.data.FakeLoginRepository
- import org.junit.Before
- import org.junit.Test
- class LoginActivityFalseTest {
- private lateinit var badrepository: FakeLoginRepository
- @Before
- fun initRepositories(){
- badrepository = FakeLoginRepository(null)
- LoginLocator.loginRepository = badrepository
- }
- @Test
- fun falseTestAssertsSavingsAndNewActivity() {
- ActivityScenario.launch<LoginActivity>(LoginActivity::class.java)
- Espresso.onView(ViewMatchers.withId(R.id.errorTextLogin)).check(matches(
- withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE)))
- Espresso.onView(ViewMatchers.withId(R.id.emailtextbox))
- .perform(ViewActions.replaceText("abc@aol.com"))
- Espresso.onView(ViewMatchers.withId(R.id.passwordTextBox))
- .perform(ViewActions.replaceText("Nederland1-"))
- Espresso.onView(ViewMatchers.withId(R.id.loginbutton)).perform(ViewActions.click())
- Espresso.onView(ViewMatchers.withId(R.id.errorTextLogin)).check(matches(
- withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
- }
- }
- LoginActivityTrueTest.kt
- package be.ysebie.multimedlembeke_app
- import android.app.Activity
- import android.content.Context
- import android.content.Intent
- import android.content.SharedPreferences
- import android.os.Bundle
- import android.os.Looper
- import androidx.test.core.app.ActivityScenario
- import androidx.test.espresso.Espresso
- import androidx.test.espresso.Espresso.onData
- import androidx.test.espresso.Espresso.onView
- import androidx.test.espresso.action.ViewActions.click
- import androidx.test.espresso.action.ViewActions.replaceText
- import androidx.test.espresso.matcher.ViewMatchers.withId
- import androidx.test.ext.junit.runners.AndroidJUnit4
- import androidx.test.filters.MediumTest
- import be.ysebie.multimedlembeke_app.data.FakeLoginRepository
- import be.ysebie.multimedlembeke_app.repositories.LoginRepository
- import com.google.common.truth.Truth.assertThat
- import kotlinx.coroutines.ExperimentalCoroutinesApi
- import org.junit.Before
- import org.junit.Test
- import org.junit.runner.RunWith
- import java.security.AccessController.getContext
- @MediumTest
- class LoginActivityTrueTest{
- private lateinit var goodrepository: FakeLoginRepository
- @Before
- fun initRepositories(){
- goodrepository = FakeLoginRepository(mutableListOf("token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoxNTE2MjM5MDIyfQ.E9bQ6QAil4HpH825QC5PtjNGEDQTtMpcj0SO2W8vmag"))
- LoginLocator.loginRepository = goodrepository
- }
- @Test
- fun trueTestAssertsSavingsAndNewActivity() {
- ActivityScenario.launch<LoginActivity>(LoginActivity::class.java)
- onView(withId(R.id.emailtextbox)).perform(replaceText("rpullerjt@edublogs.org"))
- onView(withId(R.id.passwordTextBox)).perform(replaceText("Test123!"))
- onView(withId(R.id.loginbutton)).perform(click())
- onView(withId(R.layout.fragment_challenge_details_overview_fragment))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement