Skip to content

Commit

Permalink
完善 Fragment 测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
douo committed Mar 2, 2018
1 parent 58b94e8 commit b05b366
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class CaptureInstrumentedTest {
public ActivityTestRule<CaptureTestActivity> mActivityRule =
new ActivityTestRule<>(CaptureTestActivity.class);


@Test
public void activity__ref_should_be_updated_after_activity_recreate() {
String id = getActivityInstance().toString();
Expand All @@ -46,7 +45,43 @@ public void activity__ref_should_be_updated_after_activity_recreate() {
@Test
public void view_with_id__ref_should_be_updated_after_activity_recreate() {
final String text = "text";
onView(withText("captureView")).perform(click());
onView(withText("captureViewWithId")).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("captureFragmentWithId")).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_tag__ref_should_be_updated_after_activity_recreate() {
final String text = "text";
onView(withText("captureFragmentWithTag")).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 support_fragment_with_id__ref_should_be_updated_after_activity_recreate() {
final String text = "text";
onView(withText("captureSupportFragmentWithId")).perform(click());

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

Expand All @@ -56,9 +91,9 @@ public void view_with_id__ref_should_be_updated_after_activity_recreate() {
}

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package info.dourok.esactivity.activity;

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

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import info.dourok.esactivity.BuilderUtil;
import info.dourok.esactivity.test.R;

public class CaptureTestActivity extends AppCompatActivity {
Expand All @@ -17,13 +15,27 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
textView = findViewById(R.id.content);
if (savedInstanceState == null) {
prepareFragmentWithTag();
}
}

private void prepareFragmentWithTag() {
OneButtonFragment fragment = OneButtonFragment.newInstance("captureFragmentWithTag");
getFragmentManager().beginTransaction().add(R.id.parent, fragment, "fragment").commit();
}

private void prepareSupportFragmentWithTag() {
OneButtonSupportFragment fragment =
OneButtonSupportFragment.newInstance("captureSupportFragmentWithTag");
getSupportFragmentManager().beginTransaction().add(R.id.parent, fragment, "fragment").commit();
}

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

public void captureView(View v) {
public void captureViewWithId(View v) {
TextView localTextView = findViewById(R.id.content);
EditorActivityBuilder.create(this).forContent(localTextView::setText).start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package info.dourok.esactivity.activity;

import android.app.Fragment;
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;
Expand All @@ -12,7 +11,8 @@
/** A simple {@link Fragment} subclass. */
public class OneButtonFragment extends Fragment {

CaptureTestActivity activity;
private CaptureTestActivity activity;
private String btnText;

public OneButtonFragment() {
// Required empty public constructor
Expand All @@ -22,22 +22,31 @@ public OneButtonFragment() {
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*/
public static OneButtonFragment newInstance() {
return new OneButtonFragment();
public static OneButtonFragment newInstance(String btnText) {
Bundle arguments = new Bundle();
OneButtonFragment fragment = new OneButtonFragment();
arguments.putString("btnText", btnText);
fragment.setArguments(arguments);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() == null) {
btnText = "captureFragmentWithId";
} else {
btnText = getArguments().getString("btnText");
}
}

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

Expand All @@ -51,6 +60,10 @@ public void onAttach(Context context) {
}
}

private void showContent(String s) {
activity.showContent(s);
}

@Override
public void onDetach() {
super.onDetach();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package info.dourok.esactivity.activity;

import android.content.Context;
import android.content.Intent;
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 OneButtonSupportFragment extends Fragment {

private CaptureTestActivity activity;
private String btnText;

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

/**
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*/
public static OneButtonSupportFragment newInstance(String btnText) {
Bundle arguments = new Bundle();
OneButtonSupportFragment fragment = new OneButtonSupportFragment();
arguments.putString("btnText", btnText);
fragment.setArguments(arguments);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() == null) {
btnText = "captureSupportFragmentWithId";
} else {
btnText = getArguments().getString("btnText");
}
}

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

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}

private void showContent(String s) {
activity.showContent(s);
}

@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;
}
}
9 changes: 8 additions & 1 deletion library/src/androidTest/res/layout/activity_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
Expand Down Expand Up @@ -30,7 +31,7 @@
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="captureView"
android:onClick="captureViewWithId"
android:text="captureView"
/>
<fragment
Expand All @@ -39,4 +40,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<fragment
android:id="@+id/support_fragment"
android:name="info.dourok.esactivity.activity.OneButtonSupportFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

0 comments on commit b05b366

Please sign in to comment.