From 28195e10b2eacea6de09fc77d3b1019f27ede4b5 Mon Sep 17 00:00:00 2001 From: Kamila Babayeva Date: Mon, 9 May 2022 11:31:46 +0200 Subject: [PATCH 1/2] createProfileActivity: mock Authenticator --- .../activity/CreateProfileActivityTest.kt | 43 +++++++++++++------ .../activity/CreateProfileActivity.kt | 14 +++--- .../vibester/auth/FireBaseAuthenticator.kt | 11 +++++ 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/app/src/androidTest/java/ch/sdp/vibester/activity/CreateProfileActivityTest.kt b/app/src/androidTest/java/ch/sdp/vibester/activity/CreateProfileActivityTest.kt index 3ee5de231..722ff66b6 100644 --- a/app/src/androidTest/java/ch/sdp/vibester/activity/CreateProfileActivityTest.kt +++ b/app/src/androidTest/java/ch/sdp/vibester/activity/CreateProfileActivityTest.kt @@ -11,8 +11,10 @@ import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.ext.junit.runners.AndroidJUnit4 import ch.sdp.vibester.R import ch.sdp.vibester.TestMode +import ch.sdp.vibester.auth.FireBaseAuthenticator import ch.sdp.vibester.database.DataGetter import ch.sdp.vibester.user.User +import com.google.firebase.auth.FirebaseUser import dagger.hilt.android.testing.BindValue import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest @@ -39,21 +41,36 @@ class CreateProfileActivityTest { } @BindValue @JvmField - val mockUsersRepo = mockk() + val mockAuthenticator = mockk() - private fun createMockInvocation(email: String) { - every {mockUsersRepo.createUser(any(), any(), any(), any())} answers { - lastArg<(String) -> Unit>().invoke(email) - } + private fun createMockAuthenticator() { + val mockUser = createMockUser() + every { mockAuthenticator.getCurrUser() } returns mockUser + every { mockAuthenticator.getCurrUID() } returns mockUser.uid + } - every { mockUsersRepo.getUserData(any(), any()) } answers { - secondArg<(User) -> Unit>().invoke(User()) - } + private fun createMockUser(): FirebaseUser { + val email = "mockuser@gmail.com" + val uid = "mockuseruid" + val mockUser = mockk() + every { mockUser.email } returns email + every { mockUser.uid } returns uid + return mockUser + } - every { mockUsersRepo.getCurrentUser() } answers { - null - } + @BindValue @JvmField + val mockDataGetter = mockk() + private fun createMockDataGetter(email: String) { + every {mockDataGetter.createUser(any(), any(), any(), any())} answers { + lastArg<(String) -> Unit>().invoke(email) + } + every { mockDataGetter.getUserData(any(), any()) } answers {secondArg<(User) -> Unit>().invoke(User())} + every { mockDataGetter.getCurrentUser() } answers {null } + every { mockDataGetter.setSubFieldValue(any(), any(), any(), any()) } answers {} + every { mockDataGetter.updateFieldInt(any(), any(), any(), any()) } answers {} + every { mockDataGetter.setFieldValue(any(), any(), any()) } answers {} + every { mockDataGetter.updateSubFieldInt(any(), any(), any(), any(), any()) } answers {} } @After @@ -69,9 +86,9 @@ class CreateProfileActivityTest { val intent = Intent(ApplicationProvider.getApplicationContext(), CreateProfileActivity::class.java) intent.putExtra("email", mockEmail) - intent.putExtra("isUnitTest", true) - createMockInvocation(mockEmail) + createMockDataGetter(mockEmail) + createMockAuthenticator() val scn: ActivityScenario = ActivityScenario.launch(intent) diff --git a/app/src/main/java/ch/sdp/vibester/activity/CreateProfileActivity.kt b/app/src/main/java/ch/sdp/vibester/activity/CreateProfileActivity.kt index 2d128e6ae..1fcebb3b1 100644 --- a/app/src/main/java/ch/sdp/vibester/activity/CreateProfileActivity.kt +++ b/app/src/main/java/ch/sdp/vibester/activity/CreateProfileActivity.kt @@ -18,6 +18,9 @@ import javax.inject.Inject @AndroidEntryPoint class CreateProfileActivity : AppCompatActivity() { + @Inject + lateinit var authenticator: FireBaseAuthenticator + @Inject lateinit var dataGetter: DataGetter @@ -26,14 +29,10 @@ class CreateProfileActivity : AppCompatActivity() { private val REQUEST_CODE = 500 - var isUnitTest: Boolean = false - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_create_profile) - isUnitTest = intent.getBooleanExtra("isUnitTest", false) - val email = intent.getStringExtra("email").toString() val username = findViewById(R.id.accountUsername) @@ -41,9 +40,7 @@ class CreateProfileActivity : AppCompatActivity() { val btnUploadImg = findViewById