Skip to content

Commit 67cde52

Browse files
committed
Android test examples
1 parent 680a406 commit 67cde52

File tree

56 files changed

+1230
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1230
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.gradle
2+
/local.properties
3+
/.idea/workspace.xml
4+
/.idea/libraries
5+
.DS_Store
6+
/build
7+
/captures
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 22
5+
buildToolsVersion '22.0.1'
6+
7+
defaultConfig {
8+
applicationId "testing.android.vogella.com.asynctask"
9+
minSdkVersion 15
10+
targetSdkVersion 22
11+
versionCode 1
12+
versionName "1.0"
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
compile fileTree(dir: 'libs', include: ['*.jar'])
25+
compile 'com.android.support:appcompat-v7:22.2.0'
26+
androidTestCompile 'junit:junit:4.12'
27+
androidTestCompile 'com.android.support.test:runner:0.3'
28+
androidTestCompile 'com.android.support.test:rules:0.3'
29+
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
30+
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
31+
32+
33+
34+
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/vogella/android-sdks/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package testing.android.vogella.com.asynctask;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package testing.android.vogella.com.asynctask;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.rule.ActivityTestRule;
6+
import android.support.test.runner.AndroidJUnit4;
7+
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.junit.rules.TestName;
11+
import org.junit.runner.RunWith;
12+
13+
import static android.support.test.espresso.Espresso.onView;
14+
import static android.support.test.espresso.action.ViewActions.click;
15+
import static android.support.test.espresso.assertion.ViewAssertions.matches;
16+
import static android.support.test.espresso.matcher.ViewMatchers.withId;
17+
import static android.support.test.espresso.matcher.ViewMatchers.withText;
18+
19+
@RunWith(AndroidJUnit4.class)
20+
public class EspressoTest {
21+
@Rule
22+
public MyCustomRule myRule = new MyCustomRule();
23+
24+
@Rule
25+
public ActivityTestRule<MainActivity> mActivityRule =
26+
new ActivityTestRule<>(MainActivity.class);
27+
28+
@Test
29+
public void buttonShouldUpdateText(){
30+
onView(withId(getResourceId("update"))).perform(click());
31+
onView(withId(R.id.text)).check(matches(withText("Done")));
32+
}
33+
34+
@Test
35+
public void buttonShouldUpdateText2(){
36+
onView(withId(getResourceId("update"))).perform(click());
37+
onView(withId(R.id.text)).check(matches(withText("Done")));
38+
}
39+
40+
private static int getResourceId(String s) {
41+
Context targetContext = InstrumentationRegistry.getTargetContext();
42+
String packageName = targetContext.getPackageName();
43+
return targetContext.getResources().getIdentifier(s, "id", packageName);
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package testing.android.vogella.com.asynctask;
2+
3+
4+
import android.util.Log;
5+
6+
import org.junit.rules.TestRule;
7+
import org.junit.runner.Description;
8+
import org.junit.runners.model.Statement;
9+
10+
public class MyCustomRule implements TestRule {
11+
private Statement base;
12+
private Description description;
13+
14+
@Override
15+
public Statement apply(Statement base, Description description) {
16+
this.base = base;
17+
this.description = description;
18+
return new MyStatement(base);
19+
}
20+
21+
public class MyStatement extends Statement {
22+
private final Statement base;
23+
24+
public MyStatement(Statement base) {
25+
this.base = base;
26+
}
27+
28+
@Override
29+
public void evaluate() throws Throwable {
30+
Log.w("MyCustomRule",description.getMethodName() + "Started");
31+
try {
32+
base.evaluate();
33+
} finally {
34+
Log.w("MyCustomRule",description.getMethodName() + "Finished");
35+
}
36+
}
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="testing.android.vogella.com.asynctask" >
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:theme="@style/AppTheme" >
10+
<activity
11+
android:name=".MainActivity"
12+
android:label="@string/app_name" >
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package testing.android.vogella.com.asynctask;
2+
3+
import android.app.Activity;
4+
import android.os.AsyncTask;
5+
import android.os.Bundle;
6+
import android.view.View;
7+
import android.widget.TextView;
8+
9+
public class MainActivity extends Activity {
10+
11+
12+
@Override
13+
protected void onCreate(Bundle savedInstanceState) {
14+
super.onCreate(savedInstanceState);
15+
setContentView(R.layout.activity_main);
16+
}
17+
18+
public void onClick(View view) {
19+
TextView textView = (TextView) findViewById(R.id.text);
20+
textView.setText("Running");
21+
myTask.execute("test");
22+
}
23+
24+
final AsyncTask<String, Void, String> myTask = new AsyncTask<String, Void, String>() {
25+
26+
@Override
27+
protected String doInBackground(String... arg0) {
28+
return "Long running stuff";
29+
}
30+
31+
@Override
32+
protected void onPostExecute(String result) {
33+
TextView textView = (TextView) findViewById(R.id.text);
34+
textView.setText("Done");
35+
}
36+
37+
};
38+
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:paddingBottom="@dimen/activity_vertical_margin"
6+
android:paddingLeft="@dimen/activity_horizontal_margin"
7+
android:paddingRight="@dimen/activity_horizontal_margin"
8+
android:paddingTop="@dimen/activity_vertical_margin"
9+
tools:context=".MainActivity">
10+
11+
<TextView
12+
android:id="@+id/text"
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:text="@string/hello_world" />
16+
17+
<Button
18+
android:id="@+id/update"
19+
android:text="Click"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:onClick="onClick" />
23+
</LinearLayout>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
4+
<item android:id="@+id/action_settings" android:title="@string/action_settings"
5+
android:orderInCategory="100" app:showAsAction="never" />
6+
</menu>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<string name="app_name">AsyncTask</string>
3+
4+
<string name="hello_world">Hello world!</string>
5+
<string name="action_settings">Settings</string>
6+
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
</style>
7+
8+
</resources>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
}
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.3.0-beta2'
9+
10+
// NOTE: Do not place your application dependencies here; they belong
11+
// in the individual module build.gradle files
12+
}
13+
}
14+
15+
allprojects {
16+
repositories {
17+
jcenter()
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Project-wide Gradle settings.
2+
3+
# IDE (e.g. Android Studio) users:
4+
# Gradle settings configured through the IDE *will override*
5+
# any settings specified in this file.
6+
7+
# For more details on how to configure your build environment visit
8+
# http://www.gradle.org/docs/current/userguide/build_environment.html
9+
10+
# Specifies the JVM arguments used for the daemon process.
11+
# The setting is particularly useful for tweaking memory settings.
12+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
13+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14+
15+
# When configured, Gradle will run in incubating parallel mode.
16+
# This option should only be used with decoupled projects. More details, visit
17+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18+
# org.gradle.parallel=true
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Mon Jun 08 08:43:06 CEST 2015
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip

0 commit comments

Comments
 (0)