Skip to content

Commit

Permalink
Fix 4.10 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Silver committed Nov 7, 2016
1 parent bd16bb0 commit 7ae90e9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
3 changes: 1 addition & 2 deletions 4.10-Exercise-SetUpUnitTesting/solution/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
/.idea
.DS_Store
/build
27 changes: 23 additions & 4 deletions 4.10-Exercise-SetUpUnitTesting/solution/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
/*
In this exercise you will set up unit tests and connected tests for a simple Android app.
Exercises:
1. Set up unit tests that verify the initial count and increment methods of the ClickCounter class
2. Set up connected tests that test that clicking on the button actually increments the value displayed
To complete the latter task, check out:
https://developer.android.com/training/testing/ui-testing/espresso-testing.html
*/

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
compileSdkVersion 25
buildToolsVersion "24.0.3"

defaultConfig {
applicationId "com.example.android.clickcounter"
minSdkVersion 15
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand All @@ -21,6 +37,9 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.1.1'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
package com.example.android.clickcounter;

import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.suitebuilder.annotation.MediumTest;
import android.widget.Button;
import android.widget.TextView;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

public class ButtonClickTest extends ActivityInstrumentationTestCase2<ClickActivity> {
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

private ClickActivity mClickActvity;
private Button mButton;
private TextView mTextView;
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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

public ButtonClickTest() {
super(ClickActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();

setActivityInitialTouchMode(true);
mClickActvity = getActivity();
mButton = (Button) mClickActvity.findViewById(R.id.click_button);
mTextView = (TextView) mClickActvity.findViewById(R.id.click_count_text_view);
}
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ButtonClickTest {

@MediumTest
public void testInitialValue() {
int initialNumber = Integer.parseInt(mTextView.getText().toString());
assertEquals(0, initialNumber);
}
@Rule
public ActivityTestRule<ClickActivity> mActivityRule = new ActivityTestRule<>(
ClickActivity.class);

@MediumTest
@Test
public void testClick() {
int priorNumber = Integer.parseInt(mTextView.getText().toString());
TouchUtils.clickView(this, mButton);
int newNumber = Integer.parseInt(mTextView.getText().toString());
assertEquals(priorNumber + 1, newNumber);
onView(withId(R.id.click_count_text_view)).check(matches(withText("0")));
onView(withId(R.id.click_button)).perform(click());
onView(withId(R.id.click_count_text_view)).check(matches(withText("1")));
}
}
2 changes: 1 addition & 1 deletion 4.10-Exercise-SetUpUnitTesting/solution/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-beta1'
classpath 'com.android.tools.build:gradle:2.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 7ae90e9

Please sign in to comment.