Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Espresso Tests for MainActivity Menu and Toolbar Functionality #214

Open
wants to merge 7 commits into
base: upgrade-jdk11
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add tests and setup for TablesDisplayActivity
Chinex-Boroja committed Jul 10, 2024
commit b9c225f811e260de895814c224c64495b07f115e
3 changes: 3 additions & 0 deletions tables_app/build.gradle
Original file line number Diff line number Diff line change
@@ -152,6 +152,9 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'
androidTestImplementation 'androidx.annotation:annotation:1.7.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'org.mockito:mockito-core:2.19.0'

testImplementation 'org.mockito:mockito-core:2.19.0'

//for UI Automator
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opendatakit.espresso;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.pressBack;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.intent.Intents.intended;
@@ -12,8 +13,14 @@
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.content.ComponentName;
import android.content.Intent;
import android.view.MenuItem;

import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.action.ViewActions;
@@ -26,6 +33,7 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import org.opendatakit.consts.IntentConsts;
import org.opendatakit.tables.R;
import org.opendatakit.tables.activities.ExportCSVActivity;
@@ -104,4 +112,21 @@ public void testOpenTableManager() {
hasAction(Intent.ACTION_DEFAULT),
hasExtra(IntentConsts.INTENT_KEY_APP_NAME, appName)));
}

@Test
public void testStartSync() {
onView(withId(R.id.menu_table_manager_sync)).perform(click());
onView(withText(R.string.sync)).check(matches(isDisplayed()));
}

@Test
public void testOnOptionsItemSelected() {
mainActivity.getScenario().onActivity(activity -> {
MenuItem menuItem = mock(MenuItem.class);
when(menuItem.getItemId()).thenReturn(R.id.menu_web_view_activity_table_manager);
boolean result = activity.onOptionsItemSelected(menuItem);
assertTrue(result);
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.opendatakit.espresso;


import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.rule.GrantPermissionRule;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opendatakit.tables.R;
import org.opendatakit.tables.activities.TableDisplayActivity;
import org.opendatakit.tables.fragments.TableManagerFragment;

@RunWith(AndroidJUnit4ClassRunner.class)
public class TablesDisplayActivityTest {

@Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

@Rule
public ActivityScenarioRule<TableDisplayActivity> activityRule =
new ActivityScenarioRule<>(TableDisplayActivity.class);

private ActivityScenario<TableDisplayActivity> scenario;

@Before
public void setUp() {
scenario = activityRule.getScenario();
}

@After
public void tearDown() {
// Clean up after tests
scenario.close();
}

@Test
public void testOnCreate() {
// Verify the UI elements and initial state
onView(withId(R.id.recycler_view)).check(matches(isDisplayed()));
}


@Test
public void testFragmentTransaction() {
scenario.onActivity(activity -> {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

// Start the transaction and add the fragment
TableManagerFragment fragment = new TableManagerFragment();
fragmentTransaction.add(R.id.activity_table_manager_list_fragment, fragment);
fragmentTransaction.commit();

// Verify the fragment is added
fragmentManager.executePendingTransactions();
assertTrue(fragment.isAdded());
});
}

@Test
public void testRecyclerViewItemClick() {
// Simulate a click on an item in the RecyclerView
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

// Verify that the appropriate activity is launched
intended(hasComponent(TableDisplayActivity.class.getName()));
}

@Test
public void testTableDetailsDisplay() {
// Simulate selecting a table and verify the details are displayed
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

// Verify that the details are displayed correctly
onView(withId(R.id.activity_table_display_activity_split_content)).check(matches(isDisplayed()));
onView(withId(R.id.activity_table_display_activity_split_content)).check(matches(withText("Expected Table Details")));
}
}