Skip to content

Commit

Permalink
新增引用捕获测试
Browse files Browse the repository at this point in the history
  • Loading branch information
douo committed Mar 2, 2018
1 parent 8c9c3d0 commit 58b94e8
Show file tree
Hide file tree
Showing 14 changed files with 472 additions and 26 deletions.
4 changes: 4 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ dependencies {

implementation deps.support.annotations
implementation deps.support.fragment
implementation 'com.android.support:support-v4:26.1.0'
androidTestImplementation 'com.android.support:appcompat-v7:27.0.2'
androidTestImplementation 'com.android.support.constraint:constraint-layout:1.0.2'
androidTestAnnotationProcessor project(':compiler')
testImplementation deps.junit
androidTestImplementation deps.support.test.runner
androidTestImplementation deps.support.test.espresso
Expand Down
12 changes: 12 additions & 0 deletions library/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.dourok.esactivity">

<application>
<activity
android:name=".activity.CaptureTestActivity"
android:theme="@style/AppTheme"/>
<activity
android:name=".activity.EditorActivity"
android:theme="@style/AppTheme"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package info.dourok.esactivity;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import info.dourok.esactivity.activity.CaptureTestActivity;
import info.dourok.esactivity.test.R;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.replaceText;
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;
import static info.dourok.esactivity.util.EspressoHelper.getActivityInstance;
import static org.hamcrest.Matchers.not;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class CaptureInstrumentedTest {
@Rule
public ActivityTestRule<CaptureTestActivity> mActivityRule =
new ActivityTestRule<>(CaptureTestActivity.class);


@Test
public void activity__ref_should_be_updated_after_activity_recreate() {
String id = getActivityInstance().toString();
onView(withText("captureActivity")).perform(click());

getInstrumentation().runOnMainSync(() -> mActivityRule.getActivity().recreate());

pressBack();
String idNew = getActivityInstance().toString();
onView(withId(R.id.content)).check(matches(not(withText(id))));
onView(withId(R.id.content)).check(matches(withText(idNew)));
}

@Test
public void view_with_id__ref_should_be_updated_after_activity_recreate() {
final String text = "text";
onView(withText("captureView")).perform(click());

getInstrumentation().runOnMainSync(() -> mActivityRule.getActivity().recreate());

onView(withId(R.id.edit_text)).perform(replaceText(text));
onView(withId(R.id.action_ok)).perform(click());
onView(withId(R.id.content)).check(matches(withText(text)));
}

@Test
public void fragment_with_id__ref_should_be_updated_after_activity_recreate(){
final String text = "text";
onView(withText("captureFragment")).perform(click());

getInstrumentation().runOnMainSync(() -> mActivityRule.getActivity().recreate());

onView(withId(R.id.edit_text)).perform(replaceText(text));
onView(withId(R.id.action_ok)).perform(click());
onView(withId(R.id.content)).check(matches(withText(text)));
}


@Test
public void method__ref_should_be_updated_after_activity_recreate() throws Exception {
final String text = "text";
onView(withText("methodRef")).perform(click());
getInstrumentation().runOnMainSync(() -> mActivityRule.getActivity().recreate());
onView(withId(R.id.edit_text)).perform(replaceText(text));
onView(withId(R.id.action_ok)).perform(click());
onView(withId(R.id.content)).check(matches(withText(text)));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package info.dourok.esactivity.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.TextView;
import info.dourok.esactivity.BuilderUtil;
import info.dourok.esactivity.test.R;

public class CaptureTestActivity extends AppCompatActivity {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
textView = findViewById(R.id.content);
}

public void captureActivity(View v) {
EditorActivityBuilder.create(this).forCancel(intent -> showContent(this.toString())).start();
}

public void captureView(View v) {
TextView localTextView = findViewById(R.id.content);
EditorActivityBuilder.create(this).forContent(localTextView::setText).start();
}

public void methodRef(View v) {

EditorActivityBuilder.create(this).forContent(this::showContent).start();
}

void showContent(String s) {
textView.setText(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package info.dourok.esactivity.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import info.dourok.esactivity.Builder;
import info.dourok.esactivity.BuilderParameter;
import info.dourok.esactivity.BuilderUtil;
import info.dourok.esactivity.Result;
import info.dourok.esactivity.ResultParameter;
import info.dourok.esactivity.test.R;

/** @author tiaolins */
@Builder
@Result(
name = "content",
parameters = {@ResultParameter(name = "content", type = String.class)}
)
public class EditorActivity extends AppCompatActivity {
@BuilderParameter String hint;
EditorActivityHelper mHelper;
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
mHelper = BuilderUtil.createHelper(this);
editText = findViewById(R.id.edit_text);
if (hint != null) {
editText.setHint(hint);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_editor, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.action_ok:
mHelper.finishContent(editText.getText().toString());
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package info.dourok.esactivity.activity;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/** A simple {@link Fragment} subclass. */
public class OneButtonFragment extends Fragment {

CaptureTestActivity activity;

public OneButtonFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*/
public static OneButtonFragment newInstance() {
return new OneButtonFragment();
}

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

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button btn = new Button(getActivity());
btn.setText("captureFragment");
btn.setOnClickListener(
v -> EditorActivityBuilder.create(activity).forContent(activity::showContent).start());
return btn;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof CaptureTestActivity) {
activity = (CaptureTestActivity) context;
} else {
throw new RuntimeException(context.toString() + " must implement CaptureTestActivity");
}
}

@Override
public void onDetach() {
super.onDetach();
activity = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package info.dourok.esactivity.util;

import android.app.Activity;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import info.dourok.esactivity.function.Consumer;
import java.util.Collection;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.runner.lifecycle.Stage.RESUMED;

/**
* @author tiaolins
* @date 2018/3/2.
*/
public class EspressoHelper {
private EspressoHelper() {}

private static Activity currentActivity;

public static synchronized Activity getActivityInstance() {
getInstrumentation()
.runOnMainSync(
() -> {
Collection<Activity> resumedActivities =
ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED);
if (resumedActivities.iterator().hasNext()) {
currentActivity = resumedActivities.iterator().next();
}
});

return currentActivity;
}
}
Loading

0 comments on commit 58b94e8

Please sign in to comment.