diff --git a/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivityTest.kt b/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivityTest.kt index da13ca8..62af7b7 100644 --- a/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivityTest.kt +++ b/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivityTest.kt @@ -32,7 +32,7 @@ class LoginActivityTest { onView(withId(R.id.button)) .perform(click()) - onView(withText("Riempire tutti i campi")) + onView(withText(R.string.login_empty_field)) .inRoot(withDecorView(not(activityRule.activity.window.decorView))) .check(matches(isDisplayed())) } @@ -40,13 +40,14 @@ class LoginActivityTest { @Test fun loginWithEmptyUsername() { onView(withId(R.id.instanceServer)) - .perform(typeText("instanceServer")) + .perform(typeText("https://wekan.com")) onView(withId(R.id.password)) .perform(typeText("password")) closeSoftKeyboard() onView(withId(R.id.button)) .perform(click()) - onView(withText("Riempire tutti i campi")) + + onView(withText(R.string.login_empty_field)) .inRoot(withDecorView(not(activityRule.activity.window.decorView))) .check(matches(isDisplayed())) @@ -55,15 +56,32 @@ class LoginActivityTest { @Test fun loginWithEmptyPassword() { onView(withId(R.id.instanceServer)) - .perform(typeText("instanceServer")) + .perform(typeText("https://wekan.com")) onView(withId(R.id.username)) .perform(typeText("username")) closeSoftKeyboard() onView(withId(R.id.button)) .perform(click()) - onView(withText("Riempire tutti i campi")) + + onView(withText(R.string.login_empty_field)) + .inRoot(withDecorView(not(activityRule.activity.window.decorView))) + .check(matches(isDisplayed())) + } + + @Test + fun loginWithUnformedInstance(){ + onView(withId(R.id.instanceServer)) + .perform(typeText("not an URL")) + onView(withId(R.id.username)) + .perform(typeText("username")) + onView(withId(R.id.password)) + .perform(typeText("password")) + closeSoftKeyboard() + onView(withId(R.id.button)) + .perform(click()) + + onView(withText(R.string.login_unformed_instance)) .inRoot(withDecorView(not(activityRule.activity.window.decorView))) .check(matches(isDisplayed())) } - } \ No newline at end of file diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivity.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivity.kt index 8e92d4f..811db1a 100644 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivity.kt +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivity.kt @@ -3,6 +3,7 @@ package it.unisannio.ding.ids.wedroid.app.view import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View +import android.webkit.URLUtil import android.widget.Toast import it.unisannio.ding.ids.wedroid.app.R import it.unisannio.ding.ids.wedroid.app.util.SharedPreferenceHelper @@ -32,17 +33,24 @@ class LoginActivity : AppCompatActivity() { fun loginButton(v: View) { if (username.text.isBlank() || instanceServer.text.isBlank() || password.text.isBlank()) { - Toast.makeText(this, "Riempire tutti i campi", Toast.LENGTH_LONG) + Toast.makeText(this, R.string.login_empty_field, Toast.LENGTH_LONG) .show() return } val userNameText = username.text.toString() val passwordText = password.text.toString() + val instanceServerText = instanceServer.text.toString() + + if (!URLUtil.isValidUrl(instanceServerText)){ + Toast.makeText(this, R.string.login_unformed_instance, Toast.LENGTH_LONG) + .show() + return + } val service = Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) - .baseUrl(instanceServer.text.toString()) + .baseUrl(instanceServerText) .build() .create(LoginService::class.java) @@ -50,7 +58,7 @@ class LoginActivity : AppCompatActivity() { override fun onFailure(call: Call, t: Throwable) { Toast.makeText( applicationContext, - "Controlla la tua connessione internet", + R.string.login_network_error, Toast.LENGTH_LONG ).show() } @@ -60,7 +68,7 @@ class LoginActivity : AppCompatActivity() { if (response.code() != 200) { Toast.makeText( applicationContext, - "Credenziali non corrette", + R.string.login_wrong_field, Toast.LENGTH_LONG ).show() return @@ -72,7 +80,7 @@ class LoginActivity : AppCompatActivity() { Toast.makeText( applicationContext, - "Login effettuato con successo", + R.string.login_success, Toast.LENGTH_LONG ).show() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4cb76fa..c916639 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -24,4 +24,9 @@ Relax Corteza + Riempire tutti i campi + Formato URL non valido + Controlla la tua connessione internet + Credenziali non corrette + Login effettuato con successo