From f2908ed101fd1cda3e82f1aeeb557f34a28968bc Mon Sep 17 00:00:00 2001 From: noemi3 Date: Sat, 28 Dec 2019 19:39:25 +0100 Subject: [PATCH] test login activity --- app/build.gradle | 4 ++ .../ids/wedroid/app/view/LoginActivityTest.kt | 69 +++++++++++++++++++ .../ids/wedroid/app/view/LoginActivity.kt | 6 +- app/src/main/res/layout/activity_login.xml | 2 +- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivityTest.kt diff --git a/app/build.gradle b/app/build.gradle index 6a93023..5a3cea8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -70,6 +70,10 @@ dependencies { testImplementation "com.squareup.okhttp3:mockwebserver:4.2.1" androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + androidTestImplementation 'androidx.test:runner:1.2.0' + androidTestImplementation 'androidx.test:rules:1.2.0' + + implementation "com.squareup.retrofit2:retrofit:2.6.2" implementation "com.squareup.retrofit2:converter-gson:2.6.2" 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 new file mode 100644 index 0000000..da13ca8 --- /dev/null +++ b/app/src/androidTest/java/it/unisannio/ding/ids/wedroid/app/view/LoginActivityTest.kt @@ -0,0 +1,69 @@ +package it.unisannio.ding.ids.wedroid.app.view + +import androidx.test.espresso.Espresso.closeSoftKeyboard +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.action.ViewActions.typeText +import androidx.test.espresso.assertion.ViewAssertions.matches +import androidx.test.espresso.matcher.RootMatchers.withDecorView +import androidx.test.espresso.matcher.ViewMatchers.* +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.LargeTest +import androidx.test.rule.ActivityTestRule +import it.unisannio.ding.ids.wedroid.app.R +import org.hamcrest.CoreMatchers.not +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +@LargeTest +class LoginActivityTest { + @get:Rule + val activityRule = ActivityTestRule(LoginActivity::class.java) + + @Test + fun loginWithEmptyUrl() { + 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("Riempire tutti i campi")) + .inRoot(withDecorView(not(activityRule.activity.window.decorView))) + .check(matches(isDisplayed())) + } + + @Test + fun loginWithEmptyUsername() { + onView(withId(R.id.instanceServer)) + .perform(typeText("instanceServer")) + onView(withId(R.id.password)) + .perform(typeText("password")) + closeSoftKeyboard() + onView(withId(R.id.button)) + .perform(click()) + onView(withText("Riempire tutti i campi")) + .inRoot(withDecorView(not(activityRule.activity.window.decorView))) + .check(matches(isDisplayed())) + + } + + @Test + fun loginWithEmptyPassword() { + onView(withId(R.id.instanceServer)) + .perform(typeText("instanceServer")) + onView(withId(R.id.username)) + .perform(typeText("username")) + closeSoftKeyboard() + onView(withId(R.id.button)) + .perform(click()) + onView(withText("Riempire tutti i campi")) + .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 d3a15bf..53e7c66 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 @@ -31,7 +31,7 @@ class LoginActivity : AppCompatActivity() { } fun loginButton(v: View) { - if (username.text.isBlank() || istanceServer.text.isBlank() || password.text.isBlank()) { + if (username.text.isBlank() || instanceServer.text.isBlank() || password.text.isBlank()) { Toast.makeText(this, "Riempire tutti i campi", Toast.LENGTH_LONG) .show() return @@ -43,7 +43,7 @@ class LoginActivity : AppCompatActivity() { val service = Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) - .baseUrl(istanceServer.text.toString()) + .baseUrl(instanceServer.text.toString()) .build() .create(LoginService::class.java) @@ -69,7 +69,7 @@ class LoginActivity : AppCompatActivity() { return } val users = response.body() - sph.baseUrl = istanceServer.text.toString() + sph.baseUrl = instanceServer.text.toString() sph.token = users?.token sph.userId = users?.id diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 0d5a258..1d6bf2a 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -42,7 +42,7 @@ app:layout_constraintStart_toStartOf="parent" />