From 0d50103c36bd3eb0db5aac266511d82959f3df26 Mon Sep 17 00:00:00 2001 From: "alesnim@gmail.com" Date: Thu, 1 Oct 2020 18:31:35 +0300 Subject: [PATCH] add some test for activity --- .idea/gradle.xml | 3 +- .idea/misc.xml | 7 +- app/build.gradle | 5 + .../java/dev/ugwulo/lecturenote/AuthTest.java | 95 +++++++++++++++++++ .../lecturenote/ExampleInstrumentedTest.java | 23 ++++- .../ugwulo/lecturenote/OnBoardingTest.java | 95 +++++++++++++++++++ app/src/main/res/layout/activity_auth.xml | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 8 files changed, 226 insertions(+), 8 deletions(-) create mode 100644 app/src/androidTest/java/dev/ugwulo/lecturenote/AuthTest.java create mode 100644 app/src/androidTest/java/dev/ugwulo/lecturenote/OnBoardingTest.java diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 72fd0c6..be4783f 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -5,7 +5,7 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index 7bfef59..273b6f6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,11 @@ - + + + + + + diff --git a/app/build.gradle b/app/build.gradle index 8914d73..cafe3b7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,6 +43,11 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' +// adding lib for tests + androidTestImplementation ("com.android.support.test.espresso:espresso-intents:3.1.1") + + + // firebase implementation 'com.google.firebase:firebase-database:19.3.1' diff --git a/app/src/androidTest/java/dev/ugwulo/lecturenote/AuthTest.java b/app/src/androidTest/java/dev/ugwulo/lecturenote/AuthTest.java new file mode 100644 index 0000000..42f6b75 --- /dev/null +++ b/app/src/androidTest/java/dev/ugwulo/lecturenote/AuthTest.java @@ -0,0 +1,95 @@ +package dev.ugwulo.lecturenote; + + +import android.app.Activity; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.support.test.InstrumentationRegistry; + +import androidx.test.espresso.Espresso; +import androidx.test.espresso.action.ViewActions; +import androidx.test.espresso.intent.Intents; +import androidx.test.espresso.intent.rule.IntentsTestRule; +import androidx.test.ext.junit.rules.ActivityScenarioRule; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.rule.ActivityTestRule; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Set; + +import dev.ugwulo.lecturenote.util.Settings; +import dev.ugwulo.lecturenote.view.AuthActivity; +import dev.ugwulo.lecturenote.view.MainActivity; +import dev.ugwulo.lecturenote.view.OnboardingActivity; +import dev.ugwulo.lecturenote.view.home.HomeFragment; + +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.action.ViewActions.click; +import static androidx.test.espresso.action.ViewActions.typeText; +import static androidx.test.espresso.intent.Intents.intended; +import static androidx.test.espresso.intent.matcher.ComponentNameMatchers.hasClassName; +import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; +import static org.junit.Assert.assertTrue; + + +@RunWith(AndroidJUnit4.class) +public class AuthTest { + + private String email = "test2@test.com"; + private String password = "qwerty"; + + @Rule + public ActivityTestRule activityScenarioRule = + new ActivityTestRule<>(AuthActivity.class, true, false); + + @Before + public void recreateActivity() { + /*onView(withId(R.id.edit_reg_num)).perform(typeText("18EH/0059/CS")); + onView(withId(R.id.btn_verify)).perform(ViewActions.click());*/ + activityScenarioRule.getActivity().startActivity(new Intent()); + Intents.init(); + } + + + @Test + public void loginSuccess() { + Settings.init(getInstrumentation().getContext()); + Settings.setLecturerLogin(false); + Settings.setLoggedInSharedPref(false); + onView(withId(R.id.email)).perform(typeText(email)); + onView(withId(R.id.password)).perform(typeText(password)); + onView(withId(R.id.btn_sign_in)).perform(click()); + intended(hasComponent(hasClassName(HomeFragment.class.getName()))); + + + + } + + @After + public void releaseIntents() { + /*Activity activity = activityScenarioRule.getActivity(); + SharedPreferences prefs = activity.getSharedPreferences("lecture_note", Context.MODE_PRIVATE); + prefs.edit().putBoolean("loggedin", false).apply(); + assertTrue(prefs.getBoolean("loggedin", false)); + + + prefs.edit().putBoolean("isLecturerLogin", false).apply(); + assertTrue(prefs.getBoolean("isLecturerLogin", false));*/ + Settings.init(getInstrumentation().getContext()); + Settings.setLecturerLogin(false); + Settings.setLoggedInSharedPref(false); + + } + + +} diff --git a/app/src/androidTest/java/dev/ugwulo/lecturenote/ExampleInstrumentedTest.java b/app/src/androidTest/java/dev/ugwulo/lecturenote/ExampleInstrumentedTest.java index a312fd6..a4f9b10 100644 --- a/app/src/androidTest/java/dev/ugwulo/lecturenote/ExampleInstrumentedTest.java +++ b/app/src/androidTest/java/dev/ugwulo/lecturenote/ExampleInstrumentedTest.java @@ -1,14 +1,30 @@ package dev.ugwulo.lecturenote; import android.content.Context; +import android.util.Log; +import android.view.KeyEvent; -import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.espresso.ViewAction; +import androidx.test.espresso.action.ViewActions; +import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import static org.junit.Assert.*; +import dev.ugwulo.lecturenote.util.Settings; +import dev.ugwulo.lecturenote.view.OnboardingActivity; + +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.action.ViewActions.pressKey; +import static androidx.test.espresso.action.ViewActions.typeText; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Instrumented test, which will execute on an Android device. @@ -17,6 +33,7 @@ */ @RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { + @Test public void useAppContext() { // Context of the app under test. @@ -24,4 +41,6 @@ public void useAppContext() { assertEquals("dev.ugwulo.lecturenote", appContext.getPackageName()); } + + } diff --git a/app/src/androidTest/java/dev/ugwulo/lecturenote/OnBoardingTest.java b/app/src/androidTest/java/dev/ugwulo/lecturenote/OnBoardingTest.java new file mode 100644 index 0000000..a34d30e --- /dev/null +++ b/app/src/androidTest/java/dev/ugwulo/lecturenote/OnBoardingTest.java @@ -0,0 +1,95 @@ +package dev.ugwulo.lecturenote; + + +import android.content.Context; + +import androidx.test.espresso.action.ViewActions; +import androidx.test.ext.junit.rules.ActivityScenarioRule; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import dev.ugwulo.lecturenote.util.Settings; +import dev.ugwulo.lecturenote.view.OnboardingActivity; + +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.action.ViewActions.typeText; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + + + +/** + Simple test for activity with espresso + */ +@RunWith(AndroidJUnit4.class) +public class OnBoardingTest { + boolean flag = true; + + + @Rule + public ActivityScenarioRule activityScenarioRule = + new ActivityScenarioRule<>(OnboardingActivity.class); + + + @Before + public void restartActivity() { + activityScenarioRule.getScenario().recreate(); + } + + + @Test + public void loginAsLecturer() { + onView(withId(R.id.edit_reg_num)).perform(typeText("18EH/0059/CS")); + onView(withId(R.id.btn_verify)).perform(ViewActions.click()); + assertTrue(Settings.isFirstTimeLaunch()); + Settings.setIsFirstTimeLaunch(false); + flag = !flag; + + } + + + @Test + public void loginAsStudent() { + onView(withId(R.id.edit_staff_id)).perform(typeText("ID-2020-CS")); + onView(withId(R.id.btn_verify)).perform(ViewActions.click()); + assertTrue(Settings.isFirstTimeLaunch()); + + + } + + + @After + public void afterLoginAsLecturer() { + if (flag) { + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + Settings.init(appContext.getApplicationContext()); + assertTrue(Settings.isLecturerLogin()); + assertFalse(Settings.isStudentLogin()); + } + } + + + @After + public void afterLoginAsStudent() { + if (!flag) { + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + Settings.init(appContext.getApplicationContext()); + assertTrue(Settings.isStudentLogin()); + assertFalse(Settings.isLecturerLogin()); + } + } + + @After + public void afterSetFirstTimeStatus() { + Settings.setIsFirstTimeLaunch(false); + } + + +} diff --git a/app/src/main/res/layout/activity_auth.xml b/app/src/main/res/layout/activity_auth.xml index 632c4fe..6031bd2 100644 --- a/app/src/main/res/layout/activity_auth.xml +++ b/app/src/main/res/layout/activity_auth.xml @@ -26,7 +26,7 @@ android:layout_height="wrap_content" android:textSize="23sp" android:textColor="@color/defaultTextColor" - android:text="Welcome Back User.name,"/> + android:text="Welcome Back @{User.name},"/>