Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Commit

Permalink
Add multiple collections (#90)
Browse files Browse the repository at this point in the history
* added Adapter class to handle list of collection

* add new collection works

* list of collections is scrollable

* bottom bar does not cover collections

* added padding

* fixed Friends button position

* fix bottom bar covering view (this time really pls)

* Fix collection having only one element

* changed button

* changed name to CollectionListAdapter

* add dialog to enter collection name

* refactored and handle empty and duplicate collection names

* add comments

* upload to db

* add callback to main

* update DB

* update comments

* add tests

* remove xml

* address mariem's comments

* refactor tests

* use db emulator

* fix bug

---------

Co-authored-by: Michael Greub <michael.greub@epfl.ch>
  • Loading branch information
simone-kalbermatter and MeKHell authored Mar 30, 2023
1 parent 775b719 commit b6fc53d
Show file tree
Hide file tree
Showing 18 changed files with 548 additions and 174 deletions.
21 changes: 19 additions & 2 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ dependencies {
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.5.1'
androidTestImplementation 'com.adevinta.android:barista:4.2.0'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

androidTestImplementation('com.adevinta.android:barista:4.2.0')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.junit.Assert.fail;

import android.os.Bundle;
import android.view.View;
Expand All @@ -16,6 +17,12 @@
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.matcher.BoundedMatcher;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;
import com.github.sdp.mediato.data.Database;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Before;
Expand All @@ -26,12 +33,29 @@
public class ProfileFragmentTest {

ActivityScenario<TestingActivity> scenario;
ViewInteraction outerRecyclerView = onView(withId(R.id.collection_list_recycler_view));
ViewInteraction collectionRecyclerView = onView(withId(R.id.collection_recycler_view));
ViewInteraction recyclerView = onView(withId(R.id.collection_recycler_view));
ViewInteraction userNameText = onView(withId(R.id.username_text));
ViewInteraction editButton = onView(withId(R.id.edit_button));
ViewInteraction friendsButton = onView(withId(R.id.friends_button));
ViewInteraction defaultCollection = onView(withId(R.id.collection_list));
ViewInteraction addMediaButton = onView(withId(R.id.add_media_button));
ViewInteraction addCollectionButton = onView(withId(R.id.add_collection_button));
ViewInteraction profilePic = onView(withId(R.id.profile_image));


@Before
public void setUp() {
// Launch the TestingActivity
scenario = ActivityScenario.launch(TestingActivity.class);

// Use Database emulator
try {
Database.database.useEmulator("10.0.2.2", 9000);
} catch (Exception ignored) {
}

// Set up the TestingActivity to display the ProfileFragment
scenario.onActivity(activity -> {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
Expand All @@ -49,38 +73,33 @@ public void setUp() {
// Test whether the "Friends" button is displayed and contains the correct text
@Test
public void testFriendsButton() {
ViewInteraction friendsButton = onView(withId(R.id.friends_button));
friendsButton.check(matches(isDisplayed()));
friendsButton.check(matches(withText("Friends")));
}

// Test whether the "Edit" button is displayed and contains the correct text
@Test
public void testEditButton() {
ViewInteraction editButton = onView(withId(R.id.edit_button));
editButton.check(matches(isDisplayed()));
editButton.check(matches(withText("Edit")));
}

// Test whether the profile picture is displayed
@Test
public void testProfilePicture() {
onView(withId(R.id.profile_image)).check(matches(isDisplayed()));
profilePic.check(matches(isDisplayed()));
}

// Test whether the username text is displayed and contains the correct text
@Test
public void testUsername() {
ViewInteraction userNameText = onView(withId(R.id.username_text));
userNameText.check(matches(isDisplayed()));
userNameText.check(matches(withText("myUsername")));
}

// Test the initial state of the default collection after profile creation
@Test
public void testInitialDefaultCollectionState() {
ViewInteraction defaultCollection = onView(withId(R.id.default_collection));
ViewInteraction recyclerView = onView(withId(R.id.collection_recycler_view));

// Check that the default collection is displayed
defaultCollection.check(matches(isDisplayed()));
Expand All @@ -101,16 +120,69 @@ public void testInitialDefaultCollectionState() {
// Test that an item is added to the collection on click of the AddMediaButton
@Test
public void testAddMediaButton() {
ViewInteraction collectionRecyclerView = onView(withId(R.id.collection_recycler_view));
ViewInteraction addMediaButton = onView(withId(R.id.add_media_button));

int initialItemCount = getRecyclerViewItemCount(R.id.collection_recycler_view);

collectionRecyclerView.check(matches(hasItemCount(initialItemCount)));
addMediaButton.perform(click());
collectionRecyclerView.check(matches(hasItemCount(initialItemCount + 1)));
}

// Test the initial state of the list of collections after profile creation
@Test
public void testInitialOuterRecyclerViewState() {
outerRecyclerView.check(matches(isDisplayed()));
// Check that the outer RecyclerView initially has one item (default collection)
outerRecyclerView.check(matches(hasItemCount(1)));
}

// Check that a new collection has been added to the outer RecyclerView if the user chooses a valid collection name
@Test
public void testAddValidCollection() throws UiObjectNotFoundException {
int initialItemCount = getRecyclerViewItemCount(R.id.collection_list_recycler_view);

outerRecyclerView.check(matches(hasItemCount(initialItemCount)));
addCollectionButton.perform(click());
enterTextInAlertBoxAndClickAdd("valid");
outerRecyclerView.check(matches(hasItemCount(initialItemCount + 1)));
}

// Check that no new collection has been added to the outer RecyclerView if the user enters an empty collection name
@Test
public void testEmptyCollectionNameNotAdded() throws UiObjectNotFoundException {
int initialItemCount = getRecyclerViewItemCount(R.id.collection_list_recycler_view);

outerRecyclerView.check(matches(hasItemCount(initialItemCount)));
addCollectionButton.perform(click());
enterTextInAlertBoxAndClickAdd("");
outerRecyclerView.check(matches(hasItemCount(initialItemCount)));
}

// Check that only one collection gets added if the user tries to add a collection with the same username twice
@Test
public void testDuplicateCollectionNameNotAdded() throws UiObjectNotFoundException {
int initialItemCount = getRecyclerViewItemCount(R.id.collection_list_recycler_view);

outerRecyclerView.check(matches(hasItemCount(initialItemCount)));
addCollectionButton.perform(click());
enterTextInAlertBoxAndClickAdd("duplicate");
addCollectionButton.perform(click());
enterTextInAlertBoxAndClickAdd("duplicate");
outerRecyclerView.check(matches(hasItemCount(initialItemCount + 1)));
}

// Check that only one collection gets added if the user tries to add a collection with the same username twice
@Test
public void testEnterValidCollectionAndCancelDoesNotAddCollection()
throws UiObjectNotFoundException {
int initialItemCount = getRecyclerViewItemCount(R.id.collection_list_recycler_view);

outerRecyclerView.check(matches(hasItemCount(initialItemCount)));
addCollectionButton.perform(click());
enterTextInAlertBoxAndClickCancel("duplicate");
outerRecyclerView.check(matches(hasItemCount(initialItemCount)));
}


/**
* A matcher to check if a RecyclerView has a certain amount of items.
*
Expand Down Expand Up @@ -147,6 +219,50 @@ private int getRecyclerViewItemCount(int id) {
return itemCount[0];
}

/**
* Used to interact with the AlertDialog to enter the collection name
*
* @param enterText the text to enter into the dialog box
*/
private void enterTextInAlertBoxAndClickAdd(String enterText) {
try {
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject inputField = device.findObject(
new UiSelector().resourceId("com.github.sdp.mediato:id/collection_name_input"));
inputField.setText(enterText);

UiObject addButton = device.findObject(
// In case the text of the button is ever changed: this is case sensitive!
// If the string resource says Add but the button text is displayed in capital letters, only ADD works!
new UiSelector().text("ADD"));
addButton.click();
} catch (UiObjectNotFoundException e) {
fail();
}
}

/**
* Used to interact with the AlertDialog to enter the collection name
*
* @param enterText the text to enter into the dialog box
*/
private void enterTextInAlertBoxAndClickCancel(String enterText) {
try {
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject inputField = device.findObject(
new UiSelector().resourceId("com.github.sdp.mediato:id/collection_name_input"));
inputField.setText(enterText);

UiObject addButton = device.findObject(
// In case the text of the button is ever changed: this is case sensitive!
// If the string resource says Cancel but the button text is displayed in capital letters, only CANCEL works!
new UiSelector().text("CANCEL"));
addButton.click();
} catch (UiObjectNotFoundException e) {
fail();
}
}

}


Loading

0 comments on commit b6fc53d

Please sign in to comment.