Skip to content

Commit 9730faf

Browse files
committed
add espresso demo
1 parent a8ea025 commit 9730faf

30 files changed

+654
-1
lines changed

espressodemo/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

espressodemo/build.gradle

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 27
5+
defaultConfig {
6+
applicationId "cc.istarx.espressodemo"
7+
minSdkVersion 26
8+
targetSdkVersion 27
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
compileOptions {
20+
targetCompatibility 1.8
21+
sourceCompatibility 1.8
22+
}
23+
}
24+
25+
dependencies {
26+
implementation fileTree(dir: 'libs', include: ['*.jar'])
27+
implementation 'com.android.support:appcompat-v7:27.1.1'
28+
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
29+
implementation 'com.android.support:design:27.1.1'
30+
testImplementation 'junit:junit:4.12'
31+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
32+
androidTestImplementation 'com.android.support.test:rules:1.0.2'
33+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
34+
// espresso intent test dependency, only compatible with Espresso 2.1+
35+
// and the testing support library 0.3+
36+
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
37+
}

espressodemo/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package cc.istarx.espressodemo;
2+
3+
import android.app.Activity;
4+
import android.app.Instrumentation;
5+
import android.content.Intent;
6+
import android.support.test.espresso.intent.matcher.BundleMatchers;
7+
import android.support.test.espresso.intent.rule.IntentsTestRule;
8+
import android.support.test.espresso.matcher.BoundedMatcher;
9+
import android.support.test.rule.ActivityTestRule;
10+
import android.support.test.runner.AndroidJUnit4;
11+
12+
import org.hamcrest.Description;
13+
import org.hamcrest.Matcher;
14+
import org.junit.Rule;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
18+
import java.util.Map;
19+
20+
import static android.support.test.espresso.Espresso.onView;
21+
import static android.support.test.espresso.action.ViewActions.click;
22+
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
23+
import static android.support.test.espresso.action.ViewActions.typeText;
24+
import static android.support.test.espresso.assertion.ViewAssertions.matches;
25+
import static android.support.test.espresso.intent.Intents.intended;
26+
import static android.support.test.espresso.intent.Intents.intending;
27+
import static android.support.test.espresso.intent.matcher.BundleMatchers.hasEntry;
28+
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasAction;
29+
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtras;
30+
import static android.support.test.espresso.intent.matcher.IntentMatchers.toPackage;
31+
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
32+
import static android.support.test.espresso.matcher.ViewMatchers.withId;
33+
import static android.support.test.espresso.matcher.ViewMatchers.withText;
34+
import static android.support.test.internal.util.Checks.checkNotNull;
35+
import static org.hamcrest.CoreMatchers.equalTo;
36+
import static org.hamcrest.CoreMatchers.is;
37+
import static org.hamcrest.CoreMatchers.not;
38+
import static org.hamcrest.Matchers.allOf;
39+
40+
/**
41+
* Instrumented test, which will execute on an Android device.
42+
*
43+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
44+
*/
45+
@RunWith(AndroidJUnit4.class)
46+
public class ExampleInstrumentedTest {
47+
48+
private static final String EMAIL = "[email protected]";
49+
private static final String PASSWORD = "istarx.cc";
50+
51+
// @Rule
52+
// public ActivityTestRule<EspressoDemoActivity> rule = new ActivityTestRule<>(EspressoDemoActivity.class);
53+
@Rule
54+
public IntentsTestRule<EspressoDemoActivity> intentsTestRule =
55+
new IntentsTestRule<>(EspressoDemoActivity.class);
56+
57+
58+
public static Matcher<Object> mapValueMatcher(final String expectedText) {
59+
checkNotNull(expectedText);
60+
return new BoundedMatcher<Object, Map>(Map.class) {
61+
@Override
62+
public void describeTo(Description description) {
63+
description.appendText("with item content");
64+
}
65+
66+
@Override
67+
protected boolean matchesSafely(Map item) {
68+
return hasEntry(equalTo("espresso"), is(expectedText)).matches(item);
69+
}
70+
};
71+
}
72+
73+
@Test
74+
public void espressoTestDemo() {
75+
// onView(withId(R.id.text_view))
76+
// .perform(scrollTo(), click(), typeText("Test Text"), closeSoftKeyboard())
77+
// .check(matches(allOf(isDisplayed(), withText("Expected Text"))));
78+
79+
onView(withId(R.id.email)).perform(typeText(EMAIL));
80+
onView(withId(R.id.password)).perform(typeText(PASSWORD), closeSoftKeyboard());
81+
onView(withId(R.id.login)).perform(click());
82+
83+
String expectedString = String.format("Email is: %s\nPassword is: %s", EMAIL, PASSWORD);
84+
onView(withId(R.id.intent_content)).check(matches(allOf(isDisplayed(), withText(expectedString))));
85+
}
86+
87+
@Test
88+
public void onDataTest() {
89+
// onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("espresso"), is("fast")))).perform(click());
90+
}
91+
92+
@Test
93+
public void espressoIntentTest() {
94+
onView(withId(R.id.email)).perform(typeText(EMAIL));
95+
onView(withId(R.id.password)).perform(typeText(PASSWORD), closeSoftKeyboard());
96+
onView(withId(R.id.login)).perform(click());
97+
98+
intended(allOf(
99+
toPackage("cc.istarx.espressodemo"),
100+
not(hasAction("cc.istarx.test.action")),
101+
hasExtras(allOf(
102+
hasEntry(equalTo("email"), equalTo(EMAIL)),
103+
hasEntry(equalTo("password"), equalTo(PASSWORD))
104+
))));
105+
}
106+
107+
@Test
108+
public void intendingTest() {
109+
Intent resultData = new Intent();
110+
String testStr = "Intending test string for test...";
111+
resultData.putExtra("test", testStr);
112+
Instrumentation.ActivityResult result =
113+
new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
114+
115+
intending(toPackage("cc.istarx.espressodemo")).respondWith(result);
116+
117+
onView(withId(R.id.intending_button)).perform(click());
118+
onView(withId(R.id.result_text)).check(matches(allOf(isDisplayed(),withText(testStr))));
119+
}
120+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="cc.istarx.espressodemo">
4+
5+
<!-- To auto-complete the email text field in the login form with the user's emails -->
6+
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
7+
<uses-permission android:name="android.permission.READ_PROFILE" />
8+
<uses-permission android:name="android.permission.READ_CONTACTS" />
9+
10+
<application
11+
android:allowBackup="true"
12+
android:icon="@mipmap/ic_launcher"
13+
android:label="@string/app_name"
14+
android:roundIcon="@mipmap/ic_launcher_round"
15+
android:supportsRtl="true"
16+
android:theme="@style/AppTheme">
17+
<activity android:name=".EspressoDemoActivity">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
21+
<category android:name="android.intent.category.LAUNCHER" />
22+
</intent-filter>
23+
</activity>
24+
<activity android:name=".ShowResultActivity" />
25+
<activity android:name=".IntendingTestActivity">
26+
<intent-filter>
27+
<action android:name="cc.istarx.espressodemo.customaction"/>
28+
<category android:name="android.intent.category.DEFAULT"/>
29+
</intent-filter>
30+
</activity>
31+
</application>
32+
33+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cc.istarx.espressodemo;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.widget.Button;
7+
import android.widget.EditText;
8+
import android.widget.TextView;
9+
10+
public class EspressoDemoActivity extends AppCompatActivity {
11+
12+
private Button loginButton;
13+
private Button intendingTestButton;
14+
private EditText emailText;
15+
private EditText passwordText;
16+
TextView result_text;
17+
18+
private static final int REQUEST_CODE = 1;
19+
20+
@Override
21+
protected void onCreate(Bundle savedInstanceState) {
22+
super.onCreate(savedInstanceState);
23+
setContentView(R.layout.activity_espresso_demo);
24+
25+
loginButton = findViewById(R.id.login);
26+
intendingTestButton = findViewById(R.id.intending_button);
27+
emailText = findViewById(R.id.email);
28+
passwordText = findViewById(R.id.password);
29+
result_text = findViewById(R.id.result_text);
30+
31+
loginButton.setOnClickListener(v -> {
32+
Intent intent = new Intent(EspressoDemoActivity.this,ShowResultActivity.class);
33+
intent.putExtra("email", emailText.getText().toString());
34+
intent.putExtra("password",passwordText.getText().toString());
35+
startActivity(intent);
36+
});
37+
38+
intendingTestButton.setOnClickListener((v) -> {
39+
Intent intent = new Intent("cc.istarx.espressodemo.customaction");
40+
startActivityForResult(intent, REQUEST_CODE);
41+
});
42+
}
43+
44+
@Override
45+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
46+
if (requestCode == REQUEST_CODE) {
47+
String testStr = data.getStringExtra("test");
48+
result_text.setText(testStr);
49+
}
50+
super.onActivityResult(requestCode, resultCode, data);
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cc.istarx.espressodemo;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
import android.support.v7.app.AppCompatActivity;
6+
7+
public class IntendingTestActivity extends AppCompatActivity {
8+
9+
@Override
10+
protected void onCreate(Bundle savedInstanceState) {
11+
super.onCreate(savedInstanceState);
12+
setContentView(R.layout.activity_intending_test);
13+
}
14+
15+
@Override
16+
protected void onResume() {
17+
super.onResume();
18+
19+
Intent intent = new Intent();
20+
intent.putExtra("test", "Espresso Intending Test");
21+
setResult(RESULT_OK, intent);
22+
finish();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cc.istarx.espressodemo;
2+
3+
import android.content.Intent;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.os.Bundle;
6+
import android.widget.TextView;
7+
8+
public class ShowResultActivity extends AppCompatActivity {
9+
10+
@Override
11+
protected void onCreate(Bundle savedInstanceState) {
12+
super.onCreate(savedInstanceState);
13+
setContentView(R.layout.activity_show_result);
14+
15+
Intent intent = getIntent();
16+
String email = intent.getStringExtra("email");
17+
String password = intent.getStringExtra("password");
18+
19+
TextView textView = findViewById(R.id.intent_content);
20+
textView.setText(String.format("Email is: %s\nPassword is: %s", email, password));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1" />
34+
</vector>

0 commit comments

Comments
 (0)