Switch to compat ActionMode; fix tests

pull/87/head
Alinson S. Xavier 10 years ago
parent a4864e4612
commit fcee8552f0

@ -20,7 +20,9 @@
package org.isoron.uhabits.ui; package org.isoron.uhabits.ui;
import android.support.test.espresso.NoMatchingViewException; import android.support.test.espresso.NoMatchingViewException;
import android.view.View;
import org.hamcrest.Matcher;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
@ -31,7 +33,6 @@ import java.util.Random;
import static android.support.test.espresso.Espresso.onData; import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.openContextualActionModeOverflowMenu;
import static android.support.test.espresso.Espresso.pressBack; import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.longClick; import static android.support.test.espresso.action.ViewActions.longClick;
@ -39,12 +40,16 @@ import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.isPlatformPopup; import static android.support.test.espresso.matcher.RootMatchers.isPlatformPopup;
import static android.support.test.espresso.matcher.ViewMatchers.Visibility.VISIBLE; import static android.support.test.espresso.matcher.ViewMatchers.Visibility.VISIBLE;
import static android.support.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility; import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText; import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
@ -54,6 +59,9 @@ import static org.isoron.uhabits.ui.HabitMatchers.withName;
public class MainActivityActions public class MainActivityActions
{ {
private static final Matcher<View> OVERFLOW_BUTTON_MATCHER =
allOf(isCompletelyDisplayed(), withContentDescription("More options"));
public static String addHabit() public static String addHabit()
{ {
return addHabit(false); return addHabit(false);
@ -168,13 +176,13 @@ public class MainActivityActions
public static void deleteHabits(List<String> names) public static void deleteHabits(List<String> names)
{ {
selectHabits(names); selectHabits(names);
clickActionModeMenuItem(R.string.delete); clickMenuItem(R.string.delete);
onView(withText("OK")) onView(withText("OK"))
.perform(click()); .perform(click());
assertHabitsDontExist(names); assertHabitsDontExist(names);
} }
public static void clickActionModeMenuItem(int stringId) public static void clickMenuItem(int stringId)
{ {
try try
{ {
@ -188,9 +196,26 @@ public class MainActivityActions
} }
catch(Exception e2) catch(Exception e2)
{ {
openContextualActionModeOverflowMenu(); clickHiddenMenuItem(stringId);
onView(withText(stringId)).perform(click());
} }
} }
} }
private static void clickHiddenMenuItem(int stringId)
{
try
{
// Try the ActionMode overflow menu first
onView(allOf(withContentDescription("More options"), withParent(withParent(
withClassName(containsString("Action")))))).perform(click());
}
catch (Exception e1)
{
// Try the toolbar overflow menu
onView(allOf(withContentDescription("More options"), withParent(withParent(
withClassName(containsString("Toolbar")))))).perform(click());
}
onView(withText(stringId)).perform(click());
}
} }

@ -45,7 +45,6 @@ import java.util.Random;
import static android.support.test.espresso.Espresso.onData; import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static android.support.test.espresso.Espresso.pressBack; import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.longClick; import static android.support.test.espresso.action.ViewActions.longClick;
@ -61,7 +60,6 @@ import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot; import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withClassName; import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.endsWith;
@ -77,7 +75,7 @@ import static org.isoron.uhabits.ui.MainActivityActions.addHabit;
import static org.isoron.uhabits.ui.MainActivityActions.assertHabitExists; import static org.isoron.uhabits.ui.MainActivityActions.assertHabitExists;
import static org.isoron.uhabits.ui.MainActivityActions.assertHabitsDontExist; import static org.isoron.uhabits.ui.MainActivityActions.assertHabitsDontExist;
import static org.isoron.uhabits.ui.MainActivityActions.assertHabitsExist; import static org.isoron.uhabits.ui.MainActivityActions.assertHabitsExist;
import static org.isoron.uhabits.ui.MainActivityActions.clickActionModeMenuItem; import static org.isoron.uhabits.ui.MainActivityActions.clickMenuItem;
import static org.isoron.uhabits.ui.MainActivityActions.deleteHabit; import static org.isoron.uhabits.ui.MainActivityActions.deleteHabit;
import static org.isoron.uhabits.ui.MainActivityActions.deleteHabits; import static org.isoron.uhabits.ui.MainActivityActions.deleteHabits;
import static org.isoron.uhabits.ui.MainActivityActions.selectHabit; import static org.isoron.uhabits.ui.MainActivityActions.selectHabit;
@ -152,20 +150,16 @@ public class MainTest
selectHabits(names); selectHabits(names);
clickActionModeMenuItem(R.string.archive); clickMenuItem(R.string.archive);
assertHabitsDontExist(names); assertHabitsDontExist(names);
openActionBarOverflowOrOptionsMenu(targetContext); clickMenuItem(R.string.show_archived);
onView(withText(R.string.show_archived))
.perform(click());
assertHabitsExist(names); assertHabitsExist(names);
selectHabits(names); selectHabits(names);
clickActionModeMenuItem(R.string.unarchive); clickMenuItem(R.string.unarchive);
openActionBarOverflowOrOptionsMenu(targetContext); clickMenuItem(R.string.show_archived);
onView(withText(R.string.show_archived))
.perform(click());
assertHabitsExist(names); assertHabitsExist(names);
deleteHabits(names); deleteHabits(names);
@ -227,7 +221,7 @@ public class MainTest
.onChildView(withId(R.id.label)) .onChildView(withId(R.id.label))
.perform(longClick()); .perform(longClick());
clickActionModeMenuItem(R.string.edit); clickMenuItem(R.string.edit);
String modifiedName = "Modified " + new Random().nextInt(10000); String modifiedName = "Modified " + new Random().nextInt(10000);
typeHabitData(modifiedName, "", "1", "1"); typeHabitData(modifiedName, "", "1", "1");
@ -238,7 +232,7 @@ public class MainTest
assertHabitExists(modifiedName); assertHabitExists(modifiedName);
selectHabit(modifiedName); selectHabit(modifiedName);
clickActionModeMenuItem(R.string.color_picker_default_title); clickMenuItem(R.string.color_picker_default_title);
pressBack(); pressBack();
deleteHabit(modifiedName); deleteHabit(modifiedName);
@ -272,8 +266,7 @@ public class MainTest
@Test @Test
public void testSettings() public void testSettings()
{ {
openActionBarOverflowOrOptionsMenu(targetContext); clickMenuItem(R.string.settings);
onView(withText(R.string.settings)).perform(click());
} }
/** /**
@ -282,8 +275,7 @@ public class MainTest
@Test @Test
public void testAbout() public void testAbout()
{ {
openActionBarOverflowOrOptionsMenu(targetContext); clickMenuItem(R.string.about);
onView(withText(R.string.about)).perform(click());
onView(isRoot()).perform(swipeUp()); onView(isRoot()).perform(swipeUp());
} }
@ -293,8 +285,7 @@ public class MainTest
@Test @Test
public void testHelp() public void testHelp()
{ {
openActionBarOverflowOrOptionsMenu(targetContext); clickMenuItem(R.string.help);
onView(withText(R.string.help)).perform(click());
intended(hasAction(Intent.ACTION_VIEW)); intended(hasAction(Intent.ACTION_VIEW));
} }
@ -307,8 +298,7 @@ public class MainTest
{ {
String name = addHabit(); String name = addHabit();
openActionBarOverflowOrOptionsMenu(targetContext); clickMenuItem(R.string.settings);
onView(withText(R.string.settings)).perform(click());
String date = DateHelper.getBackupDateFormat().format(DateHelper.getLocalTime()); String date = DateHelper.getBackupDateFormat().format(DateHelper.getLocalTime());
date = date.substring(0, date.length() - 2); date = date.substring(0, date.length() - 2);
@ -318,8 +308,7 @@ public class MainTest
deleteHabit(name); deleteHabit(name);
openActionBarOverflowOrOptionsMenu(targetContext); clickMenuItem(R.string.settings);
onView(withText(R.string.settings)).perform(click());
onData(isPreferenceWithText("Import data")).perform(click()); onData(isPreferenceWithText("Import data")).perform(click());
onData(allOf(is(instanceOf(String.class)), startsWith("Backups"))) onData(allOf(is(instanceOf(String.class)), startsWith("Backups")))
@ -339,8 +328,7 @@ public class MainTest
public void testExportCSV() public void testExportCSV()
{ {
addHabit(); addHabit();
openActionBarOverflowOrOptionsMenu(targetContext); clickMenuItem(R.string.settings);
onView(withText(R.string.settings)).perform(click());
onData(isPreferenceWithText("Export as CSV")).perform(click()); onData(isPreferenceWithText("Export as CSV")).perform(click());
intended(hasAction(Intent.ACTION_SEND)); intended(hasAction(Intent.ACTION_SEND));
} }
@ -351,8 +339,7 @@ public class MainTest
@Test @Test
public void testGenerateBugReport() public void testGenerateBugReport()
{ {
openActionBarOverflowOrOptionsMenu(targetContext); clickMenuItem(R.string.settings);
onView(withText(R.string.settings)).perform(click());
onData(isPreferenceWithText("Generate bug report")).perform(click()); onData(isPreferenceWithText("Generate bug report")).perform(click());
intended(hasAction(Intent.ACTION_SENDTO)); intended(hasAction(Intent.ACTION_SENDTO));
} }

@ -21,7 +21,7 @@ package org.isoron.uhabits.fragments;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.view.ActionMode; import android.support.v7.view.ActionMode;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.ProgressBar; import android.widget.ProgressBar;

@ -27,7 +27,7 @@ import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.ActionMode; import android.support.v7.view.ActionMode;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo; import android.view.ContextMenu.ContextMenuInfo;
import android.view.HapticFeedbackConstants; import android.view.HapticFeedbackConstants;
@ -285,7 +285,7 @@ public class ListHabitsFragment extends Fragment
callback.setOnSavedListener(this); callback.setOnSavedListener(this);
callback.setListener(this); callback.setListener(this);
actionMode = getActivity().startActionMode(callback); actionMode = activity.startSupportActionMode(callback);
} }
if(actionMode != null) actionMode.invalidate(); if(actionMode != null) actionMode.invalidate();
@ -381,6 +381,7 @@ public class ListHabitsFragment extends Fragment
else loader.updateHabit(refreshKey); else loader.updateHabit(refreshKey);
} }
@Override
public void onActionModeDestroyed(ActionMode mode) public void onActionModeDestroyed(ActionMode mode)
{ {
actionMode = null; actionMode = null;

@ -18,7 +18,8 @@
~ with this program. If not, see <http://www.gnu.org/licenses/>. ~ with this program. If not, see <http://www.gnu.org/licenses/>.
--> -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item <item
android:id="@+id/action_edit_habit" android:id="@+id/action_edit_habit"
@ -43,6 +44,6 @@
<item <item
android:id="@+id/action_delete" android:id="@+id/action_delete"
android:title="@string/delete" android:title="@string/delete"
android:showAsAction="never"/> app:showAsAction="never"/>
</menu> </menu>
Loading…
Cancel
Save