Skip to content

Commit 983de86

Browse files
committed
Initial sample push for Espresso 2.0 and AndroidJUnitRunner samples
- All samples can be build with gradle and AS - README files to walk through sample usage - Basic eclipse sample for static jar usage
0 parents  commit 983de86

File tree

183 files changed

+5561
-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.

183 files changed

+5561
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea
2+
*.iml
3+
local.properties
4+
build
5+
.gradle

build.gradle

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.0.0'
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+
20+
apply plugin: 'findbugs'
21+
22+
task findbugs(type: FindBugs) {
23+
ignoreFailures = true
24+
effort = "max"
25+
reportLevel = "high"
26+
//excludeFilter = new File("${project.rootDir}/config/quality/findbugs/findbugs-filter.xml")
27+
classes = files("$project.buildDir/intermediates/classes/")
28+
29+
source 'src'
30+
include '**/*.java'
31+
exclude '**/gen/**'
32+
33+
reports {
34+
xml {
35+
destination "$project.buildDir/reports/findbugs/findbugs.xml"
36+
xml.withMessages true
37+
}
38+
}
39+
40+
classpath = files()
41+
}
42+
}

espresso/BasicSample/.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.gradle
2+
local.properties
3+
.idea
4+
.DS_Store
5+
build
6+
*.iml

espresso/BasicSample/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Basic sample for Espresso
2+
3+
*If you are new to Espresso, try this sample first.*
4+
5+
This project uses the Gradle build system. You don't need an IDE to build and execute it but Android Studio is recommended.
6+
7+
1. Download the project code, preferably using `git clone`.
8+
1. Open the Android SDK Manager (*Tools* Menu | *Android*) and make sure you have installed the *Android testing support library Repository* under *Extras*.
9+
1. In Android Studio, select *File* | *Open...* and point to the `./build.gradle` file.
10+
1. Check out the relevant code:
11+
* The application under test is located in `src/main/java`
12+
* Tests are in `src/androidTest/java`
13+
1. Create the test configuration with a custom runner: `android.support.test.runner.AndroidJUnitRunner`
14+
* Open *Run* menu | *Edit Configurations*
15+
* Add a new *Android Tests* configuration
16+
* Choose a module
17+
* Add a *Specific instrumentation runner*: `android.support.test.runner.AndroidJUnitRunner`
18+
1. Connect a device or start an emulator
19+
* Turn animations off [link link]
20+
1. Run the newly created configuration
21+
22+
The application will be started on the device/emulator and a series of actions will be performed automatically.
23+
24+
If you are using Android Studio, the *Run* window will show the test results.

espresso/BasicSample/app/build.gradle

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 21
5+
buildToolsVersion '21.1.2'
6+
7+
defaultConfig {
8+
applicationId "com.example.android.testing.espresso.BasicSample"
9+
minSdkVersion 10
10+
targetSdkVersion 21
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
}
16+
packagingOptions {
17+
exclude 'LICENSE.txt'
18+
}
19+
lintOptions {
20+
abortOnError false
21+
}
22+
}
23+
24+
dependencies {
25+
// App dependencies
26+
compile 'com.android.support:support-annotations:21.0.3'
27+
compile 'com.google.guava:guava:18.0'
28+
29+
// Testing-only dependencies
30+
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
31+
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2014, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.testing.espresso.BasicSample;
18+
19+
import static android.support.test.espresso.Espresso.onView;
20+
import static android.support.test.espresso.action.ViewActions.click;
21+
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
22+
import static android.support.test.espresso.action.ViewActions.typeText;
23+
import static android.support.test.espresso.assertion.ViewAssertions.matches;
24+
import static android.support.test.espresso.matcher.ViewMatchers.withId;
25+
import static android.support.test.espresso.matcher.ViewMatchers.withText;
26+
27+
import android.app.Activity;
28+
import android.test.ActivityInstrumentationTestCase2;
29+
30+
import android.support.test.espresso.action.ViewActions;
31+
import android.support.test.espresso.matcher.ViewMatchers;
32+
33+
34+
/**
35+
* Basic tests showcasing simple view matchers and actions like {@link ViewMatchers#withId},
36+
* {@link ViewActions#click} and {@link ViewActions#typeText}.
37+
* <p>
38+
* Note that there is no need to tell Espresso that a view is in a different {@link Activity}.
39+
* </p>
40+
*/
41+
public class ChangeTextBehaviorTest extends ActivityInstrumentationTestCase2<MainActivity> {
42+
43+
public static final String STRING_TO_BE_TYPED = "Espresso";
44+
45+
public ChangeTextBehaviorTest() {
46+
super(MainActivity.class);
47+
}
48+
49+
@Override
50+
protected void setUp() throws Exception {
51+
super.setUp();
52+
// For each test method invocation, the Activity will not actually be created
53+
// until the first time this method is called.
54+
getActivity();
55+
}
56+
57+
public void testChangeText_sameActivity() {
58+
// Type text and then press the button.
59+
onView(withId(R.id.editTextUserInput))
60+
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
61+
onView(withId(R.id.changeTextBt)).perform(click());
62+
63+
// Check that the text was changed.
64+
onView(withId(R.id.textToBeChanged)).check(matches(withText(STRING_TO_BE_TYPED)));
65+
}
66+
67+
public void testChangeText_newActivity() {
68+
// Type text and then press the button.
69+
onView(withId(R.id.editTextUserInput)).perform(typeText(STRING_TO_BE_TYPED));
70+
onView(withId(R.id.activityChangeTextBtn)).perform(click());
71+
72+
// This view is in a different Activity, no need to tell Espresso.
73+
onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED)));
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2014 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.example.android.testing.espresso.BasicSample" >
20+
21+
<application
22+
android:icon="@drawable/ic_launcher"
23+
android:label="@string/app_name"
24+
android:theme="@style/AppTheme" >
25+
<activity
26+
android:name="com.example.android.testing.espresso.BasicSample.MainActivity"
27+
android:label="@string/app_name" >
28+
<intent-filter>
29+
<action android:name="android.intent.action.MAIN" />
30+
<category android:name="android.intent.category.LAUNCHER" />
31+
</intent-filter>
32+
</activity>
33+
<activity android:name="com.example.android.testing.espresso.BasicSample.ShowTextActivity"/>
34+
</application>
35+
36+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2014, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.testing.espresso.BasicSample;
18+
19+
import android.app.Activity;
20+
import android.content.Intent;
21+
import android.os.Bundle;
22+
import android.view.View;
23+
import android.widget.EditText;
24+
import android.widget.TextView;
25+
26+
/**
27+
* An {@link Activity} that gets a text string from the user and displays it back when the user
28+
* clicks on one of the two buttons. The first one shows it in the same activity and the second
29+
* one opens another activity and displays the message.
30+
*/
31+
public class MainActivity extends Activity implements View.OnClickListener {
32+
33+
// The TextView used to display the message inside the Activity.
34+
private TextView mTextView;
35+
36+
// The EditText where the user types the message.
37+
private EditText mEditText;
38+
39+
@Override
40+
protected void onCreate(Bundle savedInstanceState) {
41+
super.onCreate(savedInstanceState);
42+
setContentView(R.layout.activity_main);
43+
44+
// Set the listeners for the buttons.
45+
findViewById(R.id.changeTextBt).setOnClickListener(this);
46+
findViewById(R.id.activityChangeTextBtn).setOnClickListener(this);
47+
48+
mTextView = (TextView) findViewById(R.id.textToBeChanged);
49+
mEditText = (EditText) findViewById(R.id.editTextUserInput);
50+
}
51+
52+
@Override
53+
public void onClick(View view) {
54+
// Get the text from the EditText view.
55+
final String text = mEditText.getText().toString();
56+
57+
switch (view.getId()) {
58+
case R.id.changeTextBt:
59+
// First button's interaction: set a text in a text view.
60+
mTextView.setText(text);
61+
break;
62+
case R.id.activityChangeTextBtn:
63+
// Second button's interaction: start an activity and send a message to it.
64+
Intent intent = ShowTextActivity.newStartIntent(this, text);
65+
startActivity(intent);
66+
break;
67+
}
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2014, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.testing.espresso.BasicSample;
18+
19+
import com.google.common.base.Strings;
20+
21+
import android.app.Activity;
22+
import android.content.Context;
23+
import android.content.Intent;
24+
import android.os.Bundle;
25+
import android.widget.TextView;
26+
27+
/**
28+
* A simple {@link Activity} that shows a message.
29+
*/
30+
public class ShowTextActivity extends Activity {
31+
32+
// The name of the extra data sent through an {@link Intent}.
33+
public final static String KEY_EXTRA_MESSAGE =
34+
"com.example.android.testing.espresso.basicsample.MESSAGE";
35+
36+
@Override
37+
protected void onCreate(Bundle savedInstanceState) {
38+
super.onCreate(savedInstanceState);
39+
setContentView(R.layout.activity_show_text);
40+
41+
// Get the message from the Intent.
42+
Intent intent = getIntent();
43+
String message = Strings.nullToEmpty(intent.getStringExtra(KEY_EXTRA_MESSAGE));
44+
45+
// Show message.
46+
((TextView)findViewById(R.id.show_text_view)).setText(message);
47+
}
48+
49+
/**
50+
* Creates an {@link Intent} for {@link ShowTextActivity} with the message to be displayed.
51+
* @param context the {@link Context} where the {@link Intent} will be used
52+
* @param message a {@link String} with text to be displayed
53+
* @return an {@link Intent} used to start {@link ShowTextActivity}
54+
*/
55+
static protected Intent newStartIntent(Context context, String message) {
56+
Intent newIntent = new Intent(context, ShowTextActivity.class);
57+
newIntent.putExtra(KEY_EXTRA_MESSAGE, message);
58+
return newIntent;
59+
}
60+
}
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)