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

Navigate back press fix #433

Closed
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* This file is part of BBCT for Android.
*
* Copyright 2018 codeguru <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package bbct.android.common.navigation.test;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.NoMatchingViewException;
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<LiteActivity> activityActivityTestRule = new ActivityTestRule<LiteActivity>(LiteActivity.class);

@Before
public void setUp() throws Exception {
activityActivityTestRule.getActivity()
.getSupportFragmentManager().beginTransaction();
}

private void skipSurvey() {
try {
onView(withText(R.string.survey1)).check(matches(isDisplayed()));
onView(withText(R.string.later))
.check(matches(isDisplayed()))
.perform(click());
} catch (NoMatchingViewException e) {
//view not displayed logic
}
}

@Test
public void testNavigateBack() {
skipSurvey();
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()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This file is part of BBCT for Android.
*
* Copyright 2018 codeguru <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package bbct.android.common.navigation.test;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.NoMatchingViewException;
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<LiteActivity> activityActivityTestRule = new ActivityTestRule<LiteActivity>(LiteActivity.class);

@Before
public void setUp() throws Exception {
activityActivityTestRule.getActivity()
.getSupportFragmentManager().beginTransaction();
}

private void skipSurvey() {
try {
onView(withText(R.string.survey1)).check(matches(isDisplayed()));
onView(withText(R.string.later))
.check(matches(isDisplayed()))
.perform(click());
} catch (NoMatchingViewException e) {
//view not displayed logic
}
}

@Test
public void testDefaultNavigateUpWithNoData() {
skipSurvey();
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()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of BBCT for Android.
*
* Copyright 2018 codeguru <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package bbct.android.common.navigation.test;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.NoMatchingViewException;
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.withText;

@RunWith(AndroidJUnit4.class)
public class NavigateBackPressFromBaseballCardListTest {
@Rule
public ActivityTestRule<LiteActivity> activityActivityTestRule = new ActivityTestRule<LiteActivity>(LiteActivity.class);


@Before
public void setUp() throws Exception {
activityActivityTestRule.getActivity()
.getSupportFragmentManager().beginTransaction();
}

private void skipSurvey() {
try {
onView(withText(R.string.survey1)).check(matches(isDisplayed()));
onView(withText(R.string.later))
.check(matches(isDisplayed()))
.perform(click());
} catch (NoMatchingViewException e) {
//view not displayed logic
}
}

@Test
public void testBackPress() {
skipSurvey();
String expectedTitle = getInstrumentation().getTargetContext().getString(R.string.app_name);
Espresso.closeSoftKeyboard();
Espresso.pressBack();
onView(withText(expectedTitle)).check(matches(isDisplayed()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of BBCT for Android.
*
* Copyright 2018 codeguru <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package bbct.android.common.navigation.test;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.NoMatchingViewException;
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 NavigateBackPressFromBaseballFilterCardsTest {
@Rule
public ActivityTestRule<LiteActivity> activityActivityTestRule = new ActivityTestRule<LiteActivity>(LiteActivity.class);

@Before
public void setUp() throws Exception {
activityActivityTestRule.getActivity()
.getSupportFragmentManager().beginTransaction();
}

private void skipSurvey() {
try {
onView(withText(R.string.survey1)).check(matches(isDisplayed()));
onView(withText(R.string.later))
.check(matches(isDisplayed()))
.perform(click());
} catch (NoMatchingViewException e) {
//view not displayed logic
}
}

@Test
public void testNavigateUp() {
skipSurvey();
String expectedTitle = getInstrumentation().getTargetContext().getString(R.string.app_name);
Espresso.closeSoftKeyboard();
Espresso.pressBack();
onView(allOf(withContentDescription(R.string.filter_cards_title), isDisplayed())).perform(click());
Espresso.pressBack();
onView(allOf(withText(expectedTitle), isDisplayed()));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,6 +108,12 @@ public void onCreate(Bundle savedInstanceState) {
this.setHasOptionsMenu(true);
}

@Override
public void onResume() {
super.onResume();
getActivity().setTitle(R.string.app_name);
}

jojonarte marked this conversation as resolved.
Show resolved Hide resolved
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
jojonarte marked this conversation as resolved.
Show resolved Hide resolved

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
Expand Down