From b05b3666577d3a44504b2ff489d23e6f889f805f Mon Sep 17 00:00:00 2001 From: Tiou Lims Date: Fri, 2 Mar 2018 14:43:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20Fragment=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../esactivity/CaptureInstrumentedTest.java | 43 ++++++++++- .../activity/CaptureTestActivity.java | 20 ++++- .../activity/OneButtonFragment.java | 27 +++++-- .../activity/OneButtonSupportFragment.java | 77 +++++++++++++++++++ .../androidTest/res/layout/activity_test.xml | 9 ++- 5 files changed, 160 insertions(+), 16 deletions(-) create mode 100644 library/src/androidTest/java/info/dourok/esactivity/activity/OneButtonSupportFragment.java diff --git a/library/src/androidTest/java/info/dourok/esactivity/CaptureInstrumentedTest.java b/library/src/androidTest/java/info/dourok/esactivity/CaptureInstrumentedTest.java index 7e38615..5ffd5d2 100644 --- a/library/src/androidTest/java/info/dourok/esactivity/CaptureInstrumentedTest.java +++ b/library/src/androidTest/java/info/dourok/esactivity/CaptureInstrumentedTest.java @@ -29,7 +29,6 @@ public class CaptureInstrumentedTest { public ActivityTestRule mActivityRule = new ActivityTestRule<>(CaptureTestActivity.class); - @Test public void activity__ref_should_be_updated_after_activity_recreate() { String id = getActivityInstance().toString(); @@ -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()); @@ -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()); diff --git a/library/src/androidTest/java/info/dourok/esactivity/activity/CaptureTestActivity.java b/library/src/androidTest/java/info/dourok/esactivity/activity/CaptureTestActivity.java index 889f8b0..309cfa8 100644 --- a/library/src/androidTest/java/info/dourok/esactivity/activity/CaptureTestActivity.java +++ b/library/src/androidTest/java/info/dourok/esactivity/activity/CaptureTestActivity.java @@ -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 { @@ -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(); } diff --git a/library/src/androidTest/java/info/dourok/esactivity/activity/OneButtonFragment.java b/library/src/androidTest/java/info/dourok/esactivity/activity/OneButtonFragment.java index f81b6b4..0eee543 100644 --- a/library/src/androidTest/java/info/dourok/esactivity/activity/OneButtonFragment.java +++ b/library/src/androidTest/java/info/dourok/esactivity/activity/OneButtonFragment.java @@ -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; @@ -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 @@ -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; } @@ -51,6 +60,10 @@ public void onAttach(Context context) { } } + private void showContent(String s) { + activity.showContent(s); + } + @Override public void onDetach() { super.onDetach(); diff --git a/library/src/androidTest/java/info/dourok/esactivity/activity/OneButtonSupportFragment.java b/library/src/androidTest/java/info/dourok/esactivity/activity/OneButtonSupportFragment.java new file mode 100644 index 0000000..f228add --- /dev/null +++ b/library/src/androidTest/java/info/dourok/esactivity/activity/OneButtonSupportFragment.java @@ -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; + } +} diff --git a/library/src/androidTest/res/layout/activity_test.xml b/library/src/androidTest/res/layout/activity_test.xml index a1f6706..4106d09 100644 --- a/library/src/androidTest/res/layout/activity_test.xml +++ b/library/src/androidTest/res/layout/activity_test.xml @@ -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" @@ -30,7 +31,7 @@