-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
472 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
80 changes: 80 additions & 0 deletions
80
library/src/androidTest/java/info/dourok/esactivity/CaptureInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
library/src/androidTest/java/info/dourok/esactivity/ExampleInstrumentedTest.java
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
library/src/androidTest/java/info/dourok/esactivity/activity/CaptureTestActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
library/src/androidTest/java/info/dourok/esactivity/activity/EditorActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
library/src/androidTest/java/info/dourok/esactivity/activity/OneButtonFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
library/src/androidTest/java/info/dourok/esactivity/util/EspressoHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.