diff --git a/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromAboutTest.java b/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromAboutTest.java new file mode 100644 index 000000000..6bb6ef32b --- /dev/null +++ b/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromAboutTest.java @@ -0,0 +1,80 @@ +/* + * This file is part of BBCT for Android. + * + * Copyright 2018 codeguru + * + * BBCT for Android is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * BBCT for Android is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package bbct.android.common.navigation.test; + +import android.support.test.espresso.Espresso; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import bbct.android.common.R; +import bbct.android.lite.provider.LiteActivity; + +import static android.support.test.InstrumentationRegistry.getInstrumentation; +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; +import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.hamcrest.Matchers.allOf; + +@RunWith(AndroidJUnit4.class) +public class NavigateBackPressFromAboutTest { + @Rule + public ActivityTestRule activityActivityTestRule = new ActivityTestRule(LiteActivity.class); + + @Before + public void setUp() throws Exception { + activityActivityTestRule.getActivity() + .getSupportFragmentManager().beginTransaction(); + } + + @Test + public void testNavigateToAboutFragment() { + openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext()); + + String aboutTitle = getInstrumentation().getTargetContext().getString(R.string.about_title); + Espresso.pressBack(); + + String expectedTitle = getInstrumentation().getTargetContext().getString(R.string.bbct_title, aboutTitle); + onView(withText(expectedTitle)).check(matches(isDisplayed())); + } + + @Test + public void testNavigateBack() { + String initialTitle = (String) activityActivityTestRule.getActivity().getTitle(); + openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext()); + + String aboutTitle = getInstrumentation().getTargetContext().getString(R.string.about_title); + + onView(allOf(withText(aboutTitle), isDisplayed())).perform(click()); + String expectedTitle = getInstrumentation().getTargetContext().getString(R.string.bbct_title, aboutTitle); + onView(withText(expectedTitle)).check(matches(isDisplayed())); + + Espresso.closeSoftKeyboard(); + Espresso.pressBack(); + onView(withText(initialTitle)).check(matches(isDisplayed())); + } +} \ No newline at end of file diff --git a/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromBaseballCardDetailsTest.java b/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromBaseballCardDetailsTest.java new file mode 100644 index 000000000..cb3fec531 --- /dev/null +++ b/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromBaseballCardDetailsTest.java @@ -0,0 +1,62 @@ +/* + * This file is part of BBCT for Android. + * + * Copyright 2018 codeguru + * + * BBCT for Android is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * BBCT for Android is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package bbct.android.common.navigation.test; + +import android.support.test.espresso.Espresso; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import bbct.android.common.R; +import bbct.android.lite.provider.LiteActivity; + +import static android.support.test.InstrumentationRegistry.getInstrumentation; +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; +import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.hamcrest.Matchers.allOf; + +@RunWith(AndroidJUnit4.class) +public class NavigateBackPressFromBaseballCardDetailsTest { + @Rule + public ActivityTestRule activityActivityTestRule = new ActivityTestRule(LiteActivity.class); + + @Before + public void setUp() throws Exception { + activityActivityTestRule.getActivity() + .getSupportFragmentManager().beginTransaction(); + } + + @Test + public void testDefaultNavigateUpWithNoData() { + String cardDetailsTitle = getInstrumentation().getTargetContext().getString(R.string.card_details_title); + String expectedTitle = getInstrumentation().getTargetContext().getString(R.string.bbct_title, cardDetailsTitle); + Espresso.closeSoftKeyboard(); + onView(withText(expectedTitle)).check(matches(isDisplayed())); + Espresso.pressBack(); + onView(withText(R.string.app_name)).check(matches(isDisplayed())); + } +} \ No newline at end of file diff --git a/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromBaseballCardListTest.java b/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromBaseballCardListTest.java new file mode 100644 index 000000000..e0bc9057e --- /dev/null +++ b/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromBaseballCardListTest.java @@ -0,0 +1,59 @@ +/* + * This file is part of BBCT for Android. + * + * Copyright 2018 codeguru + * + * BBCT for Android is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * BBCT for Android is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package bbct.android.common.navigation.test; + +import android.support.test.espresso.Espresso; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + + +import bbct.android.common.R; +import bbct.android.lite.provider.LiteActivity; + +import static android.support.test.InstrumentationRegistry.getInstrumentation; +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withText; + +@RunWith(AndroidJUnit4.class) +public class NavigateBackPressFromBaseballCardListTest { + @Rule + public ActivityTestRule activityActivityTestRule = new ActivityTestRule(LiteActivity.class); + + + @Before + public void setUp() throws Exception { + activityActivityTestRule.getActivity() + .getSupportFragmentManager().beginTransaction(); + } + + @Test + public void testBackPress() { + String expectedTitle = getInstrumentation().getTargetContext().getString(R.string.app_name); + Espresso.closeSoftKeyboard(); + Espresso.pressBack(); + onView(withText(expectedTitle)).check(matches(isDisplayed())); + } +} \ No newline at end of file diff --git a/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromBaseballFilterCardsTest.java b/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromBaseballFilterCardsTest.java new file mode 100644 index 000000000..a80321614 --- /dev/null +++ b/android/src/androidTest/java/bbct/android/common/navigation/test/NavigateBackPressFromBaseballFilterCardsTest.java @@ -0,0 +1,62 @@ +/* + * This file is part of BBCT for Android. + * + * Copyright 2018 codeguru + * + * BBCT for Android is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * BBCT for Android is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package bbct.android.common.navigation.test; + +import android.support.test.espresso.Espresso; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import bbct.android.common.R; +import bbct.android.lite.provider.LiteActivity; + +import static android.support.test.InstrumentationRegistry.getInstrumentation; +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; +import static android.support.test.espresso.matcher.ViewMatchers.withText; +import static org.hamcrest.Matchers.allOf; + +@RunWith(AndroidJUnit4.class) +public class NavigateBackPressFromBaseballFilterCardsTest { + @Rule + public ActivityTestRule activityActivityTestRule = new ActivityTestRule(LiteActivity.class); + + @Before + public void setUp() throws Exception { + activityActivityTestRule.getActivity() + .getSupportFragmentManager().beginTransaction(); + } + + @Test + public void testNavigateUp() { + + String expectedTitle = getInstrumentation().getTargetContext().getString(R.string.app_name); + Espresso.pressBack(); + onView(allOf(withContentDescription(R.string.filter_cards_title), isDisplayed())).perform(click()); + Espresso.pressBack(); + onView(allOf(withText(expectedTitle), isDisplayed())); + + } +} \ No newline at end of file diff --git a/android/src/main/java/bbct/android/common/activity/BaseballCardList.java b/android/src/main/java/bbct/android/common/activity/BaseballCardList.java index ce7e136b9..f9fe13fe5 100644 --- a/android/src/main/java/bbct/android/common/activity/BaseballCardList.java +++ b/android/src/main/java/bbct/android/common/activity/BaseballCardList.java @@ -24,6 +24,8 @@ import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.ListFragment; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -106,6 +108,17 @@ public void onCreate(Bundle savedInstanceState) { this.setHasOptionsMenu(true); } + @Override + public void onResume() { + super.onResume(); + getActivity().setTitle(R.string.app_name); + + ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(false); + } + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { diff --git a/android/src/main/java/bbct/android/common/activity/MainActivity.java b/android/src/main/java/bbct/android/common/activity/MainActivity.java index 931cab021..31df3646a 100644 --- a/android/src/main/java/bbct/android/common/activity/MainActivity.java +++ b/android/src/main/java/bbct/android/common/activity/MainActivity.java @@ -69,13 +69,16 @@ public void onCreate(Bundle savedInstanceState) { BaseballCardContract.PROJECTION, null, null, null); FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction(); + ft.add(R.id.fragment_holder, new BaseballCardList(), FragmentTags.CARD_LIST) + .addToBackStack(null) + .commit(); + if (cursor == null || cursor.getCount() == 0) { - ft.add(R.id.fragment_holder, new BaseballCardDetails(), FragmentTags.EDIT_CARD); - } else { - ft.add(R.id.fragment_holder, new BaseballCardList(), FragmentTags.CARD_LIST); - cursor.close(); + this.getSupportFragmentManager().beginTransaction() + .replace(R.id.fragment_holder, new BaseballCardDetails(), FragmentTags.EDIT_CARD) + .addToBackStack(FragmentTags.EDIT_CARD).commit(); } - ft.commit(); + cursor.close(); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) {