Skip to content

Commit

Permalink
added tests and code to make navigation work. this fixes #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Jurczok committed Jan 3, 2016
1 parent 5704dea commit b88728a
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
*/
public class MainActivityUITest {

public static final GeneralSwipeAction SWIPE_OPEN = new GeneralSwipeAction(FAST, CENTER_LEFT, CENTER_RIGHT, FINGER);
public static final GeneralSwipeAction SWIPE_UP = new GeneralSwipeAction(FAST, BOTTOM_CENTER, TOP_CENTER, FINGER);

@Rule
@SuppressWarnings("unchecked")
public ActivityTestRule<MainActivity> rule = new ActivityTestRule(MainActivity.class);
Expand Down Expand Up @@ -71,19 +68,6 @@ public void toolBarShouldBeRendered() {
onView(withId(R.id.toolbar)).check(matches(isDisplayed()));
}

@Test
public void navigationViewShouldBeRendered() {
onView(withId(R.id.mainLayout)).perform(SWIPE_OPEN);
onView(withId(R.id.navigation)).check(matches(isDisplayed()));
}

@Test
public void navigationViewShouldBeClosable() {
onView(withId(R.id.mainLayout)).perform(SWIPE_OPEN);
onView(withId(R.id.mainLayout)).perform(swipeLeft());
onView(withId(R.id.navigation)).check(matches(not(isDisplayed())));
}

@Test
public void swipeUpShouldMinimizeAppBar() {
onView(withId(R.id.mainLayout)).perform(swipeUp());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.linesofcode.alltrack;

import android.support.design.internal.NavigationMenuItemView;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.action.GeneralSwipeAction;
import android.support.test.rule.ActivityTestRule;
import android.view.MenuItem;
import android.view.View;

import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.GeneralLocation.BOTTOM_CENTER;
import static android.support.test.espresso.action.GeneralLocation.CENTER_LEFT;
import static android.support.test.espresso.action.GeneralLocation.CENTER_RIGHT;
import static android.support.test.espresso.action.GeneralLocation.TOP_CENTER;
import static android.support.test.espresso.action.Press.FINGER;
import static android.support.test.espresso.action.Swipe.FAST;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.swipeLeft;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;

/**
* Copyright 2015 Jonas Jurczok (jonasjurczok@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class NavigationUITest {
public static final GeneralSwipeAction SWIPE_OPEN = new GeneralSwipeAction(FAST, CENTER_LEFT, CENTER_RIGHT, FINGER);

@Rule
@SuppressWarnings("unchecked")
public ActivityTestRule<MainActivity> rule = new ActivityTestRule(MainActivity.class);

@Before
public void setUp() {
rule.getActivity();
}

@Test
public void navigationViewShouldBeRendered() {
onView(withId(R.id.mainLayout)).perform(SWIPE_OPEN);
onView(withId(R.id.navigation)).check(matches(isDisplayed()));
}

@Test
public void navigationViewShouldBeClosable() {
onView(withId(R.id.mainLayout)).perform(SWIPE_OPEN);
onView(withId(R.id.mainLayout)).perform(swipeLeft());
onView(withId(R.id.navigation)).check(matches(not(isDisplayed())));
}

@Test
public void openNavFromMainShouldHaveMainChecked() {
onView(withId(R.id.mainLayout)).perform(SWIPE_OPEN);

onView(withText(R.string.navigation_series)).perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isDisplayed();
}

@Override
public String getDescription() {
return "Check if menu item is checked.";
}

@Override
public void perform(UiController uiController, View view) {
NavigationMenuItemView menuItem = (NavigationMenuItemView) view;
assertThat(menuItem.getItemData().isChecked(), is(true));
}
});
}

@Test
public void clickOnDataSeriesShouldCloseNavigation() {
onView(withId(R.id.mainLayout)).perform(SWIPE_OPEN);
onView(withText(R.string.navigation_series)).perform(click());

onView(withId(R.id.navigation)).check(matches(not(isDisplayed())));
onView(withId(R.id.mainLayout)).check(matches(isDisplayed()));
}
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
android:name=".App">

<activity android:name=".MainActivity"
android:label="@string/app_name">
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/org/linesofcode/alltrack/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;

import org.linesofcode.alltrack.graph.GraphAdapter;

import javax.inject.Inject;
Expand All @@ -26,7 +29,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class MainActivity extends AppCompatActivity {
public class MainActivity extends NavigatableBaseActivity {
private ActionBarDrawerToggle drawerToggle;

@Inject
Expand All @@ -43,6 +46,12 @@ protected void onCreate(Bundle savedInstanceState) {
inizializeContent();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setActiveMenuItem(R.id.nav_data_series);
}

private void inizializeContent() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.linesofcode.alltrack;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

/**
* Copyright 2015 Jonas Jurczok (jonasjurczok@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/public class NavigatableBaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);

navigationView = (NavigationView) findViewById(R.id.navigation);
navigationView.setNavigationItemSelectedListener(this);
}

protected void setActiveMenuItem(int itemId) {
navigationView.setCheckedItem(itemId);
}

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch(menuItem.getItemId()) {
case R.id.nav_data_series:
navigateToDataSeries();
break;
default:
return false;
}
DrawerLayout mainLayout = (DrawerLayout) findViewById(R.id.mainLayout);
mainLayout.closeDrawers();
return true;
}

private void navigateToDataSeries() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/menu/navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:title="@string/navigation_series"
android:checked="true"
android:icon="@drawable/ic_chart_line_black_48dp"/>
android:icon="@drawable/ic_chart_line_black_48dp"
android:id="@+id/nav_data_series"/>
</group>
</menu>

0 comments on commit b88728a

Please sign in to comment.