Skip to content

Commit

Permalink
CATROID-89 add test for api call and overwrite dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
ma-zea committed Mar 30, 2023
1 parent 7d50080 commit f0fee25
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ class CatroidWebServerAuthenticationTest : KoinTest {
assertEquals(responseExpireToken.code(), SERVER_RESPONSE_INVALID_UPLOAD_TOKEN)
}

@Test
fun testGetUserProjects() {
var token = sharedPreferences.getString(Constants.TOKEN, Constants.NO_TOKEN)
val loginResponse = webServer.login("Bearer $token", LoginUser(username, password))
.execute()
token = loginResponse.body()?.token
val response = webServer.getUserProjects("Bearer $token").execute()

assertEquals(response.code(), SERVER_RESPONSE_TOKEN_OK)
}

private fun parseRegisterErrorMessage(errorBody: String?) =
Moshi.Builder().build().adapter<RegisterFailedResponse>(RegisterFailedResponse::class.java).fromJson(errorBody)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@
import android.os.Bundle;
import android.preference.PreferenceManager;

import org.apache.commons.collections4.Get;
import org.catrobat.catroid.ProjectManager;
import org.catrobat.catroid.R;
import org.catrobat.catroid.common.Constants;
import org.catrobat.catroid.content.Project;
import org.catrobat.catroid.content.Scene;
import org.catrobat.catroid.content.Script;
import org.catrobat.catroid.content.Sprite;
import org.catrobat.catroid.content.StartScript;
import org.catrobat.catroid.koin.CatroidKoinHelperKt;
import org.catrobat.catroid.retrofit.models.ProjectResponse;
import org.catrobat.catroid.transfers.GetUserProjectsTask;
import org.catrobat.catroid.ui.ProjectUploadActivity;
import org.catrobat.catroid.ui.controller.ProjectUploadController;
import org.catrobat.catroid.uiespresso.util.rules.BaseActivityTestRule;
Expand All @@ -44,7 +49,18 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.koin.core.context.ContextFunctionsKt;
import org.koin.core.module.Module;
import org.koin.core.module.ModuleKt;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
Expand All @@ -53,7 +69,9 @@
import static org.catrobat.catroid.io.asynctask.ProjectSaverKt.saveProjectSerial;
import static org.catrobat.catroid.ui.ProjectUploadActivityKt.NUMBER_OF_UPLOADED_PROJECTS;
import static org.catrobat.catroid.ui.ProjectUploadActivityKt.PROJECT_DIR;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
Expand Down Expand Up @@ -229,6 +247,15 @@ public void uploadFailRatingDialogNotShowing() {
.check(doesNotExist());
}

@Test
public void uploadProjectWithAlreadyExistingName() {
activityTestRule.getActivity().addProjectName(PROJECT_NAME);
onView(withId(R.id.next))
.perform(click());

onView(withText(R.string.overwrite_text)).check(matches(isDisplayed()));
}

public static class ProjectUploadTestActivity extends ProjectUploadActivity {
@NotNull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import org.catrobat.catroid.retrofit.WebService
import org.catrobat.catroid.retrofit.models.ProjectResponse
import org.catrobat.catroid.retrofit.models.ProjectsCategoryApi
import org.catrobat.catroid.web.ServerAuthenticationConstants.SERVER_RESPONSE_REGISTER_OK
import org.catrobat.catroid.web.ServerAuthenticationConstants.SERVER_RESPONSE_TOKEN_OK
import retrofit2.Call
import retrofit2.Callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import android.view.View
import android.widget.ImageView
import android.widget.RadioGroup
import android.widget.TextView
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import com.google.android.material.textfield.TextInputLayout
Expand Down Expand Up @@ -144,6 +145,8 @@ open class ProjectUploadActivity : BaseActivity(),

private var projectNamesOfUser: MutableList<String> = mutableListOf()

private var extractProjectNamesFromResponseJob: Job? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityUploadBinding.inflate(layoutInflater)
Expand All @@ -164,12 +167,13 @@ open class ProjectUploadActivity : BaseActivity(),
.observe(this) { getUserProjectsResponse ->
getUserProjectsResponse?.let {
Log.d(TAG, "We got a response!")
for(response in getUserProjectsResponse) {
projectNamesOfUser.add(response.name)
extractProjectNamesFromResponseJob = GlobalScope.launch(Dispatchers.Main) {
for(response in getUserProjectsResponse) {
addProjectName(response.name)
}
}
Log.d(TAG, getUserProjectsResponse.toString())
} ?: run {
Log.d(TAG, "We got no response, something failed")
Log.e(TAG, "We got no response, something failed")
}
}

Expand Down Expand Up @@ -257,6 +261,7 @@ open class ProjectUploadActivity : BaseActivity(),
uploadProgressDialog?.dismiss()
}
getUserProjectsJob?.cancel()
extractProjectNamesFromResponseJob?.cancel()
super.onDestroy()
}

Expand Down Expand Up @@ -324,7 +329,7 @@ open class ProjectUploadActivity : BaseActivity(),
return
}

if (projectNamesOfUser.contains(binding.inputProjectName.toString())) {
if (checkIfProjectNameAlreadyExists(binding.inputProjectName.editText?.text.toString())) {
Log.e(TAG, "Name is not unique, show Overwrite Dialog!")
showOverwriteDialog()
}
Expand Down Expand Up @@ -501,7 +506,7 @@ open class ProjectUploadActivity : BaseActivity(),
}

val builder = TextInputDialog.Builder(this)
.setText(binding.inputProjectName.toString())
.setText(binding.inputProjectName.editText?.text.toString())
.setTextWatcher(textWatcher)
.setPositiveButton(
getString(R.string.ok),
Expand Down Expand Up @@ -791,6 +796,10 @@ open class ProjectUploadActivity : BaseActivity(),
}
}

fun addProjectName(name: String) {
projectNamesOfUser.add(name)
}

inner class NameInputTextWatcher : TextWatcher {
fun validateName(name: String): String? {
var name = name
Expand Down

0 comments on commit f0fee25

Please sign in to comment.